
$().ready(function(){
	productCropCheckScroll();
	$("#potd .item").hover(
		function(){
			$(this).addClass('sfhover');
		},
		function(){
			$(this).removeClass('sfhover');
		}
	);
});

function productCropCheckScroll(){
	$('#productCrop td.imgCell a').hover(
		function(){
			$(this).addClass("sfhover");
		},
		function(){
			$(this).removeClass("sfhover");
		}
	);
	if ($("#iProductCrop").length > 0) {
		productCropDiv = "#iProductCrop";
	} else {
		productCropDiv = "#productCrop";
	}
	if ($(productCropDiv+" .cropContent td").length > 0) {
		if ($.browser.msie) {
			cellWidth = $(productCropDiv+" .cropContent td").css("width").replace("px", "");
			$(productCropDiv + " .cropContent td.imgCell").append('<br><img src="/images_gl/spacer.gif" width="'+cellWidth+'" height="1" />');
		}
		
		cropWidth = $(productCropDiv).width();
		contentWidth = $(productCropDiv+" .cropContent table").width() - 20; // take away 20 for any extra padding on the first item
		
		if (contentWidth > cropWidth) {
			if ($(productCropDiv+" .cropContent .prevScroll").length == 0) {
				$(productCropDiv+" .cropContent").append('<a href="#" onclick="cropScroll(-1); return false;" class="prevScroll" title="scroll back">&lt;</a><a href="#" class="nextScroll" onclick="cropScroll(1); return false;" title="scroll forward">&gt;</a>');
			}
			$(productCropDiv+" .cropContent .nextScroll").css({ "visibility" : "visible" });
		}
	}
}
function cropScroll(wDir){
	if ($("#iProductCrop").length > 0) {
		productCropDiv = "#iProductCrop";
	} else {
		productCropDiv = "#productCrop";
	}
	cropWidth = $(productCropDiv).width();
	contentWidth = $(productCropDiv+" .cropContent table").width();
	cellWidth = $(productCropDiv+" .cropContent td").outerWidth();
	numCells = Math.ceil((contentWidth-cropWidth)/cellWidth);
	currIndex = (0 - Math.ceil($(productCropDiv+" .cropContent table").css("marginLeft").replace("px", "")/cellWidth));
	if (wDir == 1) {
		if (currIndex < numCells) {
			$(productCropDiv+" .cropContent table").animate({ marginLeft: 0-((currIndex+1)*cellWidth) },400);
			newIndex = currIndex+1;
		}
	} else {
		if (currIndex > 0) {
			$(productCropDiv+" .cropContent table").animate({ marginLeft: 0-((currIndex-1)*cellWidth) },400);
			newIndex = currIndex-1;
		}
	}
	if (newIndex < numCells) {
		$(productCropDiv+" .cropContent .nextScroll").css({ "visibility" : "visible" });
	} else {
		$(productCropDiv+" .cropContent .nextScroll").css({ "visibility" : "hidden" });
	}
	if (newIndex > 0) {
		$(productCropDiv+" .cropContent .prevScroll").css({ "visibility" : "visible" });
	} else {
		$(productCropDiv+" .cropContent .prevScroll").css({ "visibility" : "hidden" });
	}
}

function updateProductCrop(){
	loadProductCrop('productCrop', cropCurrFilter, cropCurrSortBy);
}

function updateProductCropV3(){
	loadProductCropV3('productCrop', cropCurrFilter, cropCurrSortBy);
}

function loadProductCrop(cropParentDiv, wFilter, wSortBy){
	var query = "sortby=" + wSortBy + '&filter=' + wFilter;
	
	$("#"+cropParentDiv+" .loading").show();
	$.get("/common_gl/inc_product_crop.php", query, function(data){
		$("#"+cropParentDiv+" .cropContent").html(data);
		$("#"+cropParentDiv+" .loading").hide();
		$("#"+cropParentDiv+" .menu a").removeClass('sel');
		$("#"+cropParentDiv+" .menu a."+wSortBy).addClass('sel');
		productCropCheckScroll();
	});
}


// site version 3 stuff

function loadProductCropV3(cropParentDiv, wFilter, wSortBy){
	var query = "sortby=" + wSortBy + '&filter=' + wFilter;
	
	$("#"+cropParentDiv+" .loading").show();
	$.get("/common_gl/inc_product_crop_v3.php", query, function(data){
		$("#"+cropParentDiv+" .cropContent").html(data);
		$("#"+cropParentDiv+" .menu .loading").hide();
		$("#"+cropParentDiv+" .menu h2").html($("#"+cropParentDiv+" .menu li."+wSortBy+" a").html());
		$("#"+cropParentDiv+" .menu li").removeClass('sel');
		$("#"+cropParentDiv+" .menu li."+wSortBy).addClass('sel');
		fixLiWidths('productCrop');
		productCropCheckScroll();
	});
}
function initProductCrop(){
	$("#productCrop .menu li a").each(
		function(){
			if ($(this).hasClass('sel')) {
				$(this).removeClass('sel');
				$(this).parent().addClass('sel');
			}
			$(this).parent().addClass($(this).attr('class'));
		}
	);
	$("#productCrop .menu li").hover(
		function(){
			$(this).addClass('sfhover');
		},
		function(){
			$(this).removeClass('sfhover');
		}
	);
	fixLiWidths('productCrop');
}
function fixLiWidths(wId){
	maxWidth = 0;
	$("#"+wId+" .menu li, #"+wId+" .menu h2").css({ 'width':'auto' });
	maxWidth = Math.max(maxWidth, $("#"+wId+" .menu ul:first").innerWidth());
	$("#"+wId+" .menu li ul li").each(
		function(){
			maxWidth = Math.max(maxWidth, $(this).innerWidth());
		}
	);
	$("#"+wId+" .menu li ul li").css({
		'width':maxWidth
	});
	$("#"+wId+" .heading h2").css({
		'width':maxWidth-38
	});
}
