/*
 * Javascript Functions for infiniti.conair.com
 * Mootools functions. Requires the Mootools Javascript Framework (www.mootools.net)
 * @Author Tony Collings
 * @Copyright Conair Corporation
 * @Date July 2009
 * @Version 1.0
 *
 */


var sLoadingAnimation = '<div class="ajaxLoading"><img src="css/img/grey-ajax-loader.gif" alt="" border="0" /><br />Loading... Please Wait...</div>';
var bSelectBoxesDisabled; // Boolean flag set if IE6 and z-index is toggled and SELECT boxes are present. IE6 & SELECT Bug

// AJAX Wrapper function.... 
function gogogadgetAJAX(sURL,sElementToInject){
	var oElementToInject = $(sElementToInject);
	//var objAJAX; // AJAX object
	oElementToInject.set('html',sLoadingAnimation);
	oXHR = new Request.HTML({ 

		onRequest: function(){
				//
		},
		onStateChange: function(){
				//
		}, 
		onSuccess: function(sHTML) {
			
			// IE6 SELECT BOX BUG FIX
			// Remove Select Boxes for IE6 ONLY. IE6 + z-index = problem. 
			var oBrowserName = BrowserDetect.browser;
			var oBrowserVersion = BrowserDetect.version;
			if(oBrowserName == 'Explorer' && oBrowserVersion <= 6){
				// Grab ALL Select Boxes
				var arrElementArray = $$('select');
				fixIE6SelectBox(arrElementArray,false);
				bSelectBoxesDisabled = true; 
			}
		
		
			if(oElementToInject){
				oElementToInject.set('html', '');
				oElementToInject.adopt(sHTML);
			} else {
				alert('We\'re sorry, there seems to have been a technical issue in getting the content you have requested. Please refresh the page and try again. If the problem persists please contact us here at web_helpme@conair.com. Thank you for your patience');	
			}
		},
		onFailure: function() {
			if(oElementToInject){
				oElementToInject.set('html', '<span class="ajaxError">Please accept our apologies, but we have been unable to process this <a href="http://en.wikipedia.org/wiki/Ajax_(programming)" title="AJAX" target="_blank"><acroynm title="(A)synchronous (J)avaScript (A)nd (X)ML"><strong>AJAX</strong></acroynm></a> request. Please close this window and try again. If you still experience problems please contact us at : web_helpme@conair.com<br /><p><a href="javascript:window.location=document.URL" title="Close this Window"><strong>Close</strong></a></span></p>');
			} else {
				alert('We\'re sorry, there seems to have been a technical issue in getting the content you have requested. Please refresh the page and try again. If the problem persists please contact us here at web_helpme@conair.com. Thank you for your patience');	
			}
		}
	});
	oXHR.post(""+sURL+"")
	
}


// Passes in an array of elements (should all be select boxes) and true/false to toggle on/off
function fixIE6SelectBox(arrElementArray,toggle){
	if(toggle){
		// Show Elements	
		arrElementArray.each(function(el) {
			el.setStyle('display','block');
		});
	}else{
		// Hide Elements
		arrElementArray.each(function(el) {
			el.setStyle('display','none');
		});
	}
}
 
// Applies an onfocus/onblur effect to form borders. Input : array
function fancyBorders(arrFormElements,sFocusColor,sBlurColor){
	var oFX;
	arrFormElements.each(function(oElement,index,oFX) {
		if(oElement){
			oElement.setStyles({border:'1px solid '+sBlurColor});
			oElement.addEvents({
					'focus' : function() {
							if($type(this.oFX) == 'object') this.oFX.cancel();
							oElement.setStyles({'border-width':'1px','border-style':'solid'});
							this.oFX = new Fx.Tween(oElement,  {property: 'border-color', duration: 600/*, transition: Fx.Transitions.Back.easeOut*/}).start(sBlurColor,sFocusColor);
							
					}, 
					'blur' : function() {
							if($type(this.oFX) == 'object') this.oFX.cancel();
							oElement.setStyles({'border-width':'1px','border-style':'solid'});
							this.oFX = new Fx.Tween(oElement,  {property: 'border-color', duration: 600/*, transition: Fx.Transitions.Back.easeOut*/}).start(sFocusColor,sBlurColor);
					}
			}); 
		}
	});
	
}


function fnShowModelImage(sModelImage,sModelID,oCaller){
	var iMaxImageWidth = 350;  // If raw image is greater than this it will be HTML restrained. 
	var bHalo = true; // Show Halo Effect
	var bInstructions = true; // Show Drag Instructions

	
	
	var oTargetCoords = $(oCaller).getCoordinates();
	if(!$('model-thumb-'+sModelID)){ 
		// Create a DIV, hide it, move it, fade it.
		
		var oClickDrag = new Element('div',{
			'class': 'click-to-drag',
			'html' : 'Click and Hold to Drag'
		});
		var oDragWrapper = new Element('div',{'id': 'drag-wrapper'});		
		
		var oHalo = new Element('div',{
			'class': 'halo-bg-wrap',
			'html' : CONST_HALO_HTML
		});
						
		var oDiv = new Element('div', {
			'id' : 'model-thumb-'+sModelID,
			'class': 'js-model-thumb',
			'html': '<img src="images/icons/ajax/ajax-loader.gif" alt="" border="0" />',
			'styles': {
					'opacity': 0
			}
		});
		var oHREF = new Element('a', {
			'class': 'close-link',
			'html': 'Close',
			'href': 'javascript:void(0);',
		    'events': {
				'click': function(){
					$('model-thumb-'+sModelID).destroy();
				}
			}
 
		});
		oDiv.inject(document.body);
		oDiv.setStyles({'top':oTargetCoords.top,'left':oTargetCoords.left+20});
		
		
		var oFX = new Fx.Tween(oDiv, {property: 'opacity', duration: 3000, link:'chain',transition: Fx.Transitions.Expo.easeOut}).start(0,1);
		var oImage = new Asset.image('files/models/model_thumb/'+sModelImage, {
			onload: function(){
				oDiv.empty();
				oDiv.grab(this);
				oDragWrapper.wraps(this);

				if(bHalo) oDiv.grab(oHalo,'top');
				oDiv.grab(oHREF,'bottom');
				
				if(bInstructions){
					this.addEvent('mouseenter', function(){
						this.grab(oClickDrag,'after');
						(function(){
							oClickDrag.destroy();
							oImage.removeEvents('mouseenter');
						}).delay(3000); // After 2 sec' Remove
					});				
				}
				
				if(this.getCoordinates().width > iMaxImageWidth){
					// Resize... 
					var iRatio = this.getCoordinates().width/iMaxImageWidth;
					var iHeight = this.getCoordinates().height/iRatio; 
					this.setProperties({
						height: iHeight,
						width: iMaxImageWidth
					});
				}
				var oDrag = new Drag(oDiv);	
				
			
				
			}
		});
	}
}


