$(document).ready(function() {
	function setFancyboxStyle(style)
	{
		if (style == 'orange') {
			$('#fancybox-outer').removeClass('orangestyle');
			$('#fancybox-outer').removeClass('imagestyle');
			$('.fancybox-orangestyle-logo').remove();
			
			$('#fancybox-outer').addClass('orangestyle');
			$('<img src="' + HOMEPATH + '/layout/images/popup-orangestyle-logo.jpg" class="fancybox-orangestyle-logo" alt="Hotels Scheveningen Logo" />').prependTo('#fancybox-outer');
		}
		else if (style == 'image') {
			$('#fancybox-outer').removeClass('orangestyle');
			$('#fancybox-outer').removeClass('imagestyle');
			$('.fancybox-orangestyle-logo').remove();
			
			$('#fancybox-outer').addClass('imagestyle');
		}
		else {
			$('#fancybox-outer').removeClass('orangestyle');
			$('#fancybox-outer').removeClass('imagestyle');
			$('.fancybox-orangestyle-logo').remove();
		}
	}
	
	$('div#list > div').addClass('whitebox');
	
	$('.whitebox').each(function(){
		$(this).wrapInner('<div class="whiteboxborder" />');
	});
	
	$('div#footer').each(function(){
		$(this).html('<div id="bigfootbg">' + $(this).html() + '</div>');
	});
	
	$('span.stars').each(function() {
		if ($(this).length > 0) { 
			var number = $(this).html().substring(1, 2);
			var html='';
			
			for (i = 0; i < number; i++) {
				html = html + '<span />';
			}
			
			$(this).html(html);
		}
	});
	
	$('div#hotelpics a').hover(function() {
		$('img#hoteldetailpic').attr('src', $(this).find('img').attr('src').replace('smallthumbnail', 'bigthumbnail'));
		$('img#hoteldetailpic').parent().attr('href', $(this).find('img').attr('src').replace('smallthumbnail', 'fullsize'));
	});
	
	/*$('div#adddata > a').click(function() {
		$('div#adddata > a, div#adddata > p,span#price').css('display', 'none');
		$('div.hotelrooms').css('display', 'block');
	});*/
	
	$('div.hotelrooms > a.opendetails').click(function() {
		if ($(this).hasClass('active')) {
			var i = $(this).attr('href').substr(1);
			$('div.hotelrooms').children('#' + i).hide();
			$(this).removeClass('active');
		}
		else {
			var i=$(this).attr('href').substr(1);
			$('div.hotelrooms').children('#' + i).show();
			$(this).addClass('active');
		}
		
		return false;
	});
	
	$('a.lightbox').fancybox({ titleShow: false, onStart: setFancyboxStyle });
	
	function fancybox_imageTitleFormat(title, currentArray, currentIndex, currentOpts)
	{
		var title = '<div class="fancybox-nav">';
		
		if (currentIndex > 0) {
			title += '<div class="fancybox-nav-previous"><a href="" title="Vorige foto" class="fancybox-nav-previous-link">Vorige foto</a></div>';
		}
		
		if (currentIndex < currentArray.length - 1) {
    		title += '<div class="fancybox-nav-next"><a href="" title="Volgende foto" class="fancybox-nav-next-link">Volgende foto</a></div>';
		}
		
		title += '</div>';
		
		return title;
	}
	
	$('a.lightbox-image').fancybox({ onStart: function() { setFancyboxStyle('image'); }, titlePosition: 'inside', titleFormat: fancybox_imageTitleFormat, onComplete: function() {
		$('.fancybox-nav-previous-link').click(function() {
			$.fancybox.prev();
			return false;
		});
		
		$('.fancybox-nav-next-link').click(function() {
			$.fancybox.next();
			return false;
		});
	}});
	
	$('a.lightbox-map').fancybox({ titleShow: false, scrolling: 'no', onStart: function() { setFancyboxStyle('image'); }, onComplete: function() {
		google.maps.event.trigger(map, 'resize');
		
		if (center) {
			map.setCenter(center);
		}
		else {
			map.fitBounds(bounds);
		}
	}});
	
	$('a.lightbox-clean').fancybox({ titleShow: false, onStart: function() { setFancyboxStyle('image'); } });
	
	$('a.lightbox-iframe').fancybox({ type: 'iframe', width: '90%', height: '90%' });
	
	$.datepicker.setDefaults($.datepicker.regional['nl']);
	
	var dates = $('.datepicker-range').datepicker({
		dateFormat: 'dd/mm/yy',
		minDate: 0,
		showOtherMonths: true,
		selectOtherMonths: true,
		onSelect: function(selected_date) {
			if ($(this).hasClass('datepicker-from')) {
				var otherdatepicker = $(this).parents('form').find('.datepicker-to');
				
				if ($(this).datepicker('getDate') > otherdatepicker.datepicker('getDate')) {
					var nextday = $(this).datepicker('getDate');
					nextday.setDate(nextday.getDate() + 1);
					otherdatepicker.datepicker('setDate', nextday);
				}
			}
			else {
				var otherdatepicker = $(this).parents('form').find('.datepicker-from');
				
				if ($(this).datepicker('getDate') < otherdatepicker.datepicker('getDate')) {
					var previousday = $(this).datepicker('getDate');
					previousday.setDate(previousday.getDate() - 1);
					otherdatepicker.datepicker('setDate', previousday);
				}
			}
		}
	});
	
	$('form').each(function() {
		from = $(this).find('.datepicker-from');
		to = $(this).find('.datepicker-to');
		
		if (!from.val() || !to.val()) {
			from.datepicker('setDate', 7);
			to.datepicker('setDate', 8)
			
			to.datepicker('option', 'minDate', 1);
		}
	});
	
	$('.results-sort').change(function() {
		if ($(this).val() == 0 || $(this).val() == 1) {
			if (document.URL.indexOf('sort=') == -1) {
				window.location = document.URL + (document.URL.indexOf('?') == -1 ? '?' : '&') + 'sort=' + $(this).val();
			}
			else {
				window.location = document.URL.replace(/sort=([0-9]*)/, 'sort=' + $(this).val());
			}
		}
		else {
			if (document.URL.indexOf('zoeken') == -1) {
				window.location = 'kaart/';
			}
			else {
				$('#popup-map-trigger').trigger('click');
			}
		}
	});
	
	$('.infopopup-button').click(function() {
		$.fancybox(
			$('#' + this.id + '-content').html(),
			{
				showTitle: false, onStart: function() { setFancyboxStyle('orange'); }
			}
		);
	});
	
	$('.cprice').click(function() {
		$(this).parent('a').next().find('a').trigger('click');
	});
	
	$('.room-link').click(function() {
		roomid = $(this).parent().attr('id');
		
		$.get(HOMEPATH + '/ajax.php', { do_action: 'registerroomclick', room: roomid }, function() {
			window.location = $('#' + roomid).find('a').attr('href');
		});
		
		return false;
	});
	
	$('.changehotelsearch').click(function() {
		$('.availabilitybox').parent().show();
		$('#warning').show();
		$('#price').show();
		
		return false;
	});
	
	if ($('.hotelrooms').children().length) {
		$(window).scrollTop($('#location').position().top - 34);
	}
	
});
