////////////////////////////////////////////////////////
// JQUERY LOAD
////////////////////////////////////////////////////////

var page = window.location.pathname;

if(page == "/"){
  var flashvars = {
      xml_data: "/feeds/homepage_slides/"
    };
    
    var params = {
      wmode: "transparent"
    };
    
    var attributes = {
      id: "media_player_flash"
    };
		
    swfobject.embedSWF("/custom/multimedia/swf/homepage.swf", "media_player", "1152", "460", "9.0.0", "/custom/tools/swfobject/expressInstall.swf", flashvars, params, attributes); 
}

jQuery(function($) {
  
  //------------------------------
  //NAV INDICATOR
  
  $(window).resize(function(){
    resize_page_title();
  });
  
  $(window).load(function(){
    resize_page_title();
  }); 
    
  function resize_page_title(){
    if(page == "/"){
      var div_id = "#media_player_flash";
    }
    else {
      var div_id = "#subpage_title_flash";
    }
    if($(window).width() < 1152){
      $(div_id).width($(window).width());
      //$("#page_title_wrapper").width($(window).width());
    }
    else{
      $(div_id).width(1152);
      //$("#page_title_wrapper").width(1152);
    }
  }
  
  
  //------------------------------
  //HEADER SWF CONVERSION
  
  $inc = 0;
  //h2 titles
  $("h2").each(function (i) {
    
    if(this.id == ""){
        $(this).replaceWith('<h2 id="h2Title' + i + '">' + $(this).text() + '</h2>');
        
        swfobject.embedSWF("/custom/multimedia/swf/subpage_h2.swf", "h2Title" + i, "600", "22", "9.0.0","/custom/tools/swfobject/expressInstall.swf", {title_size:"600", title_text:get_special_characters($(this).text()), permalink:"", title_id:"h2_title" + i, hex_color: "0x494845"}, {wmode:"transparent"}, {id:"h2_title" + i, name:"h2_title" + i, styleclass: "h2_title"});
    }
    
    $inc++;
  });
  
  
  //------------------------------
  //SUBPAGE IMAGE CONVERSION
  
  $(".subpage_body_image").each(function (i) {
    $(this).attr("id", "subpage_body_photo" + i);
    
    var image_permalink = undefined;
    var image_permalink_target = undefined;
    
    if($(this).parent().attr("href") != undefined){
      image_permalink = $(this).parent().attr("href");
      
      if($(this).parent().attr("target") != undefined){
         image_permalink_target = $(this).parent().attr("target"); 
      }
    }
    
    
    swfobject.embedSWF("/custom/multimedia/swf/subpage_image.swf", "subpage_body_photo" + i, "247", "170", "9.0.0","/custom/tools/swfobject/expressInstall.swf", {image_src: $(this).attr("src"), permalink: image_permalink, permalink_target: image_permalink_target}, {wmode:"transparent"}, {styleclass: "subpage_body_image"});     
  });
  
  
  //------------------------------
  //HR CONVERSION
  $("hr").replaceWith('<div class="hr"><hr /></div>');
  


  //------------------------------
  //NAV INDICATOR

  $(".here a:first").html("<span style='position: relative; top: -2px;'>&laquo; </span>" + $(".here a:first").text());
  
  
  //------------------------------
  //FANCYBOX
  
		if(jQuery.support.htmlSerialize){
				$("a.fancybox_specials").click(function () { 
						popup($(this).attr("href"));
						return false;
				});
		}
		else {
			  /*
					THIS CODE KEEPS ERRORING WITH SILVER VOYAGES
					
					$("a.fancybox_specials").fancybox({
							frameWidth: 721,
							frameHeight: 555,
							overlayOpacity: .7,
							centerOnScroll: true
					});
					*/
					
					$("a.fancybox_specials").click(function () { 
       popup($(this).attr("href"));
							return false;
     });
		}
		
	$("a.fancybox_website").fancybox({
    frameWidth: 850,
    frameHeight: 600,
    overlayOpacity: .7,
    centerOnScroll: true
  }); 
  
  $("a.fancybox_entertainment").fancybox({
    hideOnContentClick: false,
    overlayOpacity: .7,
    centerOnScroll: true
  }); 
		
		
	//------------------------------
  //ASSIGN PHOTO GALLERY BUTTON IT'S GALLERY FUNCTIONALITY
		
		var total_galleries = $(".gallery").length;
		
		for(i = 0; i < total_galleries; i++){
				var button_id = $(".gallery").eq(i);
		  var group_id = $(button_id).attr("id");
				
				var total_photos = $("#photo_gallery a.photo_gallery").length;

    for(j = 0; j < total_photos; j++){
					 var photo = $("#photo_gallery a.photo_gallery").eq(j);
					
				  var rel = $(photo).attr("rel");
						if(rel == group_id){
						  $(button_id).attr("href",$(photo).attr("href"));
								$(button_id).attr("title",$(photo).attr("title"));
								$(button_id).attr("rel",$(photo).attr("rel"));
								$(button_id).addClass($(photo).attr("class"));
								$(photo).parent().remove();
								break;
						}
				}
		}
				
		$("a.photo_gallery").fancybox({
    hideOnContentClick: false,
    overlayOpacity: .7,
    centerOnScroll: true
  });

});



