/**********************************
	CLEAR INPUT SCRIPTS 
**********************************/
function clearValue(varObj,varValue) {
	if($(varObj).val() == varValue) {
		$(varObj).val("");
	}
}

function testValue(varObj,varValue) {
	if($(varObj).val() == "") {
		$(varObj).val(varValue);
	}
}


/**********************************
	JQUERY 
**********************************/

	$(document).ready(function() {
	//$(document).bind('ajaxLoaded', function() {

	/* Clear text inputs on focus, restore on blur */
	$(".utilities input[type=text]").focus( function() {
		var $this = $(this);
		inputValue = $this.attr("value");

		clearValue($this,inputValue)
	});
	$(".utilities input[type=text]").blur( function() {
		testValue($(this),inputValue)
	});

	/* Primary nav hover states */
	$("#primary-nav > ul > li").hover(
		function() {
			var $this = $(this);
			$this.addClass("hover").siblings("a").addClass("hover");
			
			var RIGHT_SIDE_PADDING = 24;
			
			function getRight(el) {
				return el.offset().left + el.outerWidth();
			}

			var $link = $this.find('> div > a').addClass("hover"),
				$dropDown = $link.siblings("ul").addClass("hover"),
				over = getRight($dropDown) - getRight($('#main-container'));

			if (over > 0) {
				$dropDown.css('left', parseFloat($dropDown.css('left')) - over - RIGHT_SIDE_PADDING);
			}			
		},
		function() {
			$(this)
				.removeClass("hover")
					.siblings("a")
						.removeClass("hover")
						.end()
				.find('> div > a')
					.removeClass("hover")
						.siblings('ul')
							.removeClass('hover');			
		}
	);
	$("#primary-nav ul li ul li ul li a").hover(
		function() {
			var $this = $(this),
			parentID = $this.closest("li").attr("id"),
			categoryID = $this.closest("div.drop").attr("id");
			$("#"+categoryID+" span.category-image img").attr({ src: navArray[categoryID][parentID] });
		},
		function() {
			
		}
	);
	
	$('#primary-nav > ul > li:has(ul)').each(function() {
		var $this = $(this),
			liCount = $this.find('ul:first > li').length;

		$this
			.find('ul')
				.addClass('navcount-' + liCount)
				.end()
			.find('div.tab-no')
				.addClass('tab-yes')
				.removeClass('tab-no');
	});	

	/* Cart dropdown hover states */
	$("#header-container .cart").hover(
		function() {
			$(this).addClass("cart-hover");
		},
		function() {
			$(this).removeClass("cart-hover");
		}
	);

	/* Corporate hover states */
	$("#header-container .header #utility-nav .ct-corporate-container").hover(
		function() {
			$(this).addClass("ct-corporate-container-hover");
		},
		function() {
			$(this).removeClass("ct-corporate-container-hover");
		}
	);

	/* News & Offers hover states */
	$("#header-container .header .utilities .ct-news-container").hover(
		function() {
			$(this).addClass("ct-news-container-hover");
		},
		function() {
			$(this).removeClass("ct-news-container-hover");
		}
	);

	/* Find a Dealer hover states */
	$("#header-container .header .utilities .ct-dealer-container").hover(
		function() {
			$(this).addClass("ct-dealer-container-hover");
		},
		function() {
			$(this).removeClass("ct-dealer-container-hover");
		}
	);
	
	/* Quickview hover */
	$("#content-container .products-container .products .product .image").hover(
		function() {
			$(this).addClass("quickviewhover");
		},
		function() {
			$(this).removeClass("quickviewhover");
		}
	);

	/* jCarousel */
	$("#carousel").jcarousel();
	$('.product-detail .product-thumbnails').jcarousel({
		scroll: 5
	});
	$('.product-detail .recentlist').jcarousel({
		scroll: 1
	});
	$('.product-detail .likelist').jcarousel({
		scroll: 1
	});

	/* Tabs */
	$('.product-detail .product-information').tabs();
	
	/* Toggle category view between grid and list */
	$(".pageview .pairing-gridview").click(function () {
		$("#content-container").addClass("grid");
	});

	$(".pageview .pairing-listview").click(function () {
		$("#content-container").removeClass("grid");
	});
	
	/* Scroll panes */
	$('div.scrollpane').jScrollPane({ showArrows:true });

	// jScroller (marquee)
	$jScroller.add(".catland-marquee-wrap",".catland-marquee","left",4);
	$jScroller.start();

});

// Image thumbnails/zooming
$.fn.thumbnail = function(options) {
	return this.each(function() {
		var $this = $(this),
		metadata = $this.metadata() || {};

		$this.click(function() {
			$(metadata.target).attr('src', this.href);
			return false;
		});
	});
};


$(function() {
	if ($.browser.msie && $.browser.version < 7) {
		$(document).bind('overlay.on', function() { $('select').hide(); });
		$(document).bind('overlay.off', function() { $('select').show(); });
	}

	var $largeImageLink = $('div.product-image-large a');

	$('ul.product-thumbnails a').thumbnail().click(function() {
		var href = $(this).metadata().zoom;
		
		if(href) 
			$largeImageLink.attr('href', href);
	});

	$largeImageLink.click(function() {
		$(document).trigger('overlay.on');

		$('#zoom')
			.html('<div id="zoom-close"></div><img src="' + this.href + '" />')
			.show()
			.find('#zoom-close')
				.click(hideZoom);
 
		return false;
	});

	$('a.flash').click(function() {
		$(document).trigger('overlay.on');
		
		$('#zoom')
			.empty()
			.append('<div id="flash-zoom"></div><div id="zoom-close"></div>')
			.show();
			
		$('#flash-zoom').flash($(this).metadata());
		
		$('#zoom-close').click(hideZoom);
  	
		return false;
	});

	function hideZoom() {
		$(document).trigger('overlay.off');
				
		$('#zoom').hide();
		return false;		
	}
});
