var jQ,ygr={};
jQuery.noConflict();jQ=jQuery;

if(jQ.browser.msie && jQ.browser.version<7){try{document.execCommand('BackgroundImageCache',false,true)}catch(e){}}
jQ(document).ready(function(){setCartEvents();


jQ('#live_support').click(function(){openSupportWindow();return false;});
	jQ('#certified').click(function(){showCertificate();return false;});
	
	var cs=jQ('#cart_submit');
	if(cs.length)setCartEvents(cs);
	
	jQ('#we_accept').flash({
		swf:'out/?id=we_accept_banner',
		//swf:'we-accept.swf',
		width:'100%',height:20,
		params:{scale:'noscale',wmode:'transparent',salign:'l'}
	});




});

/* ---- cart ---- */
function setCartEvents(){
	var b=jQ('div.shopping input[name="qty"]').click(setQty);
	if(b.length==0)return;
	ygr.qtyInp=b;
	ygr.shpInp=jQ('div.shopping input[name="shipping_method_id"]').click(setCartTotal);
	jQ.each(ygr.qtyInp, function(i,el){
		el=jQ(el);
		if(el.attr('checked')){el.click();}
	});
}
function setQty(){
	var b;
	b=jQ(this);
	b.parent().addClass('selected');
	jQ.each(ygr.qtyInp, function(i,el){
		if(el!=b[0]){
			jQ(el).parent().removeClass('selected');
		}
	});
	setCartTotal();
}
function setCartTotal(){
	var pr,sh,tot;
	jQ.each(ygr.qtyInp, function(i,el){
		el=jQ(el);
		if(el.parent().hasClass('selected')){
			pr=Number(el.siblings().text().match(/[0-9\.]+/));
		}
	});
	jQ.each(ygr.shpInp, function(i,el){
		el=jQ(el);
		if(el.get(0).checked){
			sh=Number(el.siblings().eq(1).children('span').text().match(/[0-9\.]+/));
			el.parent().addClass('selected');
		}else{
			el.parent().removeClass('selected');
		}
	});
	jQ('#totalprice').html(myRound(pr+sh,2));
}

/* ---- common ---- */
var psYGRrsid;
function initSupportWindow(){
	var src=(location.protocol.indexOf("https")==0?"https://secure.providesupport.com/image":"http://image.providesupport.com")+"/js/stimul-cash/safe-standard.js?ps_h=YGRr\u0026ps_t="+new Date().getTime();
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', src);
	document.getElementById('live_support').appendChild(js);
}
function openSupportWindow(){
	if(psYGRrsid){
		if(!ygr.support_window || ygr.support_window.closed){
			ygr.support_window=popItUp('out/?id=live_support_url','support_window',500,560);
		}else{
			ygr.support_window.focus();
		}
	}else{
		popItUp('about:blank','support_window',500,560);
		initSupportWindow();
		var timer = setInterval(function(){
			if(psYGRrsid){
				clearInterval(timer);
				openSupportWindow();
			}
		}, 5);
	}
}
var imgCodeUrl;
function reloadImageCode(){
	var img=document.getElementById('vcode_img');
	if(!imgCodeUrl) imgCodeUrl=img.src;
	img.src=imgCodeUrl+'?'+Math.random();
}

function popItUp(u,n,w,h){
	var l=(screen.availWidth  - w) / 2, t=(screen.availHeight - h) / 2
	,op='toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+l+',top='+t
	,n=n||'_blank';
	return window.open(u,n,op);
}
function showCertificate(){
	if(!ygr.cert_window || ygr.cert_window.closed){
		ygr.cert_window=popItUp('out/?id=certificates_url','cert_window',500,350);
	}else{
		ygr.cert_window.focus();
	}
}
function myRound(x, n){
	var dec=Math.pow(10,n),
	val=(Math.round(dec*x)/dec).toString(),
	i=val.indexOf('.'),out;
	if(i<0){
		out=val+'.00';
	}else if(val.length-i==2){
		out=val+'0';
	}else{
		out=val;
	}
	return out;
}

