$(document).ready(function() {

	// top drop menu
	$('#menu>ul>li>a').dropMenu();
	
	// collections mouse overs
	$('#collections img').imgHover();
	
	// contact acordian toggle
	$('#contact_drop').each(function() {
		$(this).
			height('30px').
			bind('click', function() {
				$(this).toggle();
			});
	});
	
	// faq acordian setup
	$('#faq li').each(function() {
		var $p = $(this).find('p:first')
		var origHeight = $p.height();
		$p.css({height:0, overflow:'hidden', margin:'0 0 0 0'});
		$(this).
			bind('click', function(e) {
				if( $p.height() === origHeight ) {
					$p.animate({height:0, 'margin-bottom':'0'}, 200);
				} else {
					$p.animate({height:origHeight + 'px', 'margin-bottom':'1.5em'}, 200);
				}
				e.stopPropagation();
				return false;
			});
	});
	
	// contact page setup
	var origHeight = $('#drop_table').height();
	$('#drop_table').css({'height':'0', 'overflow':'hidden'});
	$('#contact th').wrapInner('<a href="#" />');
	$('#contact th a').bind('click', function(e) {
		var $drop_table = $('#drop_table');
		if( $drop_table.height() > 0 ) {
			$drop_table.animate({'height':'0'}, 250);
		} else {
			$drop_table.animate({'height':origHeight}, 250);
		}
		e.stopPropagation();
		e.preventDefault();
	}); // bind "#contact th a" click
	
	// form descriptive values
	$('#form').each(function() {
		focusClear('name', 'Name');
		focusClear('phone', 'Phone Number');
		focusClear('email', 'Email Address');
		focusClear('message', 'Message');
	});
	
	// dot leaders on specs pages
	$('#main .specs dt').each( function() {
		$(this).
			wrapInner( $('<span />').css({'background-color':'#FFF', 'padding-right':'3px'}) ).
			css({'background':'url(/images/leader.gif) repeat-x 0 8px'});
	});
	
	// cycle home images
	$('#home_img').tkCycle({
		delay:7000,
		fadeTime:600,
		slides:[
			'<img src="/images/home/2.jpg" alt="" />'
		]
	});

	$('#newsletter_input').inputLabel('Enter Email Address');
	$('#newsletter_submit').
		text('NEWSLETTER SIGN UP').
		bind('click', function() {
			$(this).closest('form').submit();
		});
	
}); // document ready


(function() {
	jQuery.fn.imgHover = function(settings) {

		var config = {
			"suffix":"_over"
		};
		if( settings ) {
			$.extend(config, settings);
		}
		
		var imgPreloaderCache = [];
		
		function doImg($obj) {
			
			var src = $obj.attr('src');
			var srcOver = src.substr(0, src.lastIndexOf('.')) + config.suffix + src.substr(src.lastIndexOf('.'));
			
			var preloader = document.createElement('img');
			preloader.src = srcOver;
			imgPreloaderCache.push(preloader);
			
			function bindImg() {
				$obj.bind('mouseover', function() {
					this.src = srcOver;
				});
				
				$obj.bind('mouseout', function() {
					this.src = src;
				});
			}

			$obj.bind('load', function(e) { // when the preloader image loads
				bindImg();
				$(this).unbind('load'); // unbind the events
			});
	
			this.src = this.src; // give browsers (IE) another chance to trigger event handlers for the img
			
		}

		return this.each( function() {
			
			var $obj = $(this);
			
			if( $obj.is('img') ) {
				doImg($obj);
			} else {
				$obj.find('img').each( function() {
					doImg( $(this) );
				});
			}

		});
		
	};
})(jQuery);


(function ($) {
  $.extend($, {
    cacheImage: function (src, options) {
      if (typeof src === 'object') {
        $.each(src, function () {
          $.cacheImage(String(this), options);
        });

        return;
      }

			var image = new Image();

      options = options || {};

      $.each(['load', 'error', 'abort'], function () { // Callbacks
        var e = String(this);
        if (typeof options[e] === 'function') { $(image).bind(e, options[e]); }

        if (typeof options.complete === 'function') {
          $(image).bind(e, options.complete);
        }
      });

      image.src = src;

      return image;
    }
  });

  $.extend($.fn, {
    cacheImage: function (options) {
      return this.each(function () {
        $.cacheImage(this.src, options);
      });
    }
  });
})(jQuery);


(function() {
	jQuery.fn.dropMenu = function(settings) {

/*
// Invoke for each link in the menu.
// Adjust the $dropBox selector to select the sub menu associated with the link.
*/

		var config = {
			"mouseOutDelay":350,
			"fadeInTime":125,
			"fadeOutTime":200,
			"bgColor":"#E6E6E6",
			"dropBoxSelector":"ul:first"
		};
		if( settings ) {
			$.extend(config, settings);
		}
		
		$.cacheImage(['/images/bg_drop_menu.png']);

		return this.each( function() {
															 
			var $obj = $(this);
			var delayTimer = false;
			var opening = false;
			var $dropBox = $obj.parent().find(config.dropBoxSelector);
			
			$dropBox.css({"opacity":0.0, "display":"none"});
			
			// workaround ie bugs
			if( $.browser.msie ) {
				$obj.css({"display":"block"}).parent().css({"text-align":"left"});
				$dropBox.css({"background-color":config.bgColor});
			}
			
			function openMenu(e) {
				clearTimeout(delayTimer);
				$dropBox.css({"z-index":"1001"});
				
				if( opening || ($dropBox.css('opacity') == 1) ) {
					return;
				}
				opening = true;
				
				if( $dropBox.css("display") === "none" ) {
					$dropBox.css({"opacity":0.0, "display":"block"});
				}
				$dropBox.stop().animate({"opacity":1.0}, {
					"duration":config.fadeInTime,
					"complete":function() {
						opening = false;
					}
				});
				e.stopPropagation();
				e.preventDefault();
			}
			
			function closeMenu(e) {
				$dropBox.css({"z-index":"1000"});
				delayTimer = setTimeout(function() { closeMenuNow(); }, config.mouseOutDelay); 
				e.stopPropagation();
				e.preventDefault();
			}
			
			function closeMenuNow() {	
				$dropBox.stop().animate({"opacity":0.0}, {
					"duration":config.fadeOutTime,
					"complete":function() {
						$(this).css({"display":"none"});
					}
				});
			}
			
			$obj.bind("mouseover", function(e) { openMenu(e); });
			$obj.bind("mouseout", function(e) { closeMenu(e); });
			$dropBox.bind("mouseover", function(e) { openMenu(e); });
			$dropBox.bind("mouseout", function(e) { closeMenu(e); });
			
		});
		
	};
})(jQuery);


