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

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

hveVars.videos=['fMPqt0pa2pU'];
hveVars.utu='http://www.youtube.com/v/';
//hveVars.fl_par={width:'100%',height:'100%',flashvars:{color1:'0x006699',color2:'0x54abd6',border:1}};

jQ(document).ready(function(){
	
	var v=hveVars.videos;
	jQ('div.video-placer').each(function(i,el){
		//hveVars.fl_par.swf=hveVars.utu+v[i];
		//jQ(el).flash(hveVars.fl_par);
		jQ(el).flash({swf:hveVars.utu+v[i],width:'100%',height:'100%'});
	});

	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(cs){
	cs.click(submitForm);
	var cartInps=jQ('form[name=cart]').find('input');
	hveVars.qtyInp=cartInps.filter('[name=qty]').click(setQty);
	hveVars.qtyLi=hveVars.qtyInp.closest('li');
	hveVars.shpInp=cartInps.filter('[name="shipping_method_id"]').click(setCartTotal);
	hveVars.shpLab=hveVars.shpInp.closest('label');
	setQty();
}
function setQty(){
	hveVars.qtyLi.filter('.active').removeClass('active');
	hveVars.qtyInp.filter('[checked]').closest('li').addClass('active');
	//if(jQ.browser.msie && jQ.browser.version<7) IEPNGFix.update();
	setCartTotal();
}
function setCartTotal(){
	var pr,sh,tot,l,sIn;
	pr=Number( hveVars.qtyInp.filter('[checked]').closest('strong').text().match(/\d+\.\d+/) );
	hveVars.shpLab.filter('.active').removeClass('active');
	sIn=hveVars.shpInp.filter('[checked]');
	sh=Number( sIn.next().text().match(/[0-9\.]+/) );
	sIn.closest('label').addClass('active');
	jQ('#totalprice').html(myRound(pr+sh,2));
}

function submitForm(){
	jQ(this).closest('form').submit();
}


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

var utilVars={projName:'stimul-cash'}, psWWWrsid;
function initSupportWindow(){
	var src=(location.protocol.indexOf("https")==0?"https://secure.providesupport.com/image":"http://image.providesupport.com")+"/js/"+utilVars.projName+"/safe-standard.js?ps_h=WWWr\u0026ps_t="+new Date().getTime();
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', src);
	document.getElementById('live_support').appendChild(js);
}
function openSupportWindow(){
	if(psWWWrsid){
		showWindow('support_window','out/?id=live_support_url',500,560);
	}else{
		popItUp('about:blank','support_window',500,560);
		initSupportWindow();
		var timer = setInterval(function(){
			if(psWWWrsid){
				clearInterval(timer);
				openSupportWindow();
			}
		}, 5);
	}
}
function showCertificate(u){
	u=u||'out/?id=certificates_url';
	showWindow('cert_window',u,500,350);
}
function showSpecialOffer(u){
	u=u||'out/?id=special_offer_url';
	showWindow('spo_window',u,620,620);
}
function showWindow(id,u,w,h){
	if(!utilVars[id] || utilVars[id].closed){
		utilVars[id]=popItUp(u,id,w,h);
	}else{
		utilVars[id].focus();
	}
}
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 reloadImageCode(){
	var img=document.getElementById('vcode_img');
	if(!utilVars.imgCodeUrl) utilVars.imgCodeUrl=img.src;
	img.src=utilVars.imgCodeUrl+'?'+Math.random();
}

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;
}

