
var productimages, facVars={};

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

jQ(document).ready(function(){
	
	facVars.path='';
	
	jQ('div.brands a, div.categories a').each(function(i,a){
		a=jQ(a);
		if(a.attr('scrollWidth')>a.innerWidth()){
			myTruncate(a);
		}
	});
	
	swBrands.init();
	
	var nav4=jQ('div.nav4');
	if(nav4.length){
		var sp=nav4.children(':last');
		if(sp.length && sp.offsetParent()+sp.width()>380){
			myTruncate(sp,nav4);
		}
	}
	
	homeSpo.init();
	
	/* product page */
	
	if (!productimages) return;
	
	facVars.productimagenum = 0;
	facVars.selectedColor = 0;//Object.keys(productimages)[0];
	
	jQ(productimages).each(function(k,o){
		var sw = jQ("#switch"+k);
		if (sw.length){
			if(productimages.length>1)sw.click(switchColor);
			if(k==facVars.selectedColor)sw.addClass('selected');
		}
	})
	
	drawSizes();
	setSize();
	
	jQ('#colorselect').change(switchColor);
	jQ('#sizeselect').change(setSize);
	
	//setSize();
	
	jQ('#img_prev').click(switchImage);
	jQ('#img_next').click(switchImage);
	
	jQ('#img_zoom').mouseover(zoomOn).mouseout(zoomOff).click(function(){return false;});
	
});

function switchImage(){
	//Event.stop(e);
	if(this.id=='img_next'){
		facVars.productimagenum++;
	}else if(this.id=='img_prev'){
		facVars.productimagenum--;
	}

	facVars.productimagenum = (facVars.productimagenum+productimages[facVars.selectedColor].length)%productimages[facVars.selectedColor].length;

	jQ('#prod_img').attr('src', facVars.path+productimages[facVars.selectedColor][facVars.productimagenum]);
	return false;
}
function switchColor(){
	var c, span = this.id && this.id.indexOf('switch')==0;
	if(span){
		c = this.id.substr(6);
		jQ('#colorselect').val(c);
	}else{
		c = jQ(this).val();
	}
	
	if (c==facVars.selectedColor) return;
	// || !productimages[c]) return;
		
	facVars.selectedColor = c;
	facVars.productimagenum = 0;
	jQ('#prod_img').src = facVars.path+productimages[facVars.selectedColor][facVars.productimagenum];
	
	drawSizes();
	setSize();
	
	jQ(productimages).each(function(k,o){
		var sw=jQ("#switch"+k);
		if (facVars.selectedColor == k){
			sw.addClass('selected');
		}else{
			sw.removeClass('selected');
		}
	});
	
}
function drawSizes(){
	var i, h='',
	s=productsizes[facVars.selectedColor],
	ks=jQ.keys(s);
	if(ks.length<2){
		jQ('#size_holder').html('<div>'+ks[0]+'</div>');
	}else{
		ks.each(function(i,k){h+='<option value="'+k+'">'+k+'</option>';})
		var ss=jQ('#sizeselect');
		if(ss.length){
			ss.html(h);
		}else{
			jQ('#size_holder').html('<select id="sizeselect">'+h+'</select>');
			jQ('#sizeselect').change(setSize);
		}
		
	}
}

function setSize(){
	
	var s=productsizes[facVars.selectedColor],href,ss;
	ss=jQ('#sizeselect');
	if(ss.length){
		href=s[ss.val()];
	}else{
		href=s[jQ('#size_holder').text()];
	}
	jQ('#add_to_cart').attr('href',href);
}
function zoomOn(){jQ('#prod_img_cont').addClass('zoom');}
function zoomOff(){jQ('#prod_img_cont').removeClass('zoom');}


var swBrands=new function(){
	var as,dv,ind=0;
	this.init=function(){
		as=jQ('#nav_brands_top, #nav_brands_all').click(swBrands.swtch);
		dv=jQ('#div_brands_top, #div_brands_all');
	}
	this.swtch=function(){
		var a=jQ(this);
		var i=as.index(a);
		if(i==ind)return;
		as.eq(ind).removeClass('selected');
		dv.eq(ind).hide();
		as.eq(i).addClass('selected');
		dv.eq(i).show();
		ind=i;
		return false;
	}
}

var homeSpo=new function(){
	var itms,as,ind=0;
	this.init=function(){
		var spo=jQ('div.spo');
		if(!spo.length)return;
		itms=spo.find('div.item');
		as=spo.find('div.pgr a').mouseover(homeSpo.swtch);
		as.click(homeSpo.clck);
		homeSpo.play();
	}
	this.swtch=function(i){
		var a;
		//console.debug('i=',i);
		if(i>=0){
			a=jQ(as[i]);
		}else{
			clearInterval(homeSpo.tmr);
			homeSpo.play();
			a=jQ(this);
			i=as.index(a);
		}
		if(i==ind)return;
		as.eq(ind).removeClass('selected');
		//itms.eq(ind).removeClass('selected');
		itms.eq(ind).fadeOut('normal');
		a.addClass('selected');
		//itms.eq(i).addClass('selected');
		itms.eq(i).fadeIn('normal');
		ind=i;
	}
	this.clck=function(e){
		//Event.stop(e);
		return false;
	}
	this.play=function(){
		setTimeout(function(){
			clearInterval(homeSpo.tmr);
			homeSpo.tmr = setInterval(homeSpo.nxt, 3000);
		},5000);
	}
	this.nxt=function(){
		//console.debug('ind=',ind);
		var i=ind<itms.length-1?ind+1:0;
		//console.debug('i=',i);
		homeSpo.swtch(i);
	}
}


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

jQ.extend({
	keys: function(obj){
		var a = [];
		jQ.each(obj, function(k){ a.push(k) });
		return jQ(a);
	}
})
/*
function jQ_(n){
	var el = domCache[n];
	if(!el){
		el = jQ(n);
		domCache[n]=el;
	}else if(!el.parentNode){
		el = null;
		delete domCache[n];
	}
	return el;
}
*/
function myTruncate(el,cont){
	cont=jQ(cont||el);
	el=jQ(el);
	var t,t1,d,i=0;
	t=el.text();
	el.attr('title',t);
	d=jQ.browser.msie?1:0;
	while(cont.attr('scrollWidth')>cont.innerWidth() && t.length>i){
		i++;
		t1=truncate(t,t.length-i);
		el.text(t1);
	}
}
function truncate(txt,l,t){
    l=l||30;
    t=t||'...';
    return txt.length > l ? txt.slice(0, l - t.length) + t : txt;
}


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

function showWindow(id,u,w,h){
	if(!utilVars[id] || utilVars[id].closed){
		utilVars[id]=popItUp(u,id,w,h);
	}
	utilVars[id].focus();
}

var utilVars={projName:'wear'}, 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','http://messenger.providesupport.com/messenger/'+utilVars.projName+'.html?ps_s='+psWWWrsid,500,560);
	}else{
		popItUp('about:blank','support_window',500,560);
		initSupportWindow();
		var timer = setInterval(function(){
			if(psWWWrsid){
				clearInterval(timer);
				openSupportWindow();
			}
		}, 5);
	}
}
function reloadImageCode(){
	var img=document.getElementById('vcode_img');
	if(!facVars.imgCodeUrl) facVars.imgCodeUrl=img.src;
	img.src=facVars.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(!facVars.cert_window || facVars.cert_window.closed){
		facVars.cert_window=popItUp(u,'cert_window',500,350);
	}else{
		facVars.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;
}

