/*
 * Vodafone streamline reg init call
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
 * Version 1.0
 * 
 */
(function(){
	//lightbox script from live, modified for streamline reg to allow the shadow.
	//adding to global scope so it can be added on page load
	shadowLightbox = {
		_sDetailsRef:".openShadowLightbox",	
		_sLightBoxID:"shadowLightbox",
		_sLightBoxClassName:"",
		_sContainerRef:"body",
		_eLightBox:null,
		_oPageCache:{},
		_sCurrentHref:null,
		_iTopOffset:10,
		_bMouseOver:false,
		
		/**
		* shadowLightbox.init : class constructor (description)
		*/
		init:function()
		{
			var aLightBoxes=$$(shadowLightbox._sDetailsRef);
			aLightBoxes.each(function(element){
				Event.observe(element,"click",shadowLightbox.triggerLightbox);
			});

		},
		
		triggerLightbox : function(event, bContinueAction, sTargetURL) 
		{
			if(event && !bContinueAction){ Event.stop(event); }
			if(!$(shadowLightbox._sLightBoxID)){
			var eContentHolder = document.createElement("div");
			shadowLightbox._eLightBox = eContentHolder;
			eContentHolder.id = shadowLightbox._sLightBoxID;	
			$(eContentHolder).writeAttribute('tabindex',-1).setStyle({visibility : "hidden"});
			eContentHolder.className = (shadowLightbox._sLightBoxClassName) ? shadowLightbox._sLightBoxClassName : "";
			document.body.appendChild(eContentHolder);
			
			//get the final height of the window & width
			var oDimensions = $(eContentHolder).getDimensions();
			var iFinalWidth = oDimensions.width;

			
			//get the start and end left
			var eLocalContainer = shadowLightbox._sContainerRef;
			if (eLocalContainer == false) {
				eLocalContainer = 'body';
			}
			var containerWidth = $$(eLocalContainer)[0].getWidth();
			var iEndLeft = (containerWidth-iFinalWidth)/2;
			
			var scrollHeight = document.viewport.getScrollOffsets()[1]+shadowLightbox._iTopOffset;
			//set position and fade in
			$(eContentHolder)
				.setStyle({
					visibility : "visible",
					left : iEndLeft+"px",
					top: scrollHeight+"px"
				})
				.setOpacity(0);
				$(eContentHolder).tween({
					alpha: {
						time: 0,
						duration: 0.4,
						start: 0,
						stop: 100,
						transition:FX.sineInOut
					}
				});
			//get contents
			if (sTargetURL) {
				shadowLightbox.getLightWindowContents(null, sTargetURL);
			}
			else {
				shadowLightbox.getLightWindowContents(event.target);
			}
			vdf.Tools.Overlay.showOverlay();
			$(vdf.Tools.Overlay._eOverlay).observe("click", shadowLightbox.bodyClick);
			
			Animate.play();
			shadowLightbox._eLightBox.focus();
			}
		},
	
		getLightWindowContents : function(element, sTargetURL) 
		{
			var sHref;
			
			if(element){
				var link;
				if (element.parentNode.nodeName == "A"){					
						link=element.parentNode;
				}else{					
						link =element; 
				}
			
				if(!link.href){
					link=$(element).select("a")[0];
				}				
					sHref=link.href;
			}else{
				sHref=sTargetURL;
			}
			
			if (sHref) 
			{
				shadowLightbox._sCurrentHref = escape(sHref);
				if(shadowLightbox._oPageCache[shadowLightbox._sCurrentHref])
				{
					shadowLightbox.processContents(shadowLightbox._oPageCache[shadowLightbox._sCurrentHref]);
				} else {
					new Ajax.Request(sHref, {
						method: 'get',
						parameters : {
							ajaxRequest : true
						},
						onSuccess: shadowLightbox.processContents,
						onFailure: shadowLightbox.failureHandler
					});
				}
				
				
			}
				
		},
	
		processContents : function(response, fCallBack) 
		{
			if(shadowLightbox._eLightBox.style.height === "" || shadowLightbox._eLightBox.style.height === "auto")
			{
				shadowLightbox.insertContents(response.responseText, fCallBack);
			} else 
			{
				window.setTimeout(function() {shadowLightbox.processContents(response, fCallBack);}, 1);
			}	
		},
		
		insertContents : function(response, fCallBack) 
		{
			shadowLightbox._oPageCache[shadowLightbox._sCurrentHref] = {responseText: response};
			$(shadowLightbox._eLightBox).removeClassName("loading");
			shadowLightbox._eLightBox.style.height = "auto";
			shadowLightbox._eLightBox.update(response);
			//sBGColor = $(shadowLightbox._eLightBox).getStyle("backgroundColor").backgroundColor;
			sBGColor = '#FFFFFF';
			$(shadowLightbox._eLightBox).setStyle({overflow:"hidden"});
			var fader = document.createElement("div");
			$(fader).setStyle({width: "100%", height: "100%", position: "absolute", top: "0px", left: "0px", backgroundColor : sBGColor});
			shadowLightbox._eLightBox.appendChild(fader);
			$(fader).tween(
					{
						alpha: 
						{
							time: 0,
							duration: 0.0,
							start: 100,
							stop: 0,
							transition:FX.quadInOut,
							onStop : function(element)
							{
								element.parentNode.removeChild(element);
							}
							
						}
					});
			Animate.play();
			if(fCallBack)
			{	
				fCallBack(shadowLightbox._eLightBox);	
			}
			shadowLightbox.applyClose();
			shadowLightbox.applyPrint();
			shadowLightbox.hideSelectBoxes();
		},
		
		hideSelectBoxes:function()
		{
			var allSelectBoxes=document.getElementsByTagName("select");
			var browser=navigator.appName;
			var b_version=navigator.appVersion;
			var version=parseFloat(b_version);
			if((browser=="Microsoft Internet Explorer")&&(version>=4))
			{
				for(i=0;i<allSelectBoxes.length;i++){
					allSelectBoxes[i].style.visibility="hidden";
				}
			}
		},
		
		showSelectBoxes:function()
		{
			var allSelectBoxes=document.getElementsByTagName("select");
			for(i=0;i<allSelectBoxes.length;i++){
				if(allSelectBoxes[i].style.visibility=="hidden"){
					allSelectBoxes[i].style.visibility="visible";
				}
			}
		},
		
		applyClose : function() 
		{
			//apply the close functions to the lightbox buttons
			var closeLinks = $(shadowLightbox._eLightBox).select(".link_close");
			closeLinks.each(function(element){
				element.observe("click", shadowLightbox.closeLightBox);
			});
			//apply the form submit
			var forms = $(shadowLightbox._eLightBox).select("form.ajaxSubmit");
			forms.each(function(element){
				element.observe("submit", shadowLightbox.formSubmit);
			});
			//also check for help hovers
			var helpHoverTargets = $(shadowLightbox._eLightBox).select('.helpHoverTarget');
			if (helpHoverTargets[0]){
				vdf.Tools.HelpHover.init(helpHoverTargets);
			}	
					
		},
		
		formSubmit : function(event) 
		{
			Event.stop(event);
			$(shadowLightbox._eLightBox).addClassName("loading");
			if(document.getElementById('addinfo')){
				document.getElementById('addinfo').disabled = true;
			}
			//send the form via ajax and get the response
			var eForm = event.target;
			var oParmeters = $(eForm).serialize(true);
			var sDestination = event.target.getAttribute("action");
			//START | Hema | Cloud Based Apps | July 2011 | To capture full path of form action
			try{
				if(document.getElementById('formAction')){
					var sDestination = document.getElementById('formAction').value;
				}
			}catch(e){
				//No error handling
			}
			//END | Hema | Cloud Based Apps | July 2011
			shadowLightbox._sCurrentHref = escape(sDestination);
			oParmeters.ajaxRequest = true;
			new Ajax.Request(sDestination, {
				encoding: '',
				method: 'get',
				parameters: oParmeters,
				onSuccess: shadowLightbox.processContents
			});
		},
	
		applyPrint:function()
	
		{
			var printLinks=$(shadowLightbox._eLightBox).select(".link_print");
			printLinks.each(function(element){element.observe("click",shadowLightbox.printLightBox);});
		},
		
		closeLightBox:function(event)
		{
			try{
			
			/* START | if the class "grid" is available @ #grid, no changes else, add .grid | Abdul */
			
			var gridElem = document.getElementById("grid");
			var gridClassCheck = 'grid';
			var gridClassPresent = false;
			
			  if ((" " + gridElem.className + " ").replace(/[\n\t]/g, " ").indexOf(gridClassCheck) > -1) {
				gridClassPresent = true;
			  }
			
			if(gridClassPresent == false){
				document.getElementById("grid").className="grid";
			}
			
			/* END | add .grid | Abdul */
		
			if(document.getElementById("mainShell")){
				document.getElementById("mainShell").className="";
			}
			} catch(e) {}
			shadowLightbox.showSelectBoxes();
			vdf.Tools.Overlay.hideOverlay();
			shadowLightbox._eLightBox.parentNode.removeChild(shadowLightbox._eLightBox);
			Event.stop(event);
		},
		
		closeLightBoxNew:function()
		{
			document.getElementById("grid").className="grid";
			if(document.getElementById("mainShell")){
				document.getElementById("mainShell").className="";
			}
			shadowLightbox.showSelectBoxes();
			vdf.Tools.Overlay.hideOverlay();
			shadowLightbox._eLightBox.parentNode.removeChild(shadowLightbox._eLightBox);
			shadowLightbox._eLightBox.parentNode.refresh();
			//Event.stop(event);
		},
				
		printLightBox:function(event)
		{
			document.getElementById("grid").className="grid printOverlay";
			if(document.getElementById("mainShell")){
				document.getElementById("mainShell").className="printOverlay";
			}
			window.print();
			Event.stop(event);
		},
		
		bodyClick : function(event) 
		{
			//check whether the mouse is over the panel
			if(document.getElementById('thanksChordiant')){
				closeLightBox();
			}
			shadowLightbox.closeLightBox(event);		
		}	
	};
	
	var ieHoverButton = {
		init : function () {
			//get all buttons
			var aButtons = $$('.srBtnRed,.srBtnWhite,.srSmallBtnRed,.srSmallBtnWhite');
			aButtons.each(function(elem, i){ 
				//add events to each input for focus & blur
				elem.observe('mouseover', ieHoverButton.enter);
				elem.observe('mouseleave', ieHoverButton.leave);
			});
		},
		enter : function() {
			this.addClassName('ieBtnFocus');
		},
		leave : function() {
			this.removeClassName('ieBtnFocus');
		}
	};
	
	
	//var htmlDoc = $(document.documentElement);
	//htmlDoc.addClass("jsEnabled jsLoading");
	var srEmptyFieldClass = 'srEmptyField';
	
	var inputOnFocus = function (elem) {
		if (this.hasClassName(srEmptyFieldClass)) {
			this.removeClassName(srEmptyFieldClass);
		}
	};
	var inputOnBlur = function (elem) {
		if (this.value === '') {
			this.addClassName(srEmptyFieldClass);
		}
	};
	
	document.observe("dom:loaded", function() {
		//observe new lightbox
		shadowLightbox.init();

		Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
		if (Prototype.Browser.IE6) {
			ieHoverButton.init();
		}
		
		//set input background colour and actions
		var aEm = $$('.streamlineReg input[type=text], .streamlineReg input[type=password]');
		aEm.each(function(elem, i){ 
			//add events to each input for focus & blur
			elem.observe('focus', inputOnFocus);
			elem.observe('blur', inputOnBlur);
			//initial setting
			inputOnBlur.call(elem);
			
			//elem.addClassName('inputTextInput'); //for IE6
		});
		
	});

})();

