var LZYLDLOCK=false;
var BTNShown=false;
jQuery(window).scroll(function(){
	var scrollBottom = jQuery(window).scrollTop() + jQuery(window).height();
	var scrollTopLimit = jQuery(document).height() - 350;
	
	if(scrollBottom > scrollTopLimit && !LZYLDLOCK) {
		refineB();
	}
	try {
		if(jQuery(window).scrollTop() == 0) {
			jQuery("#vabacktotop").hide();
			BTNShown = false;
		} else {
			if(BTNShown == false) {
				BTNShown = true;
				jQuery("#vabacktotop").show();
				if(backtopbtn_middle) {
					var calculatedTopBTN = 0;
					calculatedTopBTN = (jQuery(window).height() - jQuery('#vabacktotop').outerHeight()) / 2;
					if(calculatedTopBTN < 0) {
						calculatedTopBTN = 0;
					}
					jQuery("#vabacktotop").css('top',calculatedTopBTN+'px');
					jQuery("#vabacktotop").css('bottom','');
				}
			}
		}
	}catch(err){}
});
function showPriceRRViewall(prodNum){
	//$(prodNum).style.visibility = "visible";
	$('backgroundDiv_'+prodNum).style.visibility = "visible";
	$('productInfo_'+prodNum).style.visibility = "visible";
}

function hidePriceRRViewall(prodNum){
	$('backgroundDiv_'+prodNum).style.visibility = "hidden";
	$('productInfo_'+prodNum).style.visibility = "hidden";
}

