var page = window.location.pathname;
var protocol = window.location.protocol + "//";
var site = "www.grandsierraresort.com";

jQuery(function($) {
  
  //Date Picker
  /*$('.date_picker').datepicker({
    numberOfMonths: 3,
    showButtonPanel: true,
    showAnim: "fadeIn",
    showButtonPanel: false,
    dateFormat: "mm/dd/yy",
    minDate: 0,
    maxDate: '+345D',
  });*/
  
  
  //HR Conversion
  $("hr").replaceWith('<div class="hr"><hr /></div>');
  
  
  //Footer Tracking
  /*$("#nav_sub a").click(function() {
    pageTracker._trackEvent('Navigation', 'Click', $(this).text());
  });*/
  
  
  //All Non-IE Browsers
  if(jQuery.support.opacity){
    $(".subpage_body_image").each(function (i) {
  
      //if there is an link associated
      if($(this).parent().attr("href") != undefined){
        $(this).parent().addClass("subpage_body_container");
        $(this).parent().append("<span class=\"mask\"></span>");
        $(this).parent().append("<span class=\"image\"></span>");
      }
      else {
        $(this).wrap('<div class="subpage_body_container" />');
        $(this).parent().append("<span class=\"mask\"></span>");
        $(this).parent().append("<span class=\"image\"></span>");
      }
  
    });
  }
  
  
  //Internet Explorer
  if(!jQuery.support.opacity){
			
    //build_flash_header();
    //Homepage Setup
    $("#homepage_marquee").replaceWith('<div id="homepage_flash"><div id="media_player"></div></div>');

    if(page == "/"){
      var flashvars = {
        xml_data: "/feeds/homepage_slides/"
      };

      var params = {
        wmode: "transparent"
      };

      var attributes = {
        id: "media_player_flash"
      };

      swfobject.embedSWF("/swf/homepage.swf", "media_player", "1152", "460", "9.0.0", "/tools/swfobject/expressInstall.swf", flashvars, params, attributes);
    }
    else {
      //build_flash_header();
    }

   
    //EACH: Subpage Flash
    $(".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("/swf/subpage_image.swf", "subpage_body_photo" + i, "247", "170", "9.0.0","/tools/swfobject/expressInstall.swf", {image_src: $(this).attr("src"), permalink: image_permalink, permalink_target: image_permalink_target}, {wmode:"transparent"}, {styleclass: "subpage_body_image"});
    });
  }
  
  
  //Fancybox
  $("a.fancybox_specials").click(function () {
    popup(pageTracker._getLinkerUrl($(this).attr("href")));
    return false;
  });
  
  $(".fancybox_opentable").click(function() {
    $.fancybox({
      'overlayOpacity' : .9,
      'overlayColor' : "#0a0907",
      'padding' : 0,
      'autoScale' : false,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'title' : this.title,
      'width' : 980,
      'height' : 600,
      'href' : this.href,
      'type' : 'iframe'
    });
    return false;
  });

  $(".fancybox_entertainment").click(function() {
    $.fancybox({
      'overlayOpacity' : .9,
      'overlayColor' : "#0a0907",
      'padding' : 0,
      'autoScale' : false,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'title' : this.title,
      'width' : 680,
      'height' : 495,
      'href' : this.href,
      'type' : 'swf',
      'swf' : {
        'wmode' : 'transparent',
        'allowfullscreen' : 'true'
      }
    });
    return false;
  });
		

  $(".fancybox_image").fancybox({
    overlayOpacity: .9,
    overlayColor: "#0a0907",
    centerOnScroll: true
  });

  $(".fancybox_poker_popup").fancybox({
    width: 688,
    height: 526,
    overlayOpacity: .9,
    overlayColor: "#0a0907",
    scrolling: "no",
    centerOnScroll: true
  });

  $(".fancybox_swf").fancybox({
    overlayOpacity: .9,
    overlayColor: "#0a0907",
    centerOnScroll: true,
    autoscale: false,
    padding: 0,
    width: 720,
   height: 540
  });
  
  
  //Datepicker	
  $('#arrive_date').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',
    onSelect: function(){
			field_focused = true;
			set_departure_date();
			$('#offer_code').focus();
		}
  });

  $('#depart_date').datepicker({
		defaultDate: "+1w",
    numberOfMonths: 3,
    showButtonPanel: true,
    showAnim: "fadeIn",
    showButtonPanel: false,
    altFormat: "m/dd/y",
    altField: "#depart_date_alt",
    dateFormat: "mm/dd/yy",
    minDate: 0,
    maxDate: '+345D',
		onSelect: function(){
			field_focused = true;
			$('#offer_code').focus();
		}
  });
  
	//RES FORM EVENTS
	var quickres_interval;
	var field_focused = false;
	
  $("#quick_res").hover(
		function () {
			stop_animations();
			cancel_quickres_interval();
			
			if($("#quick_res").width() == 500){
			  quickres_interval = setTimeout(show_quickres, 500);
			}
			else {
				show_quickres();
			}
		},
		function (e) {
			var res_top = $(this).offset().top;
			var res_left = $(this).offset().left;
			var res_bottom = res_top + $(this).height();
			var res_right = res_left + $(this).width();
			
			if((e.pageX < res_left) || (e.pageX > res_right) || (e.pageY < res_top) || (e.pageY > res_bottom)){
				stop_animations();
				cancel_quickres_interval();
			  hide_quickres();
			}
		}
	);
	
	$("#ui-datepicker-div").click(function(){
		field_focused = true;
	});
	
	$("#quick_res").focusin(function(){
		stop_animations();
		show_quickres();
	}); 
	
	$("#quick_res").focusout(function(){
		stop_animations();
		hide_quickres();
	});
	
	$("#quick_res input").focus(function(){
		field_focused = true;
	});
	
	$("#quick_res input").click(function(){
		$(this).select();
	});
	
	$("#quick_res input").blur(function(){
		field_focused = false;
	});
	
	function stop_animations(){
		$("#quick_res").stop();
		$("#extra_options").stop();
	}
	
	function show_quickres(){		
		$("#quick_res").animate({
			width: "650px"
			}, 500, function() {
		});
		
		$("#extra_options").animate({
			width: "150px"
			}, 500, function() {
		});
	}
	
	function hide_quickres(){
		if(!field_focused){
			$("#quick_res").animate({
				width: "500px"
				}, 500, function() {
			});
			
			$("#extra_options").animate({
				width: "0px"
				}, 500, function() {
					$("#extra_options").hide();
			});
		}
	}
	
	function cancel_quickres_interval(){
		clearTimeout(quickres_interval);
	}
  
}); 
	
	
  
