$(function() {

	/* display current year in footer copyright section */
	$(".current_year").text( (new Date).getFullYear() );
	
		/* current navigation - not sure if we want this, they only have 2 colors */
 $(".section_offers .nav_special_offers_subnav").addClass("current");
 $(".section_special_offers .nav_special_offers_subnav").addClass("current");
 $(".section_our_hotel .nav_our_hotel_subnav").addClass("current");
 $(".section_meetings .nav_meetings_subnav").addClass("current");
 $(".section_location .nav_location_subnav").addClass("current");
 $(".section_dine .nav_dining_subnav").addClass("current");
 
 /* navigation */
    var currentMenu = $('.current').find('ul');
     function menuDefault() {
      if (currentMenu.css('display') === 'none'){
       currentMenu.stop().css('z-index',1080).fadeIn('slow');
      }
     }
     //Current section submenu displayed by default.
     currentMenu.css({'z-index':1080,'display':'block'});

     /*$('#main_nav').mouseleave(menuDefault);*///always restore current section submenu on menu mouseout.

     $('.has_subnav').hover(function(){//mouseenter behavior
        var hovered = $(this);
        var hoveredList = hovered.find('ul');
        hovered.addClass('hovered');
        var unhovered = $('.has_subnav').not('.hovered');
        var unhoveredList = unhovered.find('ul');

        if(!$(this).hasClass('current')) {
            $('.current').addClass('ocurrent').removeClass('current');
            $('.ocurrent').css('z-index',1000);
            //$(this).find('ul').css('z-index',99999).fadeIn();
        }
        clearTimeout(hovered.data('timeoutId'));
        unhoveredList.css('z-index',1000).fadeOut('slow');
        hoveredList.css('z-index',1090).fadeIn('fast');
     }, function(){//mouseleave behavior
        var hovered = $(this);
        hovered.removeClass('hovered');
        var hoveredList = hovered.find('ul');
        if(!$(this).hasClass('current')) {//if this isn't the current menu, hide it and restore the 'current' class to the current item.
            var timeoutId = setTimeout(function(){
                hoveredList.fadeOut('slow',function(){
                    hoveredList.css('z-index',1000);
                    $('.ocurrent').addClass('current').removeClass('ocurrent'); //restore current subnav
                    $('.current').find('ul').css('z-index',1090); //push current subnav back to the top
                });
            },1000);
        }
        hovered.data('timeoutId', timeoutId);
        if(hovered.is('.current','.ocurrent')){
            hoveredList.animate('opacity',1);
        }
     });

	

	
	/* more functionality */
	$(".more").toggle(				  
		function() {
			var target = $('.' + $(this).attr('key'));
			$(target).slideDown('slow');
			$(this).addClass('expanded');
			//$(target).addClass('expanded');
		},
		function() {
			var target = $('.' + $(this).attr('key'));	
			$(target).slideUp('slow');
			$(this).removeClass('expanded');
			//$(target).removeClass('expanded');
		}
	);
	
	$(".half_stack").click(function(){ 
	   var link = $(this).find('a');
	   var path = link.attr('href');
       if(path) {    
        if(path.indexOf("http://") >= 0) { 
         window.open(path);     
        } 
		else if(link.hasClass('new_window')) { 
         window.open(path);     
        } 
        else { 
         window.location = path;
        } 
       }
       return false; 
     });

    $(".home-stack").click(function(){
        var path = $(this).find('a').attr('href');
        if(path) {
            if($(this).find('a').attr('target')=='_blank') {
                window.open(path);
            }else {
                window.location = path;
            }
        }
        return false;
    });

	$(".has_tooltip").tooltip({ effect: 'slide', relative: 'true', offset: [15, 0]});
	$(".has_tooltip").tooltip({ effect: 'slide', relative: 'true', offset: [15, 0]});
	
	$(".mobile-tooltip a").tooltip({  
		tipClass: 'mobile-tip',  
		position: 'center right',  
		effect: 'fade',  
		offset: [15, -5],  
		relative: 'false',  
		delay: 0  
	});

	$(".slideshow").fancybox({
		'autoDimensions': false,
        'autoScale': false,
		'titleShow':	false,
		'scrolling':	'no',
		'height':		545,
		'width':		760,
		'padding':		10,
		'type':			'iframe'
	});
	$(".fancybox_global_business_program").fancybox({
		'titleShow':	false,
		'scrolling':	'no',
		'height':		382,
		'width':		643,
		'padding':		10,
		'type':			'iframe'
	});
	$(".fancybox_rfp").fancybox({
		'titleShow':	false,
		'scrolling':	'yes',
		'height':		382,
		'width':		643,
		'padding':		10,
		'type':			'iframe'
	});
	$(".meetings_photo_gallery li a").fancybox({
		'titleShow':	true,
		'titlePosition': 'inside'
	});
	/* reservations hide/show hover */

	$(".nav_reservations").hover(
		function() {
			$("#reservations_app").slideDown({
				duration: 1000,
				easing: "easeOutExpo"
			});
			$(this).addClass("active");
		}
	);

	/* reservations hide/show click */	
	/*$(".nav_reservations").toggle(
		function() {
			$("#reservations_app").slideDown({
				duration: 1000,
				easing: "easeOutExpo"
			});
			$(this).addClass("active");
		},
		function() {
			$("#reservations_app").slideUp({
				duration: 1000,
				easing: "easeInExpo"
			});
			$(this).removeClass("active");
		}
	);*/

	$(".reservation_app_close").click(
		function() {
			$("#reservations_app").slideUp({
				duration: 1000,
				easing: "easeInExpo",
				complete: function(){
					$(".nav_reservations").removeClass("active");
				}
			});	
		}
	);
    /* initialize pre-arrival concierge datepickers */
    $(".page_concierge .date_picker").datepicker();

	/* initialize reservation datepickers */
	$("#appointment_date").datepicker({minDate: 0, maxDate: '+11M'});
	$("#pet_birthday").datepicker();
	$("#depart, #arrive").datepicker({
			onSelect: DatePicked
	});
	/* set default date for reservations */
	DatePicked();
	
	/* reservations */
	$('#form_reservations').submit(function() {
		window.location = "https://gc.synxis.com/rez.aspx?" + $(this).serialize();
		return false
	});
	
	/* expandable faq */
	$('.expandable dt').toggle(
		function() {
			$(this).next().slideDown();
			$(this).addClass('active');
		},
		function() {
			$(this).next().slideUp();
			$(this).removeClass('active');
		}
	);
});

