var cal_timeout = null;

function show_smallsearch_calendar(url, date) {
	url = url + '?form_fh_location=' + escape($('form_fh_location').value);
	if (date)
		url = url + '&date=' + date + '&showborder=false';
	
	new Ajax.Request(url, {
		method:'get',
		onSuccess: function(response) {
			if ($('calendar_data_container'))
				$('calendar_data_container').innerHTML=response.responseText;
			else
				$('calendar_container').innerHTML=response.responseText;
			
			if ($('calendar_container').style.display == 'none') {
				//Effect.BlindDown($("calendar_container"), {delay: .05, duration: 1});
				$("calendar_container").show();
			}
			if ($('div_open_calendar'))
				$('div_open_calendar').style.visibility = 'hidden';
		}
	});
}

function select_smallsearch_calendar(url1, url2) {
	if ($('next_prev').value == '')				// next/prev day NOT selected?
		$('form_fh_location').value = url1;
	else
		$('form_fh_location').value = url2;
}


function show_search_calendar(url, date, showborder) {
	if (!showborder)
		url = url + '&showborder=false';

	new Ajax.Request(url + '&date='+date+'&t='+new Date().getTime(), {
		method: 'get'
		, onSuccess: function (r) {
			if (showborder)
				$('calendar_container').innerHTML = r.responseText;
			else
				$('calendar_data_container').innerHTML = r.responseText;
			
		}
	});
	
}
function show_detail_calendar(url, fh_secondid, date, duration) {
	var param = '?fh_secondid='+fh_secondid+'&date='+date+'&duration='+duration;
	show_busy_form('detail_sidebar');
	
	new Ajax.Request(url + param, {
		method:'get',
		onSuccess: function(response) {
			$("calendar_data_container").innerHTML=response.responseText;
			remove_busy_form();
		}
	});
}
function show_calendar_page(url) {
	new Ajax.Request(url, {
		method:'get',
		onSuccess: function(response) {
		$('calendar_pages').innerHTML=response.responseText;
		}
	});
}


function setDateSelection(date) {
	close_calendar();
}

function close_calendar() {
	$('calendar_container').innerHTML = '';
//	$('calendar_container').hide();
	
	if ($('div_open_calendar'))
		$('div_open_calendar').style.visibility = '';
}

function input_empty_handler(o) {
	if (!o.box_emptied) {
		o.box_emptied=true;
		o.value='';
	}
}

function move_carousel_to_next(id) {
	var carousel = $(id);
	var images = $(id).getElementsByTagName('img');
	
	if (images.length <= 1)
		return;

	if ($(id).morphing)
		return;

	if (!carousel.current_pic)
		carousel.current_pic = 0;

	var imageWidth = images[0].clientWidth;

	// move images to the right
	for(var x=0; x < images.length; x++) {
		if (x == carousel.current_pic)		// don't touch current picture
			continue;

                images[x].style.left = images[carousel.current_pic].clientWidth + 'px';
	}

	var next_pic = carousel.current_pic + 1;
	if (next_pic >= images.length)
		next_pic = 0;

	//
	var newpos_curpic  = -images[carousel.current_pic].clientWidth;
	var newpos_nextpic = 0;

	$(images[carousel.current_pic]).morph('left: ' + newpos_curpic + 'px');
	$(images[next_pic]).morph('left: ' + newpos_nextpic + 'px', { afterFinish: function() { carousel.morphing=false; } });

	$(id).morphing = true;

	carousel.current_pic = next_pic;
}

function move_bookingcarousel_to_next(id) {
	move_carousel_to_next(id);
	
	var carousel = $(id);
	var images = $(id).getElementsByTagName('img');
	
	$('bookingscarousel_name').innerHTML = images[carousel.current_pic].alt;
}


// show_tab(<container that contains tabs>, <nieuw active name>)
function show_tab(tabContainerId, activeTab) {
	var tabContainer = $(tabContainerId);
	var tabs = tabContainer.getElementsByTagName('li');

	for(x=0; x < tabs.length; x++) {
		tabs[x].className = 'tab_inactive';
		tab_id = tabs[x].id;

		$(tab_id + '_container').style.display = 'none';
	}

	$(activeTab).className = 'tab_active';
	$(activeTab + '_container').style.display = 'block';


}

function jv_checkbox_Click(name) {
	var o = document.getElementById(name);
	var v = document.getElementById(name+'_value');
	var chk = document.getElementById('chk_'+name);

	if (o.value == '') {
		o.value = v.value;
		chk.src = '/images/checkbox_on.gif';
	} else {
		o.value = '';
		chk.src = '/images/checkbox_off.gif';
	}

	if (o.onchange)
		o.onchange();
}

// jv_checkbox_set - set checked, without firing event
function jv_checkbox_set(name) {
	var v = document.getElementById(name+'_value');
	var chk = $('chk_'+name);
	chk.src = '/images/checkbox_on.gif';
	$(name).value=v.value;
}

