/**
 * menu functions
 */
window.onload = function() {
	oOnload.execEvents();
	positionImages();
	if(document.all){
		for(var i =0;i<document.images.length;i++){
			iepng24fix(document.images[i]);
		}
	}
	slideshow(1);
}

window.onresize = function() {
	positionImages();
}

self.iepng24fix = function(img){
	if(!document.all){return;}
	if(!/\.png$/.test(img.src)){return;}
  	img.style.width = img.width;
  	img.style.height = img.height;
	img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"',	sizingMethod='scale')";
	img.src="img/trans.gif";
}


var currentbgImage = '';

function changebgimage(img){

	var oElm = $('td_main');
	if(oElm){
		if(currentbgImage != img){
			var newImage = "url(" + img + ")";
			oElm.style.backgroundImage = newImage;
			currentbgImage = img;
		}
	}


}

function slideshow(nr) {
	if (!self.slideimages) return;
	var images = slideimages;
	var nrOfImg = images.length;
	var imgnr = ((nr + 1) > nrOfImg) ? 1 : (nr + 1);
	myImg.src = images[imgnr-1];

	Effect.Fade(slideimg,{
        duration : 5.0,
        to: 0.01,
        afterFinishInternal:function(effect){
        	slideimg.src = images[imgnr-1];
          	Effect.Appear(slideimg,{
          		duration: 5.0,
          		afterFinishInternal:slideshow.bind(self, imgnr, images)
          	})
        }
    })
}

function myToggle(id) {
	new Effect.toggle(id, 'blind');
	var controller = $('controller_'+id);
	controller.innerHTML = (controller.innerHTML.match(/\+./)) ? '[--]' : '[+]';
}

function myToggleSRC(id) {


	new Effect.toggle(id, 'blind');
	var controller = $('controller_'+id);
	controller.innerHTML = (controller.innerHTML.match(/\plus\.gif/)) ? '<img src="img/minus.gif">' : '<img src="img/plus.gif">';

}

function redirectkerst(){

}

function positionImages() {
	if (!$('td_main')) {return;}

	var pos = Position.cumulativeOffset($('td_main'));
	//$('man').setStyle({left: (pos[0] + 400) + 'px', top: (pos[1] - 105) + 'px', zIndex: '1'});
	// Kerstman
	if($('man')){
	$('man').setStyle({left: (pos[0] + 450) + 'px', top: (pos[1] - 10) + 'px', zIndex: '1', height: '388px'});
	$('man').setStyle({display: 'block'})
	}
	//$('wincode').setStyle({left: (pos[0] + 220) + 'px', top: (pos[1] + -42) + 'px', zIndex: '1'});
	//$('photo').setStyle({left: (pos[0] + 220) + 'px', top: (pos[1] + -42) + 'px', zIndex: '1'});
}

function hideMenu(classname) {
	var layers = document.getElementsByClassName(classname);
	// hide all layers
	for (var i=0; i<layers.length; i++) {
		layers[i].style.display = 'none';
	}
}

function showMenu(id) {

	var a_menu = id.match(/m(\d+)(_.+)/);
	var menuid = a_menu[1];
	var submenu = a_menu[2];
	var menu = 'm' + (parseInt(menuid) + 1);

	if (menu == 'm2') {hideMenu('m3');}
	hideMenu(menu);

	if ($(menu+submenu)) {$(menu+submenu).style.display = 'block';}
}

function submitform(formid,divid,url){

	var form = $(formid);

	htdform =  new Ajax.Updater(

		divid,  // DIV id must be declared before the method was called
        url,    // URL
        {
        	asynchronous:true,
        	parameters:Form.serialize(form),
        	method:'post',
          	onComplete:function(res){
          	},
          	onFailure: function(t) {
        			alert('Error ' + t.status + ' -- ' + t.statusText);
    		}
        } // Options
	);

	scrollTo(0,0);
}

function apply(formid){

	var form = $(formid);

	form.submit();

	scrollTo(0,0);
}



function showResponse(req){
	alert(req.responseText);
}

