// JavaScript Document

function createExternalLinks(){
	var links = $$('a');
	links.each(function(el){
		if (el.rel == "external"){
			el.setProperty('target', '_blank');
		}
	});
}

function replaceIEObjects(){
	
	if (navigator.appName == "Microsoft Internet Explorer") {

		//Array of elements to be replaced
		var arrElements = new Array(3);
		arrElements[0] = "object";
		arrElements[1] = "embed";
		arrElements[2] = "applet";
	
		//Loop over element types
		
		for (n = 0; n < arrElements.length; n++) {
		
			//set object for brevity
			replaceObj = document.getElementsByTagName(arrElements[n]);
			
			//loop over element objects returned
			for (i = 0; i < replaceObj.length; i++ ) {
			
				//set parent object for brevity
				parentObj = replaceObj[i].parentNode;
				
				//grab the html inside of the element before removing it from the DOM
				newHTML = parentObj.innerHTML;
				
				//remove element from the DOM
				parentObj.removeChild(replaceObj[i]);
				
				//stick the element right back in, but as a new object
				parentObj.innerHTML = newHTML;
			
			}
		}
	}
	
}

function show_notice(){
	var cartNotice = $$('#cart_notice');
	if (cartNotice.length > 0){
		var myFx = new Fx.Style('cart_notice', 'top', {'duration': 1500});
		myFx.start(-150,0).chain(function(){
			var myFx = new Fx.Style('cart_notice', 'opacity', {'duration': 3000});
			myFx.start(1,0);
		});
	}
}

