//--------------------------------------------------------------
//--------------------------------------- All Bagbuilder Actions
//--------------------------------------------------------------

//function ChangeColor		>> Main function to trigger subfunctions
//function IconHighlight	>> Sets the outline around the icons
//function SetBagVariables	>> Sets Color and Price value
//function LoadColor		>> Loads the new color in the right imageholder
//function FadeImage		>> After loading, images will fade
//function ChangeStyle		>> Change the style of the bag
//function reCalculatePrice	>> Recalculate new price
//function ChangeLinkParams	>> Remember variables if any link is clicked
//function ButtonAddToCart	>> Show or hide the button
//function UpdateCartLink	>> Updates all variables before adding to the shopping cart

//main color changing functuon
function ChangeColor(changeitem, colorvalue, varpriceCrew, varpriceFlow, activeitem) {
	IconHighlight(changeitem, activeitem);
	SetBagVariables(changeitem, colorvalue, varpriceCrew, varpriceFlow);
	LoadColor(changeitem, colorvalue);
}

//highlights the color icons on the right of the page
function IconHighlight(changeitem, activeitem) {
	
	//reset all highlight status
	for(icon=0;icon<20;icon++) {
		//check if exists
		if(document.getElementById(['Crew'+changeitem+'Icon'+icon])!=null) {
			document.getElementById(['Crew'+changeitem+'Icon'+icon]).style.border='2px solid #000000';
			document.getElementById(['Flow'+changeitem+'Icon'+icon]).style.border='2px solid #000000';
		} else {
			break;
		}
	}
	//highlight selected color icon
	document.getElementById(['Crew'+changeitem+'Icon'+activeitem]).style.border='2px dotted #FFFFFF';
	document.getElementById(['Flow'+changeitem+'Icon'+activeitem]).style.border='2px dotted #FFFFFF';
}

//sets hidden form variables for add-to-cart
//changes all product links to pass the variables
function SetBagVariables(changeitem, varvalue, varpriceCrew, varpriceFlow) {
	if(changeitem=='Bag') {
		BagColor = varvalue;
		BagPriceCrew = parseFloat(varpriceCrew);
		BagPriceFlow = parseFloat(varpriceFlow);
	}
	if(changeitem=='Strap') {
		StrapColor = varvalue;
		StrapPriceCrew = parseFloat(varpriceCrew);
		StrapPriceFlow = parseFloat(varpriceFlow);
	}
	if(changeitem=='Size') {
		BagCat = varvalue;
	}
	reCalculatePrice(BagCat);
}

//loads the images
function LoadColor(changeitem, colorvalue) {
	
	//set object variable
	var img1 = document.getElementById([changeitem+'Image']);
	var img2 = document.getElementById([changeitem+'Image2']);

	//check which image id is active
	if(img1.className=='image_visible') {
		if(changeitem=='Bag') {
			img2.src = '../collection/messenger/bagbuilder/5001_'+colorvalue+'.jpg';
		} else {
			img2.src = '../collection/messenger/bagbuilder/5001_2_'+colorvalue+'.gif';
		}
	} else {
		if(changeitem=='Bag') {
			img1.src = '../collection/messenger/bagbuilder/5001_'+colorvalue+'.jpg';
		} else {
			img1.src = '../collection/messenger/bagbuilder/5001_2_'+colorvalue+'.gif';
		}
	}
	
	//update product descriptions
	if(changeitem=='Bag') {
		document.getElementById('BagName').innerHTML = '<b>'+BagCat+' Bag '+colorvalue+'</b>';
	} else {
		document.getElementById('StrapName').innerHTML = '<b>'+BagCat+' Strap 2\" '+colorvalue+'</b>';
	}	
}

function FadeImage(fadein, fadeout) {
	//fade image if image is not the transparent gif
	if(document.getElementById(fadein).src!='http://www.taggerbags.com/images/various/transparent.gif') {
		document.getElementById(fadein).className = 'image_visible';
		if(fadeout!='') {
			document.getElementById(fadeout).className = 'image_invisible';
		}
	}
}

