// JavaScript Document

var doHR = function() {
	$$('#contentcenter hr').each(function(el) {
		if (el.getAttribute('class')!='hr') {
			el.addClass('hr');
			new Element('div', { 'class': 'hr'}).injectBefore(el);
		}
	});
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function backButtonDestroyer() { 
	if (document.cookie['frey_referrer']) {
		alert(document.cookie['frey_referrer']);
		window.stop();
		window.location = document.cookie['frey_referrer'];
	}
}




var doServiceAccordion = function () {
	var accordion = new Accordion('h3.atStart', 'div.atStart', {
		display:0,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#000');
			toggler.addClass('activeToggle');
		},
	 
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#222');
			toggler.removeClass('activeToggle');
		}
	}, $('productServices'));
}
var doTogglerPretty = function () {
	$$('h3.toggler').each(function(element) {
		var fx = new Fx.Styles(element, {duration:200, wait:false});
		
		element.addEvent('mouseenter', function(){
			fx.start({
				'background-color': '#93C4D7',
				'color': '#fff'
			});
		});
		element.addEvent('mouseleave', function(){
			fx.start({
				'background-color': '#b9d8e5',
				'color': '#000'
			});
		});
	});
}
var searchTabs = function() {
	searchTabs1 = new mootabs('searchTabs', {height: '500px', width: '100%', useAjax: true, ajaxUrl: '/tabs.php', ajaxLoadingText: '<img src="/img/spinner.gif"> Loading...'});	
}
var scroll = new Fx.Scroll(window, {
	wait: false,
	duration: 1000,
	//offset: {'x': -400, 'y': 200},
	transition: Fx.Transitions.Quad.easeOut
});

var redoTabs = function() {
	$$('backlink').each(function(element) {
		each.element.addEvent('click',function(a) {
			var res = $('searchresults');
			a = new Event(a).stop();
			var uri = new Object();
			getURL(uri);
			tabs = '<div id="searchTabs"><ul class="mootabs_title"><li title="Accomm" class="'+uri+'">Accommodation</li><li title="Attraction" class="'+uri+'">Attractions</li><li title="Tour" class="'+uri+'">Tours</li></ul><div id="Accomm" class="mootabs_panel"> </div><div id="Attraction" class="mootabs_panel"> </div><div id="Tour" class="mootabs_panel"> </div><div class="panel_adjust resultspage_end"></div></div><br/><br/>';
			res.setHTML(tabs);
	   });
		searchTabs();
	});
}

var expTabs = function() {
	searchTabs1 = new mootabs('expTabs', {height: '500px', width: '100%', useAjax: true, ajaxUrl: '/exp.php', ajaxLoadingText: '<img src="/img/spinner.gif"> Loading...'});	
}

// Current Page Reference
// copyright Stephen Chapman, 1st Jan 2005
// you may copy this function but please keep the copyright notice with it
function getURL(uri) {
uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
uri.file = uri.page;
if (uri.ext != '') uri.file += '.' + uri.ext;
if (uri.file == '') uri.page = 'index';
uri.args = location.search.substr(1).split("?");
return uri;
}


                  
var doProductLinks = function () {
	$$('a.prodlink').each(function(element) {
		element.addEvent('click',function(a) {
			var res = $('searchresults');
			a = new Event(a).stop();

			var url = element.getAttribute('href');
			new Ajax(url, {
				method: 'post',
				update: res,
				evalScripts:true,
				timeout: 10000,
				onTimeout: function() {
					res.removeClass('ajax-loading');
					res.setHTML('&nbsp;&nbsp;&nbsp;&nbsp;Timeout communicating with the remote server, please try again');
				},
				onComplete: function() {
					res.removeClass('ajax-loading');
					doHR();
					doServiceAccordion();
					doTogglerPretty();
					Lightbox.init();
					window.UnLoadOff=backButtonDestroyer();
					showP = $('showproduct');
					if (showP) {
						//alert('showp');
						scroll.toElement(showP);	
					}
					redoTabs();
					doProductLinks();

				}
			}).request();	
		});
	});
}
var fullText = function () {	
	var res = $('searchresults');

	$('fulltext').addEvent('submit', function(e) {
		new Event(e).stop();

	 	res.empty().addClass('ajax-loading');
	 	this.send({
			update: res,
			onRequest: function() {
				res.empty().addClass('ajax-loading');
			},
			onComplete: function() {
				//scroll.toElement(res);
				res.removeClass('ajax-loading');
				getNextPage();		
				productHover();
				doProductLinks();
			}
		});
	});
}
var productSearch = function () {
	var res = $('searchresults');

	$('productsearch').addEvent('submit', function(e) {
		new Event(e).stop();
		//scroll.toElement(res);
		this.send({
			update: res,
			onRequest: function() {
				res.empty().addClass('ajax-loading');
			},
			onComplete: function() {
				//var cres = $('current-search-results');
				//scroll.toElement(cres);
				res.removeClass('ajax-loading');
				getNextPage();		
				productHover();
				doProductLinks();
			}
		});
	});
}
var getNextPage = function () {
	var res = $('searchresults');

	$$('a.nextpage').each(function(element) {
		element.addEvent('click',function(a) {
			a = new Event(a).stop();
			var url = element.getAttribute('href');
			new Ajax(url, {
				method: 'get',
				update: res,
				onComplete: function() {
					res.removeClass('ajax-loading');
					getNextPage();
					productHover();		
					doProductLinks();
				}
			}).request();	
		});
	});
}
	
