var jQ,pnsVars={};
jQuery.noConflict();jQ=jQuery;

if(jQ.browser.msie && jQ.browser.version<7){try{document.execCommand('BackgroundImageCache',false,true)}catch(e){}}

jQ(document).ready(function(){

	//jQ('div.curlang select').change(function(){document.location=jQ(this).val();});
	jQ('div.support a').click(function(){openSupportWindow();return false;});
	jQ('#certified').click(function(){showCertificate();return false;});
	
	var cartInps=jQ('#cart input');
	if(cartInps.length)setCartEvents(cartInps);
	
});

/* ---- cart ---- */

function setCartEvents(cartInps){
	pnsVars.qtyInp=cartInps.filter('[name="qty"]').click(setQty);
	pnsVars.shpInp=cartInps.filter('[name="shipping_method_id"]').click(setCartTotal);
	pnsVars.qtyInp.filter('[checked]').click();
}
function setQty(){
	var b;
	b=jQ(this);
	b.parent().addClass('selected');
	pnsVars.qtyInp.each(function(i,el){
		if(el!=b[0]){
			jQ(el).parent().removeClass('selected');
		}
	});
	setCartTotal();
}
function setCartTotal(){
	var pr,sh,tot;
	pnsVars.qtyInp.each(function(i,el){
		el=jQ(el);
		if(el.parent().hasClass('selected')){
			pr=Number(el.siblings().eq(3).text().match(/[0-9\.]+/));
		}
	});
	pnsVars.shpInp.each(function(i,el){
		el=jQ(el);
		if(el.get(0).checked){
			sh=Number(el.siblings().text().match(/[0-9\.]+/));
			el.parent().addClass('selected');
		}else{
			el.parent().removeClass('selected');
		}
	});
	jQ('#totalprice').html(myRound(pr+sh,2));
}

/* ---- common utils ---- */

var psPNSrsid;
pnsVars.projName='stimul-cash';

function initSupportWindow(){
	var src=(location.protocol.indexOf("https")==0?"https://secure.providesupport.com/image":"http://image.providesupport.com")+"/js/"+pnsVars.projName+"/safe-standard.js?ps_h=PNSr\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(psPNSrsid){
		if(!pnsVars.support_window || pnsVars.support_window.closed){
			pnsVars.support_window=popItUp('http://messenger.providesupport.com/messenger/'+pnsVars.projName+'.html?ps_s='+psPNSrsid+'&ps_mht='+psPNSrmht,'support_window',500,560);
		}else{
			pnsVars.support_window.focus();
		}
	}else{
		popItUp('about:blank','support_window',500,560);
		initSupportWindow();
		var timer = setInterval(function(){
			if(psPNSrsid){
				clearInterval(timer);
				openSupportWindow();
			}
		}, 5);
	}
}
function reloadImageCode(){
	var img=document.getElementById('vcode_img');
	if(!pnsVars.imgCodeUrl) pnsVars.imgCodeUrl=img.src;
	img.src=pnsVars.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(u){
	u=u||'out.php?id=certificates_url';
	if(!pnsVars.cert_window || pnsVars.cert_window.closed){
		pnsVars.cert_window=popItUp(u,'cert_window',500,350);
	}else{
		pnsVars.cert_window.focus();
	}
}

function showSpecialOffer(u){
	u=u||'out.php?id=special_offer_url';
	if(!pnsVars.spo_window || pnsVars.spo_window.closed){
		pnsVars.spo_window=popItUp(u,'spo_window',620,620);
	}else{
		pnsVars.spo_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;
}