$(document).ready(function() {

	// Search submit
	$("#search-box .submit").click(function(e) {
		e.preventDefault();
		$(this).parent().submit();
	});
  
	// Download brochure popup
	$(".brochure-link, #redeem-offer").fancybox({
		'width'        : 660,
		'height'      : 650,
		'autoScale'       : false,
		'transitionIn'    : 'fade',
		'transitionOut'    : 'fade',
		'type'        : 'iframe',
		'scrolling'      : 'auto',
		'padding'      : 0,
		'margin'      : 0
	});
  
	var $leftNav = $("#left-nav");
	$("b", $leftNav).each(function(){
		var lineHeight = $(this).css('lineHeight').replace('px','');
		if($(this).height() > lineHeight) {
		  $(this).addClass("two-line");
		}
	});
	
	$leftNav.children(".selected").each(function() {
		var $this = $(this);
		if($this.find(".selected").size() > 0) {
			$this.addClass("parent-selected");
		}
	});
  
	// Hover effect on nav 
	$leftNav.children("li").children("a").hover(
		function() {
			$(this).stop().animate({
				paddingLeft: "20px",
				paddingRight: "11px"
			}, 100);
		},function() {
			$(this).stop().animate({
				paddingLeft: "11px",
				paddingRight: "20px"
			}, 100);
		}
	);
	
	// Hover effect on subnavs
	$(".sub-nav, .sub-nav-inner").find("a").hover(
		function() {
			$(this).stop().animate({
				paddingLeft: "35px",
				paddingRight: "11px"
			}, 100);
		},function() {
			$(this).stop().animate({
				paddingLeft: "26px",
				paddingRight: "20px"
			}, 100);
		}
	);
  
  // Clear textbox and return to default (focus/blur)
	$(".textbox-auto-clear").each(function(){
		var searchOrigVal = $(this).val(); // Store the original value
		$(this).focus(function() {
			if($(this).val() == searchOrigVal) {
				$(this).val('');
			}
		});
		$(this).blur(function(){
			if($(this).val() == '') {
				$(this).val(searchOrigVal);
			}
		});
	});
  
	var $left = $("#left");
	$(".sub-nav").each(function(index) {
		if($(this).parent().hasClass("selected")) {
			$(this).addClass("sub-nav-inner").removeClass("sub-nav");
		}
		else {
			$(this).attr("id", "sub-nav-" + index);
			$(this).parent().data("subnav", index);
			var offsetTop = $(this).parent().position().top + 20;
			$(this).css("top", offsetTop);
			$(this).appendTo($left);
		}
	});
	
	var $slideshowItems = $("#slideshow ul");
	$slideshowItems.jcarousel({
        scroll: 1,
        buttonNextHTML: '<a class="next">Next</a>',
        buttonPrevHTML: '<a class="prev">Previous</a>'
      });
	
	var $selectedPhoto = $("#selected-photo img");
	$("li a", $slideshowItems).click(function(e) {
		e.preventDefault();
		var newImg = $(this).attr("href");
		$selectedPhoto.attr("src", newImg);
	});
	
	function LoadImage(pSelector, pCallback){
		var loader = $(pSelector);
		loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
	 
		LoadThisImage($(img), loader, pCallback);
	}
	function LoadThisImage(loader, pCallback){
		image_src = loader.attr('src');
		img = new Image();
		img.hide();

		img.load(function() {
			cb_js = loader.get(0).getAttribute('onload');              
			onload_cb = function(){
				eval(cb_js);
			}       

			loader.html(this);
			loader.removeClass('loadable-image');
			loader.removeAttr('src');
			loader.removeAttr('onload');
			$(this).show(); 
			if (onload_cb){                
				onload_cb($(this));
			}              
			if (pCallback){
				cb = pCallback;
				cb($(this));
			}
		})
		.error(function() { $(this).attr('src', gFailImage).show(); })
		.attr('src', image_src)
		.show();
	}
});

// Top navigation

$(function() {
  var $subnavs = $(".sub-nav");
  var hoverTimeout = new Array();
    $("#left-nav li").hover(function() {
    var data = $(this).data("subnav");
    if(data >= 0) {
      var $subnav = $("#sub-nav-" + data);
      $subnav.css('visibility', 'visible');
      $subnavs.not($subnav).css('visibility', 'hidden');
      $(this).parent().children("a").addClass("hover");
    }
        
    }, function() {
    var data = $(this).data("subnav");
    if(data >= 0) {
      var $subnav = $("#sub-nav-" + data);
      hoverTimeout[data] = setTimeout(function() { 
        $subnav.css('visibility', 'hidden')
      }, 100);
      $(this).parent().children("a").removeClass("hover");
    }
        $(this).children("a").removeClass("hover");
    });
  
  $subnavs.hover(function() {
    var data = $(this).attr("id").replace("sub-nav-", "");
    clearTimeout(hoverTimeout[data]);
    
  }, function() {
    $(this).css('visibility', 'hidden');
  });
});
