document.domain = window.location.hostname;

function setOffer(arrival_date,departure_date,lengthofstay,redirect,hid) {
	$.ajax({
		url: '/classes/ajax.php',
		type: 'POST',
		data: 'cmd=setOffer&arrival_date='+arrival_date+'&departure_date='+departure_date+'&lengthofstay='+lengthofstay+'&redirect='+redirect+'&hid='+hid,

		success: function(result) {
			//show
			if(result!='') {
				$('#footerCntr').html(result);
			}
		}
	});

}

function setDaysToStay() {
	if (!isNaN(getDateDifference())) {
		$('#lengthofstay3').val(getDateDifference());
		$('#longer').hide(); 
		var _dt = $('#arrival_date').val().split('.'); 
		if (_dt.length < 3) { 
		} else {  
			$('#date_'+_dt[2]+'-'+_dt[1]+'-'+_dt[0]).trigger('click'); 
		} 
		$('#longer2').show();
	}
}

$(document).ready(function(){

	$('.fancyTitle').qtip();

	//house tabs
	$('a.tabsBoxLink').click(function() {
		showTabsBox($(this).attr('rel'));
		return false;
	});

	//showTabsBox function
	function showTabsBox(what) {
		$('.tabsBox .tabsBoxContent').removeClass('selected');
		$('.tabsBox #tabsBoxContent'+what).addClass('selected');

		$('.tabsBox .tabsBoxTab').removeClass('selected');
		$('.tabsBox #tabsBoxTab'+what).addClass('selected');

		//loadMap
		if(what==2) {
			$.ajax({
				url: '/classes/ajax.php',
				type: 'POST',
				data: 'cmd=loadMap&hid='+$('#hid').val(),
	
				success: function(result) {
					var result = jQuery.parseJSON(result);
					google_maps_load(result.lat, result.ltd, 8);
				}
			});
		}
	}
	
	//cycle thumbs
	if($('.thumbs > div').size() > 1) {
		$('.thumbs').cycle({
			fx: 'fade',
			speed: 1500,
			timeout: 0,
			next: '#nav_left', 
			prev: '#nav_right',
			after: function (curr, next, opts) {
				$('#paging').html((opts.currSlide+1) + "/" + opts.slideCount);
			}
		});
	}
	
	//cycle
	$('#panel, .home_offers').cycle({
		fx: 'fade',
		speed: 4000
	});

	//showImg
	var showImg = function() {
		//hide all images
		$('.left img').each(function() {
			var id=this.id.replace("image_", "");
			$(this).hide();
		});

		//show larger image
		var id=this.id.replace("thumb_", "");
		$('#image_'+id).show();
		return false;  
	};

	//show larger image
	$('.thumb img').live("click", showImg);
	$('.thumb img').live("hover", showImg);

	//hide all images
	$('.left img').each(function() {
		var id=this.id.replace("image_", "");
		if(id!=1) $(this).hide();
	});

	//colorbox
	$("a[rel='images']").colorbox({slideshow:true});

	//showPaging
	var showPaging = function() {
		//hide
		$('.paginaBox').hide();
		
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showPaging',

			success: function(result) {
				//show
				if(result!='') {
					$('.paginaBox').show();
					$('#paging').html(result);
				}
			}
		});
	};
	
	//searchHouses
	var searchHouses = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=searchHouses&'+$('form#searchItems').serialize(),

			success: function(result) {
				//alert(result);
				showKillSession();
				$('#searchresult').html(result);
				showPaging();
			}
		});
	};

	//getView
	var getView = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=getView',

			success: function(result) {
				//removeClass
				$('.linkBox li, .navigationBox li').each(function() {
					$(this).removeClass("selected");
				});
		
				//addClass
				$('.linkBox li a.'+result+', .navigationBox li a.'+result).parent().addClass("selected");
			}
		});
		return false;
	};

	//togglePrices
	var togglePrices = function(event) {
		if(event) event.preventDefault();
		//removeAttr
		$('span.blank a, span.noclick a').removeAttr("href");

		//toggle
		$('.tabsBox').toggle();
		$('.prijzenBox').toggle();
		$('.calendarBox').toggle();
		$('.step3Box').toggle();
		$('.togglePrices').toggle();
		$('.toggleDescription').toggle();

		//showPrices
		showPrices();

		//scrollTo
		$.scrollTo('.prijzenBox', 800);

		return false;
	};
	
	//pagination
	$('.paginaBox li a').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setPage&page='+$(this).attr('class'),

			success: function(result) {
				searchHouses();
				//scrollTo
				$.scrollTo('#rightCntr', 800);
			}
		});
		//return
		return false;
	});

	//resetPage
	var resetPage = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setPage&page=page1',

			success: function(result) {
				searchHouses();
			}
		});
	};

	//togglePrices
	$('.togglePrices, .toggleDescription, .showPrices').live("click", togglePrices);
	
	if($('.calendars').length!=0) getView();

	//cycle thumbs
	if($('.cycle_calendar > div').size() > 1) {
		$('.calendars').cycle({
			fx: 'scrollHorz',
			speed: 1500,
			timeout: 0,
			next: '.next', 
			prev: '.prev'
		});
	}

	//showPrices
	var showPrices = function() {
		var los;
		if ($('#lengthofstay3').val() > 0) {
			los = $('#lengthofstay3').val();
		} else if ($('#lengthofstay2').val() > 0) {
			los = $('#lengthofstay2').val();
		} else if ($('#lengthofstay').val() > 0) {
			los = $('#lengthofstay').val();
		}
		if (!los) {
			los=7;
		}
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showPrices&arrivaldate='+this.id+'&lengthofstay='+los+'&hid='+$('#hid').val(),

			success: function(result) {
				showKillSession();
				$('.step3Box').html(result);
			}
		});
		//console.log('showPrices', this.id);
		return false;
	};

	//show price for selected date
	$('.calendar span:not(.noclick) a').live("click", showPrices);
	
	//selectPeriod
	$('.selectPeriod').live("click", function() {
		var los;
		if ($('#lengthofstay3').val() > 0) {
			los = $('#lengthofstay3').val();
		} else if ($('#lengthofstay2').val() > 0) {
			los = $('#lengthofstay2').val();
		} else if ($('#lengthofstay').val() > 0) {
			los = $('#lengthofstay').val();
		}

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setTP&arrival_date='+this.id+'&lengthofstay='+los+'&hid='+$('#hid').val(),

			success: function(result) {
			}
		});

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setTP&arrival_date='+this.id+'&lengthofstay='+los+'&hid='+$('#hid').val(),

			success: function(result) {
				showKillSession();
				var result = jQuery.parseJSON(result);
				$('#price_text').html(result.price);
				$('#travel_period').html(result.setTPtext);
				$('#myOffer').html(result.offerText);
				$('#showBook').show();
				$('#carrental_from').val(result.from);
				$('#carrental_to').val(result.to);
			}
		});
		return false;
	});

	//showPrice
	var showPrice = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showPrice&hid='+$('#hid').val(),

			success: function(result) {
				showKillSession();
				$('#showPrice').html(result);
			}
		});
		return false;
	};

	//bookingOptions
	var bookingOptions = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setBookingOptions&'+$('form#bookingOptions').serialize(),

			success: function(result) {
				if($('.step3Box').length!=0) {
					showPrice();
					showPrices();
				}
				if($('#book_content').length!=0) showBook();
			}
		});
		return true;
	};
	
	$('.bookingOptions').live("click", bookingOptions);
	$('#addCoupon').live("click", bookingOptions);
	$('.counter').live("change", bookingOptions);

	//change lengthofstay
	$('.prijzenBox #lengthofstay').live("change", function() {
		$('.step3Box').html('');
	});

	//showDetails
	$('a#showDetails').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showDetails&hid='+$('#hid').val(),

			success: function(result) {
				$('#simplePopup').html(result);
			}
		});
		
		//simplePopup
		$.colorbox({width:"520px", height:"345px", inline:true, href:"#simplePopup"});

		return false;
	});

	//search init
	if($('#searchresult').length!=0) {
		getView();
		searchHouses();

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=checkNOP',

			success: function(result) {
				if(result==1) {
					//checkNOP
					$.colorbox({width:"520px", height:"345px", inline:true, href:"#pTravelers"});
				}
			}
		});
	}
	//search from menu
	$('#searchItems input').live("click", resetPage);
	$('#searchItems input, #searchItems select').live("change", resetPage);

	//setSearchItems
	var setSearchItems = function() { // alert($('form#searchHome').serialize());
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setSearchItems&'+$('form#searchHome').serialize(),

			success: function(result) {
				$('body').append('<div class="refresh">'+result+'</div>');
			}
		});
	};

	//search from home
	$('#setSearchItems').live("click", setSearchItems);

	//set result view type
	$('.linkBox li a, .navigationBox li a').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setView&view='+$(this).attr('class'),

			success: function(result) {
				getView();
				searchHouses();
			}
		});
		//return
		return ($(this).attr('href')=='#' ? false : true);
	});

	//showAll
	var showAll = function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showAll',

			success: function(result) {
				$('body').append('<div class="refresh">'+result+'</div>');
			}
		});
		return false;
	};

	//showAll
	$('.showAll').live("click", showAll);
	
	//checkNOP
	$('.checkNOP').live("click", function() {
		//checkNOP
		$.colorbox({width:"520px", height:"345px", inline:true, href:"#pTravelers"});
		return false;
	});

	//checkCR
	$('.checkCR').live("click", function() {
		//checkCR
		if ($('#carCategory').val()=='') {
			$('#carCategory').val('3');
			$.post('/classes/ajax.php', {
				cmd: 'getCarInfo',
				'cat_id': 3,
				'carrental_from': $('#carrental_from').val(),
				'carrental_to': $('#carrental_to').val(),
				'die':'yes'
			}, function(data){
				showKillSession();
				// $('#carChoice').html(data).attr('disabled', false);
				$('#car_rental_popup_price').html(data.price);
				$('#cattext').html(data.cattext);
				$('#catdescription').html(data.catdescription);
				$('#carrental_image').attr("src", "/images/cars/"+data.image);
				$('#carrental_info').html(data.description);
				$('#carrental_image').fadeIn();
			}, "json");
		}
		$.colorbox({width:"500px", height:"600px", inline:true, href:"#pCarRental"});
		return false;
	});
	
	//$.colorbox({width:"500px", height:"600px", inline:true, href:"#pCarRental"});

	// Car rental popup
	$('#carPickup').live('change',function(){
		var id = $(this).val();
		if(id) {
			$.post('/classes/ajax.php', {
				cmd: 'getCarsChoices',
				'id': id
			}, function(data){
				$('#carCategory').html(data).attr('disabled', false);
				$('#carCategory').trigger('change');
			});
		} else {
			$('#carCategory').attr('disabled', true).val(0);
		}
	}).trigger('change');
	
	
	$('#carCategory').live('change', function(){
		var cid = $(this).val();
		if(cid > 0) {
			$.post('/classes/ajax.php', {
				cmd: 'getCarInfo',
				'cat_id': cid
			}, function(data){
				showKillSession();
				// $('#carChoice').html(data).attr('disabled', false);
				$('#cattext').html(data.cattext);
				$('#catdescription').html(data.catdescription);
				$('#cartext').html(data.cartext);
				$('#carrental_image').attr("src", "/images/cars/"+data.image);
				$('#carrental_info').html(data.description);
				$('#carrental_image').fadeIn();
			}, "json");
		}
	});

	
	$('#carCategory, #carrental_from, #carrental_to, #carPickup, #carDropoff, #children1, #children2, #children3').live("change", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setCarRental&'+$('form#pCarRental').serialize(),

			success: function(result) {
				showKillSession();
				henk = result.split('^^^');
				if (henk[1] == '&euro; 0,00') {
					$('#car_rental_popup_price').html('');
				} else {
					$('#car_rental_popup_price').html(henk[1]);
				}
				
			}
		});
	});
	
	//checkTP
	$(".checkTP").colorbox({width:"520px", height:"345px", inline:true, href:"#pTravelPeriod"});

	//setNOP
	$('#setNOP').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setNOP&'+$('form#pTravelers').serialize(),

			success: function(result) {
				$('#group_text').html(result);
				parent.$.colorbox.close();
				showKillSession();
				if($('#searchresult').length!=0) searchHouses();
				if($('.step3Box').length!=0) showPrice();
				if($('#book_content').length!=0) showBook();
			}
		});
	});

	//closepopup
	$('.closepopup').live("click", function() {
		parent.$.colorbox.close();
		return false;
	});
	
	//setTP
	$('#setTP').live("click", function() {
		
		var _arrival = $('[name=arrival_date]').val();

		if(_arrival == '' || _arrival == 'undefined') {
			//alert('NO DATE SELECTED');
			return;
		}

		showKillSession();

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setSearchItems&'+$('form#pTravelPeriod').serialize(),

			success: function(result) {
				$('#travel_period').html(result);
				parent.$.colorbox.close();
				searchHouses();
			}
		});
	});
	
	//setCR
	
	$('#setCRnew').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setCarRental&'+$('form#pCarRental').serialize(),

			success: function(result) {
				searchHouses();
				henk = result.split('^^^');
				$('#overviewCarText').html(henk[0]);
				$('#car_rental_text').html(henk[0]);
				$('#overviewCarPrice').html(henk[1]);

				showPrice();
				parent.$.colorbox.close();
				//if($('.step3Box').length!=0)
				//if($('#book_content').length!=0) showBook();
			}
		});
	});
	$('#setCRnew').live("click", bookingOptions);

	//reset session
	$('.headerCntr .img, #killSession, .killSession').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=refresh',

			success: function(result) {
				$('body').append('<div class="refresh">'+result+'</div>');
			}
		});
		return false;
	});

	//arrival_date
	$('#arrival_date, #departure_date, #popup_arrival_date, #carrental_from, #carrental_to, .datepicker').datepicker({
		minDate: 0,
		maxDate: '+1y',
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd.mm.yy'
	});

	//click on cal
	$('img.cal_arrival_date, img.cal_departure_date, img.cal_carrental_from, img.cal_carrental_to').live("click", function() {
		var cal=$(this).attr('class').replace("cal_", "");
		$('#'+cal).focus();
		showKillSession();
		return false;
	});

	//show "longer"
	$('select#lengthofstay, select#lengthofstay2').live("change", function() {
		var curval=$(this).val();

		if(curval==0) {
			$('p#lengthofstay').hide();
			$('#longer').show();
			$('#popup_arrival_date').focus();
			showKillSession();
		}
	});

	//set theme
	$('.theme').live("click", function() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=setView&theme='+this.id,

			success: function(result) {
				var url = location.href;
				var url_parts = url.split('?');
				var main_url = url_parts[0];
				document.location.href=main_url;
			}
		});
		return false;
	});

	//showBook
	var showBook = function(nextstep) {
		$('#tabStep1').removeClass('selected');
		$('#tabStep2').removeClass('selected');
		$('#tabStep3').removeClass('selected');
		if (!nextstep) nextstep = 1;
		$('#tabStep'+nextstep).addClass('selected');

		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=showBook&nextstep='+nextstep+'&'+$('form#bookingOptions').serialize(),

			success: function(result) {
				showKillSession();
				var result = jQuery.parseJSON(result);
				//content
				$('#book_content').html(result.content);
				//console.log('cmd=showBook', $($(result.content)));
				//error
				if(result.error) {
					if (result.error == 'vlucht weg') {
						var accept = confirm(txt1);
						if (!accept) {
							removeComfort();
						} else {
							acceptComfort();
						}
					} else if (result.error == 'vlucht heen') {
						var accept = confirm(txt2);
						if (!accept) {
							removeComfort();
						} else {
							acceptComfort();
						}
					} else if (result.error == 'extra dag auto') {
						alert(txt3);
						acceptCardayextra();
					} else {
						$('#tabStep1').removeClass('selected');
						$('#tabStep2').removeClass('selected');
						$('#tabStep3').removeClass('selected');
						$('#tabStep'+(nextstep-1)).addClass('selected');
						alert(result.error);
					}
				}
				$('.fancyTitle').qtip();
			}
		});
		return false;
	};

	//search init
	if($('#book_content').length!=0) showBook();
	
	$('.nextbookstep').live("click", function() {
		showBook($('#step').val());
	});
	$('.prevbookstep').live("click", function() {
		showBook($('#step').val()-2);
	});

	function showKillSession() {
		var $button = $('#killSession');
		//console.log($button.filter(':animated').length, 'test');
		if($button.filter(':animated').length == 0) {
			$button.fadeIn('slow');
		}
	}
	function removeComfort() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=removeComfort',

			success: function(result) {
				alert('Je comfortpakket is geannuleerd.');
				showBook($('#step').val());
			}
		});
	}

	function acceptComfort() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=acceptComfort',

			success: function(result) {
				showBook($('#step').val());
			}
		});
	}

	function acceptCardayextra() {
		$.ajax({
			url: '/classes/ajax.php',
			type: 'POST',
			data: 'cmd=acceptCardayextra',

			success: function(result) {
				showBook($('#step').val());
			}
		});
	}
});


function parseDate(str) {
    var mdy = str.split('.')
    return new Date(mdy[2], mdy[1]-1, mdy[0]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24)
}
function getDateDifference() {
	//console.log(daydiff(parseDate($('#arrival_date').val()), parseDate($('#departure_date').val())));
	//alert(parseDate($('#arrival_date').val()));
	//alert(parseDate($('#departure_date').val()));
	return Math.round(daydiff(parseDate($('#arrival_date').val()), parseDate($('#departure_date').val())));
}

function gotoUrl(url) {
	document.location.href=url;
}
