/*
 * Javascript Functions for whitecloudcharter.com
 * @Author Tony Collings
 * @Date July 2009
 * @Version 1.0
 *
 */
 

// "Go Go Gadget!" object. A personal homage to Inspector Gadget http://en.wikipedia.org/wiki/Inspector_Gadget : -)
var gogoGadget = {
	
	
	// AJAX Wrapper function.... 
	'ajax' : function (sURL,sElementToInject){
		
		var sLoadingAnimation = '<div class="ajaxLoading"><img class="ajax-loader" src="css/img/wonb-ajax-loader.gif" alt="" border="0" />Loading... Please Wait...</div>';
		var oElementToInject = $(sElementToInject);
		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+"")
	}
}



// 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);
			}
		}); 
	});
}

/* Panoramas */
function virtualtour(sVRFile){
	/* 
		Creates DOM Structure prior to the closing body tag and injects SWF: 
		
		<div id="vr-overlay">
			<div id="vr-overlay-wrapper">
			{SWF Object id="vr-swf-file"}
			</div>
		</div>
		</body>
	*/
	
	// Destroy any that may be left open (catch)
	if($('vr-overlay')) $('vr-overlay').destroy();
	
	// 1. Create DIV ... 
	var oVROverlay = new Element('div', {
		'id' : 'vr-overlay',
		'html' : '<a href="javascript:void(0);" onclick="closeElement(\'vr-overlay\');" title="Close Virtual Tour">Close Virtual Tour</a>',
		'styles': {
			'z-index' : 4000,
			'width': 510, //'height':400, 
			'top': 100, 'left' : '50%', 'margin-left' : '-250px',
			'opacity' : 0
		},
		'events': {
			'mouseleave': function(){},
			'click' : function(){}
		}
	});
	oVROverlay.inject($(document.body), 'bottom');
	
	// 2. Inject the SWF... 
	var oVRWrapper = new Element('div',{'id' : 'vr-overlay-wrapper'}); // Create a wrapper for the SWIFF object to assign event handles too. 
 	oVRWrapper.inject(oVROverlay,'top');
		
	var oVRSWF = new Swiff(sVRFile, {
		id: 'vr-swf-file',
		width: 500,
		height: 400,
		params: {
			wmode: 'transparent',
			allowfullscreen: 'true'
			//bgcolor: '#ff3300'
		}
	});
		
	
	// 3. Fade In... 
	var oVRFX = new Fx.Tween(oVROverlay, {
		property: 'opacity',
		duration: 400, // 1000 = 1 second
		onComplete: function(){
			oVRWrapper.adopt(oVRSWF);	
		}
	}).start(0,1);
	
	
	// 4. Draggable
	var bDraggable = true; // Set to false to disable dragging. 
	if(bDraggable){
		var oDrag = new Drag(oVROverlay,{
			onBeforeStart : function(){
				oVROverlay.setStyles({'margin-left':0,'left':oVROverlay.getCoordinates().left}); // Prevents a nasty 'jump' due to negative CSS margin. 
			}
		});
		// Kill mouse events when 'in' the Pano to enable Flash Events (Drag, Move etc...) 
		oVRWrapper.addEvents({
			'mouseenter': function(){
				oDrag.detach();
			},
			'mouseleave': function(){
				oDrag.attach();
			}
		});
	}


}

function virtualtour_alt(sVRFile){
	// This method switches out the main banner for the pano'
	var oMainBanner = $('banner');

if(!$defined(oMainBanner.retrieve('innerHTML'))) oMainBanner.store('innerHTML',oMainBanner.getProperty('html'));
	oMainBanner.store('height',oMainBanner.getCoordinates().height);
	
	oMainBanner.setStyles({'height':oMainBanner.getCoordinates().height-5,'margin':0,'padding':0,'opacity':0}); // Ensure element doesn't (collapse)
	oMainBanner.empty();
	var oVRSWF = new Swiff(sVRFile, {
		id: 'vr-swf-file',
		width: 950,
		height: oMainBanner.getCoordinates().height-5,
		params: {
			wmode: 'opaque',
			allowfullscreen: 'true'
			//bgcolor: '#ff3300'
		}
	});
	
	oMainBanner.adopt(oVRSWF);
	
	var oVRFX = new Fx.Tween(oMainBanner, {
		property: 'opacity',
		duration: 400 // 1000 = 1 second
	}).start(0,1);		
	
	// Add Close button
	var oClose = new Element('div', {
		'id' : 'close-button',
		'html' : '<a href="javascript:void(0);" onclick="restore();" title="Close Virtual Tour">Close Virtual Tour</a>',
		'styles': {'z-index' : 4000}
	});
	
	oClose.inject(oMainBanner,'bottom');
	oClose.setStyles({
		'top': (oMainBanner.getCoordinates().height-oClose.getCoordinates().height), 
		'left' : (oMainBanner.getCoordinates().width-oClose.getCoordinates().width)
	});
	
}

function restore(){
	var oMainBanner = $('banner');
	oMainBanner.empty();
	oMainBanner.setProperty('html',oMainBanner.retrieve('innerHTML'));
	oMainBanner.setStyle('height',oMainBanner.retrieve('height')-5);
}

function closeElement(oElement){
	// Fade out and then destroy();
	oElement = $(oElement);
	oElement.destroy();
	/* 
	// Had to kill fade, 'causing too many problems in FF2
	var oVRFX = new Fx.Tween(oElement, {
		property: 'opacity',
		duration: 400, // 1000 = 1 second
		onComplete: function(){
			oElement.empty();
		}
	}).start(1,0);
	*/
}


