// JavaScript Document
$(document).ready(function(){
	//replace headlines with cufon-based frutiger font
	Cufon.replace('.head,.headline,.subhead,.storeBox b');
	//preload over images
	$(".btn_over").each(function(){var ext=$(this).attr("src").split(".")[1];var overImg=$(this).attr("src").replace("."+ext,"_f2."+ext);preloadImage(overImg);});
	//make over functionality
	$(".btn_over").hover(function(){var ext=$(this).attr("src").split(".")[1];if($(this).attr("src").match("_f2."+ext)){$(this).attr("src",$(this).attr("src").replace("_f2",""));}else{$(this).attr("src",$(this).attr("src").replace("."+ext,"_f2."+ext));}});
	//close popup window
	$("#closeBtn").click(function(){closePop();});
	//clickclear field
	$('.clickClear').bind('focus',function(){$(this).removeClass('clickClear').unbind('focus').val('');});
	//search box
	//$('#submitSearch').click(function(){submitSearchForm();});
	//preview product image
	$('a.view').click(function(){
		$('.previewContent').html('<img src="/images/products/'+$(this).attr('data-pic')+'">');
		var t=($(window).height()-510)/2;
		t=t<10 ? 10 : t;
		$('.preview').css({'margin-top':t+'px'});
		$('.modalFade').show();
	});
	//close pop
	$('.preview .close').click(function(){$('.modalFade').hide();$('.popContent').html('');});
	//buy Options
	$('.selBuyOptions').click(function(){
		$(this).next('.buyOptions').show();
		$('body').click(function(){
			$('.buyOptions').hide();
			$('body').unbind('click');
		});
		$('.buyOptions').click(function(e){e.stopPropagation();});
		return false;
	});
	//shipping options
	$("select.shipSelector").change(function(){updateShipping();});
	updateShipping();
});

function btn_overs(sel,newImage){$("#"+sel).attr("src",newImage);}

function submitSearchForm(){
	var searchInput=$('#searchUI').val();
	if(searchInput!='' && searchInput!='Search'){
		return true;
	}else{
		alert('You must enter something in the search box.');
		return false;		
	}	
}

function updateShipping(){
	if(typeof(priceTable)!='undefinied' && typeof(numItems)!='undefined'){
		var key=$("select.shipSelector").val();		
		$('#shipCharge').html(priceTable[(numItems-1)][key]+'.00');
		var totalPrice=(parseInt($('#subTotalPreShip').val())+parseInt(priceTable[(numItems-1)][key])).toFixed(2);
		var totalPriceDisplay=CommaFormatted(totalPrice);
		$('#shippingSelection').val(key);		
		$('#totalPricePasser').val(totalPrice);
		$('#cartTotal').html(totalPriceDisplay);
		
	}
}

function CommaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    amount = new String(amount);
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}