function jv_checkbox_unset(name) {
	var chk = $('chk_'+name);
	chk.src = '/images/checkbox_off.gif';
	$(name).value='';
}


// fix_checkbox_onreload - Firefox caches hidden input fields.. this sets the right image for the checkboxes
function fix_checkbox_onreload() {
	var checkbox_list = $$('.jv_checkbox');
	
	for(var x=0; x < checkbox_list.length; x++) {
		var name = checkbox_list[x].id.replace('chk_container_', '');
		
		if ($(name).value == '')
			$('chk_'+name).src = '/images/checkbox_off.gif';
		else
			$('chk_'+name).src = '/images/checkbox_on.gif';
	}
}
// todo: remove fix_checkbox_onreload if event below doesn't get set back ;)
//document.observe('dom:loaded', function() { fix_checkbox_onreload(); });




// put a transparent div over the parent and show the ajax-loader image
function show_busy_form(parent_id) {
	if ($('busy_form_div'))
		return;

	var p = $(parent_id);

	var c = document.createElement('div');
	c.id = 'busy_form_div';
	c.style.width = p.offsetWidth + 'px';
	c.style.height= p.offsetHeight+ 'px';
	c.style.backgroundColor = '#000000';
	c.style.position = 'absolute';
	c.style.float = 'left';
	c.style.opacity=0.4;
	c.style.filter = 'alpha(opacity=40)';
	c.style.zIndex = 99999;
	c.style.textAlign = 'center';
	

	var i = document.createElement('img');
	i.src = '/images/ajax_loader.gif';
	c.appendChild(i);

	hideIE6Elements($(parent_id));
	p.insertBefore(c, p.firstChild);

	i.style.marginTop = (p.offsetHeight/2 - 21) + 'px';		// vertical center image
}

// remove transparent div
function remove_busy_form() {
	if ($('busy_form_div')) {
		showIE6Elements($('busy_form_div').parentNode);
		$('busy_form_div').parentNode.removeChild($('busy_form_div'));
	}
}

// hide <select>-elements for IE6
function hideIE6Elements(parent_obj) {
	if (!Prototype.Browser.IE || !navigator.appVersion.match('MSIE 6.0'))
		return;

	var items = parent_obj.getElementsByTagName('select');

	for(x=0; x < items.length; x++) {
		items[x].style.visibility = 'hidden';
	}
}

// show <select>-elements for IE6
function showIE6Elements(parent_obj) {
	if (!Prototype.Browser.IE || !navigator.appVersion.match('MSIE 6.0'))
		return;

	var items = parent_obj.getElementsByTagName('select');

	for(x=0; x < items.length; x++) {
		items[x].style.visibility = 'visible';
	}
}


function page_popup(html) {
	if ($('page_popup')) {
		alert('page_popup already active');
		return;
	}
	
	var bg = document.createElement('DIV');
	bg.id = 'page_popup';
	bg.className = 'popup_background';
	
	var htmlContainer = document.createElement('DIV');
	htmlContainer.className = 'page_popup_content';
	htmlContainer.innerHTML = html;
	
	bg.appendChild(htmlContainer);
	
	document.body.insertBefore(htmlContainer, document.body.childNodes[0]);
	document.body.insertBefore(bg, htmlContainer);
}

function close_page_popup() {
	$('page_popup').parentNode.removeChild($('page_popup'));
	$('page_popup_content').parentNode.removeChild($('page_popup_content'));
}



function fasdayno(date) {
	var toks = date.split('-');
	toks[1] = toks[1]-1;	// JS counts from 0 to 11
	var d = new Date(toks[0], toks[1], toks[2]);

	var d_2009 = new Date(2009, 0, 1);

	var diff_sec = Math.ceil(d.getTime() - d_2009.getTime()) / 1000;
	var days = Math.round(diff_sec / (60*60*24));

	return days + 1;	// +1, include 1 jan 09
}


function show_vacationlist(toLeft) {
	if (toLeft) {
		$('popup_calendar').morph('width: 560px; margin-left: -285px;');
	} else {
		$('popup_calendar').morph('width: 560px;');
	}
}
function hide_vacationlist() {
	$('popup_calendar').morph('');
}

function navigateTo(url) {
	if (Prototype.Browser.IE && navigator.appVersion.match(/MSIE 6.0/)) {
		setTimeout('window.location = \''+url+'\';', 0);
	} else {
		window.location.href = url;
	}
}

function show_loading(id, height) {
	
	$(id).innerHTML = '';
	
	i = document.createElement('img');
	i.src = '/images/ajax_loader.gif';
	
	s = document.createElement('div');
	s.innerHTML = ' Loading..';
	
	d = document.createElement('div');
	d.className = 'loading_div';
	d.appendChild(i);
	d.appendChild(s);
	
	$(id).appendChild(d);
}

function show_more_prices(id) {
	$('prices_' + id).setStyle({height: 'auto'});
	$('prices_more_link_' + id).setStyle({display: 'none'});
	$('prices_more_' + id).setStyle({display: ''});
}
