// JavaScript Document (jQuery)

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};    

$(function() {

   setInterval( "slideSwitch()", 3000 );

   $('#slider1').bxSlider({
       displaySlideQty: 3,
       moveSlideQty: 1
     });

	//initialize language dropdowns
    $("ul#level-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function(){ $("ul", this).fadeOut("fast"); } 
    );
    if (document.all) {
        $("ul#level-one li").hoverClass ("sfHover");
    }

	//initialize date picker
	$('.date-pick').datePicker()
	$('#check-in').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#check-out').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#check-out').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#check-in').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);

	$('.more-photos').click(function() {
		window.open(this.href,'_blank','height=400,width=600,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		return false;
	});
	
	$('a#book-now').click(function() {		   
		var splitDate = $('input#check-in').val().split("/");
		var date_arrive = splitDate[2] + '-' + splitDate[0] + '-' + splitDate[1];
		var date_in = new Date(splitDate[2], splitDate[0] - 1, splitDate[1]);
		
		splitDate = $('input#check-out').val().split("/");
		var date_depart = splitDate[2] + '-' + splitDate[0] + '-' + splitDate[1];
		var date_out = new Date(splitDate[2], splitDate[0] - 1, splitDate[1]);
		
		var duration = (date_out.getTime() - date_in.getTime())/(1000*60*60*24);
		
		var propertyID = $('select#hotel-id').val();

      var bookurl = $('#bn-'+propertyID).attr('href');
		
		var numberRooms = $('select#number-rooms').val();
		var numberAdults = $('select#number-adults').val();
		var languageCode = $('input#language-code').val();

		window.open(bookurl+'?propertyID='+propertyID+'&departureDate='+date_depart+'&arrivalDate='+date_arrive+'&lengthOfStay='+duration+'&numberOfRooms='+numberRooms+'&numberOfAdults='+numberAdults+'&language='+languageCode);
		return false;
	});	

	$('.thickbox').click(function() {
		var markerId = parseInt($(this).attr('id').replace(/\D*/,''));
		for (var i=0;i<infoBoxes.length;i++) {
			if (markerId != i) {
				infoBoxes[i].close();				
			} else {
				map.setCenter(markers[markerId].position);
				infoBoxes[markerId].open(map,markers[markerId]);
				$('div.title').html(address[markerId][4]);
			}
		}
		return false;
	});
	
	$('img.close').click(tb_remove);

	codeAddress();
	
});
