compareLinkEnabled = false;

jQuery.fn.center = function (leftEdge) {
	this.css("position","absolute");
	var newTop = ( $(window).height() - this.height() ) / 2+$(window).scrollTop() ;
	var newLeft = ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() ;

	//alert('this.height:'+this.height() + '  this.width:' + this.width() + '   scrollTop:' + $(window).scrollTop()+ '  scrollLeft:' + $(window).scrollLeft() + '  newTop:' +newTop + '  newLeft:' + newLeft + '  leftEdge:' + leftEdge);
	if (newLeft < (leftEdge+75))
	{
		newLeft =leftEdge+75;
	}
	this.css("top", newTop + "px");
	this.css("left", newLeft + "px");
	return this;
}




$(document).ready(function(){


	$('#searchform').submit(function() {
		$("#searchform #search").addClass('form-search-submitted');
	});

	generateCompareLink();
	
	$('input.form-required-field').blur(function(){
		var curVal= $(this).val();
		if (curVal=="")
			$(this).addClass('form-required-field-error');
		else
			$(this).removeClass('form-required-field-error');
	});


	$('input[name="part_qty_1"]').change(function(){
		var qty = $(this).parent().find('input[name="part_qty_1"]').val();
		var min = $(this).parent().find('input[name="min"]').val();
		var mult = $(this).parent().find('input[name="mult"]').val();

		qty = parseInt(qty);
		min = parseInt(min);
		mult = parseInt(mult);
		if (qty!=0)
		{
			// check that the quantity meets the minimum reqs
			if ( (min && (qty < min)) || (mult && (qty%mult) != 0) ) {
				$(this).parent().find('input[name="part_qty_1"]').addClass('qtyError');
				$(this).parent().parent().addClass('qtyError');
			} else {
				$(this).parent().parent().removeClass('qtyError');
				$(this).parent().find('input[name="part_qty_1"]').removeClass('qtyError');
			}
		}
	});
	$('form.quantity').bind('submit', function(){
		var qty = $(this).find('input[name="part_qty_1"]').val();
		var min = $(this).find('input[name="min"]').val();
		var mult = $(this).find('input[name="mult"]').val();
		qty = parseInt(qty);
		min = parseInt(min);
		mult = parseInt(mult);
		if (qty!=0)
		{
			// check that the quantity meets the minimum reqs
			if ( min && (qty < min) ) {
				alert('Please enter a quantity of at least ' + min);
				return false;
			}

			// check that the quantity meets the multiple reqs
			if ( mult && (qty%mult) != 0 ) {
				alert('Please enter a quantity in multiples of ' + mult);
				return false;
			}
		}
	});


	$('input.checkbox').click(function (){
		generateCompareLink();
	});

	$('#promo-button-link').click(function (){
		$('#promo-button-link').hide();
		$('#promo-button-content').show(350);
		fixProductDetailsRow();
	});

	$('#promo-button-type-link').click(function (){
		$('#promo-button-link').hide();
		$('#promo-button-content').show(350);
		fixProductDetailsRow();
	});

	$('#promo-body-type-link').click(function (){
		$('#promo-button-link').hide();
		$('#promo-button-content').show(350);
		fixProductDetailsRow();
	});

	 

	$('.compareLink').click(function(){
		if ( !compareLinkEnabled ) {
			alert('Please select 2 or 3 items to compare.');
			return false;
		}
	});

	$('a.preview').click(function(){
		return false;
	});
});

function generateCompareLink() {
		j = 0;
		url = '/Catalog/CompareProducts.aspx?action=Compare';
		$('input.checkbox').each(function(i){
			if ( $(this).attr('checked') ) {
				$(this).parent().addClass('checked');
			} else {
				$(this).parent().removeClass('checked');
			}

			if ( $(this).attr('checked') ) {
				value = $(this).val();
				value = value.split('|');
				//console.log(value);
				j++;
				url += '&part_number_'+j+'=' + value[0];
				url += '&part_mfg_'+j+'=' + value[1];
				url += '&part_act_'+j+'=yes';
				url += '&part_qty_'+j+'=';
			}
		});

		if ( j == 2 || j == 3 ) {
			// enable the compare link
			$('.compareLink').attr('href', url);
			$('.comparetext').addClass('compareEnabled');
			compareLinkEnabled = true;
		} else {
			$('.comparetext').removeClass('compareEnabled');
			compareLinkEnabled = false;
		}
}

