stricter.ajax.loading="_ajaxloading";

$(document).ready(function() {
	$("#TB_closeAjaxWindow").remove();
	$("TB_closeWindow").remove();
	rebuildStack();
	
	if( document.getElementById('movie_0') )
		document.getElementById('stage_video').innerHTML=document.getElementById('movie_0').innerHTML;

	var zoptions = {
		zoomWidth: 496,
		zoomHeight: 442,
		xOffset: 0,
		yOffset: 0,
		position: "right"
	};
	$("#product_image_a").jqzoom(zoptions);
});


var movefactor = 2;
var optionStack = new Array();
var colorStack = new Array();
var sizeStack = new Array();
var canSubmit = false;

function ProductItem(id) {
	this.price;
	this.ref;
	this.qty;
	this.colorValue;
	this.sizeValue;
	this.priceDivided;
}

var aTabs=new Array();
aTabs[0]="tab1";
aTabs[1]="tab2";
aTabs[2]="tab3";
aTabs[3]="tab4";
//=====================
function openTab(suffix)
{
	var i=0;
	var ln = aTabs.length;

	for (i=0; i<ln; i++)
	{
		if(  aTabs[i] == suffix ) {
			$( "#"+aTabs[i]  ).css("z-index","2");
			$( "#"+aTabs[i]  ).css("border-bottom-color","#f5f5f5");
			$( "#"+aTabs[i]+"_content"  ).fadeIn(200);
			document.getElementById('img_'+aTabs[i]).src = svpath+'/themes/'+theme+'/images/i_arrow_down_gray.png';
			tabAjaxFactory( aTabs[i] );
		} else {
			$( "#"+aTabs[i]  ).css("z-index","1");
			$( "#"+aTabs[i]  ).css("border-bottom-color","#e6e6e6");
			$( "#"+aTabs[i]+"_content"  ).fadeOut(100);
			document.getElementById('img_'+aTabs[i]).src = svpath+'/themes/'+theme+'/images/i_arrow_right_gray.png';
		}
	}
}

//===========================================
function tabAjaxFactory(term)
{
	var ref = document.forms['prdform'].elements['ref'].value;

	switch( term ) {
		case 'tab1':  break;
		case 'tab2':  break;
		case 'tab3':
			stricter.ajax.post(svpath+'/products/payments/', 'prdform','tab3_content');
		break;
		case 'tab4':  break;
	}
}


var aBoxes=new Array();
aBoxes[0]="send2friend";
aBoxes[1]="ask2gift";
aBoxes[2]="add2wishlist";
//===========================================
function friendGiftWish( suffix )
{
	var i=0;
	var ln = aBoxes.length;

	if( accessCSS( suffix ).visibility=="visible" ) {
		accessCSS( suffix ).visibility = "hidden";
		accessCSS( suffix ).display="none";
		return;
	}

	for (i=0; i<ln; i++)
	{
		if(  aBoxes[i] == suffix ) {
			accessCSS(aBoxes[i]  ).visibility = "visible";
			accessCSS( aBoxes[i]  ).display="block";
			ajax('products', aBoxes[i], null, aBoxes[i], '&id=1');
		}
		else {
			accessCSS(aBoxes[i]).visibility = "hidden";
			accessCSS(aBoxes[i]).display="none";
		}
	}
}

//=====================
function orderByMail(ref)
{
	var popW = 450;
	var popH = 430;
	screenW = screen.width; 
	screenH = screen.height; 
 
	popL = (screenW / 2) - (popW / 2) - 8;
	popT = (screenH / 2) - (popH / 2) - 20;
 
	var params = params + ",left=" + popL + ",top=" + popT + ",width=" + popW + ",height=" + popH; 
 
	w = window.open(svpath+'/products/orderByMail/'+ref,'bymail', params);
}

//=====================
function reloadImage(ref, color) {
	document.getElementById('product_image').src= svpath+"/images/products/"+ref+"/ref/medium/"+color+".jpg";
	document.getElementById('product_image_a').href=svpath+'/images/products/'+ref+'/ref/large/'+color+'.jpg';
}

