/*jslint evil: true, forin: true */

/* Files contains vfLoader, vfContextualHelp, vfModalLayer and init call */

/*
 * Vodafone JS information, used in Lazy script load plugin
 * Stellent Asset ID VF016111
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
 * Version 1.1
 * 
 */
vdfScripts = {
	UI : {
		id: 'UI',
		url : "http://online.vodafone.co.uk/dispatch/Portal/SimpleGetFileServlet?dDocName=VF016113&revisionSelectionMethod=latestReleased&inline=false",
		localUrl : "assets/js/jquery.vdf.ui.js",
		jsPath: 'vdf.bUILoaded',
		initFunction: null,
		library: 'jQuery'
	},
	FormValidation : {
		id: 'FormValidation',
		url : "http://online.vodafone.co.uk/dispatch/Portal/SimpleGetFileServlet?dDocName=VF016112&revisionSelectionMethod=latestReleased&inline=false",
		localUrl : "assets/js/jquery.vdf.formValidation.js",
		jsPath: 'vdf.bFVLoaded',
		initFunction: null,
		library: 'jQuery'
	}
};

/**
 * Vodafone Lazy script load plugin
 * Provides lazy-load functionality for scripts. Check for the existance of a class and load if needed.
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
* @version 1.2
**/
var vfLoader = (function (vfLoaderObject) {
	var _bUseLocalPath = true;
	//Because we shouldn't trust the value above to be manually set
	//check if the domain ends with .co.uk or .com
	/*if (window.location.hostname.indexOf('.co.uk') === -1 
			&& window.location.hostname.indexOf('.com') === -1 ) {
		_bUseLocalPath = true;
	}*/
	var _oDeferredScripts = {};
	
	/**
	* @description - Called to init a script, if it does not exist in memory then load the script. Fires a callback
	* @param - item to load and init (object from vdf_config), arguments to pass to init function
	* @returns - false
	**/
	vfLoaderObject.initScript = function(oScriptInformation, oArgument, fCallbackOption) 
	{
		var oItem = checkExistance(oScriptInformation.jsPath);
	
		//if js path resolves to null the script is not loaded, set the callback and load the script
		if (oItem === null || typeof(oItem) == 'undefined') {
			//check the required scripts
			if (oScriptInformation.requires) {
				var bDeferred = false;
				for (var i=0,l=(oScriptInformation.requires.length); i<l;i++) {
					var sRequired = oScriptInformation.requires[i];
					var oResult = checkExistance(vdf.config.scripts[sRequired].jsPath);
					
					//if the script is not in scope
					if (oResult === null || typeof(oResult) == 'undefined') {
						bDeferred = true;
						initScript(vdf.config.scripts[sRequired]);
						
					}
				}
				if (bDeferred) {
					//defer loading of the current script
					_oDeferredScripts[oScriptInformation.id] = oArgument;
					var sTimeout = "initDeferredScript('"+oScriptInformation.id+"')";
					setTimeout(sTimeout,200);
					return 'deferred';
				}
			}
			
			//set init callback
			var fCallback = function() {
				var oSItem = checkExistance(oScriptInformation.jsPath);
				//call init function if defined
				if (jQuery.isFunction(fCallbackOption)) {
					fCallbackOption();
				}
				else if (jQuery.isFunction(oSItem[oScriptInformation.initFunction])) {
					oSItem[oScriptInformation.initFunction](oArgument);
				}
			};
			//insert script tag using href of js
			var sUrl = oScriptInformation.url;
			if (_bUseLocalPath === true) {
				sUrl = oScriptInformation.localUrl;
			}
			//loadScript(sUrl, fCallback);
			jQuery.getScript(sUrl, fCallback);
			return 'loading';
		}
		else if (oItem === false) {
			//error state, load failed
			return 'error';
		}
		else {
			//call init function if defined
			if (jQuery.isFunction(fCallbackOption)) {
				fCallbackOption();
			}
			else if (jQuery.isFunction(oItem[oScriptInformation.initFunction])) {
				oItem[oScriptInformation.initFunction](oArgument);
			}
			return 'existed';
		}
	};
	
	/**
	* @description - Called to init a script, if it does not exist in memory then load the script. Fires a callback
	* @param - item to load and init (object from vdf_config), arguments to pass to init function
	* @returns - false
	**/
	var initDeferredScript = function(sScriptID) 
	{
		initScript(vdf.config.scripts[sScriptID], _oDeferredScripts[sScriptID]);
	};
	
	
	/**
	* @description  - Return the item at the end of the path
	* @param- item to load and init (object from vdf_config), arguments to pass to init function
	* @returns - false
	**/
	var checkExistance = function(sJSPath) 
	{
		try {
			var aJsPath = sJSPath.split('.');
			var oItem = aJsPath[0] === 'jQuery' ? jQuery : vdf;
			for (var i=1,iLen=aJsPath.length; i < aJsPath.length; i++) {
				oItem = oItem[aJsPath[i]];
			}
			
			return oItem;
		}
		catch (e) {
			//problem with the path
			return null;
		}
	};
	
	
	/**
	* @description  - load the requested script, fires callback
	* @param- Href to script, callback function
	* @deprecated - Replaced above by jQuery getScript call
	*.@returns - false
	**/
	var loadScript = function(sHref, fCallbackfunction)
	{
		var eScriptTag = document.createElement("script");
		eScriptTag.setAttribute("type","text/javascript");
		eScriptTag.setAttribute("src", sHref);
		var $eScriptTag = jQuery(eScriptTag);
		
		$eScriptTag.bind("load", function() {
			fCallbackfunction();
		});
		//IE event handler - check this is not Opera as it shares this binding
		if (jQuery.browser.opera !== true) { 
			$eScriptTag.bind("readystatechange", function() {
				if(eScriptTag.readyState == "complete" || eScriptTag.readyState == "loaded") {
					fCallbackfunction();
				}
			});
		}
		document.body.appendChild(eScriptTag);
	};
	
	return vfLoaderObject;
	

//--closes class
}(vfLoader || {}));