//calculate the prices
function reCalculatePrice(newstyle) {
	
	//define variables
	var BagPrice;
	var BagPriceText;
	var StrapPrice;
	var StrapPriceText;
	var FlapPrice;
	var FlapPriceText;
	var TotalPrice = 0;
	var TotalPriceText;
	
	if(isNaN(parseFloat(window['BagPrice'+BagCat]))) {
		BagPrice = 'OUT';
		BagPriceText = '<b style="color:#FF0000;">OUT!</b>';
		TotalPrice = 'OUT';
	} else {
		BagPrice = parseFloat(window['BagPrice'+BagCat]);
		BagPriceText = '<b>&euro; '+BagPrice.toFixed(2)+'</b>';
	}
	
	if(isNaN(parseFloat(window['StrapPrice'+BagCat]))) {
		StrapPrice = 'OUT';
		StrapPriceText = '<b style="color:#FF0000;">OUT!</b>';
		TotalPrice = 'OUT';
	} else {
		StrapPrice = parseFloat(window['StrapPrice'+BagCat]);
		StrapPriceText = '<b>&euro; '+StrapPrice.toFixed(2)+'</b>';
	}
	
	if(isNaN(parseFloat(window['FlapPrice'+BagCat]))) {
		FlapPrice = 'OUT';
		FlapPriceText = '<b style="color:#FF0000;">OUT!</b>';
		TotalPrice = 'OUT';
	} else {
		FlapPrice = parseFloat(window['FlapPrice'+BagCat]);
		FlapPriceText = '<b>&euro; '+FlapPrice.toFixed(2)+'</b>';
	}
	
	if(isNaN(TotalPrice)) {
		TotalPriceText = '<b style="color:#FF0000;">OUT!</b>';
	} else {
		TotalPriceText = '<b>&euro; '+(BagPrice+StrapPrice+FlapPrice).toFixed(2)+'</b>';
	}
	
	//replace HTML
	document.getElementById('BagPriceText').innerHTML = BagPriceText;
	document.getElementById('StrapPriceText').innerHTML = StrapPriceText;
	document.getElementById('FlapPriceText').innerHTML = FlapPriceText;
	document.getElementById('TotalPriceText').innerHTML = TotalPriceText;
	
	//set button state
	if(BagPrice > 0 && StrapPrice > 0 && FlapPrice > 0) { 
		ButtonAddToCart(1);
	} else {
		ButtonAddToCart(0);
	}
}

//(de)Activates the button state
function ButtonAddToCart(add_var) {
	if(add_var==1) {
		//update the button
		document.getElementById('button_add').style.display = '';
		document.getElementById('button_out').style.display = 'none';
	} else {
		//update the button
		document.getElementById('button_add').style.display = 'none';
		document.getElementById('button_out').style.display = '';
	}
}

function UpdateCartLink(target) {
	//Check BagCat to make the SKUs
	if(BagCat=='Crew') {
		SkuCat = '5001';
	} else {
		SkuCat = '5101';			
	}
	
	//create the link
	querystring = '?status=1';
	querystring = querystring+'&bagbuilder=1';
	querystring = querystring+'&PRODUCT_SKU1='+SkuCat+'-'+BagColor;
	querystring = querystring+'&PRODUCT_AMOUNT1=1';
	querystring = querystring+'&PRODUCT_COLORCODE1='+BagColor;
	querystring = querystring+'&PRODUCT_SKU2='+SkuCat+'-'+FlapSKU;
	querystring = querystring+'&PRODUCT_AMOUNT2=1';
	querystring = querystring+'&PRODUCT_COLORCODE2=';
	querystring = querystring+'&PRODUCT_SKU3='+SkuCat+'-2-'+StrapColor;
	querystring = querystring+'&PRODUCT_AMOUNT3=1';
	querystring = querystring+'&PRODUCT_COLORCODE3='+StrapColor;
	querystring = querystring+'&COMBINE_CODE='+CombineCode;
	querystring = querystring+'&addtocart=1';
	document.getElementById(target).href = querystring;
}