var lastClickedImgM1=null;
var lastClickedImgM2=null;
var lastClickedImgM3=null;
var lastClickedImgM4=null;

function imgClick(img){

	var menu = img.id.substring(0,2);

	if (menu == 'm1'){
		img.src = img.src.replace('type=1','type=2');
		if(lastClickedImgM1){
			lastClickedImgM1.src = lastClickedImgM1.src.replace('type=2','type=1');
		}
		lastClickedImgM1 = img;

	}

	if (menu == 'm2'){
		img.src = img.src.replace('type=3','type=5');
		if(lastClickedImgM2){
			lastClickedImgM2.src = lastClickedImgM2.src.replace('type=5','type=3');
		}
		lastClickedImgM2 = img;
	}

	if (menu == 'm3'){
		img.src = img.src.replace('type=4','type=6');

		if(lastClickedImgM3){
			lastClickedImgM3.src = lastClickedImgM3.src.replace('type=6','type=4');
		}

		lastClickedImgM3 = img;
	}

}

function menuonmouseout(img){

	//alert(img.id);
	//alert(lastClickedImg.id)
	var menu = img.id.substring(0,2);

	if (menu == 'm1'){
		if(lastClickedImgM1){
			if (img.id != lastClickedImgM1.id){
				img.src = img.src.replace('type=2','type=1');
			}
		}
		else{
			img.src = img.src.replace('type=2','type=1');
		}
	}

	if (menu == 'm2'){

		if(lastClickedImgM2){

			if (img.id != lastClickedImgM2.id){
				img.src = img.src.replace('type=5','type=3');
			}
		}
		else{
			img.src = img.src.replace('type=5','type=3');
		}
	}

	if (menu == 'm3'){
		if(lastClickedImgM3){
			if (img.id != lastClickedImgM3.id){
				img.src = img.src.replace('type=6','type=4');
			}
		}
		else{
			img.src = img.src.replace('type=6','type=4');
		}
	}
}

// START stuff for availability functions

var photowin = false;
var lcation = 'http://www.hoteliers.com/showPic.php?ID=';
var wins = 0;

function openPic(ID, width, height) {
 window.open(lcation + ID, 'mimyref' + wins, 'menubar=no, scrollbars=no, resizable=no, width=' + width + ', height=' + (height + 30) +',  status = no');
 wins++;
}

var curDay = 02;
var curMonth = 03;
var febdays = 29;

var startRange = Array();
startRange[0] = 2007;
startRange[1] = 03;
startRange[2] = 02;

var endRange = Array();
endRange[0] = 2008;
endRange[1] = 02;
endRange[2] = 29;

var startMonth = 03;
var endMonth = 12;


function dInM(mon) {
	if (mon == 2)
	{
		return febdays;
	}
	else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)
	{
		return 30;
	}
	return 31;
}

function getNDay() {

 theMonth = document.searchform.arriveMonth.options.selectedIndex;
 theDay = parseInt(document.searchform.arriveDate.options.selectedIndex);

 if (! theMonth == 0 && ! theDay == 0) {
  	dInMonth = dInM(theMonth);
 	 if (theDay > dInMonth) {
  		document.searchform.arriveDate.options.selectedIndex = theDay = dInMonth;
  	 }
  	newDay = theDay + 1;
 	newMonth = theMonth;
  	if (newDay > dInMonth) {
  		newDay = 1;
  		newMonth = theMonth + 1;
		if (newMonth > 12) {
			newMonth = 1;
		}
	}

}

}

function checkDDate() {


document.getElementById('nNights').value = calcNights()


 theMonth = document.searchform.departMonth.options.selectedIndex;
 theDay = parseInt(document.searchform.departDate.options.selectedIndex);
 dInMonth = dInM(theMonth);
 if (theDay > dInMonth) {
  document.searchform.departDate.options.selectedIndex = theDay = dInMonth;
 }
}

// END availability functions