/*
 * Vodafone Contextual Help plugin
 * Originally for the VF Total Comms project
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
 * Version 0.9
 * 
 * 
 */
 
(function($) {
	var aIDCreated = ',';
	$.fn.vfContextualHelp = function(config) {
		$.fn.vfContextualHelp.defaults = {
			sDisplayType : "manufacturors",
			sDefaultPosition : 'contextualHelp',
			iTimeout : 500,
			bGroupItems : true
		};
		var options = $.extend({}, $.fn.vfContextualHelp.defaults, config);
		
		var eHelpContainer;
		//move the hover content to the end of the document
		if (options.bGroupItems) {
			
			var aHelpContainer = $('#cxHelpContainer');
			if(aHelpContainer.length) {
				eHelpContainer = aHelpContainer[0];
			}
			else {
				eHelpContainer = $('<div id="cxHelpContainer">');
				$('body').append(eHelpContainer);
			}
		}
		
		return this.each(function(index, eAnchorElement) {
			var $eAnchorElement = $(eAnchorElement);
			
			//give the anchor an ID if required
			var sAnchorID = $eAnchorElement.attr('id');
			if (sAnchorID.length === 0) {
				sAnchorID = 'contextualAnchor_' + index;
				$eAnchorElement.attr('id',sAnchorID);
			}
			//use href to get ID
			var sHelpElementID = $eAnchorElement.attr("href");
			//else create from the id of them element
			if (typeof sHelpElementID === 'undefined') {
				sHelpElementID = '#' + $eAnchorElement.attr("id") + '_item';
			}
			var bPreExisting = aIDCreated.indexOf(','+sHelpElementID+',') === -1 ? false : true;
			aIDCreated = aIDCreated + sHelpElementID + ',';
			
			var $eHelpElement = $(sHelpElementID);
			
			//add to a single container?
			if (options.bGroupItems) {
				$(eHelpContainer).append($eHelpElement);
			}
			
			var oHoverInfo = {
				eHelpElement : $eHelpElement, 
				eAnchorElement : $eAnchorElement, 
				bStillover : false,
				sDisplayType : options.sDisplayType,
				sDefaultPosition : options.sDefaultPosition
			};
			
			//save info in data for the helpHover
			$eAnchorElement.data('helpHoverData',oHoverInfo);
			
			var fTimeoutCallShow = function() {
				showContextualHelpElement($eAnchorElement);
			};
			var fTimeoutCallHide = function() {
				hideContextualHelpElement($eAnchorElement);
			};
			
			$eAnchorElement
				.mouseover(function(event){ 
					$(this).data('helpHoverData').bStillover = true;
					setTimeout(fTimeoutCallShow,options.iTimeout);
				})
				.focus(function(event){ 
					$(this).data('helpHoverData').bStillover = true;
					setTimeout(fTimeoutCallShow,options.iTimeout);
				})
				.mouseout(function(event){ 
					$(this).data('helpHoverData').bStillover = false;
					setTimeout(fTimeoutCallHide,options.iTimeout);
				})
				.blur(function(event){ 
					var oLocalHoverData = $(this).data('helpHoverData');
					oLocalHoverData.bStillover = false;
					hideContextualHelpElement(oLocalHoverData.eAnchorElement);
				})
				.click(function(e){
					return false;
				}); 
			$eHelpElement
				.mouseenter(function(){
					$eAnchorElement.data('helpHoverData').bStillover = true;		 
				 })
				.mouseleave(function(){ 
					$eAnchorElement.data('helpHoverData').bStillover = false;
					hideContextualHelpElement($eAnchorElement);
				});
			
			//style help hover if required
			if (bPreExisting === false) {
				styleHelpHover(oHoverInfo);
			}
		});
	};
	
	/**
	* Shows the contextual Help element
	*/
	var showContextualHelpElement = function($eAnchorElement) {
		var oThisHover = $eAnchorElement.data('helpHoverData');
		if (oThisHover.bStillover) { 
			oThisHover.eHelpElement.show();
			positionContextualHelpElement(oThisHover);
		}
	};
	
	/**
	* Hides the Popup
	* @param  $eHelpElement
	*/
	var hideContextualHelpElement = function($eAnchorElement) {
		var oThisHover = $eAnchorElement.data('helpHoverData');
		if (oThisHover.bStillover === false) { 
			//setTimeout($eHelpElement.hide,1000);
			oThisHover.eHelpElement.hide();
			//$eHelpElement.fadeOut(500);
		}
	};
	
	/**
	* Sets the position of the Help Popup
	*/
	var positionContextualHelpElement = function(oThisHover) {
		var $eHelpElement = oThisHover.eHelpElement;
		var $eAnchorElement = oThisHover.eAnchorElement;
		var sDisplayType = oThisHover.sDisplayType;
		var sDefaultPosition = oThisHover.sDefaultPosition;
		
		var aPosition;
		switch (sDisplayType) {
			case 'russian' :
				aPosition = $eAnchorElement.position();
				aPosition.left = aPosition.left + $eAnchorElement.width() + 10;
				$eHelpElement.css({"top":aPosition.top,"left":aPosition.left});
				break;
			case 'classic' :
				/*aPosition = $eAnchorElement.position();
				aPosition.left = aPosition.left + $eAnchorElement.width();
				$eHelpElement.css({"top":aPosition.top-25,"left":aPosition.left});*/
				var aTargetPostition = $eAnchorElement.position();
				var iTopOffset = 0;
				var iHoverWidth = 219;
				var sPreferredPositionID = $eHelpElement.attr('class').match(/hoverPosition[A-Za-z0-9\-]*/);
				sPreferredPositionID = (sPreferredPositionID) ? sPreferredPositionID[0] : '';
				
				switch(sPreferredPositionID) {
					case 'hoverPositionBottomCentered':
						aTargetPostition.left += ( ($eAnchorElement.width() / 2) - iHoverWidth / 2);
						aTargetPostition.top += ($eAnchorElement.height());
					break; 
					
					case 'hoverPositionBottomRight':
						aTargetPostition.left += ( ($eAnchorElement.width()) - iHoverWidth);
						aTargetPostition.top += ($eAnchorElement.height());
					break; 
					
					case 'hoverPositionBottomLeft':
						aTargetPostition.top += ($eAnchorElement.height());
					break; 
					
					case 'hoverPositionAboveCentered':
						aTargetPostition.left += ( ($eAnchorElement.width() / 2) - iHoverWidth / 2);
						aTargetPostition.top -= ($eHelpElement.height()+iTopOffset);
					break;  
					
					case 'hoverPositionAboveRight':
						aTargetPostition.left += ( ($eAnchorElement.width()) - iHoverWidth);
						aTargetPostition.top -= ($eHelpElement.height()+iTopOffset);
					break; 
					
					case 'hoverPositionRight':
						aTargetPostition.left += $eAnchorElement.width();
						aTargetPostition.top -= 24;
					break; 
					
					case 'hoverPositionLeft':
						aTargetPostition.left -= iHoverWidth;
						aTargetPostition.top -= 24;
					break; 
					
					default: //Above-Left
						aTargetPostition.top -= ($eHelpElement.height()+iTopOffset);
				}
				$eHelpElement.css({ 
					left: aTargetPostition.left,
					top: aTargetPostition.top
				});
				break;
			default :
				aPosition = $eAnchorElement.offset();
				//the following should be expanded when it's actually required, currently a placeholder
				var bOverrulePosition = $eHelpElement.hasClass('contextualHelpRight');
				//if it's not to be displayed on the right hand side display above as normal
				if (bOverrulePosition || sDefaultPosition === 'contextualHelpRight') {
					aPosition.left = aPosition.left -4;
					aPosition.top = aPosition.top -6;
					$eHelpElement.css({"top":aPosition.top,"left":aPosition.left});
				}
				else {
					var iManualOffset = 23;
					aPosition.left = aPosition.left - ($eHelpElement.width()/2);
					aPosition.top = aPosition.top - ($eHelpElement.outerHeight() - iManualOffset);
					$eHelpElement.css({"top":aPosition.top,"left":aPosition.left});
				}
				break;
		}
	};
	
	/**
	* Configures HelpHover content div
	* @param- target element, positioning to style for
	* @returns - false
	**/	
	var styleHelpHover = function(oHoverInfo){
		if (oHoverInfo.sDisplayType === 'classic') {
			var sPreferredPositionID = oHoverInfo.eHelpElement.attr('class').match(/hoverPosition[A-Za-z0-9\-]*/);
			sPreferredPositionID = (sPreferredPositionID) ? sPreferredPositionID[0] : '';
			if(sPreferredPositionID.indexOf('Bottom') == -1) {
				sContent = '<div class="helpHoverContent">'+ oHoverInfo.eHelpElement.html() + '</div><div class="helpHoverFooter"></div>';
			}
			else {
				sContent = '<div class="helpHoverHeader"></div><div class="helpHoverContent">'+oHoverInfo.eHelpElement.html() + '</div>';
			}
			//populate with the saved content
			oHoverInfo.eHelpElement.html(sContent);
		}
	};
	
})(jQuery);