//=====================
function getAdColor(ref, ad) {
	document.getElementById('product_image').src= svpath+"/images/products/"+ref+"/ad/medium/"+ad+".jpg";
	document.getElementById('product_image_a').href=svpath+'/images/products/'+ref+'/ad/large/'+ad+'.jpg';
}

//=====================
function resetShipping(){
	document.getElementById('shippingResult').innerHTML = ' ';
	document.forms['cepform'].elements['cep1'].value = '';
	document.forms['cepform'].elements['cep2'].value = '';
}

//=====================
function colorOver(eid)
{
	if(color.value!=eid)
		$('#color_'+eid).animate({left:'-='+movefactor+'px', top:'-='+movefactor+'px'}, 100);

	$(".optionSize").addClass('optionSizeDisabled');
	$(".optionSize").removeClass('optionSizeSelected');
	$(".optionSize").removeClass('optionSizeEnabled');
	
	var i=0;
	do {
		$("#size_"+colorStack["_"+eid][i]).addClass('optionSizeEnabled');
		i++;
	} while(colorStack["_"+eid][i]!=null);

}

var desel=false;
//=====================
function colorOut(eid)
{
	if(color.value!=eid && desel!=true)
		$('#color_'+eid).animate({left:'+='+movefactor+'px', top:'+='+movefactor+'px'}, 80);

	rebuildStack();
	
	desel=false;
}

//=====================
function sizeOver(eid)
{	
	$(".imgshadow").css('background-image', transp);
	$(".img_x").attr('src', svpath+"/images/colors/000.png");

	$(".optionSize").removeClass('optionSizeSelected');
	
	if(color.value) {
		$(".optionSize").removeClass('optionSizeEnabled');
		$(".optionSize").addClass('optionSizeDisabled');
		
		for(x in colorStack["_"+color.value])
		{
			xname = colorStack["_"+color.value][x];
			$("#size_"+xname).addClass('optionSizeEnabled');
			$("#size_"+xname).removeClass('optionSizeDisabled');
		}
	}

	var i=0;
	for(x in sizeStack["_"+eid]){
		$("#img_"+sizeStack["_"+eid][x]).attr('src', svpath+"/images/colors/"+sizeStack["_"+eid][x]+".png");
		$("#shadow_"+sizeStack["_"+eid][x]).css('background-image', nottransp);
	}
}

//=====================
function sizeOut(eid)
{
	rebuildStack();
}

//=====================
function selectColor(cval)
{
	document.getElementById('product_image').src=svpath+'/images/products/'+ref.value+'/ref/medium/'+cval+'.jpg';
	document.getElementById('product_image_a').href=svpath+'/images/products/'+ref.value+'/ref/large/'+cval+'.jpg';

	if(color.value==cval && ncolors>1 ) {
		color.value="";
		rebuildStack();
		desel=true;
		document.getElementById('colorName').innerHTML="";		
		return;
	}

	color.value=cval;
	rebuildStack();
}

//=====================
function selectSize(cval)
{
	if(size.value==cval && nsizes>1 ) {
		size.value="";
		rebuildStack();
		document.getElementById('sizeName').innerHTML="";
		return;
	}

	size.value=cval;
	rebuildStack();
}