function clickEgg(eggid, language) {

	var handlerFunc = function(t) {
		if (t.responseText == '5') {
			$('eggs').style.display = 'none';
			$('eggs_popup_last').style.display = 'block';
		}
		else {
			$('eggs').style.display = 'none';
			$('eggs_popup_notlast').style.display = 'block';
			$('eggs_popup_notlast').style.background = 'url(img/eastereggs/toppopupklein_'+t.responseText+'_'+language+'.jpg)';
		}
		//alert(t.responseText);
	}

	var errFunc = function(t) {
	    alert('Please try again...');
	}

	new Ajax.Request('eggs.php?eggid='+eggid, {asynchronous:true, onSuccess:handlerFunc, onFailure:errFunc});

}

function ScrollToCat(id) {

	var id = 'cat_'+id;

	var iReturnValue = 0;
	elementid = document.getElementById(id);
	while( elementid != null ){
		iReturnValue += elementid.offsetTop;
		elementid = elementid.offsetParent;
	}

	self.scrollTo(0, iReturnValue);
}

function clickLib(id, pageid, itemid, postfix, cat, level, vacancy) {

	if(!postfix){
		postfix = '';
	}

	if(postfix == 'mcwellness') {
		window.open('http://www.mc-solution.nl/','MCWellness'); 	
	}
	if(postfix == 'breakers') {
		window.open('http://www.breakersbeachhouse.nl/','Breakers'); 	
	}
	if(postfix == 'latour') {
		window.location='http://www.restaurantlatour.nl'; 	
	}
	if(postfix == 'rememberme') {
		window.location='http://www.ikwerkmetpassie.nl'; 	
	}
	
	// make sure the home button links to homepage
	if (pageid == '1') {
		document.location = '?';
		return;
	}
	//alert(postfix);
	// only update content1div if we have to show a page
	if (pageid > 0) {
		new Ajax.Updater('content1div', '?mod=HTD_pages&xhr_txt=1&htd_pagesID='+pageid+'&htd_menuID='+itemid + '&postfix=' +  postfix, {asynchronous:true,evalScripts:true});
	}
	else if (postfix != '') {
		new Ajax.Updater('content1div', '?mod=HTD_pages&xhr_txt=1&postfix='+postfix, {asynchronous:true,evalScripts:true});
	}
	else if (vacancy != '') {
		new Ajax.Updater('content1div', '?mod=HTD_pages&xhr_txt=1&htd_pagesID='+pageid+'&htd_menuID='+itemid + '&postfix=' +  postfix + '&vacancy=' + vacancy, {asynchronous:true,evalScripts:true});
	}

	// only update if we have a valid item
	if (itemid > 0) {
		new Ajax.Updater('content2', '?mod=HTD_right&xhr_right=1&htd_menuID='+itemid, {asynchronous:true,evalScripts:true});
	}

	// only update pics for level1
	//if ((level == 1) || (level == 4)) {
	// pics Change on a level change
	if(itemid > 0){
		var cat = document.location.search.match(/\?cat=(.*)\&/)[1];
		new Ajax.Updater('pics', '?mod=HTD_pics&xhr_pic=1&cat='+cat+'&htd_menuID='+itemid, {asynchronous:true,evalScripts:true});
	}

	// show the menu
	if (id) showMenu(id);
	// if page = 97 execute function to re-position images
}