function focusClear(id, text) {
	var $obj = $("#" + id);
	function clearIfDefault() {
		if( $obj.val() === text )
			$obj.val('');
	}
	function defaultIfBlank() {
		if( $obj.val() === '' )
			$obj.val(text);
	}
	$obj.
		bind('focus', clearIfDefault).
		bind('blur', defaultIfBlank).
		parents('form:first').bind('submit', clearIfDefault);
	defaultIfBlank();
}

(function() {
	jQuery.fn.tkCycle = function(settings) {

		if( $.browser.msie && (parseInt($.browser.version, 10) < 8) ) {
			return;
		}

		var config = {
			delay: 3000,
			fadeTime: 750,
			slides: []
		};
		if( settings ) {
			$.extend(config, settings);
		}

		return this.each( function() {

			var slides = config.slides;
			var index = 0;
			var timer = false;
			
			var $obj = $(this);
			
			slides.unshift( $obj.html() );
			
			var width = $obj.width();
			var height = $obj.height();
			
			var $markup = $(
				
			);			
		
			var $top = $('<div />');
			var $bottom = $('<div />');
		
			$top.width(width).height(height).
				css({'position': 'absolute', 'z-index':'201', 'opacity':0.0});
			$bottom.width(width).height(height).
				css({'position': 'absolute', 'z-index':'200', 'opacity':1.0});
			if( config.css ) {
				$top.css(config.css);
				$bottom.css(config.css);
			}
			$bottom.html( slides[0] );
			$top.html( slides[1] );
			$obj.
				css({'visibility':"hidden"}).
				before($bottom).
				before($top);
			
			function nextIndex() {
				return ( index < (slides.length - 1) ? index + 1 : 0 );
			}
			function prevIndex() {
				return ( index > 0 ? index - 1 : slides.length - 1 );
			}
			
			function advance() {
				$top.stop().animate({opacity:1.0}, config.fadeTime, function() {
					index = nextIndex();
					$bottom.html( slides[index] );  // copy the top image to the bottom
					setTimeout(function() { // prevent glitch
						$top.css({opacity:0.0});  // hide the top image
						$top.html( slides[ nextIndex() ] );  // preload the next slide into the hidden top container
					}, 1);
				});
			}
			
			function goBack() {
				$top.html( slides[ prevIndex() ] );  // preload the slide into the hidden top container
				$top.stop().animate({opacity:1.0}, config.fadeTime, function() {
					index = prevIndex();
					$bottom.html( slides[index] );  // copy the top image to the bottom
					setTimeout(function() { // prevent glitch
						$top.css({opacity:0.0});  // hide the top image
						$top.html( slides[ nextIndex() ] );  // preload the next slide into the hidden top container
					}, 1);
				});
			}
			
			timer = setInterval(function() { advance(); }, config.delay);
			
			$('#gallery_next').bind('click', function(event) {
				clearTimeout(timer);
				event.stopPropagation();
				event.preventDefault();
				advance();
			});
			
			$('#gallery_prev').bind('click', function(event) {
				clearTimeout(timer);
				event.stopPropagation();
				event.preventDefault();
				goBack();
			});
			
		});
		
	};
})(jQuery);


(function() {
	jQuery.fn.inputLabel = function(passedLabel) {

		var config = {
			label:passedLabel
		};
		
		function focusUpdate($obj, $textPassBox, isPass) {
			if( isPass ) {
				$textPassBox.hide();
				$obj.
					show();
				return;
			}
			if( $obj.val() == config.label ) {
				$obj.val('');
			}
		}
		
		function blurUpdate($obj, $textPassBox, isPass) {
			if( isPass ) {
				if( $obj.val() == '' ) {
					$obj.hide();
					$textPassBox.show();
				}
				return;
			}
			if( $obj.val() == '' ) {
				$obj.val(config.label);
			}
		}
		
		return this.each( function() {

			var
				$obj = $(this),
				isPass = false;
			
			if( !$obj.is('input') && !$obj.is('textarea') ) {
				return;
			}
			
			if( $obj.attr('type') == 'password' ) {

				var $textPassBox = $('<input type="text" class="' + $obj.attr('class') + '" />');
				$obj.
					after($textPassBox).
					hide();
				$textPassBox.
					val(config.label).
					bind('focus', function() {
						$textPassBox.hide();
						$obj.
							show().
							focus();
					});
				isPass = true;
			}
			
			$obj.
				bind('focus', function() {
					focusUpdate($obj, $textPassBox, isPass);
				}).
				bind('blur', function() {
					blurUpdate($obj, $textPassBox, isPass);
				});
				
			$obj.parents().find('form:first').
				bind('submit', function() {
					focusUpdate($obj, $textPassBox, isPass);
				});

			blurUpdate($obj, $textPassBox, isPass);
		
		}); // each match
		
	};
})(jQuery);



