jQuery.noConflict();

var baseURL = "http://s7d5.scene7.com/is/image/SureFitLaundry/";
var zoomURL = "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SureFitLaundry&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults";

var defaultColor = 'AAA';

// "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SureFitLaundry&sku=SATEST&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults&productTitle=Product%20Name"
// "http://s7d5.scene7.com/s7ondemand/zoom/flasht_zoom.jsp?company=SureFitLaundry&sku=BNYCUSHY_530&zoomwidth=600&zoomheight=600&config=ImageSetConfigDefaults

var queryString = "";
function init(){
	var previewLink = jQuery("#previewEmbroidery");
	var submitForm = jQuery('form[name="MainForm"]');
	previewLink.bind('click', launchPreview);
	submitForm.bind('submit', validateFormSubmit);
}
jQuery(document).ready(init);

function urlEncodeCustom(inputString)                   
{                   
  var encodedInputString=escape(inputString);
  encodedInputString=encodedInputString.replace("+", "%2B");
  encodedInputString=encodedInputString.replace("/", "%2F");
  return encodedInputString;
}

function launchPreview(){
	queryString = "";
	var valid = validateForm(true);
	if(!valid) return false;
	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);
	
	return popupWindow(url);
}


function popupWindow(url)
{
	popWindow = window.open(url,"scene7win","width=710,height=780","scrollbars=no,resizable=no,toolbar=no,location=no,status=yes,menubar=no,titlebar=no");
	popWindow.focus();
	return false;
}

function validateFormSubmit()
{
	return validateForm(false);
}

