$(document).ready(function() {
	var quickView = false;
	
	if ($.cookie('L3DisplayMode') == 'details' && $('#content').hasClass('l3')) {
		$('#content').addClass('details');
		$('.navControls.top .displayModes .button').removeClass('selected');
		$('.navControls.top .displayModes .btnDetailsView').addClass('selected');
	}

	$('.navControls.top .displayModes .button').unbind().click(function() {
		$('.button').removeClass('selected');		
		if ($(this).hasClass('btnDetailsView')) {
			$(this).addClass('selected');
			$('#content').addClass('details');
			$.cookie('L3DisplayMode', 'details', {path: '/'});
		}
		else {
			$(this).addClass('selected');
			$('#content').removeClass('details');
			$.cookie('L3DisplayMode', 'grid', {path: '/'});
		}
		return false;		
	});

	$('.row').each(function(i) {
		var catNameHi = 0;
		var prodNameHi = 0;
		var prodDescHi = 0;
		var priceHi = 0;
		var seeMoreLinkHi = 0;
		var ratingsHi = 0;
		
		$('.prod', this).each(function(i) {
			if (!$(this).hasClass('toprated')) {
				if ((i+1) % 3 == 0) {
					$(this).addClass('last');
				}
			}
									   
			if ($('.prodName a', this).length > 0) {
				var strDesc = $('.prodName a', this).html();
				if (strDesc.length > 60) $('.prodName a', this).html(strDesc.substr(0, 60) + '...');
			}
			else {
				var strDesc = $('.prodName', this).html();
				if (strDesc.length > 60) $('.prodName', this).html(strDesc.substr(0, 60) + '...');
			}
			var curProdNameHi = $('.prodName', this).height();
			if (curProdNameHi > prodNameHi)
			{
				prodNameHi = curProdNameHi;
			}
			
			if ($('.prodDesc a', this).length > 0) {
				var str = $('.prodDesc a', this).html();
				if (str.length > 40) $('.prodDesc a', this).html(str.substr(0, 40) + '...');
			} else {
				var str = $('.prodDesc', this).html();
				if (str.length > 40) $('.prodDesc', this).html(str.substr(0, 40) + '...');
			}
			var curProdDescHi = $('.prodDesc', this).height();
			if (curProdDescHi > prodDescHi)
			{
				prodDescHi = curProdDescHi;
			}
			
			if ($('.catName', this).length > 0 && $('.seeMore', this).length > 0) {
				var curCatNameHeight = $('.catName', this).height();
				if (curCatNameHeight > catNameHi) catNameHi = curCatNameHeight;
				
				var curSeeMoreLinkHeight = $('.seeMore', this).height();
				if (curSeeMoreLinkHeight > seeMoreLinkHi) seeMoreLinkHi = curSeeMoreLinkHeight;
			}
			
			var curRatingsHeight = $('.rating', this).height();
			if (curRatingsHeight > ratingsHi)
			{
				ratingsHi = curRatingsHeight;
			}
			
			var curPriceHeight = $('.price', this).height();
			if (curPriceHeight > priceHi)
			{
				priceHi = curPriceHeight;
			}
			
		}).each(function() {
			$('.prodName', this).height(prodNameHi);
			$('.prodDesc', this).height(prodDescHi);
			$('.price', this).height(priceHi);
			$('.rating', this).height(ratingsHi);
			if ($('.catName', this).length > 0 && $('.seeMore', this).length > 0) {
				$('.catName', this).height(catNameHi);
				$('.seeMore', this).height(seeMoreLinkHi);
			}
		});
	});
	
	$('.prodImg').each(function() {
		$(this).hover(
			function() {
				if (quickView == false) {
					$('.btnQuickView', this).click(showQuickView).show();
				}
			},
			function() {
				$('.btnQuickView', this).hide();
			}
		);
	});
	
});

