/************** globalmoo.js *************************************************/
/* 
	Requires Mootools v1.2  - mootools.js
*/

//James Scott - To put the DART tag ord variable once in the global JS
var ord=Math.random()*10000000000000000;

/* 
Use mootools equivalent $()
function returnObjById( id ) { } 
*/

/*Attach events*/
//	alert('Browser.Engine.name = "' + Browser.Engine.name + '"\nBrowser.Engine.version=' + Browser.Engine.version + '\nnavigator.appVersion=' + navigator.appVersion);
//if ( (! Browser.Engine.trident) || (! navigator.appVersion.toLower().indexOf("msie 5///"))) {  // if browser is NOT ie, or if browser version is above 5.5
window.addEvent('domready', function() {
	if ((! Browser.Engine.trident) || (document.compatMode && document.all)) {  // if browser is NOT ie, or if IE 6+
		//Jump menus - old format
		if ($("divLDGListNav") && $$('.jumpMenu')) {
			$$('.jumpMenu').each(function(currMenu, i) {
				var newSelect = new Element('select');
				newSelect.addEvent('change', function(){
					if (this.value != 0){location.href = this.value};
				});
				var newOption = new Element('option', {
					'value' : '',
					'html'  : '-- choisir une option --'
				});
				newSelect.adopt(newOption);

				currMenu.getElements('li').each(function(currLi, j) {
					var currA = currLi.getElement("a"); //Get first anchor
					if (currA) { 
						newOption = new Element('option', {
							'value' : currA.get('href'),
							'html'  : currA.get('html')
						});
						if (currLi.hasClass('selected')) {
							newOption.set('selected', 'selected');
						}
						newSelect.adopt(newOption);
					}

				});
				newSelect.replaces(currMenu);
			});
			$("divLDGListNav").setStyle('display', 'block');
		} //Jump menus - new format
		else if ($$('.jumpMenu')) {
			$$('.jumpMenu').each(function(currMenu, i) {
				var newSelect = new Element('select');
				var newGoBtn = new Element('input', {
					'type'  : 'image',
					'src'   : '/Images/SearchBtn_tcm126-129434.png',
					'alt'   : 'Go',
					'events': {
						'click': function(){
							if (newSelect.value != 0){location.href = newSelect.value};
							return false;
						}
					}
				}); 
				var newOption = new Element('option', {
					'value' : '',
					'html'  : currMenu.getElement('p').get('text')
				});
				newSelect.adopt(newOption);

				currMenu.getElements('li').each(function(currLi, j) {
					var currA = currLi.getElement("a"); //Get first anchor
					if (currA) { 
						newOption = new Element('option', {
							'value' : currA.get('href'),
							'html'  : currA.get('html')
						});
						if (currLi.hasClass('selected')) {
							newOption.set('selected', 'selected');
						}
						newSelect.adopt(newOption);
					}

				});
				newSelect.replaces(currMenu.getElement('span'));
				newGoBtn.inject(newSelect, 'after');
				/*newSelect.setStyle('display', 'block');*/
			});
		}
		
		//Header Login Toggle
		if ($('hdrLoginToggle')) {
			var effectLoginBox = new Fx.Morph('hdrLoginToggle', {duration: 500, link: 'chain'});
			//Done in user control
			//effectLoginBox.set('.loginClosed'); 

			//attach Event to toggle the Header Login box
			$('hdrLoginLink').addEvent('click', function(){
				effectLoginBox.start({top: [-25,0]});
				return false;
			});
			$('hdrCloseLink').addEvent('click', function(){
				effectLoginBox.start({top: [0,-40]});
				return false;
			});
		}

		//See whats nearby LH menu
		if ($('divLLHMenu')) {
			var accToggles = $$('.accordHead');
			var accElements = $$('.accordList');
			var accOpenIndex = 0;
			accToggles.each(function(currToggle, i) {
				if (currToggle.className.search(/Open/) > 0) {accOpenIndex = i;}
			});

			var accordion = new Accordion(accToggles, accElements, {
				show: accOpenIndex,
				
				onActive: function(toggler, element){
					toggler.className = toggler.className.replace(/accordClosed/, "accordOpen");
				},

				onBackground: function(toggler, element){
					toggler.className = toggler.className.replace(/accordOpen/, "accordClosed");
				}
			});
			//Now show the menu - hidden during load so it won't pop
			$("divLLHMenu").setStyle('display', 'block');
		}

		//Check all links and modify external links to go via bounce.aspx
		var bounceURL = "/redirect/bounce.aspx?cid=";
		var myDomain = location.hostname.toLowerCase();

		$$('a').each( function(currA, i){
			//If external link, modify href to go via bounce.aspx
			if (currA.href != '' && currA.protocol.toLowerCase().search(/https?:/) != -1 && currA.hostname.toLowerCase() != myDomain) { 
				var thisCID;
				var replacementCID = currA.className.match(/cid(\d+)/i);
				if (replacementCID) { thisCID = replacementCID[1]; }else{ thisCID = pageCID;}
				currA.href = bounceURL + thisCID + "&url=" + currA.href;
			}
		
			//If class="newWin", force link to open in new window
			if (currA.className.match("newWin")) {
				currA.addEvent('click', function() {
					window.open(this.href);
					return false;
				})
			}
		});

		//Initialize home page effects if present
		if ($('overlay') && $('popMapContent')){
		
			$('popMapContent').setStyle('display', 'none');
			$('popMapContent').id = 'popMap';
			
			var mapDiv = $('popMap').dispose();
			mapDiv.inject($('overlay'), 'after');
		
			var effectOverlay = new Fx.Morph('overlay', {duration: 1300, link: 'chain'});
			var effectZoomMap = new Fx.Morph('popMap', {duration: 1300, link: 'chain'});
			var effectFadeMap = new Fx.Morph('popSubMap', {duration: 300, link: 'chain'});

			//attach Event to open the overlay and popMap 
			$('mapTrigger').addEvent('click', function(){

				$('popMap').setStyle('display', 'none');
				$('popSubMap').setStyle('display', 'none');

				$('overlay').setStyles({
					'width': window.getSize().x,
					'left': (window.getSize().x - 959) / 2 * -1,
					'height': window.getScrollHeight()
				});

				$('popMap').setStyles({
					'display': 'block',
					'top':135,
					'right': 252,
					'width': 25,
					'height': 25,
					'opacity': 1
				});

				if (oHomeFlash) {oHomeFlash.toElement().externalStop();}
				$$('select').setStyle('visibility', 'hidden');

				effectOverlay.set({
					'display': 'block',
					'opacity': 0
				});

				effectFadeMap.set({
					'opacity': 0
				});

				effectOverlay.start({
					'opacity': 0.9
				});

				effectZoomMap.start({
					'display': 'block',
					'top':	[135,70],
					'width':  [25,597],
					'height': [25,547]
				}).chain(function(){
					effectFadeMap.start({
						'display': 'block',
						'opacity': 1
					})
				});
				return false;
			});

			//attach Event to close the popMap and overlay
			var closeMap = function(){
				effectFadeMap.start({
					'opacity': 0
				}).chain(function(){
					effectZoomMap.set({
						'display': 'none',
						'opacity': 0
					});
				});

				effectOverlay.start({
					'opacity': 0
				}).chain(function() {
					$('overlay').setStyle('display', 'none');
					$('popMap').setStyle('display', 'none');
					$('popSubMap').setStyle('display', 'none');
					$$('select').setStyle('visibility', 'visible');
					if (oHomeFlash) {oHomeFlash.toElement().externalPlay();}
				});
				return false;
			}

			$('overlay').addEvent('click', closeMap);
			$('divMapClose').addEvent('click', closeMap);

			$$('#ulMapTop>li').each(function(currTheme) {
				currTheme.addEvent('click', function(){
					$$('#ulMapTop>li.liThemeSelected').each(function(currLi){currLi.className='liTheme'});
					this.className='liThemeSelected';
				});
			});
		}

		if ($('SearchAutonomyHeader1_autoSearch')) {
			var startText1 = 'Recherche visitEngland'; //Set from Tridion RFE Constant
			$('SearchAutonomyHeader1_autoSearch').set({
				'value': startText1,
				'events': {
					'click': function(){if (this.value == startText1) this.value = '';},
					'blur': function(){if (this.value == '') this.value = startText1;}
				}
			});
		}

		if ($('SearchHolidayAttraction1_txtLocation')) {
			var startText2 = '(lieu en anglais)'; //Set from Tridion RFE Constant
			$('SearchHolidayAttraction1_txtLocation').set({
				'value': startText2,
				'events': {
					'click': function(){if (this.value == startText2) this.value = '';},
					'blur': function(){if (this.value == '') this.value = startText2;}
				}
			});
		}

		if ($('SearchHolidaySmallAccomodationControl1_txtLocation')) {
			var startText3 = '(lieu en anglais)'; //Set from Tridion RFE Constant
			$('SearchHolidaySmallAccomodationControl1_txtLocation').set({
				'value': startText3,
				'events': {
					'click': function(){if (this.value == startText3) this.value = '';},
					'blur': function(){if (this.value == '') this.value = startText3;}
				}
			});
		}

		//Start Google Map if on page
		if ($('divGMap')){ GMapControlRender();}

	} else {
		//alert('Browser not supported - upgrade!\n\n' + navigator.appVersion);
	}

});

window.addEvent('unload', function() {
	if ($('divGMap')) { GUnload();}
});

/************** globalmoo.js *************************************************/