var params = {
  wmode: "transparent",
  allowscriptaccess:"always",
  loop: "false",
  play: "true"
};

// rails auth token enabled in jquery
$(document).ajaxSend(function(event, request, settings) {
	if (typeof(AUTH_TOKEN) == "undefined") return;
	 // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
	  settings.data = settings.data || "";
	  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

$(function(){
	// HIDE ALL ELEMENTS VIA NO_JS CLASS
	if($(".no_js").length > 0) $(".no_js").hide();
	if($(".yes_js").length > 0) $(".yes_js").show();
	
	

	// EMBED SWFs ON HOME PAGE
	if($("#home_flash").length > 0){
		closeMe();
		swfobject.embedSWF("/larkspur_collection_hotel_rollover.swf", "swf1", "731", "495", "9.0.0", "expressInstall.swf", 0, params, {id:"larkspur_hotelsDropDown", name:"larkspur_hotelsDropDown"});
		$("#hotels").mouseover(function(){
			showHotelsFlash();		
		});
		$(".slide").show(); // show hidden slides - unobtrusive
		
	}

	
	if($("#home_flash_div").length > 0){
		cycleHomePage2();		
	}
	if($("#rest_div").length > 0){
		cycleHomePage3();	
		// RESTAURANT DROP DOWN REPLACES DETAILS FOR SELECTED RESTAURANT
		$(".rest_dropdown").change(function(){
				// hide then show selected restaurant details
				var selected_restaurant_id = $(this).val();
				if(selected_restaurant_id != "") {
					var restaraunt_selector = "#restaurant_details_" + selected_restaurant_id;
					$(".restaurant_details").hide();
					$(restaraunt_selector).show();	
				}
			});	
	}
	if($("#home_main_div").length > 0){
		cycleHomePage();		
	}
	// CONFIG RESERVATION/DESTINATION UIs
	if($(".reservation_form").length > 0){
		$("#destination_reservation_div").hide();
		$("#reservation_radios").show();
		$(".destination_radio").click(
			function(){
					$("#destination_reservation_div").show();
					$("#hotel_reservation_div").hide();
					$(".destination_radio").attr("checked","checked");
				}
		);
		
		// SHOW RES FORM
		$(".hotel_radio").click(
			function(){
					$("#destination_reservation_div").hide();
					$("#hotel_reservation_div").show();
					$(".hotel_radio").attr("checked","checked");
				}
		).attr("checked","checked");
		
		// NAV TO DROPDOWN DESTINATION 
		$("#destination_select").change(function(){
			if($(this).val() != "") window.location = "/collection/destinations/" + $(this).val()
			});
	}
	
/*	// attach datepickers
   if($("#arrive").length > 0){
		$("#arrive").datepicker({minDate: "today"});
		$("#depart").datepicker({hideIfNoPrevNext: true, minDate: $('#arrive').datepicker('getDate')});
	}
*/
 if($("#arrive").length > 0){
        $("#arrive").datepicker({minDate: "today",showOn: 'both', buttonImage: '/images/icon_calendar.gif',buttonImageOnly: true,
        onSelect: function(date_text) {
                    validate_change();
                }});
        $("#depart").datepicker({hideIfNoPrevNext: true, minDate: $('#arrive').datepicker('getDate'),showOn: 'both',
             buttonImage: '/images/icon_calendar.gif',buttonImageOnly: true,    onSelect: function(date_text) {
                            validate_change();
                        } });
       
    }
	// SEARCH focus/clear
	if($(".search").length > 0){
		$(".search").attr("value","Search");
		$(".search").focus(function(){
			if(this.value == "Search"){
				this.value = "";
				$(this).css({color:"#000"})
			}	
		});
	}
	// EMAIL focus/clear
    // if($(".email").length > 0){
    //  $(".email").attr("value","Enter Email Address");
    //  $(".email").focus(function(){
    //      if(this.value == "Enter Email Address"){
    //          this.value = "";
    //          $(this).css({color:"#000"})
    //      }   
    //  });
    // }
    function validate_change(){
        var depart = $("#depart");
        var arrive = $("#arrive");
        var depart_date = depart.datepicker('getDate');
        var arrive_date = arrive.datepicker('getDate');
        if(depart_date <= arrive_date)
        {
            var newDate = new Date(arrive_date);
            newDate.setDate(newDate.getDate() + 1);
            depart.datepicker('setDate',newDate);
        }
        }
}); // end of jquery onload

function cycleHomePage3(){
	$('#rest_div').cycle({
	       fx:        'fade',
	       delay:    0,
		   timeout: 6000,
	       cssBefore: { opacity: 1 },
	       animOut:   { opacity: 0 },
		   slideExpr: 'img'
	    });
}
function cycleHomePage2(){
	$('#home_flash_div').cycle({
	       fx:        'fade',
	       delay:    0,
		   timeout: 6000,
	       cssBefore: { opacity: 1 },
	       animOut:   { opacity: 0 },
		   slideExpr: 'img'
	    });
}
function cycleHomePage(){
	$('#home_main_div').before('<div id="slideNav">').cycle({
	       fx:        'fade',
	       delay:    0,
		   timeout: 6000,
	       cssBefore: { opacity: 1 },
	       animOut:   { opacity: 0 },
		   slideExpr: 'div.slide',
		   pager:  '#slideNav' 
	    });
}

function showHotelsFlash(){
	$('#home_main_div').cycle('pause');
	 $("#outer_hotels").animate({height:"495px"});
}

function closeMe(){ 
	$("#outer_hotels").css({height:"0px"});
	$("#outer_hotels").addClass("hidden");
	$('#home_main_div').cycle('resume');
}

function reservation_form_submit ( form ) {
  createLinkerUrl(form);
  
	var hotel_id = $("#hotel_select").val();
	var arrive_date =	$("#arrive").datepicker("getDate");
	var depart_date =	$("#depart").datepicker("getDate");
	if (hotel_id == ""){
		alert("Please select a hotel."); 
		return false;
	}
	if (depart_date <= arrive_date){
		alert("Your departure date must be later than your arrival date.");
		return false;
	}
	
	var alt_dest = "";
	if (hotel_id == "11012" || hotel_id == "11158" ) {  //LL Bellevue, Renton
		alt_dest = "SEA";
	}
	else if (hotel_id == "11170" || hotel_id == "11009") {  //RiverPlace, LL Hillsboro
		alt_dest = "PDX";
	}
	else if (hotel_id == "11008" || hotel_id == "11159" || hotel_id == "11163") {  //LL Folsom, Roseville, Sacramento
		alt_dest = "SAC";
	}
	else if (hotel_id == "11169" || hotel_id == "11365" || hotel_id == "11160" || hotel_id == "11162" || hotel_id == "11010" || hotel_id == "11161") {   //LL Campbell, Milpitas, Sunnyvale, St. Claire, Toll House, Pruneyard
		alt_dest = "SJC";
	}
	else if (hotel_id == "17622" || hotel_id == "17233" || hotel_id == "11165" || hotel_id == "11164" || hotel_id == "16793") {  //Hotel Abri, LHUS, Villa Florence, LL SSF, Lodge at Tiburon
		alt_dest = "BAY";
	}
	else {
		alt_dest = "";  //Belamar Hotel, Casa Munras, LL Pleasnton (doesn't have alt_dest)
	}
	if(alt_dest == ""){
		$("#altdest").remove(); // removed hidden field so that there will not be an empty altdest attr
	}
	else {
		$("#altdest").attr("value", alt_dest);
	}
	
	return formLinker(form);
}


/* book now destination form */
function swapform(id) {
  document.getElementById('form1').style.display = 'none';
  document.getElementById('form2').style.display = 'none';
  document.getElementById('form'+id).style.display = 'block';
}

// For use with Flash
function pageTracker_link ( url ) {
	pageTracker._link(url);
}