function swapVAimage(prodID,imgURL,ele,colorCode,catID,imgURLTwo,pricing){
	$('VA_'+prodID+'_'+catID+'_1').src = imgURL;
	$('VA_'+prodID+'_'+catID+'_2').src = imgURLTwo;
	
	var chips = $$('.cgid_'+catID+' .CARTchip'+prodID);
	var i=0;
	for(i=0; i<chips.length; i++){
		if (chips[i] != null) { // safari fix
			chips[i].className = "CARTchip"+prodID+" availableChip";
		}
	}

	var links = $$('.cgid_'+catID+' .prLink_'+prodID);
	var ii=0;
	for(ii=0; ii<links.length; ii++){
		if (links[ii] != null) { // safari fix
			links[ii].href = changeValue(links[ii].href,'variantColor',colorCode);
		}
	}
	ele.className = "CARTchip"+prodID+" selectedChip";
}
function rotateVAimage(prodID,catID){
	if(IsMouseOverVAImg) {
		jQuery('#Gradiant_'+prodID+'_'+catID).show();
		//jQuery('#VA_'+prodID+'_'+catID+'_2').fadeTo(1000,1);
		jQuery('#VA_'+prodID+'_'+catID+'_1').stop().fadeTo(1,.001);
	}
}
function rotateBackVAimage(prodID,catID){
	if(IsMouseOverVAImg) {
		jQuery('#VA_'+prodID+'_'+catID+'_1').stop().fadeTo(1,1);
		//jQuery('#VA_'+prodID+'_'+catID+'_2').fadeTo(1000,.001);
	}
}
function changeValue(str,param,value) {
	var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (str.match(re)) {
        return str.replace(re,'$1' + param + "=" + value + '$2');
	}
}
function showMCSign(prodID,ele,catID){
	$('cgid'+catID+'_pid'+prodID).style.display = 'block';
	ele.className = "onState";
}
function hideMCSign(prodID,ele,catID){
	$('cgid'+catID+'_pid'+prodID).style.display = 'none';
	ele.className = "offState";
}
function toggleView(e,idd) {
	if (!e) var e = window.event;

	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	
	jQuery(".refineattributes").each(function(){ if(jQuery(this).attr('id') != idd){hideallrefineboxes(jQuery(this).attr('id'))}});
	jQuery(".soringLinks").each(function(){ if(jQuery(this).attr('id') != idd){hideallrefineboxes(jQuery(this).attr('id'))}});
	var boxx = $(idd+"_box"); 
	if($(idd)&& idd != 'body') {
		var iddDisplayValue = "";
		try{
			if($(idd).opened) {
				iddDisplayValue = $(idd).opened;
			} else {
				iddDisplayValue = 'no';
				$(idd).opened = 'no';
			}
		}catch(err){
			iddDisplayValue = 'no';
			$(idd).opened = 'no';
		}
		if(iddDisplayValue == "yes"){
			
			$(idd).opened="no";
			jQuery("#"+idd).fadeOut(500);
			boxx.className="dropDownBox";
		}
		if(iddDisplayValue == "no" || iddDisplayValue == ""){
			$(idd).opened="yes";
			boxx.className="dropDownBox dropDownBoxOpened";
			//new Effect.BlindDown($(idd), { duration: .4, afterFinish: function(effect) 
		 		//{}});
			jQuery("#"+idd).fadeIn(300);
		}
	}
}
function hideallrefineboxes(idd) {
	var boxx = $(idd+"_box"); 
	if($(idd)) {
		var iddDisplayValue = "";
		try{
			if($(idd).opened) {
				iddDisplayValue = $(idd).opened;
			} else {
				iddDisplayValue = 'no';
				$(idd).opened = 'no';
			}
		}catch(err){
			iddDisplayValue = 'no';
			$(idd).opened = 'no';
		}
		if(iddDisplayValue == "yes"){	
			$(idd).opened="no";
			jQuery("#"+idd).fadeOut(500);
			boxx.className="dropDownBox";
		}
	}	
}
// returns parent div with id="prod_..."
function getParentDiv(ele) {
	var divObj = null;
	try {
		var parentsObj = ele.ancestors();
		var iDiv = 0;
		for(var i = 0; i < parentsObj.length; i++) {
		    if(parentsObj[i].id.indexOf("prod_") > -1) {
		        iDiv = i;
		        break;
		    }
		}
		divObj = parentsObj[iDiv];
	}catch(err){}
	return divObj;
}
function refineA(anchorr){

	var url = anchorr.href;
	/*
	var winSearch = window.location.search;
	var idx = url.indexOf(winSearch);
	if (idx >= 0)
	{
		alert(window.location.search);
		window.location.hash = url.substring(idx + winSearch.length);
	}
	*/
	try {
		document.body.style.cursor='wait';
		jQuery("#ajaxLoadingSpinner").css("visibility","visible");
		jQuery("#productsgallery").fadeTo(1000, 0.2);

		new Ajax.Request(url+'&ajax=true', {
		  method: 'get',
		  onSuccess: function(transport) {
			if(transport.status == 200) {
				try {
					jQuery('#contentProductDisp').replaceWith(transport.responseText); 
				    document.body.style.cursor='default';
				} catch (err) {
					document.location = url;
				}
			} else {
				//alert("AJAX HTTP Status Code: "+transport.status+" manually loading selection, please hit ok.");
				document.location = url;
			}
			var newURLParts = document.getElementById('newURL').value.split('?');
			if(newURLParts.length == 2){
				var ttt = newURLParts[1]+'&rfnd=true';
				if(window.location.href.indexOf('allproducts=true') != -1) {
					ttt += '&allproducts=true';
				}
				window.location.hash = ttt;
			}
			try{
				if($('trackingDataOMN')) {
					_metadata = eval($('trackingDataOMN').value)[0];
					_analytics.trackMetrics('custom',_metadata);
				}
				resx.links = certonaIds.substring(0,certonaIds.length-1);
				certonaResx.run();
			} catch (e){}
		},
		onFailure: function(){document.location = url;}
		});
	} catch (err) {
		document.location = url;
	}
}
function refineB(){
	if(typeof lazyloadinglinks  != "undefined" && lazyloadinglinks != null && lazyloadinglinks.length > 0 && lazyloadinglinks[currPageCounter+1] != null) {
		var url = lazyloadinglinks[currPageCounter+1];
		currPageCounter++;		
		try {
			document.body.style.cursor='wait';
			jQuery("#ajaxLoadingSpinner").css("visibility","visible");
			jQuery("#ajaxLoadingSpinner2").css("visibility","visible");
			LZYLDLOCK=true;
			new Ajax.Request(url+'&ajax=true&lazy=true', {
			  method: 'get',
			  onSuccess: function(transport) {
				jQuery("#ajaxLoadingSpinner").css("visibility","hidden");
				jQuery("#ajaxLoadingSpinner2").css("visibility","hidden");
				if(transport.status == 200) {
					try {
						jQuery('#productsgallery').append(transport.responseText); 
					    document.body.style.cursor='default';
						if(!lazyloadinglinks[currPageCounter+1]){
							jQuery("#showMoreResultHolder").css("visibility","hidden");
							jQuery("#showMoreResultsBox").css("display","none");
						}
					} catch (err) {
						document.location = url;
						LZYLDLOCK=false;
					}
					try{
						resx.links = certonaIds.substring(0,certonaIds.length-1);
						certonaResx.run();
					} catch (e){}
				} else {LZYLDLOCK=false;}
				LZYLDLOCK=false;
			},
			onFailure: function(){LZYLDLOCK=false;}
			});
		} catch (err) {}
	}
}
function opencallout(calloutnumber){
	
	jQuery('#callout' + calloutnumber + 'open').animate({height:'show'},{duration:1000});
	jQuery('#callout' + calloutnumber).hide();
	
}
function closecallout(calloutnumber){
	jQuery('#callout'+ calloutnumber).show();
	jQuery('#callout' + calloutnumber + 'open').animate({height:'hide'},{duration:1000});
	
	
}
