/* 
 * Title: readshotellib.js
 * Date: 26-02-2008
 * Author: Salva Vilarrasa
 * 
 * This is a client library that uses Mootools as a javascript framework.
 * The main class is called readshotel and inside we define the methods for the readshotel object.
 * This library handles user clicks on main menus to retreive the desired data in AJAX.
 * It also uses swfobject.js to inject flash content into defined areas.
 * 
 */

/* For use locally unless otherwise specified during init */

var readshotel = {

	json_fileextension: ".php",
	
	swfPath: "swf/",
	/*myCal: "",*/

	flashFinished: function() {
		//alert ('fadeFinished');
		this.fadeAnim();
	},
	
	destroyAnim: function() {
		$('topDiv').remove();
	},
	
	fadeAnim: function() {
		var fadeOut = new Fx.Styles($('topDiv'), {duration: 5000});
		
 
		//Supported browsers in window object in Mootools:
		//window.ie	will be set to true if the current browser is internet explorer (any).
		//window.ie6	will be set to true if the current browser is internet explorer 6.
		//window.ie7	will be set to true if the current browser is internet explorer 7.
		//window.gecko	will be set to true if the current browser is Mozilla/Gecko.
		//window.webkit	will be set to true if the current browser is Safari/Konqueror.
		//window.webkit419	will be set to true if the current browser is Safari2 / webkit till version 419.
		//window.webkit420	will be set to true if the current browser is Safari3 (Webkit SVN Build) / webkit over version 419.
		//window.opera	is set to true by opera itself.
		
		//we detect browser for FF problem in reloading the flash intro
		//and we swap the flash with an image (this does the trick :-)
		if (window.gecko || window.webkit)
		{					
			// $('topDiv').innerHTML = "<img src='"+this.img.src+"' />";
			$('topDiv').innerHTML = "";
		}
				
		fadeOut.start({
			'opacity': [1,0]
		});
				
		fadeOut.addEvent('onComplete', function(){
			if (readshotel.destroyAnim() != null)
			{
				readshotel.destroyAnim();
			}
		});
	},
	

	startAnim: function() {
		if (!Cookie.get("readsIntro")){
			this.topDiv = new Element('div');
		
			var h = window.getHeight();
			var w = window.getWidth();
	
			if (window.innerHeight !== undefined)
			{
				// most browsers
				h = window.innerHeight;
				w = window.innerWidth;
			}
			else
			{
				// IE variants
				//var D = (document.body.clientWidth) ? document.body : document.documentElement;
				//h = D.clientHeight;
				//w = D.clientWidth;
			}
	
			this.topDiv.setProperty('id','topDiv');
			
			//this.topDiv.setOpacity('0.5');
			
			if(window.ie){
				//set styles for IE!!!!
				this.topDiv.setStyles({
					display: 'block',
					border: '0px solid black',
					height: h,
					width: w,
					top: '0px',
					left: 0,
					//margin: '0 auto',
					'z-index': '1000',
					//background: '#000000',
					background: '#ffffff url(images/pattern.jpg) repeat',
					position: 'absolute'
					
				});
			}
			else
			{			
				this.topDiv.setStyles({
					display: 'block',
					border: '0px solid black',
					height: h,
					width: w,
					top: '0px',
					left: 0,
					margin: '0 auto',
					'z-index': '1000',
					//background: '#000000',
					background: '#ffffff url(images/pattern.jpg) repeat',
					position: 'absolute'
					
				});
			}
			
			//this.topDiv.injectBefore('top');		
			this.topDiv.injectAfter('top');		
			
			this.insertFlash(readshotel.swfPath+"intro.swf", "flashAnim", w, h, "8", "#ffffff", this.topDiv.id);
			
			//this.img = new Image(764,995);
			//this.img.src = "../images/intro.jpg";
			
			//Set the cookie stating that intro flash has been already shown.
			Cookie.set('readsIntro', 'true');
		}
	},
	
	removePopUp: function() {
		if($('slideout')) {
		
			var mySlide = new Fx.Slide('slideout'); //Id of the div containing reservation form
			mySlide.slideOut();
			mySlide.addEvent('onComplete', function(){
				if($('topDiv'))
					$('topDiv').remove();		
			});
		}
		else {	
		
			if($('topDiv'))
				$('topDiv').remove();
		}
	},
	
	getDate: function() {
			
		d = $('date').value.split("/");
		if(d.length > 2){
			var myDate = new Date();
			myDate.setFullYear(d[2],(d[1]-1),d[0]);
			
			var times = new Array();
			
			switch(myDate.getDay()) {
				case 1:
					alert('Monday is closed');
					return;
				break;
				
				case 2: //Tuesday to Thursday
				case 3:
				case 4:
					times.push(new Option("12:00"),new Option("12:15"),new Option("12:30"),new Option("12:45"),new Option("13:00"),new Option("13:15"),
					new Option("13:30"),new Option("13:45"),new Option("14:00"),new Option("14:15"),new Option("14:30"),new Option(" "),
					new Option("17:30"),new Option("17:45"),new Option("18:00"),new Option("18:15"),new Option("18:30"),
					new Option("18:45"),new Option("19:00"),new Option("19:15"),new Option("19:30"),new Option("19:45"),new Option("20:00"),
					new Option("20:15"),new Option("20:30"),new Option("20:45"),new Option("21:00"),new Option("21:15"),new Option("21:30"),
					new Option("21:45"),new Option("22:00"),new Option("22:15"),new Option("22:30"),new Option("22:45"),new Option("23:00"));
				break;
				
				case 5: //Friday and Saturday
				case 6:
					times.push(new Option("12:00"),new Option("12:15"),new Option("12:30"),new Option("12:45"),new Option("13:00"),new Option("13:15"),
					new Option("13:30"),new Option("13:45"),new Option("14:00"),new Option("14:15"),new Option("14:30"),new Option(" "),
					new Option("17:30"),new Option("17:45"),new Option("18:00"),new Option("18:15"),new Option("18:30"),
					new Option("18:45"),new Option("19:00"),new Option("19:15"),new Option("19:30"),new Option("19:45"),new Option("20:00"),
					new Option("20:15"),new Option("20:30"),new Option("20:45"),new Option("21:00"),new Option("21:15"),new Option("21:30"),
					new Option("21:45"),new Option("22:00"),new Option("22:15"),new Option("22:30"),new Option("22:45"),new Option("23:00"),
					new Option("23:15"),new Option("23:30"));
				break;
				
				case 0: //Sunday
					times.push(new Option("12:00"),new Option("12:15"),new Option("12:30"),new Option("12:45"),new Option("13:00"),
					new Option("13:15"),new Option("13:30"),new Option("13:45"),new Option("14:00"),new Option("14:15"),new Option("14:30"),
					new Option("14:45"),new Option("15:00"),new Option("15:15"),new Option("15:30"),new Option("15:45"),
					new Option("16:00"),new Option("16:15"),new Option("16:30"),new Option("16:45"),new Option("17:00"),new Option("17:15"),
					new Option("17:30"),new Option("17:45"),new Option("18:00"),new Option("18:15"),new Option("18:30"),new Option("18:45"),
					new Option("19:00"),new Option("19:15"),new Option("19:30"),new Option("19:45"),new Option("20:00"),new Option("20:15"),
					new Option("20:30"),new Option("20:45"),new Option("21:00"),new Option("21:15"),new Option("21:30"),new Option("21:45"),
					new Option("22:00"));
				break;
			}
			
			var sTime = $('time');
			if(sTime) {
				sTime.innerHTML = "";
				for(i=0;i<times.length;i++) {
					sTime.options[i] = times[i];
				}	
			}
			
		}
		return false;
	},
		
	loadPopUp: function(conId) {
		var url = "AJAXItemContent.php";
 
		new Ajax(url, {
			postBody: 'cId='+conId,
			evalScripts: true,
			method: 'post',
			onComplete: readshotel.createPopUp
			/*update: $('topDiv')*/
		}).request();
		
		return false;
	},
	
	createPopUp: function(request) {
			
		this.topDiv = $('topDiv');
		if(!this.topDiv) {	
			this.topDiv = new Element('div');
		
			var h = window.getHeight();
			var w = window.getWidth();
	
			var centeredLeft = ((w-580) / 3).round();
			
			this.topDiv.setProperty('id','topDiv');
			this.topDiv.setStyles({
				display: 'block',
				border: '0px solid black',
				/*height: h,*/
				/*width: w,*/
				top: '0px',
				margin: '0 auto',
				//'text-align': 'center',
				'padding-left': centeredLeft,
				'padding-top': '30px',
				'z-index': '900',
				background: 'transparent',
				position: 'absolute'
				
			});
			
			/*
			formDiv = new Element('div');
			formDiv.setProperty('id','formDiv');
			formDiv.setStyles({
				display: 'block',
				
				left: centeredLeft

			});
			*/
		}
		
		this.topDiv.injectInside('container');		
		this.topDiv.setHTML(request);
		
		//Need to check if div will contain the form to do all that stuff!!!
		if(request.search('readshotelForm') != -1) {
			myCal = new Calendar({ date: 'd/m/Y' }, { classes: ['dashboard'], direction: 0.5, blocked: ['0 * 2008 1'] });
			myCal.addEvent('onHideComplete', readshotel.getDate);
			
			//Set current date
			var d = new Date();
			var day = d.getDate();
			var month = d.getMonth()+1;
			var year = d.getFullYear();
			
			if(month.toString().length == 1)
				month = "0"+month;
				
			var bDate = $('date-booked');
			bDate.value = day+'/'+month+'/'+year;	
			
			$('readshotelForm').addEvent('submit', function(e) {
				//Prevent the submit event
				new Event(e).stop();
			 
				/**
				 * This empties the log and shows the spinning indicator
				 */
				//var log = $('log_res').empty().addClass('ajax-loading');
			 
				var errors = false;
				
				//Validate required fields
				var userName = $('name');
				var tel = $('telephone');
				var mail = $('email');
							
				if(userName.value == null || userName.value == ""){
					userName.setStyle('background-color','#FFFFA0');
					errors = true;
				}
				
				if(tel.value == null || tel.value == ""){
					tel.setStyle('background-color','#FFFFA0');
					errors = true;
				}
				
				if(mail.value == "") {
					mail.setStyle('background-color','#FFFFA0');
					errors = true;
				}
				
				
				//Check for errors before sending the form.
				if(errors) {
					return;
				}	
				
				
				/**
				 * send takes care of encoding and returns the Ajax instance.
				 * onComplete removes the spinner from the log.
				 */
				this.send({
					/*update: log,*/
					update: $('slideout'),
					onComplete: function() {
						//log.removeClass('ajax-loading');
					}
				});
			});
		}
	},
	
	insertFlash: function(movie, id, width, height, flashVersion, bgcolor, divId) {
		var so = new SWFObject(movie, id, width, height, flashVersion, bgcolor);
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque"); // or "tranparent"
	
		if($(divId))
			so.write(divId);
	},
	
	//Used to insert in a <div id="content"> a flash player with vicente's SlideShow
	//The flash needs to receive a colon separated string of image relative urls ex: ("imges/img1.jpg:images/img2.jpg")
	//In its FlashVars param.
	createSlideShow: function(secId) {
		var url = "getImagesFromFolder" + readshotel.json_fileextension;
 		var method;
 		//if(secId!='home')
 		//	method = readshotel.showSmallSlideShow;
 		//else
 		if (secId)
 			method = readshotel.showSlideShow;

		new Ajax(url, {
			postBody: 'section='+secId,
			evalScripts: true,
			method: 'post',
			onComplete: method
		}).request();
		
		
	},
	
	//This methods receives a JSON object from a php script.
	//the request object is converted to a JavaScript Array using eval().
	showSlideShow: function(request) {
		var data = eval(request);
		var imagepaths = data.join(":");
		//console.log(imagepaths);
		
		//alert (imagepaths);
				
		//var so = new SWFObject(movie, id, width, height, flashVersion, bgcolor);
		var so = new SWFObject(readshotel.swfPath+"ReadsHotelSlideShow.swf", "readshotelHome", "719", "364", "9", "#000000");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");

		so.addVariable("imagePath", imagepaths);
		so.addVariable("time", "4000");
		so.addVariable("horizontalScaling", "Relative"); // Relative makes the horizontal scaling relative to the vertical scaling.
		so.addVariable("fadeMode", "Dual"); // Dual fade mode transitions completely each image on and off before switching images.
		so.addVariable("buttonsMode", "Never");
	
		so.useExpressInstall('expressinstall.swf');
		
		if($('topimage'))
			so.write('topimage');
		
	},
	
	showSmallSlideShow: function(request) {
		
		var data = eval(request);
		var imagepaths = data.join(":");
		//console.log(imagepaths);
		
		//var so = new SWFObject(movie, id, width, height, flashVersion, bgcolor);
		var so = new SWFObject(readshotel.swfPath+"ReadsHotelSlideShow.swf", "readshotelHome", "719", "364", "9", "#000000");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("wmode", "opaque");

		so.addVariable("imagePath", imagepaths);
		so.addVariable("time", "4000");
	
		so.useExpressInstall('expressinstall.swf');
	
		if($('flash')) {
			so.write('flash');

		}
	},
	
	loadContent: function(secId, parentId, element) {
		this.loadHeader(secId);
		this.loadMainContent(secId, parentId);
		this.loadQuoteImage(secId);
		
		
		//clean all elements containing id=current.
		$$('#current').each(function(el){
			el.removeProperty('id');
		});
		 
		//set the current selected element.
		$(element).setProperty('id','current');
		
		return false;
	},
		
	loadHeader: function(secId) {
		
		function setHeaders(request){
			
			var data = eval(request);
			
			if(!data || data.length == 0) {
				data = new Array(2);
				data[0] = "WELCOME to READS HOTEL";
				data[1] = "whatever";
			}
			
			header.setHeader(data[0]);
			header.setSubHeader(data[1]);
			//header.setImgPath('imgpath');
		}
		
		var header = $('readshotelHeader');
		if(header){
			
			var url = "AJAXHeaders" + readshotel.json_fileextension;
 
			new Ajax(url, {
				postBody: 'secId='+secId,
				evalScripts: true,
				method: 'post',
				onComplete: setHeaders
				
			}).request();
		}
	},
	
	loadMainContent: function(secId, parId) {
		var url = "AJAXMainContent.php";
 
		new Ajax(url, {
			postBody: 'secId='+secId+'&parentId='+parId,
			evalScripts: true,
			method: 'post',
			/*onComplete: readshotel.showcontent,*/
			update: $('content')
		}).request();
		
		
	},
	
	loadSubSectionContent: function(secId) {
		var url = "AJAXMainContent.php";
 
		new Ajax(url, {
			postBody: 'secId='+secId,
			evalScripts: true,
			method: 'post',
			/*onComplete: readshotel.showcontent,*/
			update: $('innercontent')
		}).request();
		
		return false;
	},
	
	loadQuoteImage: function(secId) {
		var image = new Element('img');
 		
 		if($('quotes')) {
 			$('quotes').innerHTML = "";
 		}
 		else {
 			return;
 		}
 			
 		if(secId == '2') {
 				image.setProperty('src','images/quote.jpg');
 				image.injectInside('quotes');
 		} else {
 			
 			function randomQuote(request) {
		
				var data = eval(request);
				
				if(!data || data.length == 0) {
					//Set ghandi (default) quote if no data returned
					image.setProperty('src','images/quote.jpg'); 
	 				image.injectInside('quotes');
				
				} else {
					//Get Random between 0 and length of the array.
					var tmpImg = data[Math.floor(Math.random()*data.length)];
					image.setProperty('src', tmpImg);
					image.injectInside('quotes');
				}
			}
 					
 			//Set the secId to 1 for quotes and call the php method via AJAX			
 			new Ajax('getImagesFromFolder'+readshotel.json_fileextension, { 
				postBody: 'section=1',	
 				evalScripts: true, 
 				method: 'post',	
 				onComplete: randomQuote
 			}).request();
		
		}
		
	},
	
	initToolTips: function() {
		var tips = new Tips($$('.toolTip'), {
			initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
		});
		return tips;
	},

	
	init: function(site_config) {
		//Initialization section.
		if (site_config == 'DEV')
		{
			/* For use locally: */
			readshotel.json_fileextension = ".php";
		}
		else
		{
			/* For use with BPWEB: */
			readshotel.json_fileextension = ".php5";
		}

		readshotel.startAnim();
		
		//this.createSlideShow('home');
		
		//this.initToolTips();
	
	}
	
};