/*
 * Vodafone Modal layer plugin
 * Originally for the VF Total Comms project
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
 * Version 1.1
 * 
 */
(function($) {
	var sParentElement = 'body';
	var eModalContainer = false;
	$.fn.vfModalLayer = function(fCallback) {
		//set the callback function
		if (fCallback) {
			callbackFunction = fCallback;
		}
		//no direct action to take at the point of binding
		return this;
	};
	
	/**
	* Open a modal layer
	* Searches for casses and matches and switches to the ajax branch, or static branch
	* @param  event
	* @returns  false
	* @function
	*/
	var startModalLayer = function (event) {
		var eTargetElem = this;
		var sModalType = 'modalType_ajax';
		var aModelElements, sModalClassString, oModalLayer, sData;
		var bInitCall = true;
		var bAjaxCall = true;
		var sClassStr = "ajax";
		
		sModalClassString = jQuery(eTargetElem).attr('class').match(/modalStyle_[A-Za-z0-9]*/);
		if (sModalClassString) {
			sModalClassString = sModalClassString[0].replace('_','');
		}
		else {
			sModalClassString = 'modalStyleClassic';
		}
		
		//set callback function
		var fModalFuntions = callbackFunction;

		//create or reuse existing layer
		oModalLayer = $(sParentElement).data(sModalType);
		if (oModalLayer) {
			bInitCall = false;
			oModalLayer.Message(sData, sClassStr, fModalFuntions, bInitCall, eTargetElem);
		}
		else {
			var oSettings = {
				sModalType : sModalType,
				sModalClassString : sModalClassString
			};
			oModalLayer = new Modal(oSettings);
			
			//get initial content
			if (bAjaxCall === false) {
				sData = aModelElements.html();
				oModalLayer.Message(sData, sClassStr, fModalFuntions, bInitCall, eTargetElem);
			}
			else {
				//load remote content
				jQuery.ajax({
					url: eTargetElem.href,
					type: 'GET',
					success: function(sDataReturned) {
						sData = sDataReturned;
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						var sErrorMessage = '<h3>Sorry! We are not able to process your request now, please try later.</h3>';
						sData = '<div class="closeOverlay"><span class="link_close"><a href="#">Close</a></span></div>';
						sData += '<div class="modalWidthElement" style="width:450px">'+ sErrorMessage +'</div>';
					},
					complete : function (XMLHttpRequest, textStatus) {
						oModalLayer.Message(sData, sClassStr, fModalFuntions, bInitCall, eTargetElem);
					}
				});
			}
		}
		
		return false;
	};
	
	//Class constructor
	var Modal = function(oSettings) {
		var data = { };
		var sModalType = '';
		this.eModalElement = null;
		
		/**
		* Single text message Method, thats hands a callback, an ln Data and an Target to the show Method
		* @param {String} sData
		* @param {String} sClassStr
		* @param {Object} fCallback
		* @param {Boolean} bInitCall
		* @param {Object}  eEventTarget
		*/
		this.Message = function(sData,sClassStr,fModalFunctions, bInitCall, eEventTarget) {
			if (bInitCall) {
				data.data = sData;
				data.from = sClassStr;
				
				if (data.from !== 'ajax') {
					$(data.from).empty();
				}
				
				this.updateContent(sData);
			}
			this.show(fModalFunctions, bInitCall, eEventTarget);
		};
		
		this.updateContent = function(sData) {
			this.eModalElement.find(".modalLayerContent").empty();
			this.eModalElement.find(".modalLayerContent").html(sData);
		};

		/**
		* Shows the ModalLayer with initializing of all necessary Elements
		* Contains a callback, and if it is not an initial call, contents are not filled again.
		* @param {Object} fCallback
		* @param {Boolean}  bInitCall
		* @param  eEventTarget
		* @returns false
		*/
		this.show = function(fCallback, bInitCall, eEventTarget) {
			//set active modal for any timeout calls
			$(sParentElement).data('activeModal',this);
			showMask();
			
			var $eModalLayer = this.eModalElement;
			$eModalLayer.removeAttr("style");
			
			//chose transition
			if (typeof(this.animations[sModalType]) == 'object') {
				this.animations[sModalType].show.call(this, bInitCall, eEventTarget, fCallback);
			}
			//use default
			else {
				this.animations.modalStyleClassic.show.call(this, bInitCall, eEventTarget, fCallback);
			}
			
			//set focus
			$eModalLayer.focus();
			
			//make centered
			//fixPosition();
			
			if (bInitCall) {
				//initialise the JS of internal elements - NOT USED
			}
			return false;
		};
		
		/**
		* Hides the ModalLayer, ANimation is small but cute
		* @returns false
		*/
		this.hide = function() {
			var _this = this;
			var fEndOfAnimation = function () {
				_this.eModalElement.hide();
				var sModalID = _this.eModalElement.attr('id');
				if (sModalID == 'modalType_ajax') {
					_this.eModalElement.remove();
					//delete vdf.Tools.ModalLayer.aModalLayers[sModalID];
					$(sParentElement).data(sModalType, null);
				}
				//raise event to highlight that the modal has closed
				jQuery(sParentElement).trigger('modalClosedEvent', [sModalID]);
			};
			hideMask();
			
			//chose transition
			if (typeof(this.animations[this.sModalType]) == 'object') {
				this.animations[this.sModalType].hide.call(this,fEndOfAnimation);
			}
			//use default
			else {
				this.animations.modalStyleClassic.hide.call(this,fEndOfAnimation);
			}
			return false;
		};
		
		/**
		* Contains animation functions for lightbox. Each definition must include a show and hide
		* @returns false
		*/
		this.animations = {
			//manufacturors pages animations
			modalStyleManufacturors : {
				show : function (bInitCall, eEventTarget, fCallback) {
					var $eModalLayer = this.eModalElement;
					fixPosition();
					$eModalLayer.fadeIn(300, function () {
						
						jQuery(sParentElement).trigger('modalOpenEvent', [$eModalLayer.attr('id')]);
						if (fCallback) {
							fCallback.call(this, bInitCall, eEventTarget);
						}
					});
				},
				hide : function (fEndOfAnimation) {
					var $eModalLayer = this.eModalElement;
					$eModalLayer.animate({ 
						width: "0px",
						height: "0px",
						top:"50%",
						left:"50%",
						opacity: 0
					}, 100, fEndOfAnimation);
				}
			},
			//classic co.uk
			modalStyleClassic : {
				show : function (bInitCall, eEventTarget, fCallback) {
					var _this = this;
					var $eModalLayer = _this.eModalElement;
					
					$eModalLayer.css({display:'block', visibility:'hidden'});
					
					var aModalWidthElement = $eModalLayer.find('.modalWidthElement');
					//get the final height of the window & width
					var iFinalWidth = 0;
					var iFinalHeight = $eModalLayer.outerHeight();
					
					if (aModalWidthElement[0]) {
						iFinalWidth = jQuery(aModalWidthElement[0]).outerWidth();
					}
					else {
						iFinalWidth = $eModalLayer.outerWidth();
					}

					//set the start height & width & left and right
					var iStartHeight = 100;
					var iStartWidth = 150;
					
					//get the start and end left
					var iContainerWidth = jQuery(sParentElement).width();
					
					var iStartLeft = (iContainerWidth-iStartWidth)/2;
					var iFinalLeft = (iContainerWidth-iFinalWidth)/2;
					
					var iViewportHeight = eModalContainer.height();
					
					var iFinalTop = (iViewportHeight - iFinalHeight)/2;
				
					$eModalLayer.css({
							left : iStartLeft,
							width : iStartWidth,
							height : iStartHeight,
							top : iViewportHeight/2,
							overflow : 'hidden',
							visibility:'visible'
						})
						.animate({ left:iFinalLeft, width:iFinalWidth}, 300);
						
					window.setTimeout(function () {
									//vdf.Tools.ModalLayer.oActiveModal.animations.modalStyleClassic.show2.call(_this,iFinalHeight, iFinalTop);
									$(sParentElement).data('activeModal').animations.modalStyleClassic.show2.call(_this,iFinalHeight, iFinalTop, bInitCall, eEventTarget, fCallback);
								},600);
				},
				//function used in show
				show2 : function (iFinalHeight, iFinalTop, bInitCall, eEventTarget, fCallback) {
					var _this = this;
					var sModalID = _this.eModalElement.attr('id');
					_this.eModalElement
						.animate({height:iFinalHeight, top:iFinalTop}, 
								300, 
								function() { 
									jQuery(this).css('overflow','auto');
									//raise event to highlight that the modal has closed
									jQuery(sParentElement).trigger('modalOpenEvent', [sModalID]);
									if (fCallback) {
										fCallback.call(this, bInitCall, eEventTarget);
									}
								}); 
				},
				hide : function (fEndOfAnimation) {
					var $eModalLayer = this.eModalElement;
					fEndOfAnimation();
				}
			}
		};
	
		/*
		* Inital operations, happen when object is created
		*/
		
		//create container
		if (eModalContainer === false) {
			eModalContainer = jQuery('<div id="modalContainer"></div>');
			var iViewportHeight = window.innerHeight ? window.innerHeight : jQuery(window).height();
			eModalContainer.height(iViewportHeight);
			$(sParentElement).append(eModalContainer);
		}
		
		sModalType = oSettings.sModalClassString;
		this.eModalElement = $(createModalLayer(oSettings.sModalType,sModalType));
		
		eModalContainer.append(this.eModalElement);
		
		$(window).resize(resizeAction);
		
		//create mask
		if ($('#mask').length === 0) {
			eModalContainer.append(createMask(sModalType));
		}
	
		//append to global modal list
		$(sParentElement).data(sModalType,this);
	};
	
	/**
	* Initial setup of the ModalLayer
	* @param {String} sModalName
	* @param {String} sModalClasses
	* @returns The String containing the Grid for a general Layer
	*/
	var createModalLayer = function(sModalName, sModalType) {
		var sCloseBtnHTML = sModalType === 'modalStyleManufacturors' ? '<a class="closeBtn" href="#">close</a>' : '';
		//set type of modal
		var sModalID = sModalName ? sModalName : 'ModalLayer';
		return '<div id="' + sModalID + '" class="ModalLayer ' + sModalType +'" tabindex="-1">' +
							sCloseBtnHTML +
							'<div class="modalLayerContent">' +
							'</div>' +
						'</div>';
	};
	
	/**
	* Adds an special class given as sType to the eModalElement 
	* @param sType
	*/
	var setType = function(sType) {
		this.eModalElement.addClass(sType);
	};
	
	/**
	* Creates the mask used for the modal layer. Also fixes the height of the mask for IE6.
	* @returns the mask element
	*/
	var createMask = function(sModalType) {
		var eMask;
		if (sModalType === 'modalStyleManufacturors') {
			eMask = jQuery('<div id="mask"></div>');
		}
		else {
			eMask = jQuery('<div id="mask" class="MaskClassic"></div>');
		}
		/*if (jQuery('body').hasClass('browserIE6')) {
			var iHeight = ie6Fix.maskFix();
			eMask.height(iHeight);
		}*/
		return eMask;
	};
	/**
	* Show mask
	*/
	var showMask = function() {
		jQuery("#modalContainer").css("visibility","visible");
		if (jQuery('body').hasClass('browserIE6')) {
			ie6Fix.hideSelectBoxes(true);
		}
	};
	
	/**
	* Hide mask
	*/
	var hideMask = function() {
		jQuery("#modalContainer").css("visibility","hidden");
		if (jQuery('body').hasClass('browserIE6')) {
			ie6Fix.hideSelectBoxes(false);
		}
	};
	
	/**
	* Takes care of the position of the ModalLayer. Additional this is attached to a resize event of the window
	* Makes sure, that the Layer is alwas centered and at least inside the window.
	*/
	var fixPosition = function() {
		var oActiveModal = $(sParentElement).data('activeModal');
		if (oActiveModal) {
			var $eModalLayer = $(oActiveModal.eModalElement);
			
			//var top = $("#mask").height()/2-$eModalLayer.height()/2;
			var iViewportHeight = eModalContainer.height();  
			var top = iViewportHeight/2-$eModalLayer.height()/2;
			if ($eModalLayer.css("position")==="absolute") {
				top = 30;
				top += document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
			}
			if (top < 0) {
				top = 0;
			}
			$eModalLayer.css('top', top);
			$eModalLayer.css('left', $("#mask").width()/2-$eModalLayer.width()/2);    
		}
	};
	
	/**
	* When the viewport is resized we should resize the modalContainer
	*/
	var resizeAction = function() {
		var iViewportHeight = window.innerHeight ? window.innerHeight : jQuery(window).height();
		eModalContainer.height(iViewportHeight);
	};
	
	/**
	* When called submits the form using ajax and repopulates the lightbox with the returned information
	*/
	var modalFormSubmit = function(event,sLiveEvent) {
		var eForm = $(event.currentTarget);
		var oParmeters = eForm.serialize();
		var sDestination = eForm.attr("action");
		
		//update with loading state
		sLoadingBoxHTML = '<div class="loadingBox">The form is being submmitted, please wait.</div>';
		$(sParentElement).data('activeModal').updateContent(sLoadingBoxHTML);
		
		//send the form via ajax and get the response
		$.post(sDestination, oParmeters, function(data){
			//update the modal, removes loading state
			$(sParentElement).data('activeModal').updateContent(data);
		});
		return false;
	};
	
	/**
	* Default call back function
	*/
	var callbackFunction = function () {
		//fixPosition();
	};
	
	//create live events
	$(".modalLayer").live('click', startModalLayer);
	//Live Event for each button and anchor that looks like this. 
	$('#mask, .ModalLayer .closeBtn, .ModalLayer .link_close, .ModalLayer a.cancel').live("click", function(oEvent,sLiveEvent){
		$(sParentElement).data('activeModal').hide();
		return false;
	});
	//live event for form submission in modallayer
	$("#modalContainer form.ajaxForm").live('submit', modalFormSubmit);
	
})(jQuery);

