jQuery.noConflict();

var baseURL = "http://s7d5.scene7.com/is/image/SwissKnifeShop/";
var zoomURL = "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SwissKnifeShop&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults";

// "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SwissKnifeShop&sku=SATEST&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults&productTitle=Product%20Name"
// "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SwissKnifeShop&sku=BNYCUSHY_530&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults

var queryString = "";
function init(){
	var previewLink = jQuery("#previewEngraving");
	previewLink.bind('click',launchPreview);
}
jQuery(document).ready(init);

function launchPreview(){
	queryString = "";
	var query = getFormElements();
	var templateId = getTemplate();
	
	// double encode to convert "%" eg %22 -> %2522
	var pageTitle = escape(getItemName());
	//pageTitle = escape(pageTitle);

	var variation = getColorWay();
	var url = baseURL + templateId + variation + "?$productTitle=" + pageTitle + query + "&windowtitle="+ pageTitle;
	console.log(url);
	
	window.open(url,"scene7win","width=510,height=580","scrollbars=no,resizable=no,toolbar=no,location=no,status=yes,menubar=no,titlebar=no");
}
function getColorWay() {
	var variation = ""
	if(jQuery("#colorWay").length > 0){
		var colorVariation = jQuery("#colorWay").prev();
		if(colorVariation.val() != 0){
			var variation = "_"+colorVariation.val();
		}
	}
	console.log(variation);
	return variation;
}

function getFormElements(){
	jQuery.each(formFieldIds,function(index,id){
		
		var selector = "span[id^='"+formFieldIds[index]+"']";
		var elCollection = jQuery(selector);
		var el = jQuery(elCollection[0]).prev();
		
		if(el){
			if(id == 'fontColor') {
				var colorId = el.val();
				var value = getRGB(colorId);
				console.log(value);
				queryString += value;
			}
			else if(id == 'fontFace'){
				id = 'font';
				var value =el.val();
				if(value == 0){
					value = 330;
				}
				queryString += "&$font="+value; 
			}
			else{
				queryString += "&$"+id+"="+el.val();
			}
		}
	});
	return queryString;
}

function getTemplate(){
	var templateEl = jQuery('#scene7template');
	return templateEl.text();
	
}

function getItemName(){
	var el = jQuery('.ProductNameColorLARGE');
	return el.text();
	
}

function getRGB(value){
	var r; // red value
	var g; // green value 
	var b; // blue value
	
	if(value == 457 || value == 474 || value == 26){ /* WHITE */
		r = 255; g = 255; b = 255;
	}
	
	else if(value == 458 || value == 475 || value == 471){ /* BLACK */
		r = 0; g = 0; b = 0;
	}
	
	else if(value == 399 || value == 470){ /* Powder Blue */
		r = 155; g = 196; b = 226;
	}
	
	else if(value == 400 || value == 471){ /* Red */
		r = 206; g = 17; b = 38;
	}
	
	else if(value == 401 || value == 472){ /* Green Leaf */
		r = 86; g = 142; b = 20;
	}
	
	else if(value == 456 || value == 473){ /* Yellow */
		r = 252; g = 209; b = 22;
	}
	
	else if(value == 459 || value == 476){ /* Khaki */
		r = 186; g = 169; b = 151;
	}
	
	else if(value == 460 || value == 477){ /* Lava Orange */
		r = 226; g = 61; b = 40;
	}
	
	else if(value == 461 || value == 478){ /* Slate */
		r = 145; g = 150; b = 147;
	}
	
	else if(value == 462 || value == 479){ /* Brown */
		r = 107; g = 48; b = 33;
	}
				
	else if(value == 463 || value == 480){ /* Lake Blue */
		r = 0; g = 107; b = 119;
	}
	
	else if(value == 464 || value == 481){ /* Orange */
		r = 244; g = 124; b = 0;
	}
	
	else if(value == 465 || value == 482){ /* Pacific Green */
		r = 38; g = 104; b = 109;
	}
	
	else if(value == 466 || value == 483){ /* Lagoon Blue */
		r = 0; g = 84; b = 106;
	}
	
	else if(value == 28){ /* Uncolored */
		r = 93; g = 93; b = 93;
	}
	
	else if(value == 27){ /* SILVER */
		r = 192; g = 192; b = 192;
	}
	else{
		r =255; g = 255; b = 255;
	}
	
	var qString = "&$red=" + r + "&$grn=" + g + "&$blu=" + b;
	return qString;
}

function viewLargerLink(){
	if(jQuery("#viewLargerLink")){
		var selector = "input[name='ProductCode']";
		var elCollection = jQuery(selector);
		var productCode = jQuery(elCollection[0]).val();
		var windowTitle = escape(getItemName());
		
		// double encode to convert "%" eg %22 -> %2522
		var productTitle = escape(getItemName());
		productTitle = escape(productTitle);
		
		if(jQuery("#colorWay").length > 0){
			var colorVariation = jQuery("#colorWay").prev();
		}
		
		jQuery("#viewLargerLink").bind('click',function(){
			var variation = "";
			if(jQuery("#colorWay").length > 0){
				if(colorVariation.val() != 0){
					var variation = "_"+colorVariation.val();
				}
			}
			//console.log(productCode+variation);
			var url =  zoomURL+"&sku="+productCode+variation+"&windowtitle="+windowTitle+"&vc=productTitle%3D"+productTitle;
			console.log(url);
			window.open(url,"scene7win","width=510,height=580","scrollbars=no,resizable=no,toolbar=no,location=no,status=yes,menubar=no");
		});
	}
}
jQuery(document).ready(viewLargerLink);


/*
	CONSOLE.LOG
*/

if(typeof console == "undefined"){
	console = {};
	console.log = function(content){
		//alert(contents);
	}
}