/* scroll functionality */
function scrollToAnchor(aname, offset) {
	offset = 30;
	$("html,body").animate({
		scrollTop: $("a[name='" + aname + "']").offset().top - offset
	}, 500);
};
function scrollToTop() {
	$("html,body").animate({
		scrollTop: 0
	}, 500);
};

/* date picker functions */
function setInitDate(handle) {
	var myDate = new Date();
	var month = myDate.getMonth()+1;
	if(month<10) {
		month = '0' + month;
	}
    if (handle === 'depart') {
        var prettyDate = new Date();
        prettyDate.setTime(prettyDate.getTime() + 86400000);
    } else{
        var prettyDate =month + '/' + myDate.getDate() + '/' + myDate.getFullYear();
    }
    $('#'+handle).datepicker('setDate', prettyDate);
    return prettyDate;
};

/* links the depart + arrival date picker functions for reservations */
var DatePicked = function(text, instance) {
    	var departure = $("#depart");
	var arrival = $("#arrive");
	var arrivalDate = arrival.datepicker("getDate");

	if(arrivalDate == null) {
		setInitDate("arrive");
		setInitDate("depart");
	}
    var arrivalDate = arrival.datepicker("getDate");
    var minDepartDate = new Date();
    arrival.datepicker('option', 'minDate', 0);
    minDepartDate.setTime(arrivalDate.getTime() + 86400000);
    departure.datepicker("option", "minDate", minDepartDate);

    /*if the arrival date was just selected, choose the next day as the departure date.*/
    if (typeof instance !== 'undefined' && instance.id === 'arrive'){
        var newDepartDate = new Date();
        /*Account for new years eve selections*/
        if (arrivalDate.getMonth()===11 && arrivalDate.getDate() === 31){
            console.log(instance);
            var nextYear = parseInt(instance.selectedYear)+1;
            var departureDateValue = '01/01/'+nextYear;
            departure.attr('value',departureDateValue);
        }else if((arrivalDate.getMonth()===9) && arrivalDate.getDate() === 31) {
            /* fixing October 31 bug. */
            var departureDateValue = '11/01/'+arrivalDate.getFullYear();
            departure.attr('value',departureDateValue);
        }else if(((arrivalDate.getMonth()===0)||(arrivalDate.getMonth()===2)||(arrivalDate.getMonth()===4)||(arrivalDate.getMonth()===6)||(arrivalDate.getMonth()===7)) && arrivalDate.getDate() === 31){
            /*Fixing First of the month issue (otherwise reverts to current month*/
            var nextMonth = arrivalDate.getMonth()+2;
            nextMonth = nextMonth.toString();
            if (nextMonth.length === 1) nextMonth = '0'+nextMonth.toString();
            var departureDateValue = nextMonth+'/01/'+arrivalDate.getFullYear();
            departure.attr('value',departureDateValue);
        }else if((arrivalDate.getMonth()===1) && ((arrivalDate.getFullYear()===2012)||(arrivalDate.getFullYear()===2016)) && arrivalDate.getDate() === 29){
            departure.datepicker("setDate", newDepartDate);
            var updateDate = departure.datepicker("getDate");
            /* updating the actual text of the leap year input field */
            departure.attr('value','03/01/2012');
        }else if((arrivalDate.getMonth()===1) && ((arrivalDate.getFullYear()===2012)||(arrivalDate.getFullYear()===2016)) && arrivalDate.getDate() === 28){
            departure.datepicker("setDate", newDepartDate);
            var updateDate = departure.datepicker("getDate");
            /* updating the actual text of the leap year input field */
            departure.attr('value','02/29/2012');
        }else{
            newDepartDate.setTime(arrivalDate.getTime() + 86400000);
            departure.datepicker("setDate", newDepartDate);
            var test = arrival.datepicker('getDate');
            var testm = new Date(test.getTime());
            testm.setTime(test.getTime() + 86400000);

            departure.datepicker("option", "minDate", testm);
        }
    }
};