//changes the link click with the right variables
function ChangeLinkParams(target) {
	target.href = target.href+"&BagColor="+BagColor+"&StrapColor="+StrapColor+"&BagCat="+BagCat;
}

//change style of Bag
function ChangeStyle(newstyle) {
	
	//update the variables of this Bag
	SetBagVariables('Size', newstyle);
		
	//set variables
	if(newstyle=="Crew") { 
		//show-hide the color icons for the right model
		document.getElementById('CrewBagColorIcons').style.display = '';
		document.getElementById('CrewStrapColorIcons').style.display = '';
		document.getElementById('FlowBagColorIcons').style.display = 'none';
		document.getElementById('FlowStrapColorIcons').style.display = 'none';
		//switch the ImageIcon
		document.getElementById('CrewStyleIcon').src = '../images/various/icon_messenger_1008_o_r1_c1.jpg';
		document.getElementById('FlowStyleIcon').src = '../images/various/icon_messenger_1008_r1_c2.jpg';
		//display the right names and prices
		document.getElementById('BagHeader').innerHTML = document.getElementById('BagHeader').innerHTML.replace(("Flow"), newstyle);
		document.getElementById('BagName').innerHTML = document.getElementById('BagName').innerHTML.replace("Flow", newstyle);
		document.getElementById('StrapName').innerHTML = document.getElementById('StrapName').innerHTML.replace("Flow", newstyle);
		document.getElementById('FlapName').innerHTML = document.getElementById('FlapName').innerHTML.replace("Flow", newstyle);
		targetwidth = 560; 
		targetheight = 452;
	} else { 
		//show-hide the color icons for the right model
		document.getElementById('CrewBagColorIcons').style.display = 'none';
		document.getElementById('CrewStrapColorIcons').style.display = 'none';
		document.getElementById('FlowBagColorIcons').style.display = '';
		document.getElementById('FlowStrapColorIcons').style.display = '';
		//switch the ImageIcon
		document.getElementById('CrewStyleIcon').src = '../images/various/icon_messenger_1008_r1_c1.jpg';
		document.getElementById('FlowStyleIcon').src = '../images/various/icon_messenger_1008_o_r1_c2.jpg';
		//display the right names and prices
		document.getElementById('BagHeader').innerHTML = document.getElementById('BagHeader').innerHTML.replace(("Crew"), newstyle);
		document.getElementById('BagName').innerHTML = document.getElementById('BagName').innerHTML.replace("Crew", newstyle);
		document.getElementById('StrapName').innerHTML = document.getElementById('StrapName').innerHTML.replace("Crew", newstyle);
		document.getElementById('FlapName').innerHTML = document.getElementById('FlapName').innerHTML.replace("Crew", newstyle);
		targetwidth = 480; 
		targetheight = 384;
	}
	
	//make image array, to read later
	var img = new Array (
		document.getElementById('BagImage'),
		document.getElementById('BagImage2'),
		document.getElementById('StrapImage'),
		document.getElementById('StrapImage2'),
		document.getElementById('FlapImage')
	);

	//scale all images in the array
	for(i=0;i<img.length;i++) {
		if(newstyle=="Crew") {
			img[i].width += Math.ceil((targetwidth-img[i].width)/3);
			img[i].height += Math.ceil((targetheight-img[i].height)/3);
		} else {
			img[i].width += Math.floor((targetwidth-img[i].width)/3);
			img[i].height += Math.floor((targetheight-img[i].height)/3);
		}
	}
	
	//loop or break?
	if(img[1].width!=targetwidth) { 
		setTimeout('ChangeStyle(\''+newstyle+'\')',40);
	} else {
		reCalculatePrice(newstyle);
			
		//Update descriptions below bagbuilder
		ShowHide('CrewDescription');
		ShowHide('FlowDescription');
		ShowHide('CrewImages');
		ShowHide('FlowImages');

	}
}	

//--------------------------------------------------------------

