// *****************************************************************************
window.addEvent('domready', function(){

	/**
	 * If you want to set options, set them here, otherwise initialize is
	 * called automatically when the first module is registered
	 */
	HistoryManager.initialize();


	//	// google analytics via mootools / domready.
	//	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	//	new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
	//		onload: function() {
	//			var pageTracker = _gat._getTracker("UA-364659-4");
	//			pageTracker._initData();
	//			pageTracker._trackPageview();
	//		}
	//	});
	

	//Slideshow Script
	var obj = {
		wait: 8000, 
		effect: 'fade', //fade|wipe|slide|random
		duration: 1500, 
		loop: true, 
		thumbnails: true,
		backgroundSlider: false,
		onClick: function(i){}
	}
	show = new SlideShow('slideshowContainer','slideshowThumbnail',obj);
	show.play();
	
	function startGallery() {
			var myGallery = new gallery($('myGallery'), {
			timed: false,
			useHistoryManager: false,
			showArrows: true,
			embedLinks: false
		});
		}
	
	//Function to Post Ajax Form
	// On form submit put
	//	onsubmit="javascript:sendForm(); return false;"
	//function sendForm() {
	//	$('Email_Request_Form').send({
	//								 update: $('elid'), 
	//								 onComplete: loading(elid)
	//								 }) //assuming that you want to update a div called with the elid id
	//}
	
	/**
	 * Simple example how to save Ajax
	 *
	 * This adds Ajax content loading to all ajaxified links.
	 * All allowed links are saved in hrefs, so we can validate
	 * the data from HistoryManager
	 */
	var ajaxs = $$('a.loadMe');

	// all possible hrefs are here, we use the index
	var hrefs = ajaxs.getProperty('href');
	var titles = ajaxs.getProperty('name');
	
	hrefs[9] = 'thanku.asp';
	titles[9] = 'Resrvation Request';
	//alert(hrefs);
	// no index now
	var currentIndex = -1;

	// Fade in Effect for ajax content
	var myMorph = new Fx.Style('ajaxContent', 'opacity', {duration: 500, wait: false});
	
	var req = new Ajax(hrefs[currentIndex], {
		method: 'get',
		update: $('ajaxContent'),
		data: { 'do' : '1' },
		onRequest: function() { 
								//alert('Request made. Please wait...');
								//$('ajaxContent').setStyle('visibility', 'hidden');
								//myMorph.start(1, 0);
								
								$('ajaxContent').setStyle('opacity', '0');
								$('ajaxContent').setStyle('display', 'none');
								$('ajaxLoader').setStyle('display', 'block');
								
								
								//$('ajaxContent').innerHTML="<div id='ajaxLoader'></div>";
								//alert("TEST");
							},
		onComplete: function(response) {
								//setTimeout(,1000);	
								$('ajaxLoader').setStyle('display', 'none');
								$('ajaxContent').setStyle('display', 'block');
								if (currentIndex==3) {
														startGallery();
														//Wait for gallery to load
														setTimeout(myMorph.start(0, 1),250);
								}
								else
								{
								myMorph.start(0, 1);
								}

								//alert('Request completed successfully.');
								//$('ajaxContent').setStyle('opacity', '1');
							},
		onSuccess: function(response){
								//a successful request
								document.title = titles[currentIndex] + " - ARIA **** Boutique Hotel Folegandros";
								//Google Analytics
								try {
									pageTracker._trackPageview(hrefs[currentIndex]);
								}
								catch (err) {
									//alert(err.message);
								}
								
								//alert(hrefs[currentIndex]);
							},

		onFailure: function(response){
								//a failed request
							},
		autoCancel: true
		});

	
	var reqHistory = HistoryManager.register(
		'page-index',
		[0], // default, page 0
		function(values) {
			//ajaxUpdate(currentIndex);
			ajaxUpdate(values[0]);
		},
		function(values) {
			return 'page-index(' + values[0] + ')';
			//return 'page-index=' + values[0] + '';
			//return 'page-index=' + values[0] + '';
			//return 'file=' + hrefs[currentIndex];
		},
		/page-index\((\d+)\)/ // the regexp to match "page-index(0)"
		// /page-index\=(\d+)/
		// /page-index\((\d+)\).*/
	);
	
	/**
	 * Loads the url (by index) if the index is valid and not the current page
	 */
	function ajaxUpdate(index) {
		var url = hrefs[index] || null;
		
		//alert('currentIndex=' + currentIndex + ' Index=' + index);
		if (!url || (currentIndex == index)) return;

		//TODO Den Allazei to epilegmeno Font
		// fancy state change with classes
		//alert(ajaxs[index]);

		//if (currentIndex != -1) ajaxs[currentIndex].removeClass('ajaxified-selected');
		//ajaxs[index].addClass('ajaxified-selected');

		currentIndex = index;

		// updating the history
		//TODO Fix History manager
		reqHistory.setValue(0, index);

		// request
		//alert(url);
		req.url = url;
		req.request();
	};

	ajaxs.each(function(el, i) {
		el.addEvent('click', function(e) {
			if (e) new Event(e).stop();
			ajaxUpdate(i);
		});
	});
	
	
	/**
	 * on start it start checking the hash
	 */
	HistoryManager.start();

});
// **********************************************************************
