/**
*
*	Host of custom JavaScript controls for pourlavictoire.com
*	@author: Jason Barney <jbarney@firstscribe.com>
*	@ver:	2011-03-14
*/


/**
*	jQuery custom iviewer for product view pages
*	Used prototype library for control (familiarity)
*/

/**
*	This function swaps out the left column of images, the main image, and selects 
*	the color from the correct drop down at the bottom
*/
function loadColor(color)
{
	//hide all the current ones
	$$('li.pourlavictoire-angle').invoke('hide');
	
	//show the side gallery items corresponding to this color
	$$('li.pourlavictoire-angle').each(function(s)
	{
		s.classNames().each(function(classname)
		{
			if(classname == color)
			{
				s.show();	
			}
		});
	});
	
	//choose this color from the option list below	
	
	$$('select.super-attribute-select').each(function(s) 
	{
		var optionLabel = /color/i;
		var checkContent = s.up(1).previousSiblings();
		previousContent = checkContent[0].innerHTML;
		if(previousContent.match(optionLabel))
		{
			s.childElements().each(function(ele,i)
			{
				var colorOpt = ele.innerHTML;
				var colorVal = ele.value;
				var compare = colorOpt.replace(/\s/gi,"_");
				//magento likes to add a trailing space so check for that as well
				if(color == compare || color == compare.slice(0,-1))
				{
						Form.Element.setValue(s,colorVal);
						spConfig.configureElement(s); //trigger magento event handler						
				}
			});
			
		}

	});
	
	//load color
	$$('.plv-first').each(function(s)
	{
		s.up(1).classNames().each(function(classname)
		{
			if(classname == color) //match
			{
				var toLoad = s.src;
				iviewer.loadImage(toLoad);
				firstLoaded = false;
			}
		});
	});
}
/**
*	This function loads the main image into the jQuery tool
*	@TODO: Fade in and out rather than jump
*/
function loadMainImage(image)
{
	iviewer.loadImage(image);
}

/**
*	Attach event listener to color selector
*/
function observeColorSelector()
{
	$$('select.super-attribute-select').each(function(s) 
	{
		var optionLabel = /color/i;
		var checkContent = s.up(1).previousSiblings();
		previousContent = checkContent[0].innerHTML;
		if(previousContent.match(optionLabel))
		{
			s.observe('change', colorSelect);
			maskColors(s);
		}		
	});
}

/**
*	Event observer for color selection dropdown
*/
function colorSelect()
{
	var colorSelector = this;
	for(var i = 0; i < colorSelector.length; i++)
	{
		if(colorSelector.options[i].selected)
		{
			var toLoad = colorSelector.options[i].innerHTML.replace(/\s/gi,"_");
			var checkEnd = /_$/;
			if(toLoad.match(checkEnd))
			{
				toLoad = toLoad.slice(0,-1) //chop off extra space on non-IE browsers
			}
			 loadColor(toLoad)
			 if(typeof omnitureColorClick == 'function')
			 {
			 	omnitureColorClick(toLoad);
			 }
		}
	}
}

/**
*	Hide out of stock colors
*/
function maskColors(colorSelectorInput)
{
	var colorChoices = Array();
	for(var i = 0; i < colorSelectorInput.length; i++)
	{
		var toLoad = colorSelectorInput.options[i].text.replace(/\s/gi,"_");
		if(toLoad != 'Choose an Option...')	colorChoices.push(toLoad);
	}
	//go throug each gallery link image and insure it's in the color drop down
	$$('a.gallery-color-picker').each(function(s)
	{
		var colorNames = s.classNames();
		colorNames.each(function(color)
		{
			//find the color
			if(color != 'gallery-color-picker')
			{
				thisColor = color;
			}
		});
		//make sure color is in the toLoad array
		var showColor = false;
		colorChoices.each(function(color)
		{
			if(color == thisColor)
			{
				
				showColor = true;
			}
		});
		if(! showColor) s.up().hide(); //hide parent <li> item so images float with no gaps.
		
	});
}

Event.observe(window,'load',observeColorSelector);