////////////////////////////////////////////////////////
// ANCHOR SCROLLING
////////////////////////////////////////////////////////
	
function scroll_to(id){
$('html,body').animate({
    scrollTop: $("#"+id).offset().top - 10
},'slow', 'easeOutSine');
}



////////////////////////////////////////////////////////
// GENERIC 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 DATE PICKER
////////////////////////////////////////////////////////


//EVENT: Arrival onSelect event
function set_departure_date(){
  //set new departure date by adding one day to the arrival_date
  var arrival_date = $("#arrive_date").datepicker("getDate");
  var depart_date = new Date(arrival_date);
  depart_date.setDate(depart_date.getDate() + 1);

  //set new departure date
  $("#depart_date").datepicker("setDate", depart_date);
  $("#depart_date").datepicker("option", "minDate", depart_date);

}



////////////////////////////////////////////////////////
// FUNCTION: CHECK RES FORM
////////////////////////////////////////////////////////

function check_res_form(){
  //variables
  var promo = new Array();//parameters: [LMS offer code:String, offer aliases:Array, redirect url:String]
  var redirect = "";

  ///////////////////////////////////////////////////////////////////////////////////////////////////
  //checks for promo, and adjusts offer code based on variations customers might mispell or mistype
  ///////////////////////////////////////////////////////////////////////////////////////////////////
  promo.push(["2nites", ["2nites", "2nights", "twonites", "twonights"]]);
	promo.push(["2nites2", ["2nites2", "2nights2", "twonites2", "twonights2"]]);
	promo.push(["2nites3", ["2nites3", "2nights3", "twonites3", "twonights3"]]);
	promo.push(["2nites4", ["2nites4", "2nights4", "twonites4", "twonights4"]]);
	promo.push(["2nites5", ["2nites5", "2nights5", "twonites5", "twonights5"]]);
	promo.push(["2nites6", ["2nites6", "2nights6", "twonites6", "twonights6"]]);
	promo.push(["2nites7", ["2nites7", "2nights7", "twonites7", "twonights7"]]);
	promo.push(["11deal", ["11deal", "11 deal", "elevendeal", "eleven deal"]]);

  //loop through promos
  for(i = 0; i < promo.length; i++){
    //loop through promo offer codes
    for(j = 0; j < promo[i][1].length; j++){
      var promo_code = $("#offer_code").val().toLowerCase();
      if(promo_code == promo[i][1][j]){
      //checks if redirect exists for promo
      if(promo[i][2] != undefined && window.location != promo[i][2]){
        redirect = promo[i][2];
      }
      //otherwise, use LMS-proper offer code in place of alias
      else {
        $("#offer_code").val(promo[i][0]);
      }

      break;
    }
  }
}

switch(true){
  case redirect != "":
    window.location = redirect;
    return false;
    break;

  case $("#arrive_date").val() == "":
    alert("Please choose an arrival date.");
    return false;
    break;

  case $("#arrive_date").val() == "":
    alert("Please choose a departure date.");
    return false;
    break;

  case Number($("#num_adults").val()) + Number($("#num_kids").val()) > 5:
    alert("The total number of people for this stay cannot exceed 5.");
    return false;
    break;

  case $("#offer_code").val() != "" && $("#rate").is(":checked"):
    alert('AAA members cannot use additional offer codes. Please uncheck "AAA Member" if you would like to use the offer code entered.');
    return false;
    break;
		
	case ($("#offer_code").val() == "2nites") && String(window.location).indexOf("2nites") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "35deal") && String(window.location).indexOf("35deal") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "60deal") && String(window.location).indexOf("60deal") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "2fch") && String(window.location).indexOf("2fch") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "2ftg") && String(window.location).indexOf("2ftg") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "2fvn") && String(window.location).indexOf("2fvn") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://grandsierraresort.com/deals/offer/2local?offer=2local";
			return false;
		}
		else {
			var dialog = "Please accept the following terms to continue:\n\n";
			dialog += "     * Credit card billing address must match state photo ID at check in.\n";
			dialog += "     * Subject to availability based upon residency greater than 50 mile radius from GSR.\n";
			dialog += "     * Cannot be combined with any other offer.\n";
			dialog += "     * $50 charge applied for no show or cancellation received less than 48 hours prior to arrival.";
			if(confirm(dialog) == false){
				return false;
			}
		}
		break;
		
	case ($("#offer_code").val() == "11deal") && String(window.location).indexOf("11deal") == -1:
		if(is_local() || $("#u_zip").val() == ""){
			alert("We're sorry, this offer is restricted to guests from outside the local Reno/Tahoe area. Please see our other special offers:");
			window.location = "http://www.grandsierraresort.com/special-offers";
			return false;
		}
		break;

  default:
	  $("#return_url").val(window.location);
    return true;
  }
}