function validateForm(lineRequired){
	var errors = "";
	var error = false;
	var numberOfLines = -1;
	var numberOfLinesWithText = 0;
	var fontError = false;
	var fontColorError = false;
	
	jQuery.each(errorFieldIds,function(index,id){
		var selector = "span[id^='"+errorFieldIds[index]+"']";
		var elCollection = jQuery(selector);
		var el = jQuery(elCollection[0]).prev();
		var str = '';
		
		if(el){
			if(id == 'fontColor') {
				if(el.val()==1354) {
					fontColorError = true;
				}
			}
			else if(id == 'Font'){
				if(el.val()==1578) {
					fontError = true;
				}
			}
			else if(id == 'ChooseSize'){
				if(el.val()==1580) {
					errors += "No size selected!\n";
					error = true;
				}
			}
			else if(id == 'NumberofLines'){
				if(el.val()==1361)
				{
					numberOfLines = 0
					if(lineRequired)
					{
						errors += "No number of embroidery lines selected!\n";
						error = true;
					}
				}else if(el.val()==1358)
				{
					numberOfLines = 1
				}else if(el.val()==1359)
				{
					numberOfLines = 2
				}else if(el.val()==1360)
				{
					numberOfLines = 3
				}else if(el.val()==5317)
				{
					numberOfLines = 4
				}
				else if(el.val()==5318)
				{
					numberOfLines = 5
				}
				else if(el.val()==5319)
				{
					numberOfLines = 6
				}
			}
			else if(id == 'LChest1' || id=='LChest2' || id=='LChest3' || id=='RChest1' || id=='RChest2' || id=='RChest3')
			{
				str = el.val();
				str = jQuery.trim(str);
				if(str.length>0) numberOfLinesWithText++;
			}
		}
	});

	if((lineRequired && fontError) || (!lineRequired && fontError && numberOfLines>0))
	{
		errors += "No font color selected!\n";
		error = true;
	}
	
	if((lineRequired && fontColorError) || (!lineRequired && fontColorError && numberOfLines>0))
	{
		errors += "No font style selected!\n";
		error = true;
	}
				
	if(numberOfLines>0 && numberOfLines!=numberOfLinesWithText)
	{
		errors += "Please fill " + numberOfLines  + " line(s) of text for embroidery!\n";
					error = true;
	}
		
	if(error) alert(errors);
	return !error;
}

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 if(id == 'Font'){
				var fontId = el.val();
				var value = getFont(fontId);
				queryString += 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 getFont(value){
	var font=''; //font
	var fontItalic='i0'; //font
	
	if(value == 1347){//Block
		font = 'Arial';
	}
	
	if(value == 1348){//Block Italic
		font = 'Arial';
		fontItalic = 'i';
	}
	
	if(value == 1353){//Script 1
		font = 'LETTERS';
	}
	
	if(value == 1376){//Script 2
		font = 'Aktuelle';
	}
	
	if(value == 5172){//Varsity
		font = 'Varsity Regular';
	}
	
	if(value == 1578){//Arial
		font = 'Arial';
	}
	
	var qString = "";
	qString += "&$LFont1="+urlEncodeCustom(font)+"&$LItal1="+fontItalic;
	qString += "&$LFont2="+urlEncodeCustom(font)+"&$LItal2="+fontItalic;
	qString += "&$LFont3="+urlEncodeCustom(font)+"&$LItal3="+fontItalic;
	qString += "&$RFont1="+urlEncodeCustom(font)+"&$RItal1="+fontItalic;
	qString += "&$RFont2="+urlEncodeCustom(font)+"&$RItal2="+fontItalic;
	qString += "&$RFont3="+urlEncodeCustom(font)+"&$RItal3="+fontItalic;
	return qString;
}

function getRGB(value){
	var r; // red value
	var g; // green value 
	var b; // blue value

	if(value == 457 || value == 474 || value == 1357 || value == 1354){ /* White */
		r = 255; g = 255; b = 255;
	}
	
	else if(value == 458 || value == 475 || value == 2118 || value == 5173){ /* Black */
		r = 0; g = 0; b = 0;
	}
	
	else if(value == 399 || value == 1823 || value == 5169){ /* Maroon */
		r = 64; g = 25; b = 44;
	}
	
	else if(value == 400 || value == 1356){ /* Red */
		r = 204; g = 0; b = 1;
	}
	
	else if(value == 1379){ /* Blue */
		r = 1; g = 0; b = 204;
	}
	
	else if(value == 401 || value == 1382){ /* Pink */
		r = 220; g = 153; b = 160;
	}
	
	else if(value == 456 || value == 1388){ /* Yellow */
		r = 254; g = 247; b = 107;
	}
	
	else if(value == 459 || value == 1387){ /* Orange */
		r = 227; g = 80; b = 29;
	}
	
	else if(value == 460 || value == 1378){ /* Brown */
		r = 180; g = 130; b = 71;
	}
	
	else if(value == 461 || value == 1355){ /* Navy Blue */
		r = 2; g = 46; b = 83;
	}
	
	else if(value == 462 || value == 1385){ /* Aquamarine */
		r = 44; g = 54; b = 126;
	}
				
	else if(value == 463 || value == 1383){ /* Royal Blue */
		r = 46; g = 68; b = 144;
	}
	
	else if(value == 464 || value == 1386){ /* Teal */
		r = 52; g = 175; b = 154;
	}
	
	else if(value == 465 || value == 2117 || value == 5171){ /* Green */
		r = 0; g = 134; b = 99;
	}
	
	else if(value == 466 || value == 1381){ /* Dark Green */
		r = 0; g = 77; b = 61;
	}
	
	else if(value == 466 || value == 1384){ /* Grey */
		r = 127; g = 127; b = 127;
	}

	else if(value == 466 || value == 1961 || value == 5170){ /* Silver */
		r = 178; g = 178; b = 178;
	}

	else if(value == 466 || value == 1389){ /* Gold */
		r = 204; g = 180; b = 136;
	}

	else if(value == 466 || value == 1377){ /* Pale Khaki */
		r = 224; g = 200; b = 172;
	}
	
	var qString = "";
	qString += "&$RedLC1=" + r + "&$GreenLC1=" + g + "&$BlueLC1=" + b;
	qString += "&$RedLC2=" + r + "&$GreenLC2=" + g + "&$BlueLC2=" + b;
	qString += "&$RedLC3=" + r + "&$GreenLC3=" + g + "&$BlueLC3=" + b;
	qString += "&$RedRC1=" + r + "&$GreenRC1=" + g + "&$BlueRC1=" + b;
	qString += "&$RedRC2=" + r + "&$GreenRC2=" + g + "&$BlueRC2=" + b;
	qString += "&$RedRC3=" + r + "&$GreenRC3=" + g + "&$BlueRC3=" + 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);
	}
}