function toggle(box,theId,offname)
{
    if (document.getElementById) {
        var row = document.getElementById(theId);
        if (box.checked) {
            row.className = "rowselected";
        } else {
            row.className = offname;
        }
    }
}




function OpenHelpWindow(HelpCode) {
day = new Date();
id = day.getTime();
eval("pageHelp = window.open('/content/help.aspx?Code='+HelpCode, 'help', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=275,height=250,left = 100,top = 100');");
pageHelp.focus();
}

function OpenHelpWindowWithSize(HelpCode, pHeight, pWidth) 
{
	day = new Date();
	id = day.getTime();
	eval("pageHelp = window.open('/content/help.aspx?Code='+HelpCode, 'help"+  HelpCode +"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + pWidth + ",height=" + pHeight + ",left = 100,top = 100');");
	pageHelp.focus();
}

function CompareParts() 
	{
	var inputok=false;
	for (var j = 1; j <= document.catalog_act.max_part.value; j++) 
		{
		box = eval("document.catalog_act.part_act_" + j); 		    
		if (box.checked == true) inputok= true;
		}
	if (inputok==false)
		{
		alert('Please select at least one item for comparison view');
		return false;
		}
	else
		{
		return true;
		}
	}




function SaveParts() 
	{
	var inputok=false;
	for (var j = 1; j <= document.catalog_act.max_part.value; j++) 
		{
		box = eval("document.catalog_act.part_act_" + j); 		    
		if (box.checked == true) inputok= true;
		}
	if (inputok==false)
		{
		alert('Please select at least one item to save');
		return false;
		}
	else
		{
		return true;
		}
	}



function AddToCart() 
	{
	var inputok=false;
	for (var j = 1; j <= document.catalog_act.max_part.value; j++) 
		{
		box = eval("document.catalog_act.part_act_" + j); 		    
		txt= eval("document.catalog_act.part_qty_" + j); 		    
		if (txt==undefined)
		{
		}
		else
			{
			if (box.checked == true) inputok= true;
			if (txt.value!='') inputok=true;
			}	

		}
	if (inputok==false)
		{
		alert('Please select at least one item to add to the cart');
		return false;
		}
	else
		{
		return true;
		}
	}

/* *** */

/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = -20;
		yOffset = 20;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){

		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 

		$('#preview').center(e.pageX);
		$("#preview").fadeIn("fast");

			//.css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() - 200 + "px")
			//.css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() - 100 + "px")
			//.css("top",(e.pageY - xOffset) + "px")
			//.css("left",(e.pageX + yOffset) + "px")
    },
	function(){
		//SOS alert('off');
		this.title = this.t;	
		$("#preview").remove();
    });	
	/*
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() - 200 + "px")
			.css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() - 100 + "px");
			//.css("top",(e.pageY - xOffset) + "px")
			//.css("left",(e.pageX + yOffset) + "px");
	});			
	*/
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});




	function OpenCCVWindow() 
	{
		day = new Date();
		id = day.getTime();
		eval("pageCCV = window.open('/Account/ccv/default.htm', 'ccv', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=375,left = 200,top = 200');");
		pageCCV.focus();
	}





// promo box button height
$(document).ready(function(){
	$('div.promo-wide-box div.promo-button').height(   $('div.promo-wide-box div.promo-body').height()        );
	$('div.promo-wide-box div.promo-button').css("line-height" ,   $('div.promo-wide-box div.promo-body').height() + "px"        );
});


function SessionWarning()
{
	if (confirm("There has been no activity for some time.\nClick 'OK' if you wish to continue your session."))
	{
		document.location.href = document.location.href;
	}
}