//=====================
function rebuildStack()
{
	$(".optionSize").removeClass('optionSizeEnabled');
	$(".optionSize").removeClass('optionSizeSelected');
	$(".optionSize").addClass('optionSizeDisabled');

	$(".optionColor").css('top',movefactor+'px');
	$(".optionColor").css('left',movefactor+'px');
	
	var i=0;
	if(color.value>0) {
		if(colorStack["_"+color.value]) {
			while(colorStack["_"+color.value][i]!=null){
				$("#size_"+colorStack["_"+color.value][i]).addClass('optionSizeEnabled');
				i++;
			}			
		}
		$("#color_"+color.value).css('left','0px');
		$("#color_"+color.value).css('top','0px');

	} else if(!size.value) {
		$(".optionSize").addClass('optionSizeEnabled');
	}

	i=0;
	if(size.value>0) {
		$(".imgshadow").css('background-image', transp);
		$(".img_x").attr('src', svpath+"/images/colors/000.png");

		if(sizeStack["_"+size.value]){
			while(sizeStack["_"+size.value][i]!=null){
				$("#img_"+sizeStack["_"+size.value][i]).attr('src', svpath+"/images/colors/"+sizeStack["_"+size.value][i]+".png");
				$("#shadow_"+sizeStack["_"+size.value][i]).css('background-image', nottransp);
				i++;
			}			
		}

		$("#size_"+size.value).addClass('optionSizeSelected');
	} else if(!color.value) {
		for(x in colorStack)
		{
			xname = x.replace(/_/,"");
			$("#img_"+xname).attr('src', svpath+"/images/colors/"+xname+".png");
			$("#shadow_"+xname).css('background-image', nottransp);	
		}
	}
	
	if(!color.value ){
		$(".optionSize").removeClass('optionSizeDisabled');
		
	}

	if(!size.value) {
		for(x in colorStack)
		{
			xname = x.replace(/_/,"");
			$("#img_"+xname).attr('src', svpath+"/images/colors/"+xname+".png");
			$("#shadow_"+xname).css('background-image', nottransp);	
		}		
	}

	var item = optionStack["prd_"+ref.value+color.value+size.value];
	
	if(color.value  && size.value) {
		if(item && item.qty>0){
			document.getElementById('bt_comprar').src=svpath+"/themes/"+theme+"/images/products/b_comprar.png";
			
			canSubmit=true;
		} else {
			document.getElementById('bt_comprar').src=svpath+"/themes/"+theme+"/images/products/b_indisponivel.png";
			canSubmit=false;
		}
		if(item)
			alterPrice(item);
	} else {
		document.getElementById('bt_comprar').src=svpath+"/themes/"+theme+"/images/products/b_comprar.png";
		canSubmit=false;
	}

	if(color.value)
		document.getElementById('colorName').innerHTML=document.getElementById('img_'+color.value).title;

	if(size.value)
		document.getElementById('sizeName').innerHTML=document.getElementById('psize_'+size.value).innerHTML;
}

//=======
function alterPrice(item){
	var html = "";
	
	var price = ""+item.price.toFixed(2);
	price = price.replace('.',',');
	var sprice = ""+item.special_price.toFixed(2);
	sprice = sprice.replace('.',',');
	var dprice = ""+item.price_divided.toFixed(2);
	dprice = dprice.replace('.',',');

	if(item.priceDiscount > 0) {
		html = '<p class="price">'+
			'<span class="darkgrayB"><del>de R$ '+price+'</del></span>'+ 
			'por <span class="orangeB" style="font-size:14px;">R$ '+sprice+'</span> ou</p>'+
			'<p class="card">'+
			'&nbsp; até '+nparcels+'x sem juros de '+ 
			'<span class="orangeB" style="font-size:14px;">R$ '+dprice+'</span> no cartão</p>';
	} else {
		html = '<p class="price">'+
		'&raquo; por <span class="orangeB" style="font-size:14px;">R$ '+price+'</span> ou</p>'+
		'<p class="card">'+
		'&nbsp; até '+nparcels+'x sem juros de'+ 
		' <span class="orangeB" style="font-size:14px;">R$ '+dprice+'</span> no cartão</p>';
	}
	document.getElementById('priceContainer').innerHTML=html;
	
	document.forms['prdform'].elements['price'].value = price;

}

//==========
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

//==========
function checkCart() {

	if(document.forms['prdform'].elements['manufacturerCartAllowed'].value==0){	
		orderByMail(ref.value);
		return false;
	}

	if(!canSubmit && color.value && size.value)
		alert("Produto indisponível");
	else if(!canSubmit && (!color.value || !size.value))
		alert("Selecione cor e tamanho");
	return canSubmit;
}

//========
function switchImagesVideos(t)
{
	$("#thumbs_images").hide();
	$("#thumbs_videos").hide();
	$("#stage_image").hide();
	$("#stage_video").hide();
	$("#zoom_icon").hide();

	switch(t) {
		case 'images':
			$("#thumbs_images").show();
			$("#stage_image").show();
		break;
		case 'videos':
			$("#thumbs_videos").show();
			$("#stage_video").show();
		break;
	}
}

//========
function playVideo(idx)
{
	$("#stage_image").hide();
	$("#zoom_icon").hide();
	$("#stage_video").show();
	document.getElementById('stage_video').innerHTML=document.getElementById('movie_'+idx).innerHTML;
}
