YAHOO.namespace('felixdoolittle');

function showSimpleDialog(url, width, height) {
	var dialog = window.open(url, "dialog", "scrollbars=0, resizable=1, menubar=0, toolbar=0, location=0, statusbar=0, width=" + width + ", height=" + height );
	dialog.focus();
}

function submitCallback(obj) {
	var response = obj.responseText;
	response = response.split("<!")[0];
	//document.getElementById("resp").innerHTML = response;
	alert(obj.responseText);
	eval(response);
}

function submitFailure(obj) {
	alert("Submission failed: " + obj.status);
}

function initProductDialog() {
	initMasterDialog( gProductDialogWidth );
}

function initMasterDialog(dlgWidth) {
	var handleCancel = function() {
		var productImage = document.getElementById("productImage");
		var productStyle = document.getElementById("productStyle");
		var productSKU = document.getElementById("productSKU");
		var productCollection = document.getElementById("productCollection");
		try {
			productImage.src = 'images/spacer.gif';
			productImage.alt = '';
			productStyle.innerHTML = '';
			productSKU.value = '';
			productCollection.value = '';
			YAHOO.felixdoolittle.dlg.show()
		} catch( e ) {
			//ignore if element is not present on page
		}	
		this.cancel();
	}
	var handleSubmit = function() {
		this.submit();
	}
	
	YAHOO.felixdoolittle.dlg = new YAHOO.widget.Dialog("detailsDialog", { modal:true, visible:false, width:dlgWidth, fixedcenter:true, constraintoviewport:true, draggable:true });
	
	YAHOO.felixdoolittle.dlg.callback.success = submitCallback;
	YAHOO.felixdoolittle.dlg.callback.failure = submitFailure;

	var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn:handleCancel,scope:YAHOO.felixdoolittle.dlg,correctScope:true} );

	YAHOO.felixdoolittle.dlg.cfg.queueProperty('postmethod','form');	
	YAHOO.felixdoolittle.dlg.cfg.queueProperty("keylisteners", listeners);
	
	try {
		if(gProductDialogAddToCartDisabled) {
			YAHOO.felixdoolittle.dlg.cfg.queueProperty("buttons", [ { text:"Close", handler:handleCancel } ]);			
		} else {
			YAHOO.felixdoolittle.dlg.cfg.queueProperty("buttons", [ { text:"Confirm and Add to Cart", handler:handleSubmit }, { text:"Close", handler:handleCancel } ]);				
		}
	} catch( e ) {
		//variable gProductDialogAddToCartDisabled not defined. treat as if it is set to false
		    YAHOO.felixdoolittle.dlg.cfg.queueProperty("buttons", [ { text:"Confirm and Add to Cart", handler:handleSubmit }, { text:"Close", handler:handleCancel } ]);					
	}
	
	
	YAHOO.felixdoolittle.dlg.render();
}

function showProductDialog(imageURL, imageName, sku, coll) {
	var productImage = document.getElementById("productImage");
	var productStyle = document.getElementById("productStyle");
	var productSKU = document.getElementById("productSKU");
	var productCollection = document.getElementById("productCollection");
	try {
		try {
			if(gProductDialogImagePrefix != null && gProductDialogImagePrefix != 'undefined') {
				//imageURL = imageURL.substring(0, imageURL.lastIndexOf('/')) + "/" + gProductDialogImagePrefix + imageURL.substring(imageURL.lastIndexOf('/'));
				imageURL = "images" + "/" + gProductDialogImagePrefix + imageURL.substring(imageURL.lastIndexOf('/'));
			}
		} catch( e ) {
			//ignore if image prefix is not defined
		}
		productImage.src = imageURL;
		productImage.alt = imageName;
		productStyle.innerHTML = imageName;
		productSKU.value = sku;
		productCollection.value = coll;
		YAHOO.felixdoolittle.dlg.show()
	} catch( e ) {
			//if dialog is not yet ready, ignore
			//window.status = e.message;
	}

	try {
		loadFlash();
	} catch(e) {}
	
}

function showGiftCertificateDialog(amount, imageURL, imageName) {
	var productImage = document.getElementById("productImage");
	var giftAmount = document.getElementById("giftAmount");
	try {
		try {
			if(gProductDialogImagePrefix != null && gProductDialogImagePrefix != 'undefined') {
				//imageURL = imageURL.substring(0, imageURL.lastIndexOf('/')) + "/" + gProductDialogImagePrefix + imageURL.substring(imageURL.lastIndexOf('/'));
				imageURL = "images" + "/" + gProductDialogImagePrefix + imageURL.substring(imageURL.lastIndexOf('/'));
			}
		} catch( e ) {
			//ignore if image prefix is not defined
		}
		productImage.src = imageURL;
		productImage.alt = imageName;
		giftAmount.value = amount;
		YAHOO.felixdoolittle.dlg.show()
	} catch( e ) {
			//if dialog is not yet ready, ignore
			//window.status = e.message;
			//alert(e.message);
	}
	
}

function showFont(font) {
	var fontExample = document.getElementById("fontExample");
	
	if( fontExample != null) {
		fontExample.src = 'images/fonts/face_' + font + '.jpg';
	}
}

function showColor(color) {
	var colorExample = document.getElementById("colorExample");
	
	if( colorExample != null) {
		colorExample.src = 'images/colors/' + color + '.jpg';
	}
}

function showFontDetails() {
	var fontSelect = document.getElementById("option.font");
	if( fontSelect != null) {
		showSimpleDialog('fonts.aspx'+'?font='+fontSelect.value, 680, 210);
	}
}

function checkGiftCertificateAmount() {
	var giftAmount = document.getElementById("giftAmount");
	var amount = giftAmount.value;
	
	amount = amount.replace(/^\s+|\s+$/, ''); //trim leading and trailing spaces
	for(var i = 0; i < amount.length; i++) {
		if( amount.charAt(i) < "0" || amount.charAt(i) > "9" ) {
			amount = 25;
			break;
		}
	}
	
	if( amount < 25 ) {
		amount = 25;
	}
	if( amount > 500) {
		amount = 500;
	}

	if( ( amount % 25 ) != 0 ) {
		amount = 25 * Math.floor( amount / 25 ) + 25;
		//alert( "Amount has been increase to $" + amount + " to be in an increment of $25." );
	}
	
	giftAmount.value = amount;

}
