/*
 * 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
 *
 */

// Global Mootools DIV for injection in overlays... 
var CONST_HALO_HTML = '<div class="halo-bg halo-bg-n"></div><div class="halo-bg halo-bg-ne"></div><div class="halo-bg halo-bg-e"></div><div class="halo-bg halo-bg-se"></div><div class="halo-bg halo-bg-s"></div><div class="halo-bg halo-bg-sw"></div><div class="halo-bg halo-bg-w"></div><div class="halo-bg halo-bg-nw"></div>';
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) {
	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 fnZoom(sZoomImage,oJSON){
	var sZoomDir = 'images/products/zoom/';
	var iMaxImageWidth = 550;  // If raw image is greater than this it will be HTML restrained. 
	var bHalo = true; // Show Halo Effect
	var oFX; // Fading FX!
	
	var sZoomTitleHTML = ''; // Zoom title HTML. 
	if(oJSON.products_model != '') sZoomTitleHTML += '<span class="zoom-product-model">'+oJSON.products_model+'</span>';
	if(oJSON.products_title != '') sZoomTitleHTML += '<span class="zoom-product-title">'+oJSON.products_title+'</span>';

	
	var oTargetCoords = $('zoom-btn').getCoordinates();
	
	if(!$('product-zoom')){ 
		// DIV Creation
		var oDiv = new Element('div', {
			'id' : 'product-zoom',
			'html': 'Loading... ',
			'styles': {
					'opacity': 0
			}
		});
		var oHalo = new Element('div',{'class': 'halo-bg-wrap','html' : CONST_HALO_HTML});
		var oCloseBtn = new Element('div',{'id':'zoom-close-btn','html':'<img src="css/img/zoom-close-btn.gif" alt="Close" border="0" />',
			'events': {
				'click' : function(){
					// Fade out then destroy... 
					if($defined(oFX)) oFX.cancel();
					oFX = new Fx.Tween(oDiv, {
						property: 'opacity', 
						duration: 800, 
						transition: Fx.Transitions.Expo.easeIn,
						onComplete: function(){
							oDiv.destroy();		
						}
					}).start(1,0);
				}
			}
		});

		
		var oImageTitle = new Element('div',{'id':'zoom-image-title','html': sZoomTitleHTML});		
		
		
		
		// DIV Injection
		oDiv.inject(document.body);
		oDiv.setStyles({'top':115,'left':(oTargetCoords.right-539)});
		oFX = new Fx.Tween(oDiv, {property: 'opacity', duration: 1500}).start(0,1);
		// CONTENT Injection
		var oImage = new Asset.image(sZoomDir+sZoomImage, {
			onload: function(){
				oDiv.empty();
				oDiv.grab(this);
				oDiv.grab(oCloseBtn,'top');
				if(bHalo) oDiv.grab(oHalo,'top');
				oDiv.grab(oImageTitle,'bottom');
				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 fnOpenPopUp(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';
	scrollTo(0,0);
	try 
	{
		var oPopup = window.open(sURL,sTitle,'resizable=yes,location=0,menubar=0,toolbar=0,width=850,height=600,scrollbars=1,top=100,left='+((screen.width/2)-450)+'');
		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 this site.\n");
	}	
}

