// JavaScript Document
var todayDate;
var todayDate2;
var sideQQ      = false;
var minDiff     = 3; //min nights to stay
var defaultDiff = 7; //number of nights w suggest
var locale      = '';
var localeb     = "";

var daysRegion  = [];
var monthRegion = [];


var geo = {
	
	countryResort : {
		AG: new Array("SAT",""),
        BS: new Array("SRB","SEB",""),
        JM: new Array("SGO","INN","SMB","SNG","SRC","SWH","BRP",""),
        LC: new Array("SGL","SHC","SLU",""),
		ALL: "ALL"
	},
	
	resorts : {
		SAT: "Sandals Antigua",
		SRB: "Sandals Royal Bahamian",
		SGO: "Sandals Grande Ocho Rios",
		BRP: "Sandals Royal Plantation",
		INN: "Sandals Carlyle",
		SMB: "Sandals Montego Bay",
		SNG: "Sandals Negril",
		SRC: "Sandals Royal Caribbean",
		SWH: "Sandals Whitehouse",
		SGL: "Sandals Grande St. Lucian",
		SHC: "Sandals Halcyon Beach",
		SLU: "Sandals La Toc",
		SEB: "Sandals Emerald Bay"	
	}
}


function toggleResort(vcountry)
{	
	//alert(vcountry);
	var resort = $('#rst_code');
	var newOption = '';
	resort.html('');
	
	if(vcountry != "ALL")
	{
		var optionArray = geo.countryResort[vcountry];
		// looping for all the resort
		for(var index = 0; index < optionArray.length - 1; index++)
		{
			resCode = optionArray[index];	
			newOption += '<option value="'+resCode+'" >' +  geo.resorts[resCode] + '</option>';
		}
		
	}
	else
	{
		index = 0;
		
		for(resCode in geo.resorts)
		{
			newOption += '<option value"'+resCode+'" >'	+ geo.resorts[resCode] + '</option>';
			index++;
		}	
	}
	
	
	resort.html(newOption);
	
	var resortdd = $('#rst_code').val();
	//alert(resortdd);
	
		if(resortdd == "ALL")
		{
			$("#gsDestination").val("");
		};
		if(resortdd == "SRC")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Royal Caribbean");
		};
		if(resortdd == "SMB")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Montego Bay");
		};
		if(resortdd == "SGO")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Grande riviera");
		};
		if(resortdd == "BRP")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Royal Plantation");
		};
		if(resortdd == "SNG")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Negril");
		};
		if(resortdd == "SWH")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Whitehouse");
		};
		if(resortdd == "INN")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Carlyle");
		};
		if(resortdd == "SLU")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals La Toc");
		};
		if(resortdd == "SHC")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals Halcyon");
		};
		if(resortdd == "SGL")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals Grande St. Lucian");
		};
		if(resortdd == "SRB")
		{
			$("#gsDestination").val("NAS");
			$("#gsprefhotelcode").val("Sandals Royal Bahamian");
		};
		if(resortdd == "SEB")
		{
			$("#gsDestination").val("GGT");
			$("#gsprefhotelcode").val("Sandals Emerald Bay");
		};
		if(resortdd == "SAT")
		{
			$("#gsDestination").val("ANU");
			$("#gsprefhotelcode").val("Sandals Antigua");
		};
	
			
}

