//YAHOO.namespace( 'payment' );
YAHOO.namespace( 'payment.panel' );

YAHOO.payment.panel.shownAlert = false;

//use to check required personalizations before submitting to cart
YAHOO.shopperschoice.checkPersArray = [];

	function loadPersWelcome() {
		if (!YAHOO.payment.panel.perswelcome) {
							YAHOO.payment.panel.perswelcome = new YAHOO.widget.Panel("personalizeWelcomeBox", { width:"250px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.1}, context:[ "productImageMain", "tl", "tl" ], constraintoviewport: false, underlay:"shadow", close:true, visible:false, draggable:false, modal:false} );
							YAHOO.payment.panel.perswelcome.render();
							YAHOO.payment.panel.perswelcome["personalizeWelcomeBox"] = YAHOO.payment.panel.perswelcome;										
						}
		//YAHOO.payment.panel.persalert.setBody('<img src="/images/personalize-arrow.gif" align="left" /><b>Did You Notice?</b><br />Your personalization is being shown on the product image!');
		YAHOO.payment.panel.perswelcome.setBody('<b>Personalization</b><br />You can personalize this item by using the text box found to the right.');
		YAHOO.payment.panel.perswelcome.show();
	}
	

//vance: if you want to keep track, I'll probably also send through font_face, font_size, font_weight, a 'Yes'/'No' for if it should use the XY coordinates as a center point.  error message for not matching regex, and a default starting value
	function moveGrillCoverText(text, config) {
		var mainI = YAHOO.util.Dom.get('productImageMain');
		
		if (YAHOO.payment.panel.perswelcome) {
			YAHOO.payment.panel.perswelcome.hide();
		}

		if (text && text != '' && config) {
			var coverText = YAHOO.util.Dom.get('personalizeTextDiv'+config.persid);
			
			text = text.replace(/^\s*/, "").replace(/\s*$/, "");
			var plaintext = text;
			var testText = text.search(config.regex);

			if (testText != -1) {
				if (config) {
					//if we dont have a font color passed, go look for the dropdown and get current color
					if (config.fontcolor == '') {
						var colordrop = YAHOO.util.Dom.get('persColorDrop' + config.persid + '-' + config.itemid);
						if (colordrop) {
							config.fontcolor = colordrop.value;
						} else {
							config.fontcolor = 'black';
						}											
					}

					//set value in cart form
					if (config.optionsgroup && config.optionsgroup != '' && config.optionsgroup > 0) {
						var cartvalue = YAHOO.util.Dom.get('personalization_cart_'+config.persid+'_'+config.optionsgroup);
					} else {
						var cartvalue = YAHOO.util.Dom.get('personalization_cart_'+config.persid+'_'+config.itemid);
					}
					if (cartvalue) {
						cartvalue.value = plaintext + ":" + config.fontcolor;
					}
					//store in cookie for further use
					if (config.perstype != '') {
						createCookie('persCookie'+config.perstype, plaintext, 30);
					}
				}
				
				if (config.sale_price && config.sale_price != '') {
					persPrice = parseFloat(config.sale_price);
					if (persPrice > 0) {
						var persPriceDiv = YAHOO.util.Dom.get('personalizationPrice-' + config.persid + '-' + config.itemid);
						if (persPriceDiv) {
							persPriceDiv.style.display = 'block';
						}
					}				
				}
				
				
				if (YAHOO.payment.panel.covertip) {
						YAHOO.payment.panel.covertip.hide();
				}
				if ( mainI && coverText ) {

					if (config.showonpic && config.showonpic == 'Yes') {

						xx = YAHOO.util.Dom.getX('productImageMain');
						yy = YAHOO.util.Dom.getY('productImageMain');
						
						var xyarray = new Array();

						if (config) {
							if (config.x && config.y) {
								xyarray[0] = xx + config.x;
								xyarray[1] = yy + config.y;
							} else {
								xyarray[0] = xx;
								xyarray[1] = yy;
							}
							
							if (config.fontface == 'image' && config.fontcolor != '' && config.brand != '') {
								var imagetext = '';
								for(k=0;k<text.length;k++) {
									imagesrc = '/images/personalization/'+config.brand.toLowerCase()+'/'+config.fontcolor+'_text_'+text.charAt(k)+'.png';
									//alert(imagesrc);
									imagestring = '<img src="'+imagesrc+'"';
									if (config.imageheight > 0 && config.imagewidth > 0) {
										imagestring = imagestring + ' height="' + config.imageheight + '" width="' + config.imagewidth + '"';

										config.imageheight = parseInt(config.imageheight);
										config.imagewidth = parseInt(config.imagewidth);

									}
									imagestring = imagestring + '>';
									if (config.orientation == 'v') {
										imagestring = imagestring + "<br>";
									}
									var imgholder = new Image();
									imgholder.src = imagesrc;
									var imgheight = imgholder.height;
									var imgwidth = imgholder.width;
									//alert(imgheight);
									//alert(imgwidth);
									//alert(imagestring);
									imagetext = imagetext + imagestring;
								}
								text = imagetext;
								//alert(text);
							
							} else {
								if (config.fontcolor != '') {
									YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'color', config.fontcolor);
								}
								if (config.fontsize != '') {
									YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'font-size', config.fontsize);
								}
								if (config.fontface != '') {
									YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'font-family', config.fontface);
								}
								if (config.fontface != '') {
									YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'font-weight', config.fontweight);
								}
							}
						} else {
								xyarray[0] = xx;
								xyarray[1] = yy;
							}
						
						//YAHOO.util.Dom.setXY('personalizeTextDiv', xyarray);							
						//YAHOO.util.Dom.setXY('personalizeTextDiv', xyarray);		
						
						coverText.style.display = 'block';
						
						if (config.orientation == 'v' && config.fontface != 'image') {
							YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'line-height', '0.9em');
							YAHOO.util.Dom.setStyle('personalizeTextDiv'+config.persid, 'text-align', 'center');
							var verttext = '';
							for(k=0;k<text.length;k++) {
								verttext = verttext + text.charAt(k) + "<br>";
							}
							text = verttext;
						}
						coverText.innerHTML = text;
						
						//YAHOO.util.Dom.setXY('personalizeTextDiv', xyarray);							
						
						if (config.centered && config.centered == 'Yes') {

								var textWidth = YAHOO.util.Dom.get('personalizeTextDiv'+config.persid).offsetWidth;
								var textWidthFinal = parseInt(textWidth);
								var textHeight = YAHOO.util.Dom.get('personalizeTextDiv'+config.persid).offsetHeight;
								var textHeightFinal = parseInt(textHeight);
								
								//alert(textHeightFinal);
								//alert(textWidthFinal);
								
								if (textHeightFinal==0 && textWidthFinal == 0 && imgheight == 0 && imgwidth == 0 && config.fontface == 'image' && config.imagewidth > 0 && config.imageheight > 0) {

								//alert(textHeightFinal);
								//alert(textWidthFinal);
								//alert(config.imageheight);
								//alert(config.imagewidth);
								
									if (config.orientation == 'h') {
										textWidthFinal = textWidthFinal + config.imagewidth;
										if (plaintext.length == 1) {
											textHeightFinal = textHeightFinal + config.imageheight;
										}
									} else if (config.orientation == 'v') {
										textHeightFinal = textHeightFinal + config.imageheight;
										if (plaintext.length == 1) {
											textWidthFinal = textWidthFinal + config.imagewidth;
										}
									}
								}
								
								//alert(textHeightFinal);
								//alert(textWidthFinal);

								if ((config.x + config.y) > 0) {
									if (config.orientation == 'h') {
										var centeredX = xx + (config.x - (textWidthFinal / 2));
										var centeredY = yy + config.y;
									} else if (config.orientation == 'v') {
										var centeredX = xx + config.x;
										var centeredY = yy + (config.y - (textHeightFinal / 2));
									} 
								} else {

									var centeredX = xx + ((250 - textWidthFinal) / 2);
									var centeredY = yy + ((250 - textHeightFinal) / 2);

								}
								
								YAHOO.util.Dom.setX('personalizeTextDiv'+config.persid, Math.round(centeredX));
								YAHOO.util.Dom.setY('personalizeTextDiv'+config.persid, Math.round(centeredY));


						}
						
						
						
						
						if (!YAHOO.payment.panel.persalert) {
							YAHOO.payment.panel.persalert = new YAHOO.widget.Panel("personalizeAlertBox", { width:"150px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}, context:[ "persTextBox"+config.persid+"-"+config.itemid, "tr", "br" ], constraintoviewport: false, underlay:"shadow", close:true, visible:false, draggable:false, modal:false} );
							YAHOO.payment.panel.persalert.render();
							YAHOO.payment.panel.persalert["personalizeAlertBox"] = YAHOO.payment.panel.covertip;										
						}
						//YAHOO.payment.panel.persalert.setBody('<img src="/images/personalize-arrow.gif" align="left" /><b>Did You Notice?</b><br />Your personalization is being shown on the product image!');
						YAHOO.payment.panel.persalert.setBody('<b>Did You Notice?</b><br />Your personalization is being shown on the product image!');
						ShowPersonalizeAlert(5000);
						
						PNGfix();

						//YAHOO.util.Dom.setStyle('personalizeTextDiv', 'border', '1px solid black');
						//YAHOO.util.Dom.setStyle('personalizeTextDiv', 'border', 'none');
					}
				}
			} else {
				if ( coverText ) {
					coverText.innerHTML = '';
				}
				if (!YAHOO.payment.panel.covertip) {
					YAHOO.payment.panel.covertip = new YAHOO.widget.Panel("personalizeCoverTip", { width:"150px", /*effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.1},*/ context:[ "persTextBox"+config.persid+"-"+config.itemid, "tl", "bl" ], constraintoviewport: false, underlay:"shadow", close:false, visible:false, draggable:false, modal:false} );
					YAHOO.payment.panel.covertip.render();
					YAHOO.payment.panel.covertip["personalizeCoverTip"] = YAHOO.payment.panel.covertip;
					YAHOO.util.Dom.setStyle('personalizeCoverTip', 'color', 'red');
					YAHOO.util.Dom.setStyle('personalizeCoverTip', 'background-color', '#fee');
				}
				YAHOO.payment.panel.covertip.setBody(config.errormsg);
				YAHOO.payment.panel.covertip.show();
				YAHOO.payment.panel.covertip.show();
			}
		} else {
			var coverText = YAHOO.util.Dom.get('personalizeTextDiv'+config.persid);
			if (text == '' && config.is_required == 'No') {
				//erase value in cart form
				if (config.optionsgroup && config.optionsgroup != '' && config.optionsgroup > 0) {
					var cartvalue = YAHOO.util.Dom.get('personalization_cart_'+config.persid+'_'+config.optionsgroup);
				} else {
					var cartvalue = YAHOO.util.Dom.get('personalization_cart_'+config.persid+'_'+config.itemid);
				}
				if (cartvalue) {
					cartvalue.value = 'N:A';
				}
			}
			if (text == '' && coverText) {
				coverText.innerHTML = '';	
				if (YAHOO.payment.panel.persalert) {
					YAHOO.payment.panel.persalert.hide();
				}
				if (config && config.perstype != '') {
					createCookie('persCookie'+config.perstype, '', 30);
				}
			}
			if (text == '') {
				var persPriceDiv = YAHOO.util.Dom.get('personalizationPrice-' + config.persid + '-' + config.itemid);
				if (persPriceDiv) {
					persPriceDiv.style.display = 'none';
				}
			}
		}
		
		YAHOO.shopperschoice.calculatePriceWithCustomization();
	}
	
	function PersonalizeFromDropdown(config) {
		if (config) {
			var mainI = YAHOO.util.Dom.get('productImageMain');
			var oaPersCover = YAHOO.util.Dom.get('reqSelectionPersCover'+config.persid);
			var oaPersSelect = YAHOO.util.Dom.get('persOptionDrop'+config.persid+'-'+config.itemid);
			if (mainI && oaPersCover && oaPersSelect) {
			//alert(oaPersSelect.length);
				for(persx=0;persx<oaPersSelect.length;persx++) {
					
					if (oaPersSelect[persx].selected == true) {
						//alert(oaPersSelect[persx].className);
						var persClassPieces = oaPersSelect[persx].className.split(" ");
						showPersOnImage = persClassPieces[0];
						persImageURL = persClassPieces[1];
					}
				}
				
				var detectPersSelectOnScreen = YAHOO.util.Dom.getX(oaPersSelect);
				if (showPersOnImage == 'Yes' && detectPersSelectOnScreen) {
					xx = YAHOO.util.Dom.getX(mainI);
					yy = YAHOO.util.Dom.getY(mainI);
					xx = parseInt(xx);
					yy = parseInt(yy);
					replace_image_x = parseInt(config.x);
					replace_image_y = parseInt(config.y);
					xfinal = parseInt(xx + replace_image_x);
					yfinal = parseInt(yy + replace_image_y);
					//To strip whitespace and make lower case
					rel_brand_name = config.brand;
					rel_brand_folder = rel_brand_name.toLowerCase();
					rel_brand_folder = rel_brand_folder.replace(/[^A-Za-z0-9]/, '');
					image_url = '<img src="/images/personalization/'+rel_brand_folder+'/'+persImageURL+'" />';
					
					var reqSelectDiv = YAHOO.util.Dom.get('reqSelectionPersCover'+config.persid);
					if (reqSelectDiv) {
						reqSelectDiv.innerHTML = image_url;
						reqSelectDiv.style.display = 'block';
						YAHOO.util.Dom.setX(reqSelectDiv, xfinal);
						YAHOO.util.Dom.setY(reqSelectDiv, yfinal);
					}
					
					if (!YAHOO.payment.panel.persalert) {
						YAHOO.payment.panel.persalert = new YAHOO.widget.Panel("optionPersAlertBox", { width:"150px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}, context:[ "productImageMain", "tr", "tl" ], constraintoviewport: false, underlay:"shadow", close:true, visible:false, draggable:false, modal:false} );
						YAHOO.payment.panel.persalert.render();
						YAHOO.payment.panel.persalert["personalizeAlertBox"] = YAHOO.payment.panel.covertip;										
					}
						//YAHOO.payment.panel.persalert.setBody('<img src="/images/personalize-arrow.gif" align="left" /><b>Did You Notice?</b><br />Your personalization is being shown on the product image!');
						YAHOO.payment.panel.persalert.setBody('<b>Did You Notice?</b><br />Your personalization is being shown on the product image!');
					ShowPersonalizeAlert(5000);
				}
				
			}
		}
	}
	
	function ShowPersonalizeAlert(delay) {
		if (YAHOO.payment.panel.persalert && YAHOO.payment.panel.shownAlert == false && delay) {
			YAHOO.payment.panel.persalert.show();
			setTimeout("HidePersonalizeAlert()", delay);
			YAHOO.payment.panel.shownAlert = true;
		}
	}
	
	function HidePersonalizeAlert() {
		if (YAHOO.payment.panel.persalert) {
			YAHOO.payment.panel.persalert.hide();
		}
	}
	
	function ValidatePersonalizationText(itemid, persid, regex, required, optionacc) {
		if (itemid && persid && regex && required && optionacc) {
			if (optionacc == 'Yes') {
				//kind of a goofy way to check for it, but if the text box for the required selection customization is hidden, ignore it and push to cart
				//otherwise, if it is showing, proceed with checks
				var testTextBox = YAHOO.util.Dom.get('persTextBox'+persid+'-'+itemid);
				var testX = YAHOO.util.Dom.getX(testTextBox);
				if (isNaN(testX) || testX == 0) {
					if (!YAHOO.payment.panel.errortip) {
						//do nothing, but no errors produced
					} else {
						YAHOO.payment.panel.errortip.hide();	
					}
					return true;
				}
			}
			var validateText = YAHOO.util.Dom.get('persTextBox'+persid+'-'+itemid);
			if (validateText) {
				if (required == 'No' && validateText.value == '') {
					//the personalization is not required, but the text box is blank so no big deal
					if (!YAHOO.payment.panel.errortip) {
						//do nothing, but no errors produced
					} else {
						YAHOO.payment.panel.errortip.hide();	
					}
					return true;				
				} else if (required == 'Yes' || (required == 'No' && validateText.value != '')) {
					var testValidateText = validateText.value.search(regex);
					if (testValidateText != -1) {
						if (!YAHOO.payment.panel.errortip) {
							//do nothing, but no errors produced
						} else {
							YAHOO.payment.panel.errortip.hide();	
						}
						return true;
					} else {
						if (!YAHOO.payment.panel.errortip) {
							YAHOO.payment.panel.errortip = new YAHOO.widget.Panel("personalizeErrorTip", { width:"150px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.1}, context:[ "buttonOfTruth", "tl", "bl" ], constraintoviewport: false, underlay:"shadow", close:false, visible:false, draggable:false, modal:false} );
							YAHOO.payment.panel.errortip.render();
							YAHOO.payment.panel.errortip["personalizeErrorTip"] = YAHOO.payment.panel.errortip;
						}
						if (required == 'No') {
							YAHOO.payment.panel.errortip.setBody('Please make corrections to your personalization before adding to the cart.');
						} else {
							YAHOO.payment.panel.errortip.setBody('You must personalize this item before you purchase it.');
						}
						YAHOO.util.Dom.setStyle('personalizeErrorTip', 'color', 'red');
						YAHOO.util.Dom.setStyle('personalizeErrorTip', 'background-color', '#fee');
						YAHOO.payment.panel.errortip.show();
						return false;
					}		
				} else if (required == 'No' && validateText.value == '') {
					//the personalization is not required, but the text box is blank so no big deal
					return true;
				}
			}
		}
		
	}
	
	function CheckForPersCookieAndFill(pers_type, fill_box) {
		if (pers_type && pers_type !='') {
			var persCookieVal = readCookie('persCookie'+pers_type);
			if (persCookieVal && persCookieVal != '') {
				var persText = YAHOO.util.Dom.get(fill_box);
				if (persText) {
					persText.value = persCookieVal;
					return true;
				}
			}
		}	
		return false;
	}