////////////////////////////////////////////////////////
// QUICK RES CHECKBOX
////////////////////////////////////////////////////////

function toggleAAA(){
  if($("#quick_res_aaa").val() == "0"){
    $("#aaa_container").addClass("on");
    $("#quick_res_aaa").val("1");
    document.quick_res_form.rate.checked = true;
  }
  else{
    $("#aaa_container").removeClass();
    $("#quick_res_aaa").val("0");
    document.quick_res_form.rate.checked = false;
  }
}



////////////////////////////////////////////////////////
// QUICK RES DATE PICKER
////////////////////////////////////////////////////////

jQuery(function($) {
  $('#quick_res_arrival').datepicker({
    numberOfMonths: 3,
	  showButtonPanel: true,
	  showAnim: "fadeIn",
	  showButtonPanel: false,
	  altFormat: "m/dd/y",
	  altField: "#arrive_date_alt",
	  dateFormat: "mm/dd/yy",
	  minDate: 0,
	  maxDate: '+345D'
  });
  
});



////////////////////////////////////////////////////////
// FLASH SPECIAL CHARACTERS
////////////////////////////////////////////////////////

function get_special_characters(text_string){
   var list = new Array();
   list.push(["+","%2B"]);
   list.push(["&eacute;","%E9"]);
   
   for(i = 0; i < list.length; i++){
    if(text_string.indexOf(list[i][0]) != -1){
      text_string = text_string.split(list[i][0]).join(list[i][1]);
    }
   }
   
   return text_string;
}


////////////////////////////////////////////////////////
// ENTERTAINMENT TABS
////////////////////////////////////////////////////////

function swap_tab(tab, content){
  $(".page_tabs .on").removeClass("on");
  $(".page_tab.on").removeClass("on");
  $("#" + tab).addClass("on");
  $("#" + content).addClass("on");
		
		return false;
}


////////////////////////////////////////////////////////
// SPECIALS POP-UP
////////////////////////////////////////////////////////

function popup(url){
  window.open(url,'remote','width=745,height=500,scrollbars=Yes,resizable=no,status=yes,menubar=no,hotkeys=no');
}


////////////////////////////////////////////////////////
// QUICK RES CHECK PROMO CODE
////////////////////////////////////////////////////////

function check_promo(){
	 var offer_code = document.quick_res_form.offer_code.value.toUpperCase();
		
  switch(true){
		  case offer_code == "FALL":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL";
						return false;
						break;
						
				case offer_code == "FALL1" || offer_code == "FALL 1" || offer_code == "FALL-1":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL1";
						return false;
						break;
						
				case offer_code == "FALL2" || offer_code == "FALL 2" || offer_code == "FALL-2":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL2";
						return false;
						break;
						
				case offer_code == "FALL3" || offer_code == "FALL 3" || offer_code == "FALL-3":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL3";
						return false;
						break;
						
				case offer_code == "FALL4" || offer_code == "FALL 4" || offer_code == "FALL-4":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL4";
						return false;
						break;
						
				case offer_code == "FALL7" || offer_code == "FALL 7" || offer_code == "FALL-7":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALL7";
						return false;
						break;
						
				case offer_code == "FALLTV" || offer_code == "FALL TV" || offer_code == "FALL-TV":
				  document.quick_res_form.action = "https://res.grandsierraresort.com/promo/index.php?promo_id=FALLTV";
						return false;
						break;
		}
}