/*
 * Vodafone init call
 * Author: Ryan Mitchell (2010) (http://www.sapient.com)
 * Version 0.0 (Branched from 1.0)
 * 
 */
(function($){
	$.noConflict();
	var htmlDoc = $(document.documentElement);
	htmlDoc.addClass("jsEnabled jsLoading");
	
	//create vdf if it doesnt already exist
	if (typeof vdf === 'undefined') {
		vdf = {};
	}
	
	$(document).ready(function(){
		htmlDoc.removeClass("jsLoading");
		
		$("input.modalLayer,a.modalLayer").vfModalLayer();
		$('.contextualAnchor').vfContextualHelp();
		$('.helpHoverTarget').vfContextualHelp({sDisplayType:'classic'});
		$('.contextualItem').vfContextualHelp({sDisplayType:'russian',bGroupItems:false});
		
		// load VF.UI ?
		var aInitQueue = [];
		var aAccordionElements = jQuery('.accordionList');
		if (aAccordionElements[0]){
			var fAccordionCallback = function () {
				aAccordionElements.vdfAccordion();
			};
			aInitQueue.push(fAccordionCallback);
		}
		
		//Tab init moved into the iPhoneCarousel.js
		
		var aCarouselElements = jQuery('.carousel');
		if (aCarouselElements[0]){
			var fCarouselCallback = function () {
				aCarouselElements.vdfCarousel();
			};
			aInitQueue.push(fCarouselCallback);
		}
		
		if (aInitQueue.length > 0) {
			//create loader callback
			var fLoaderCallback = function() {
				for (var i=0,iLen=aInitQueue.length; i<iLen; i++) {
					aInitQueue[i]();
				}
			};
			vfLoader.initScript(vdfScripts.UI, 'UI', fLoaderCallback);
		}
		
		/*Load form validation ?
		if (jQuery("form.validateFormExample")[0]) {
			//vdf.Loader.initScript(vdf.config.scripts.MessagePane);
			var fFormValidationCallback = function (e) {
				var oMessagePane = vfMessagePane.init();
				FormValidation.initialize("validateFormExample");
			};
			vfLoader.initScript(vdfScripts.FormValidation, "validateFormExample", fFormValidationCallback);
		}*/
	});

})(jQuery);



