$(document).ready(function() {
    
    // mmmm... spicy __KEEP THIS AT TOP__ messes with accordion
    $("#arrival-dd, #arrival-m-y, #nights, #rooms, #adults, #children").spicyselect();
    
    //form email validation
    //$("#contactForm, #weddingRFP, #meetingRFP, #emailSignUpForm").submit(function(){validate();});
    $("#contactForm, #weddingRFP, #meetingRFP, #emailSignUpForm").validate();
    
    
    // Show sub-nav on home-page    
    
    $("#main-nav li").hover(function() {
       $("ul", this).css({"display":"block"});
    }, function(){
        $("ul", this).css("display", "none");
    });
    
    //Cycle Home Page Masthead images
	$('.mast').cycle({
		fx: 'fade', 
		timeout: 3000,
		//next: '.mast-next',
		//prev: '.mast-prev',
		pause: 1
	});
	
	/*$("a[rel^='prettyPhoto']").each(function(){
		//alert($(this).attr("href"));
		$.prettyPhoto.open($(this).attr("href"));
		//alert('here');
	});*/
	
	
    // Highlight main-nav when hovering over sub-nav
    
    $("#main-nav li ul").hover(function() {
        $(this).prev().addClass("current-page");
    }, function() {
        $(this).prev().removeClass("current-page");
    })
    
    
    
    // Featured nav roll-ups.
    
    $("#featured-nav li div.section").hover(function(){
        $(this).stop().animate({
            "height": 316
        })
        $("h3", this).addClass("featured-current");
    }, function(){
        $(this).stop().animate({
            "height": 30
        });
        $("h3", this).removeClass("featured-current");
    })
    
    // contact us form date picker
	$('.contact_date')
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/calendar.png',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+1Y',
			dateFormat: 'MM d, yy'
		})
	$('.contact_date').datepicker('setDate', '+0');
    
    
    
    // The semi-transparent page borders
    
    $("#page").prepend('<div class="page-border-left"></div><div class="page-border-right"></div>')
    $(".page-border-left, .page-border-right").css({"opacity":"0.5", "height": "10000px"})
    
    
    /*If user had promo open on last page visit - reopen it*/
    if($.cookie("promo-open")=="true"){
    	$("#promo-banner, #promo-banner-home").after('<img id="next-slide" src="/extension/cdev_base/design/cdev_base/images/btn-next.png" ').cycle({ 
            fx:     'fade', 
            speed:  'slow', 
            next:   '#next-slide',
            timeout: 0 
        });
    	
    	$("#promo-banner-closed").css({"width": 0});
    	$("#promo-banner").css({"width":193}).show();
    	$("#close-promo-banner, #next-slide").show();
    }
    
    // Promo Banner open    
    $("#promo-banner-closed").click(function(){
    	$("#promo-banner, #promo-banner-home").after('<img id="next-slide" src="/extension/cdev_base/design/cdev_base/images/btn-next.png" ').cycle({ 
            fx:     'fade', 
            speed:  'slow', 
            next:   '#next-slide',
            timeout: 0
            
        });
        
        
        $(this).animate({
            "width": 0
        },{
            complete: function(){
                $(this).hide();
            }
        });
        
        // internal
        $("#promo-banner").animate({
            "width": 193
        }, {
            complete: function(){
        		$.cookie("promo-open", true);
                $("#close-promo-banner, #next-slide").show();
            }
        });
        
        // home
        $("#promo-banner-home").animate({
            "width": 439
        }, {
            complete: function(){
                $("#close-promo-banner, #next-slide").show();
            }
        });
    	
    });
    
    
    // Promo Banner close
    
    $("#close-promo-banner").click(function(){
        $(this).hide();
        $("#next-slide").remove();
        $("#promo-banner, #promo-banner-home").animate({
          "width": 0
        }, {
            complete: function() {
        	$.cookie("promo-open", false);
            $(this).hide();
            $("#promo-banner-closed").animate({
                "width": 45
            }, {
                "duration": 250
            })
          }
        });
        
    })
    
    
    
    
    // Room types open and close
    $("#room-types li .view-details, #room-types li h3, #room-types li span.room img").click(function(){
    	if($(this).parents("li").css("height")=="41px"){
	        var elHeight = $(this).parents("li").children("span.room").children().height()+35;
	        
	    	if( ($(this).parents("li").children("p.teaser").innerHeight()+90) > elHeight  )
	    		elHeight = $(this).parents("li").children("p.teaser").innerHeight()+90;
	    	
	        $(this).parents("li").animate({ 
	        	height: elHeight 
	        })
	        
	        $(this).parents("li").children("p.view-details").children().attr("src", "/extension/cdev_base/design/cdev_base/images/btn-close-details.png");
	        //$("img",this).attr("src", "/extension/cdev_base/design/cdev_base/images/btn-close-details.png");
    	}else{
    		$(this).parents("li").animate({
                height: 41
            });
            
            $(this).parents("li").children("p.view-details").children().attr("src", "/extension/cdev_base/design/cdev_base/images/btn-view-details.png");
            //$("img",this).attr("src", "/extension/cdev_base/design/cdev_base/images/btn-view-details.png");
    	}
    });
        
        
    
    // Side nav accordion
    
    /*$("#side-nav").accordion({
        fillSpace: true,
        collapsible: true,
        active: false
    });*/
    
    var height = 0;
    $("#side-nav").children("li").each(function(){
    	height = height + $(this).innerHeight();
    });
    
    vCenter = (303 - height) / 2;
    $("#side-nav").css("padding-top",vCenter+"px");

    // Large slides
    // $("#slides").cycle();
    
    
    //Res Widget Stuff
	$("form.quick-reservations").submit(function(){

		var arrival = $("select[name='arrival-m-y']").val().split("-");
		
		$("input[name='arrivalMonth']").val(arrival[0]);
		//$("input[name='arrivalDay']").val(arrival[1]);
		$("input[name='arrivalYear']").val(arrival[1]);
		
		var dDate = new Date();
		var dayShift = $("select[name='nights']").val();
		dDate.setFullYear($("input[name='arrivalYear']").val(),$("input[name='arrivalMonth']").val(),$("select[name='arrival-dd']").val());
		dDate.setDate(dDate.getDate()+(dayShift*1));

		//alert ('Check Out: ' + dDate);
		
		var dMonth = dDate.getMonth()
		var dDay = dDate.getDate()
		var dYear = dDate.getFullYear()
				
		$("form.quick-reservations").attr("action","http://www.fairmont.com/EN_FA/Reservations/ReservationRoom");
		$("input[name=hc]").val('SCP');
		$("input[name=na]").val($("select[name='adults']").val());
		$("input[name=nn]").val($("select[name='nights']").val());
		$("input[name=nc]").val($("select[name='children']").val());
		$("input[name=ad]").val($("select[name='arrival-dd']").val() +'/'+ $("input[name='arrivalMonth']").val()+'/'+ $("input[name='arrivalYear']").val());
		$("input[name=dd]").val(dDate.getDate() +'/'+ dDate.getMonth()+'/'+ dDate.getFullYear());
		
		//alert ($("input[name=ad]").val())
	});
    
    $("#emailSignUpForm input[type=checkbox]").each(function(){
    	$(this).click(function(){
    		if($(this).attr("name")=="all"){
    			if( $(this).attr("checked")==true ){
    				$("#emailSignUpForm input[type=checkbox]").attr("checked",true);
    			}else{
    				$("#emailSignUpForm input[type=checkbox]").attr("checked",false);
    			}
    		}else{
        		if( $(this).attr("checked")==false )
        			$("#emailSignUpForm input[name=all]").attr("checked",false);
    		}
        });
    });
	    
	   
});

Cufon.replace('h2, h5');