function is_local(){	
  var u_zip = prompt("Please enter your 5 digit zip code:");
  var zips = new Array ("", null, 89402,89403,89408,89410,89411,89413,89423,89424,89428,89429,89431,89432,89433,89434,89435,89436,89439,89440,89441,89442,89448,89449,89450,89451,89452,89460,89501,89502,89503,89504,89505,89506,89507,89508,89509,89510,89511,89512,89513,89515,89519,89520,89521,89523,89533,89555,89557,89570,89595,89599,89701,89702,89703,89704,89705,89706,89711,89712,89713,89714,89721,95715,95721,95724,95728,95910,96105,96106,96109,96111,96113,96118,96122,96124,96125,96126,96129,96135,96140,96141,96142,96143,96145,96146,96148,96150,96151,96152,96154,96155,96156,96157,96158,96160,96161,96162);
		for(i = 0; i < zips.length - 1; i++){
			 if(u_zip == zips[i]){
				  return true;
				}
		}
}



////////////////////////////////////////////////////////
// FUNCTION: BUILD SUB NAVIGATION
////////////////////////////////////////////////////////

function build_subnavigation(segment){
  if(segment != "reno-spotlight" && segment != null){
    $("#body_right").prepend("<ul id=\"nav_sub\"></ul>");
    $("#site_map a[href*='/" + segment + "/']:first").parent().clone().appendTo("#nav_sub");
    $("#nav_sub .here").parent().parent().addClass("parent");
    
    /*$("#nav_sub a").click(function() {
      pageTracker._trackEvent('Navigation', 'Click', $(this).text());
    });*/
  }
}



////////////////////////////////////////////////////////
// ENTERTAINMENT TABS
////////////////////////////////////////////////////////

function swap_tab(tab, content){
  $(".page_tabs .on").removeClass("on");
  $(".page_tab.on").removeClass("on");
  $("#" + tab).addClass("on");
  $("#" + content).addClass("on");
}

if($(".page_tabs").attr("class") != undefined){
  $(".page_tabs").find("a").attr("href", "javascript: void(0);");
}



////////////////////////////////////////////////////////
// FUNCTION: 404 RESULTS
////////////////////////////////////////////////////////

function build_404_results(segment){
  $("#page_content").append("<ul id=\"results_404\"></ul>");
  
  if(segment == null){
      $("#site_map").contents().clone().appendTo("#results_404");
  }
  else {
      $("#site_map a[href*='/" + segment + "/']:first").parent().clone().appendTo("#results_404");
  }
  
  $("#results_404 a").click(function() {
      pageTracker._trackEvent('404 Results', 'Click', $(this).text());
  });
  
  $("#body_right").hide();
}