var productHover = function() {

	$$('div.bg1').each(function(element) {
								
		var fx = new Fx.Styles(element, {duration:550, wait:false});
		
		element.addEvent('mouseenter', function(){
			fx.start({
				'background-color': '#d2ebf3'
			});
		});	
		
		element.addEvent('mouseleave', function(){
			fx.start({
				'background-color': '#b9d8e5'
			});
		});
	});

	$$('div.bg2').each(function(element) {
		var fx = new Fx.Styles(element, {duration:550, wait:false});
		
		element.addEvent('mouseenter', function(){
			fx.start({
				'background-color': '#d2ebf3'
			});
		});	
		
		element.addEvent('mouseleave', function(){
			fx.start({
				'background-color': '#99c7d8'
			});
		});
	});
}



window.addEvent("domready", function(){
	var res = $('searchresults');
	var body = document.getElementsByTagName('body');
	body[0].setAttribute("onunload", "backButtonDestroyer()");

	myShow = new Slideshow('header', {
		hu: 'img/header/',
		images: ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg'],
		duration: [2500,9000]
	});
	doHR();
	searchTabs();
	expTabs();
	productHover();
	//productSearch();
	//fullText();

	// SEARCH TYPE SWITCHER ----------------------------------------------------
	/*$('searchtype').addEvent('change', function(e) {
		e = new Event(e).stop();

		var url = "searchtype.php?searchtype=" + $('searchtype').getValue();

		var type=$('searchswitch').empty().addClass('ajax-loading');

		new Ajax(url, {
			method: 'post',
			update: type,
			timeout: 10000,
			onTimeout: function() {
				res.removeClass('ajax-loading');
				res.setHTML('&nbsp;&nbsp;&nbsp;&nbsp;Timeout communicating with the remote server, please try again');
			},
			onComplete: function() {
				type.removeClass('ajax-loading');
			}
		}).request();
	});*/



	/*var resetForm = function() {
		$('searchtype').options[0].selected = true;
		$$('input[type="checkbox"]').each(function(checkbox) {
			checkbox.setProperty('checked', false);
		});
	}
	$('reset').addEvent('click', function() {
		resetForm();
	});*/

	
	// RESET FORM on page refresh -------------------------------------
	//resetForm();
					Lightbox.init();
});

// http://www.blackmac.de/archives/44-Mootools-AJAX-timeout.html
Ajax = Ajax.extend({
    request: function(){
    if (this.options.timeout) {
        this.timeoutTimer=window.setTimeout(this.callTimeout.bindAsEventListener(this), this.options.timeout);
        this.addEvent('onComplete', this.removeTimer);
    }
    this.parent();
    },

    callTimeout: function () {
        this.transport.abort();
        this.onFailure();
        if (this.options.onTimeout) {
            this.options.onTimeout();
        }
    },

    removeTimer: function() {
        window.clearTimeout(this.timeoutTimer);
    }
});

function writetostatus(input){
    window.status=input
    return true
}