function changeImagePos() {

	divs = new Array('pos_1','pos_2','pos_3','pos_4','pos_5');

	for(var x = 0; x < divs.length; x++) {

		var iReturnValue = 0;
		var elementid = $(divs[x]);

		if(elementid) {
			//iReturnValue += elementid.offsetTop;
			iReturnValue += findPosY(elementid);
			//elementid = elementid.offsetParent;

			var obj = $('pic_'+divs[x]);

			//var value = iReturnValue + 400 + 'px';
			var value = iReturnValue + 'px';

			obj.style.top = value;
		}
	}
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function updateBg(cat) {
	var nr = $(cat+ '_thumb').style.backgroundImage.match(/(\d+)(_thumb.jpg)/)[1];
	var pos = Position.cumulativeOffset($('piccontainer'));

	var img = $('defimage');
	img.src = 'img/home/'+cat+nr+'.jpg';
	img.setStyle({left:pos[0] + 'px', top:pos[1] + 'px'});
	new Effect.Appear('defimage');
}

function changeBg() {
	if (!document.getElementById('piccontainer')) {return;}

	// set class to cat button
	$(this.cat).className = 'menuitem level1 hover';

	var pos = Position.cumulativeOffset($('piccontainer'));
	var img = $('defimage');
	img.src = 'img/home/'+this.index+'.jpg';
	img.setStyle({left:pos[0] + 'px', top:pos[1] + 'px'});
	new Effect.Appear('defimage');
}

function removeHovers() {
	$('defimage').style.display='none';

	var cats = ['business', 'leisure', 'wineanddine', 'nowathtd'];
	for(var i=0; i<cats.length; i++) {
		$(cats[i]).className = 'menuitem level1';
	}
}

function createPics() {
	//alert('createPics');
	var imgwidth = 113;
	var lastwidth = 311;

	// these categories all need a randomly chosen image
	var cats = {1:{'cat':'business','pics':3},
				2:{'cat':'leisure','pics':3},
				3:{'cat':'wineanddine','pics':3},
				4:{'cat':'nowathtd','pics':3},
				5:{'cat':'aboutus','pics':3}};

	for(var i=1; i<6; i++) {
		var el = document.createElement('td');
		var random_pic_nr = (Math.floor(Math.random() * cats[i].pics) + 1);

		el.className = 'picdiv';
		el.style.width = (i==5) ? lastwidth + 'px' : imgwidth + 'px';
		if (i==5) {el.colSpan = '7';}
		el.cat = cats[i].cat;
		el.id = el.cat + '_thumb';
		el.index = el.cat + random_pic_nr;
		el.style.backgroundImage = 'url(img/home/' + el.index + '_thumb.jpg)';
		if (i<5) {el.onmouseover = changeBg;}

		$('piccontainer').appendChild(el);

		// add 'border' cells
		if (i < 5) {
			var td = document.createElement('td');
			td.style.backgroundColor = '#5e5947';
			$('piccontainer').appendChild(td);
		}
	}
}

function showipix(){
	var obj = $('ipix');
	if(obj){
		if(obj.style.display == "none"){
			obj.style.display = 'block';
			obj.style.zIndex = 10000;
			var frame = $('iframe_ipix');
			frame.src = '?mod=HTD_ipix';
		}
		else{
			obj.style.display = 'none';
		}

	}
}

function showslideshow(){
	var obj = $('slideshow');
	if(obj){
		if(obj.style.display == "none"){
			obj.style.display = 'block';
			obj.style.zIndex = 10000;
			var frame = $('iframe_slideshow');
			frame.src = '?mod=HTD_slide';
		}
		else{
			obj.style.display = 'none';
		}
	}
}

// function for corporate link to hoteliers.com
function doSendCorplogin() {

	if (document.hcorpForm.email != '' && document.hcorpForm.password != '') {
		document.hcorpForm.target = document.hcorpForm.hTarget.value;
		document.hcorpForm.action = 'http://www.hoteliers.com/cwlpEngine.php?ID=' + document.hcorpForm.hotelID.value;
		document.hcorpForm.submit();
	}
}

/******************************************
	URL OBJECT METHODS
/*****************************************/

/**
 * url object constructor.
 * @param url string typicaly "document.url".
 */
function __url(url){
	this.url=unescape(url);

	this.location = '';
	this.vars= new Array(1);
	this.vals= new Array(1);

	// methods
	this.getUrl			= url_getUrl;
	this.getVar			= url_getVar;
	this.getVarIndex	= url_getVarIndex;
	this.getVars		= url_getVars;
	this.parse			= url_parse;
	this.setVar			= url_setVar;

	// init
	this.parse();
}

/**
 * extract location and get vars and add them to the url object.
 */
function url_parse(){
	var a_url = this.url.split('?');

	this.location = a_url[0];
	if(a_url.length==1){ a_url[1]='';}

	var getVars = a_url[1];
	var gvars = getVars.split('&');
	var v;

	for(var i=0;i<gvars.length;i++){
		v=gvars[i].split('=');
		if(v.length==1){v[1]='';}
		this.setVar(v[0],v[1]);
	}
}

function url_setVar(name,value){
	var l=this.getVarIndex(name);
	if(!l){ l=this.vars.length; }

	this.vars[l]	= name;
	this.vals[l]	= value;
}

function url_getVarIndex(name){
	for(var i=1;i<this.vars.length;i++){
		if(this.vars[i]==name){
			return i;
		}
	}
	return false;
}

function url_getVar(name){
	var index;
	if(index=this.getVarIndex(name)){
		return this.vals[index];
	}
	return false;
}

function url_getVars(){
	var vars = new Array();
	for(var i=1;i<this.vars.length;i++){
		vars[vars.length]=escape(this.vars[i])+'='+escape(this.vals[i]);
	}
	return vars.join('&');
}

function url_getUrl(){
	return this.location+'?'+this.getVars();
}


/******************************************
	ONLOAD OBJECT METHODS
/*****************************************/

/**
 * onload object constructor.
 */
function __onload(){
	this.events			= new Array(0);
	this.addEvent		=onload_addEvent;
	this.execEvents	=onload_execEvents;
}

/**
 * add an event to be executed when the page is loaded.
 * @param code string code to be evaluated.
 */
function onload_addEvent(code){
	this.events[this.events.length]	= code;
	return true;
}

/**
 * execute onload events.
 * This method is typicaly called by the "body.onload()" event.
 * (onload="oOnload.execEvents();")
 */
function onload_execEvents(){
	for(var i=0;i<this.events.length;i++){
		eval(this.events[i]);
	}
	return true;
}


/******************************************
 GLOBAL FUNCTIONS
/*****************************************/

function displayToggle(el_id){

	el=document.getElementById(el_id);
	if(!el.style.display || el.style.display=='none'){
		el.style.display='visible';
	}
	else{
		el.style.display='none';
	}
}

function Show_Stuff(Click_Menu)
{
	var obj = document.getElementById(Click_Menu);
	if (obj.style.display == "none"){
		obj.style.display = "";
	} else {
		obj.style.display = "none";
	}
}


function loadId(id){
	oUrl.setVar('id',id);
	self.location.href=oUrl.getUrl();
}

function newAdminWin(url, name){
	if(!name){name='cms';}
	window.open(
		url,
		name,
		'height=500,width=500,locationbar=0,personalbar=0,menubar=0,toolbar=0,scrollbar=1,scrollbars=yes'
	);
}

function getDateFormat(qfdatename){

	var day = qfdatename+'[d]';
	var month = qfdatename+'[m]';
	var year = qfdatename+'[Y]';

	var oday = $(day);
	if(!oday){
		oday = document.getElementsByName(day);
		oday = oday[0];
	}

	var omonth = $(month);
	if(!omonth){
		omonth = document.getElementsByName(month);
		omonth = omonth[0];
	}

	var oyear = $(year);
	if (!oyear){
		oyear = document.getElementsByName(year);
		oyear = oyear[0];
	}

	var date = new Date();
	date.setMonth(omonth.value-1);
	date.setDate(oday.value);
	date.setYear(oyear.value);
	return date;
}



function calcNofNights(fieldarrivaldate,fielddeparturedate,updatefieldname) {

	var starttime = getDateFormat(fieldarrivaldate).getTime();
	var endtime = getDateFormat(fielddeparturedate).getTime();
	var diff = parseInt((endtime - starttime) / (1000 * 3600 * 24));

	var field = $(updatefieldname)
	if (!field){
		field = document.getElementsByName(updatefieldname);
		field = field[0];
	}

	field.value = (diff<0)?0:diff;
}


/******************************************
 CREATE OBJECT INSTANCES
/*****************************************/

var oUrl		= new __url(document.URL);
var oOnload = new __onload();

