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

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

jQ(document).ready(function(){pageOnLoad();});


function pageOnLoad(){
	/* --- common --- */
	
	jQ('a.edit').click(function(){window.open(this.href,'edit','toolbar=0');return false;});
	jQ('.search_bg a').click(function(){
		document.location.href=document.location.protocol+'//'+document.location.host+'/?search='+jQ('#search').val()+'&lang='+jQ('#currentlang').html();
		return false;
	});
	jQ('.search_bg input').click(function(){
		if(this.value==this.defaultValue)this.value=""}
	).blur(function(){
		if(this.value=="")this.value=this.defaultValue}
	);
	//jQ('form tfoot').find('a','button').click(fn_submit);
	jQ('a.bookmark').click(makeBookmark);
	jQ('#reload_vcode_img').click(function(){
		var i=jQ('#vcode_img');
		v.attr('src',v.attr('src')+'?');
		return false;
	});
	
	var crt=jQ('.cart');
	if(crt.length){
		crt.find('.checkout').click(function(){try{document.checkout.submit();}catch(ex){alert(1); };return false});
		crt.find('.update').click(function(){try{document.cart.submit();}catch(ex){};return false});
		crt.find('.continue').click(function(){document.location.pathname='/';return false});
		//crt.find('img.photo').click(function(){return popImg('imgs/pills/blisters/'+jQ(this).attr('prod')+'.jpg');});
		crt.find('img.photo').click(popImg);
	}

	var crt=jQ('.product');
	if(crt.length){
		crt.find('img.photo').click(popImg);
	}
	/* --- site specific --- */
	
	jQ('#big_pic img').mouseover(function(){ jQ('#big_pic').css('background-image','url(/imgs/'+this.id+'.jpg)') });
	var prod=jQ('table.prod');
	if(prod.length){
		prod.each(function(i,el){
			var u=jQ(el).find('.add-inf a').attr('href');
			jQ(el).find('h3, img').click(function(){location=u;});
		});
	}

	swfobject.embedSWF("out/?id=we_accept_banner", "we_accept", "235", "20", "8",null,null,{scale:'noscale',wmode:'transparent',salign:'l'});
	//cart
	jQ('tr.radioCheck').click(function(){
		jQ('.radioCheck input[type="radio"][name="shipping_method_id"]').removeAttr('checked');
		jQ(this).children('td').children('input[type="radio"][name="shipping_method_id"]').attr('checked', 'checked');
		jQ('#cart-shmet').submit();
	});
	//text tables
	jQ('.prod-text.faq tbody tr:even td').css({backgroundColor:'#ECFBFF'});
	jQ('.prod-text.faq tbody tr:odd td').css({backgroundColor:'#F3F3FF'});
	//best product package slider
	var slg = jQ('#line-best-packs .bg_best4');
	if(slg.css('marginRight') && slg.css('marginLeft') && slg.css('paddingRight') && slg.css('paddingLeft')){
		var productBlocks = slg.length;
		var prBlocksWidth = slg.width();
		var margRight = Number(slg.css('marginRight').replace(/\D+/g,"")); 
		var margLeft = Number(slg.css('marginLeft').replace(/\D+/g,""));
		var paddRight = Number(slg.css('paddingRight').replace(/\D+/g,""));
		var paddLeft = Number(slg.css('paddingLeft').replace(/\D+/g,""));
		var slideTo = prBlocksWidth+margRight+margLeft+paddRight+paddLeft;
		var lineWidth = productBlocks*(prBlocksWidth+margRight+margLeft+paddRight+paddLeft);
		jQ('#line-best-packs').css({width:lineWidth});
		var startSlide = true;
		var z = 1;
		/*
		if(startSlide){
			startSlide = false;
			bestPackGalery2(slideTo, z);
			z++;
		}
		*/
		setInterval(function(){
			if(z>productBlocks) z = 1; 
			bestPackGalery2(slideTo, z); 
			z++; 
		}, (2000+(5*slideTo+1)));
	}
}
/*
function bestPackGalery(slideTo){
	var buf='';
	jQ('#line-best-packs').animate({left:'-'+slideTo+'px'},3000,function(){
		buf = jQ('#line-best-packs .bg_best4:first').remove();
		jQ('#line-best-packs').append(buf).css({left:'0px'});
		
	});
}*/
function bestPackGalery2(slideTo, z){
var oldDivBlock = '';
var oldDivBlockClone = '';
var newDivBlock = '';
var n = 0;
for(var i=0; i<slideTo; i++){
	setTimeout(function(){
		document.getElementById('line-best-packs').style.left = '-'+n+'px'; 
		n++;
		if(n==slideTo){
			oldDivBlock = document.getElementById('product_item_'+z);
			oldDivBlockClone = oldDivBlock.cloneNode(true);
			oldDivBlock.parentNode.removeChild(oldDivBlock);
			newDivBlock = document.getElementById('line-best-packs').appendChild(oldDivBlockClone);
			n = 0;
			document.getElementById('line-best-packs').style.left = '-'+n+'px';
		}
	}, 5*(i+1));
}
}

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

var utilVars={projName:'rx-partners'}, psWWWrsid;

function openSupportWindow(){ showWindow('support_window','out/?id=live_support_url',500,560); }

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);
	}
	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 makeBookmark(){
	var host=document.location.host,
	u='http://'+host+'/';
	try{
		if (window.sidebar){
		window.sidebar.addPanel(this.title, u, '');
		}else if(document.all){
		window.external.AddFavorite(u, this.title);
		}
	}catch(ex){alert(ex.message)}
	return false;
}

/* --------------- */

function popImg(){
	var u='/imgs/pills/blisters/'+jQ(this).attr('prod')+'.jpg';
	var h='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
	+'<head><title></title>'
	+'<style>html,body,table{margin:0;padding:0;height:100%;width:100%;text-align:center}</style>'
	+'</head><body onclick="close();"><table><tr><td><img src="'+u+'" /></td></tr></table></body></html>';
	var wnd=popItUp('about:blank','img_window',520,520);
	wnd.document.write(h);
	wnd.focus();
	return false;
}

function buy_selected_product_package(prod){
	var buy_url = jQ('#index_product_'+prod+'_packages').val();
	window.location = buy_url;
}


//function showLiveSupport(){popItUp('out/?id=livesupport',620,620)}