function processQQ() {
	
	var country       = $("#resortCountry").val();
	var resort        = $("#rst_code").val();
	var arrival       = $("#startdate").val();
	var departure     = $("#departureDate").val();
	var DepartureDate = $("#startdate").val();
	var ReturningDate = $("#departureDate").val();
	
	
	//Changing Dates US format for the obe.sandals.com
	var arrivalb = arrival.split("/");
	var departureb = departure.split("/");
		
	var aa =  arrivalb[1]+"/"+arrivalb[0]+"/"+arrivalb[2];
	var dd = departureb[1]+"/"+departureb[0]+"/"+departureb[2];
	
	
	
	$("#startdateb").val(DepartureDate);
	$("#departureDateb").val(ReturningDate);
	
	if(resort != "" && country != "ALL"){
			
		if(localeb == 'fr')
		{
			$('#qq').attr("action","http://obe.sandals.com/?startDate="+aa+"&departureDate="+dd+"&rst_code="+resort+"&departure_country=other&bypassForm=1");
		}
    	if(localeb == 'us')
		{
			$('#qq').attr("action","http://obe.sandals.com/?startDate="+aa+"&departureDate="+dd+"&rst_code="+resort+"&departure_country=other&bypassForm=1");
		}
		$('#qq').submit();
		
		
		
	} else {
		alert("Please Select a Country and Resort to Quote.");
	};
	/*var country   = $("#resortCountry").val();
	var resort    = $('#rst_code').val();
	var arrival   = $('#startdate').val();
	var departure = $('#departureDate').val();
	if(resort != "" && country != "ALL"){
		//setting cookie
		$.ajax({
			type : 'POST',
			url : '/Sandals/includes/themes/sandals/templates/inc/qq-cookie.cfm',
			dataType : 'html',
			data: { arrivalDate:arrival, departureDate:departure },
			success : function(data){
				$('#qq').submit();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			}
		});
	} else {
		alert("Please Select a Country and Resort to Quote.");
	}*/
}

$(document).ready(function(){ 
	// Main Menu
   
	//setup qq calendars
	if($("#startdate").length > 0){
		sideQQ = true;
	}
		
	//$( "#startdate" ).datepicker( $.datepicker.regional[ "fr" ] );
	//findLocale(locale)
	//runQQcalendars();
	//runButtons();
	
});




