/* sizing form display enhancements                  *
 * alter CSS so that the highest priority content is *
 * shown to the user based on radio selection while  *
 * other content is hidden                           */
//Event.observe(window, 'load', sizingInit, false);

function sizingInit() {
	/* assign event handler to standard sizing radio */
	if (standardSizingRadios = $A($$('div.main div.standard_size div.option input'))) {
		standardSizingRadios.each( function(radio){
			Event.observe(radio, 'click', function(){ toggleFitInfo('standard'); }, false);
		});
	}
	/* assign event handler to fabulous fit radio */
	if (fabulousFitRadios = $A($$('div.main div.fabulous_fit div.option input'))) {
		fabulousFitRadios.each( function(radio){
			Event.observe(radio, 'click', function(){ toggleFitInfo('fabulous-fit'); }, false);
		});
	}
	/* default state for page load */
	//toggleFitInfo('standard');
	
}

function toggleFitInfo(toggleClass) {
/* handler for defining toggle cases based on input           *
 * so as to be abstracted from event handling assignments     *
 * toggleClass as a name for a case                           *
 * element references are passed to a general toggle function */ 

	switch(toggleClass) {
		case 'standard':
			toggleContent('div.main div.standard_size div.on','div.main div.fabulous_fit div.on');
			toggleContent('div.main div.fabulous_fit div.off','div.main div.standard_size div.off');
			break;
		case 'fabulous-fit':
			toggleContent('div.main div.fabulous_fit div.on','div.main div.standard_size div.on');
			toggleContent('div.main div.standard_size div.off','div.main div.fabulous_fit div.off');
			break;
	}
}

function toggleContent(contentToShow,contentToHide) {
/* inputs are treated as Prototype.js $$() CSS selectors *
 * show the elements that match the first argument       *
 * hide the elements that match the second argument      */

	/* select and show standard content 'on' */
	if (showThese = $A($$(contentToShow))) {
		showThese.each( function(el){ el.show(); });
	}
	/* select and hide fabulous fit content 'on' */
	if (hideThese = $A($$(contentToHide))) {
		hideThese.each( function(el){ el.hide(); });
	}
}


/* cc verification code popup window                 *
 * opens a new window for informational purposes     *
 * when a 'what's this?' element clicked in checkout */

var ccVerInfoPopUpWindow;
Event.observe(window, 'load', ccVerificationCodePopInit, false);

function ccVerificationCodePopInit() {
	/* assign event handlers to window popup teaser */
	if (ccvPopSource = $A($$('div.main div.methods div.verification_code label span'))) {
		ccvPopSource.each( function(popSource){
			Event.observe(popSource, 'click', openRequestedPopUp, false);
			Event.observe(popSource, 'mouseover', function(){ this.addClassName('popSourceHover'); }, false);
			Event.observe(popSource, 'mouseout', function(){ this.removeClassName('popSourceHover'); }, false);
		});
	}
}

function openRequestedPopUp() {
	ccVerInfoPopUpWindow = window.open("../cc_verification_code.html",
					"CC_Verification_Code_Information",
 					"width=400,height=360,menubar=no,location=no,resizable=no,scrollbars=no,status=no");
}

/* dress image browser                                       *
 * swaps images in a preview pane based on two controls:     *
 * image_toggle: rotate between default image (filename.ext) *
 *               and alternate (filename-back.ext)           *
 * image_zoom:   rotate between previous view and            *
 *               zoom image (filename-zoom.ext)              */

Event.observe(window, 'load', dressBrowseInit, false);

function dressBrowseInit() {
	/* setup object */
	imageBrowser = Object;
	/* assign event handler to toggle button */
	/* EFLORES 1.19.10: Commenting out view back functionality*/
	/*if (swapImgToggleEls = $A($$('div.main div.product_image a.image_toggle'))) {
		swapImgToggleEls.each( function(imgToggle){
			Event.observe(imgToggle, 'click', swapImage, false);
		});
	} */
	/* assign event handler to zoom button */
	if (swapImgZoomEls = $A($$('div.main div.product_image a.image_zoom'))) {
		swapImgZoomEls.each( function(imgZoom){
			Event.observe(imgZoom, 'click', zoomImage, false);
		});
	}
}

function zoomImage() {
	/* select image preview element */
	if (imagePreviewEls = $A($$('div.main div.product_image img'))) {
		imagePreviewEls.each( function(imgPreview){
			fileDirs = Array;
			fileNameParts = Array;
			var src;
			/* get the URL of the preview image */
			src = imgPreview.src;
			/* break the file URL into parts */
			/* pop out file name as string after last forward slash */
			fileDirs = src.split('/');
			file = fileDirs.pop();
			/* break the file name into parts */
			/* pop out the extension as string after last '.' */
			fileNameParts = file.split('.');
			fileExtension = fileNameParts.pop();
			if (file.match(/-zoom/)) { // are we already at zoom state?
				var newFile;
				if (imageBrowser.previousImageSrc) { // reference the previous non-zoom image
					newFile = imageBrowser.previousImageSrc;
				} else { // reference the default image
					file = file.replace(/-zoom/,"");
					newFile = fileDirs.join('/') + '/' + file;
				}
				/* load non-zoom image into preview pane */
				imgPreview.src = newFile;
			} else {
				/* assign the previous image into an object property so as */
				/* to load same (default or -back) when zoom is toggled off  */
				imageBrowser.previousImageSrc = src;
				/* reconstruct file name with proper suffix for zoom image */
				var newFile;
				newFile = fileNameParts.join('.');
				/* remove '-back' from name if exists */
				newFile = newFile.replace(/-back/,"");
				newFile = fileDirs.join('/') + '/' + newFile + '-zoom.' + fileExtension;
				/* load zoom image into preview pane */
				imgPreview.src = newFile;
			}
		});
	}
}

function swapImage() {
	/* select image preview element */
	if (imagePreviewEls = $A($$('div.main div.product_image img'))) {
		imagePreviewEls.each( function(imgPreview){
			fileDirs = Array;
			fileNameParts = Array;
			var src;
			/* get the URL of the preview image */
			src = imgPreview.src;
			/* break the file URL into parts */
			/* pop out file name as string after last forward slash */
			fileDirs = src.split('/');
			file = fileDirs.pop();
			/* break the file name into parts */
			/* pop out the extension as string after last '.' */
			fileNameParts = file.split('.');
			fileExtension = fileNameParts.pop();
			if (file.match(/(-back|-zoom)/)) { // are we already at zoom or alternate image state?
				/* remove -back and -zoom from from file name so */
				/* as to match name of default image */
				file = file.replace(/(-back|-zoom)/,"");
				/* reconstruct file name for default image */
				var newFile;
				newFile = fileDirs.join('/') + '/' + file;
				/* load default image into preview pane */
				imgPreview.src = newFile;
			} else { // default state
				/* reconstruct file name for -back image */
				var newFile;
				newFile = fileDirs.join('/') + '/' + fileNameParts.join('.') + '-back.' + fileExtension;
				/* load -back image into preview pane */
				imgPreview.src = newFile;
			}
		});
	}
}



function catalogOvers() {

	/*
	process catalog product images
	____________________________________________*/

	// get all catalog product images
	var products = $A($$('div.catalog div.product img'));
	var recommendations = $A($$('div.catalog div.recommended img'));
	products.each(
		function(product) { hoverState(product); }
	);
	recommendations.each(
		function(product) { hoverState(product); }
	);
}

/*Event.observe(window, 'load', catalogOvers, false);*/