function getmap(){
	var map_exists = $$('#gmap');
	if (map_exists.length > 0){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}

function hotel_redirect(){
	$('hotel_selector').addEvent('change', function(e){
		var location = '';
		switch (this.value){
			case 'morvan-hotels':
				window.location = '/';
				break;
			case 'royal-hotel':
				window.location = 'http://www.royalhoteljersey.com/';
				break;
			default:
				if (this.value != ''){
					window.location = '/' + this.value + '/';
				}
				break;
		}
	});
}

function pullouts(){
	var pullouts = $$('.pullout');
	if (pullouts.length > 0){
		for (a=0; a<pullouts.length; a++){
			var content = pullouts[a].innerHTML;
			var content = '<div class="pulloutTop"></div><div class="pulloutContent">' + content + '</div><div class="pulloutBottom"></div>';
			pullouts[a].setHTML(content);
			pullouts[a].addClass('bg');
		}
	}
}

function popups(){
	var popups = $$('.popup');
	popups.each (function(el){
		var title = el.getProperty ('alt');
		var parent = el.getParent();
		var parentclone = parent.clone();
		//console.log(parent);
		if (title.indexOf(',') != -1){
			var lines = title.split(',');
			var len = lines.length;
			title = '';
			for (a=0; a<(len-1); a++){
				title = title + '<span>' + lines[a] + '</span>';
			}
			title += lines[len-1];
		}
		var current_classes = el.className;
		var container = new Element('div');
		container.addClass('popup_container');
		container.addClass(current_classes);
		var copy = el.clone();

		if(parent.getTag() == "a"){
			
			parentclone.empty();
			copy.injectInside(parentclone);
			parentclone.injectInside(container);
			styledel = parentclone;
		}else{
			copy.injectInside(container);
			styledel = copy;
		}


		var border = new Element('div');
		var p = new Element('p');
		p.setHTML(title);
		p.injectInside(border);
		p.setStyle('opacity', 0);
		border.addClass('popup_title');
		border.injectAfter(copy);
		el.replaceWith(container);


		var myEffects = p.effects({duration: 1000, transition:Fx.Transitions.Sine.easeInOut});
		myEffects.start({'margin-bottom': [-50, 0], 'opacity': [0, 0.8]});
		styledel.addEvent('mouseover', function(e){
			p.setStyle('opacity', 0);
		});
		styledel.addEvent('mouseout', function(e){
			p.setStyle('opacity', 0.8);
		});
	});
}

function setforms(){
	if ($('hotel_booking')){
		var hotel = $('hotel_select');
		hotel.addEvent('change',function(e){
			if (hotel.value){
				switch(hotel.value){
					case 'royal-hotel':
						alert ('Please wait whilst we direct you to our exclusive Royal Hotel online booking area...');
						pause($('hotel_booking'));
						window.location = 'https://secure.royalhoteljersey.com/(S(gsk4le55ysqexfrn5mzdvnqo))/reservnl.aspx?lang=en';
						break;
				}
			}
		});
		$('hotel_booking').addEvent('submit', function(e){
			var hotel = $('hotel_select').value;
			if (hotel==''){
				alert ('Please select a hotel.');
				var e = new Event(e);
				e.stop();
			}else{
				pause($('hotel_booking'));
			}
		});
	}
	if ($('flight_booking')){
		$('flight_booking').addEvent('submit', function(e){
			pause($('flight_booking'));
		});
	}
}
function pause(target){
	var width = target.getSize().size.x;
	var height = target.getSize().size.y;
	target.setStyle('opacity', '0.5');
	var overlay = new Element('div').addClass('pause').setStyles({'width': width, 'height': height});
	overlay.injectInside(target);
}

function hotel_booking(){
	if ($('hotel_booking') && $('hotel_select') && $('children')){
		var checks = $$('#children, #hotel_select');
		checks.each(function(el){
			switch (el.id){
				case 'children':
					var _on = 'blur';
					break;
				case 'hotel_select':
					var _on = 'change';
					break;
			}
			el.addEvent(_on, function(){
				var hotels = new Array (
					'monterey-hotel',
					'royal-hotel',
					'uplands-hotel',
					'fort-dauverge',
					'norfolk-lodge',
					'samares-coast'								
				);
				var children_allowed = new Array(
					1,
					100,
					0,
					1,
					1,
					0
				);
				var selected_hotel = $('hotel_select').value;
				var hotel_id = hotels.indexOf(selected_hotel);
				var children_required = $('children').value * 1;
				if (children_required > children_allowed[hotel_id]){
					switch (children_allowed[hotel_id]){
						case 0:
							var msg = 'Unfortunately, we do not allow the booking of children online for the Morvan Hotel\'s ' + selected_hotel + '.\r\nWe apologise for any inconvenience. Please call us should you wish to find out more...';
							break;
						default:
							var msg = 'Unfortunately, we only allow ' + children_allowed[hotel_id] + (children_allowed[hotel_id] == 1 ? ' child' : ' children') + ' per booking when booking online for the Morvan Hotel\'s ' + selected_hotel + '.\r\nWe apologise for any inconvenience. Please call us should you wish to find out more...';
							break;
					}
					$('children').value = children_allowed[hotel_id];
					alert (msg);
				}
			});
		});
	}
}

function tips(){
	var Tips1 = new Tips($$('.tip'));

}

function slideshows(){
	/* settings */
	var slideshows = $$('.slideshow');
	slideshows.each(function(container){
		var images = container.getElements('img');
		if (container.getProperty('rel')){
			var showDuration = container.getProperty('rel') * 1;
		}else{
			var showDuration = 5000;
		}
		var currentIndex = 0;
		var interval;
		/* opacity and fade */
		images.each(function(img,i){ 
			if(i > 0) {
				img.setStyle('opacity',0);
			}
		});
		/* worker */
		var show = function() {
			var myEffects = new Fx.Styles(images[currentIndex], {duration: 2000,transition: Fx.Transitions.linear});
			myEffects.start({
				'opacity': [1, 0]
			});
			var myEffects = new Fx.Styles(images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0], {duration: 2000,transition: Fx.Transitions.linear});
			myEffects.start({
				'opacity': [0, 1]
			});
			
			
			// images[currentIndex].fade('out');
			// images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
		};
		/* start once the page is finished loading */
		interval = show.periodical(showDuration);
		/*
		window.addEvent('load',function(){
			interval = show.periodical(showDuration);
		});
		*/
	});
}


window.addEvent ('domready', function(){
	createExternalLinks();
	replaceIEObjects();
	getmap();
	hotel_redirect();
	pullouts();
	popups();
	setforms();
	hotel_booking();
	tips();
	slideshows();
});