function findLocale(locale)
{
	
	switch(locale)
	{
		case 'us':
			daysRegion  = ['Su','Mo','Tu','We','Th','Fr','Sa'];
			monthRegion =  ['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'];
		break;
		case 'ie':
			daysRegion  = ['Su','Mo','Tu','We','Th','Fr','Sa'];
			monthRegion =  ['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'];
		break;
		
		case 'it':
			daysRegion  = ['Lu','Ma','Me','Gi','Ve','Sa','Do'];
			monthRegion =  ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'];
		break;
		
		case 'fr':
			daysRegion  = ['Lu','Ma','Me','Je','Ve','Sa','Di'];
			monthRegion =  ['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Dicembre'];
			localeb = locale;
		break;
		
		case 'de':
			daysRegion  = ['Mo','Di','Mi','Do','Fr','Sa','So'];
			monthRegion =  ['Januar','Februar','MŠrz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
		break;
	}
	
	runQQcalendars();
	runButtons();
		
}

function runButtons(){
	$('.bookBtn').unbind('mouseenter mouseleave');
	$('.bookBtn').hover(
		function(){
			$(this).toggleClass('bookBtn_hover');
			$(this).find('.bookBtn_1').toggleClass('bookBtn_1_hover');
			$(this).find('.bookBtn_2').toggleClass('bookBtn_2_hover');
		},
		function(){
			$(this).toggleClass('bookBtn_hover');
			$(this).find('.bookBtn_1').toggleClass('bookBtn_1_hover');
			$(this).find('.bookBtn_2').toggleClass('bookBtn_2_hover');
		}
	);	
}

function runQQcalendars(){
	
    if(sideQQ){		
		//getting today's date and putting in format mm/dd/yyyy
		var today = new Date();
		var myMonth = today.getMonth()+1;
		var myDay = today.getDate();
		var myYear = today.getFullYear();
		myMonth = myMonth < 10 ? "0"+myMonth : myMonth;
		myDay = myDay < 10 ? "0"+myDay : myDay;
		todayDate = myMonth + '/' + myDay + '/' + myYear;
		//todayDate = myDay + '/' + myMonth + '/' + myYear;
		
		var temp = new Date(todayDate);
		var a = new Date();
		a.setDate(temp.getDate()+14);
		
		var mes  = a.getMonth()+1;
		var dias = a.getDate();
		var ano  = a.getFullYear();
		
		todayDate = dias + '/' + mes + '/' + ano;
		//todayDate = a.getDate() + '/' + a.getMonth()+1 + '/' + a.getFullYear();
		//alert(todayDate);
		
		//getting suggested date and putting in format mm/dd/yyyy
		var suggestedDate = new Date();
		suggestedDate.setDate(suggestedDate.getDate()+defaultDiff);
		
		var myMonth = suggestedDate.getMonth()+1;
		var myDay = suggestedDate.getDate();
		var myYear = suggestedDate.getFullYear();
		myMonth = myMonth < 10 ? "0"+myMonth : myMonth;
		myDay = myDay < 10 ? "0"+myDay : myDay;
		todayDate2 = myMonth + '/' + myDay + '/' + myYear;
		//todayDate2 = myDay + '/' + myMonth + '/' + myYear;
		
		
		var tempb = new Date(todayDate2);
		var ab = new Date();
		ab.setDate(tempb.getDate()+14);
		ab.setMonth(tempb.getMonth());
		
		
		var mes2  = ab.getMonth()+1;
		var dias2 = ab.getDate();
		var ano2  = ab.getFullYear();
		//todayDate2 = ab.getMonth()+1 + '/' + ab.getDate() + '/' + ab.getFullYear();
		todayDate2 = dias2 + '/' + mes2 + '/' + ano2;
		
		
		if(qqCookieArrival && qqCookieDeparture){
			dateSelected1 = qqCookieArrival;
			dateSelected2 = qqCookieDeparture;
			
		} else {
			dateSelected1 = todayDate;
			dateSelected2 = todayDate2;
		}
		
		//$("#startdate").datepicker($.datepicker.regional[ "fr" ]);
		$("#startdate").val(dateSelected1);
		$("#departureDate").val(dateSelected2);
		
		
		
		//setup datepickers for sidebar QQ
		var dates = $('#startdate, #departureDate').datepicker({
			showAnim:"fadeIn",
			dateFormat: 'dd/mm/yy',
			duration: 200,
			showButtonPanel:true,
			dayNamesMin: daysRegion,
			monthNames: monthRegion,
			onSelect: function(selectedDate) {
								
				if(this.id == "startdate"){
					//set min date in checkout
					var dateObject = $("#startdate").datepicker('getDate');
					dateObject.setDate(dateObject.getDate()+minDiff);
					dates.not(this).datepicker("option","minDate",dateObject);
					
					//set suggested date
					var dateObject = $(this).datepicker('getDate');
					dateObject.setDate(dateObject.getDate()+defaultDiff);
					dates.not(this).datepicker('setDate',dateObject);
					
				}
				
			},
			beforeShow: function(input, inst){
				inst.dpDiv.css({ marginLeft: '-15px'});
				
			}
		});
		
		$('#startdate').datepicker("option","minDate",a);
		$('#departureDate').datepicker("option","minDate",ab);
		
		$('#ui-datepicker-div').hide(); /// for some strange reason, a calendar gets put on the page by default... so we hide it
	}
}


function accFadeOver(out_id, in_id) {
	f_Out = $('#' + out_id);
	f_In = $('#' + in_id);
	f_Out.stop(true).fadeTo(500, 0);		
	f_In.stop(true).fadeTo(500, 1);
}

function changeDestination(dvalue)
{
		if(dvalue == "SRC")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Royal Caribbean");
			
		};
		if(dvalue == "SMB")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Montego Bay");
		};
		if(dvalue == "SGO")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Grande riviera");
		};
		if(dvalue == "BRP")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Royal Plantation");
		};
		if(dvalue == "SNG")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Negril");
		};
		if(dvalue == "SWH")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Whitehouse");
		};
		if(dvalue == "INN")
		{
			$("#gsDestination").val("MBJ");
			$("#gsprefhotelcode").val("Sandals Carlyle");
		};
		if(dvalue == "SLU")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals La Toc");
		};
		if(dvalue == "SHC")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals Halcyon");
		};
		if(dvalue == "SGL")
		{
			$("#gsDestination").val("UVF");
			$("#gsprefhotelcode").val("Sandals Grande St. Lucian");
		};
		if(dvalue == "SRB")
		{
			$("#gsDestination").val("NAS");
			$("#gsprefhotelcode").val("Sandals Royal Bahamian");
		};
		if(dvalue == "SEB")
		{
			$("#gsDestination").val("GGT");
			$("#gsprefhotelcode").val("Sandals Emerald Bay");
		};
		if(dvalue == "SAT")
		{
			$("#gsDestination").val("ANU");
			$("#gsprefhotelcode").val("Sandals Antigua");
		};
		//alert($("#gsprefhotelcode").val());
		//alert($("#gsDestination").val());
		
}