function fnShowProductThumb(sProductThumb,sShootID,oCaller){
	var iMaxImageWidth = 350; // If raw image is greater than this it will be HTML restrained. 
	var bHalo = true; // Show Halo Effect
	var bInstructions = true; // Show Drag Instructions
	
	var oTargetCoords = $(oCaller).getCoordinates();
	if(!$('product-thumb-'+sShootID)){ 
		// Create a DIV, hide it, move it, fade it.
		
		var oClickDrag = new Element('div',{
			'class': 'click-to-drag',
			'html' : 'Click and Hold to Drag'
		});
		var oDragWrapper = new Element('div',{'id': 'drag-wrapper'});				
		
		var oHalo = new Element('div',{
			'class': 'halo-bg-wrap',
			'html' : CONST_HALO_HTML
		});		
			
		var oDiv = new Element('div', {
			'id' : 'product-thumb-'+sShootID,
			'class': 'js-product-thumb',
			'html': '<img src="images/icons/ajax/ajax-loader.gif" alt="" border="0" />',
			'styles': {
					'opacity': 0
			}
		});
		var oHREF = new Element('a', {
			'class': 'close-link',
			'html': 'Close',
			'href': 'javascript:void(0);',
		    'events': {
				'click': function(){
					$('product-thumb-'+sShootID).destroy();
				}
			}
 
		});
		oDiv.inject(document.body);
		oDiv.setStyles({'top':oTargetCoords.top,'left':oTargetCoords.left+20});
		var oFX = new Fx.Tween(oDiv, {property: 'opacity', duration: 3000, link:'chain',transition: Fx.Transitions.Expo.easeOut}).start(0,1);
		var oImage = new Asset.image('files/shoot_requests/product_thumb/'+sProductThumb, {
			onload: function(){
				oDiv.empty();
				oDiv.grab(this);
				oDragWrapper.wraps(this);
				
				if(bHalo) oDiv.grab(oHalo,'top');
				oDiv.grab(oHREF,'bottom');
				
				if(bInstructions){
					this.addEvent('mouseenter', function(){
						this.grab(oClickDrag,'after');
						(function(){
							oClickDrag.destroy();
							oImage.removeEvents('mouseenter');
						}).delay(3000); // After 2 sec' Remove
					});
				}

				
				if(this.getCoordinates().width > iMaxImageWidth){
					// Resize... 
					var iRatio = this.getCoordinates().width/iMaxImageWidth;
					var iHeight = this.getCoordinates().height/iRatio; 
					this.setProperties({
						height: iHeight,
						width: iMaxImageWidth
					});
				}
				var oDrag = new Drag(oDiv);	
			}
		});
	}
}


function fnOpenModelCardWindow(sURL,sTitle){
	// Override the sTitle Parameter. 
	// Microsoft implements the open() method in a slightly different way : http://msdn.microsoft.com/en-us/library/ms536651%28VS.85%29.aspx
	// So falling back on the lowest denominator
	var sTitle = '_blank';
	try 
	{
		var oPopup = window.open(sURL,sTitle,'resizable=yes,location=0,menubar=0,toolbar=0,width=800,height=600,scrollbars=0,top=100,left='+((screen.width/2)-400)+'');
		if (!oPopup.opener) oPopup.opener = self;
		if (window.focus) oPopup.focus();
	} 
	catch (e) 
	{
		alert("We just tried to open a \'Popup Window\', but have noticed pop-ups are blocked.\nTo enjoy this site at its best, please enable pop-ups for models.conair.com.\n");
	}	
}
function fnOpenStoryboardWindow(sURL,sTitle){
	// Override the sTitle Parameter. 
	// Microsoft implements the open() method in a slightly different way : http://msdn.microsoft.com/en-us/library/ms536651%28VS.85%29.aspx
	// So falling back on the lowest denominator
	var sTitle = '_blank';	
	try 
	{
		var oPopup = window.open(sURL,sTitle,'resizable=yes,location=0,menubar=0,toolbar=0,width=800,height=600,scrollbars=0,top=100,left='+((screen.width/2)-400)+'');
		if (!oPopup.opener) oPopup.opener = self;
		if (window.focus) oPopup.focus();
	} 
	catch (e) 
	{
		alert("We just tried to open a \'Popup Window\', but have noticed pop-ups are blocked.\nTo enjoy this site at its best, please enable pop-ups for models.conair.com.\n");
	}	
}