function showQuickView(e) {
	var btnQuickView = $(this);
	btnQuickView.css('cursor', 'wait');
	var width = $(window).width();
	documentHeight = $(document).height();
	
	var height;
	if (document.body.clientHeight) {
		height = document.body.clientHeight;
	}
	else if (document.documentElement.clientHeight) {
		height = document.documentElement.clientHeight;
	}
	
	var coverDiv = $('<div id="quickViewCover" class="busy"/>')
		.css({top: 0, left: 0, width: width, height: documentHeight, opacity: '0.1'})
		.appendTo('body');
	quickView = true;
	$('#content').addClass('busy');
	
	var pipURL = $(this).parent().parent().find('.btnDetails').attr('href');	// Get Details URL
	var quickViewLink = pipURL.replace('CatalogSearchResultView', 'QuickView');	// Replace Details URL with Quick View
		
	var prodDescription = $(this).parent().parent().html();
	var x = Math.round( ($(document).width() - 545) / 2 );
	var y = Math.round( document.body.scrollTop + ((height - 328) / 2));
	var isIE6 = ($.browser.msie && parseInt($.browser.version) <= 6);
	var container = $('<div id="quickView" />');
	
	if (isIE6) {
		container.addClass('noShadow').bgiframe();
	}
	
	var renderContent = function(html) {
		
		// render QV off-page in order to calculate rendered size and make adjustments
		container
			.css({left: -1000, top: 0})
			.appendTo('body')
			.append('<span class="btnClose" /><div class="qvContent"></div><div class="bottomEdge" />').find('.qvContent').append(html);
				
		$('.btnClose', container).click(function() {
			$(this).parent().addClass('noShadow').fadeOut('fast', function() { 
				$(this).remove();
				$('#quickViewCover').remove();
			});
		});
		
		if ($('.prodDetails', container).height() > 250) {
			$('.prodDescBullets', container).css({height: 'auto', minHeight: '0'});
		}
		
		var qvHeight = 374;
		if (!isIE6) {		
			var qvRenderedHeight = container.height();
			if (qvRenderedHeight > 374) qvHeight = qvRenderedHeight;
		}		
		
		// animate QV
		coverDiv.removeClass('busy');
		$('.btnQuickView').attr('style', false);
		container.children().hide();
		container
			.css({width:1, height:1, left: e.pageX, top: e.pageY})
			.animate({width:470, height:qvHeight, left:x, top:y}, 300, function() {
				$(this).css({border: 0, backgroundColor: 'transparent'}).children().show()
			});
	}
	
	$.ajax({
		url: quickViewLink,
		type: 'GET',
		dataType: 'html',
		success: function(html) {
			var html = $(html);
			$('.prodDescBullets li', html).each(function(i) {
				if (i > 2) {
					$(this).remove();
				}
			});
			renderContent(html);
		}
	});
}


function SaveNumberPerPage(target) {
	var thisSelect = document.getElementById(target);
	var thisSelection = thisSelect[thisSelect.selectedIndex].value;
	createCookie("HDNumberPerPage", thisSelection, 1);	
	window.location.reload();
}

function SetNumberPerPage(target) {
	var thisSelect = document.getElementById(target);
	var thisSelection = Get_Cookie("HDNumberPerPage");	
	if(thisSelection != null){
		thisSelect.value = thisSelection;
	}
}

function createCookie(name,value,days) {
 	if (days) {
  		var date = new Date();
  		date.setTime(date.getTime()+(days*24*60*60*1000));
  		var expires = "; expires="+date.toGMTString();
 	} else {
  		var expires = "";
 	}
 	document.cookie = name+"="+value+expires+"; path=/";
} 

function Get_Cookie(name) { 
	var start = document.cookie.indexOf(name+"="); 
	var len = start+name.length+1; 
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
	if (start == -1) return null; 
	var end = document.cookie.indexOf(";",len); 
	if (end == -1) end = document.cookie.length; 
	return unescape(document.cookie.substring(len,end)); 
} 

