// JavaScript Document (jQuery)

Cufon.replace('h1');

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};    

$(function() {

	//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 splitProperty = $('select#hotel-id').val().split("-");
		var propertyID = splitProperty[0];
		var propertyBrand = splitProperty[1];
		var omnitureTag = splitProperty[2];
		var propertyBrandCode = splitProperty[3];
		
		var numberRooms = $('select#number-rooms').val();
		var numberAdults = $('select#number-adults').val();
		var languageCode = $('input#language-code').val();
		
		if (propertyBrand == 'corporate') {
			window.open('http://www.starwoodhotels.com/corporate/search/results/standard.html?skinCode=null&city=Paris&stateProvince=&country=FR&arrivalDate='+date_arrive+'&departureDate='+date_depart+'&numberOfRooms='+numberRooms+'&numberOfAdults='+numberAdults+'&hotelName=&localeoverwrite='+'&ES='+omnitureTag,'_blank');		
		} else {
			window.open('http://www.starwoodhotels.com/'+propertyBrand+'/search/ratelist.html?propertyID='+propertyID+'&requestedAffiliationCode='+propertyBrandCode+'&roomOccupancyTotal=1&departureDate='+date_depart+'&arrivalDate='+date_arrive+'&lengthOfStay='+duration+'&numberOfRooms='+numberRooms+'&numberOfAdults='+numberAdults+'&language='+languageCode+'&ES='+omnitureTag,'_blank');
		}
		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);

	$('h1').show();  /* ensure that the headline is not displayed until the cufon replacement is made */

	initializeGoogleMap();
	codeAddress();
	
});