$(function() {
	//Documentation located in special.docs.js
	/*
	
	This had to be replaced by the code above since load() is not working properly in IE.
	Sometimes it stops execution of the script hence there is no rotation on the backdrop
	
	$("body.home div#backdrop img:first").load(function(){
		$("body.home div#backdrop img").css("display", "block").css("border", "10px solid red");
		$('body.home div#backdrop').cycle({ 
			fx:     'fade', 
			speed:  1000, 
			timeout: 5000 
		});	
	});*/
	
	$('.table-container').clickableCalendar({
		arriveInput: $('form.reload-dates input[name="arrival"]'),
		departInput: $('form.reload-dates input[name="departure"]'),
		preselect:true,
		generateSubmit:true
	});
	$('.calendar').calendarToggle();
	
	setTimeout(function() {
		$("body.home div#backdrop img").css("display", "block");
		$('body.home div#backdrop').cycle({ 
			fx:     'fade', 
			speed:  1000, 
			timeout: 5000 
		});	
	} ,2500)	
	
	$('#home-push-wrapper').cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 0, 
		prev: '#home-push-prev a',
		next: '#home-push-next a',
		before: function(){ $(".control-links").hide();},
		after: function(){ $(".control-links").fadeIn(); }
	});
	
	// Specials Sign Up form
		
	$("#email").focus(function() {
			
			if($(this).val() == "Enter email address"){
				$(this).val('');
				$("#fakesubmit").fadeOut();
				$("#package-sign-up-form").slideDown();
			}
			
     });
	
	$("#fakesubmit").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		
		$("#package-sign-up-form").slideDown();
		$(this).fadeOut();	
	});
	
	$("#close-sign-up").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		
		$("#package-sign-up-form").slideUp("fast");
		$("#fakesubmit").fadeIn();
	});
	
	// Video Modal Management
	
	if($("body").hasClass("video-cta")){
			
			var classes = $("body").attr("class");
			
			if (classes.indexOf("video-golf") > -1){
				linkCtaTxt = "Golf Video";
				filterID = 131;
				modalCtaTxt = "View Golf Packages";
				modalLink = "";
				modalVideo = "cv_iStiMnsk";
			} else if (classes.indexOf("video-family-ppc") > -1){
				linkCtaTxt = "A Better Place";
				filterID = "";
				modalCtaTxt = "view special packages";
				modalLink = "";
				modalVideo = "6LzIfmIX0Bo";
			} else if (classes.indexOf("video-family") > -1){
				linkCtaTxt = "Family Video";
				filterID = 134;
				modalCtaTxt = "View Family Packages";
				modalLink = "";
				modalVideo = "QJh3ct2yKPs";
			} else if (classes.indexOf("video-romance") > -1){
				linkCtaTxt = "Romance Video";
				filterID = 130;
				modalCtaTxt = "View Romance Packages";
				modalLink = "";
				modalVideo = "9eN93phLB5s";
			} else if (classes.indexOf("video-wedding") > -1){
				linkCtaTxt = "Wedding Video";
				filterID = 418;
				modalCtaTxt = "View Wedding Packages";
				modalLink = "";
				modalVideo = "UEBeUrlDpek";
			} else if (classes.indexOf("video-meeting") > -1){
				linkCtaTxt = "Meeting Video";
				filterID = 419;
				modalCtaTxt = "View Meeting Packages";
				modalLink = "";
				modalVideo = "RoslJGfQnQk";
			} else if (classes.indexOf("video-tennis") > -1){
				linkCtaTxt = "Tennis Video";
				filterID = 132;
				modalCtaTxt = "View Tennis Packages";
				modalLink = "";
				modalVideo = "y1TDvurp7I8";
			} else if (classes.indexOf("video-condo") > -1){
				linkCtaTxt = "Homes & Condos video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalLink = "";
				modalVideo = "YmzZNH2wSXI";
				if (BTpodID == 113){ //meetings POD
					modalLink = "/sc-meeting-packages.php";
					modalVideo = "4VL3Q5psPjc";
				} else if (BTpodID == 111){ //weddings POD
					modalLink = "/south-carolina-wedding-offers.php";
					modalVideo = "4VL3Q5psPjc";
				}
			} else if (classes.indexOf("video-village") > -1){
				linkCtaTxt = "The Village Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalLink = "";
				modalVideo = "zVCF4LQ7omw";
				if (BTpodID == 113){ //meetings POD
					modalLink = "/sc-meeting-packages.php";
					modalVideo = "uzredekKodw";
				} else if (BTpodID == 111){ //weddings POD
					modalLink = "/south-carolina-wedding-offers.php";
					modalVideo = "uzredekKodw";
				}				
			} else if (classes.indexOf("video-board") > -1){
				linkCtaTxt = "The Boardwalk Inn Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalLink = "";
				modalVideo = "9MAqchriRAY";
				if (BTpodID == 113){ //meetings POD
					modalLink = "/sc-meeting-packages.php";
					modalVideo = "ayrh9BdlnEU";
				} else if (BTpodID == 111){ //weddings POD
					modalLink = "/south-carolina-wedding-offers.php";
					modalVideo = "ayrh9BdlnEU";
				}					
			} else if (classes.indexOf("video-resort") > -1){
				linkCtaTxt = "The Resort Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalLink = "";
				modalVideo = "6mkf8Ah34hE";
				if (BTpodID == 113){ //meetings POD
					modalLink = "/sc-meeting-packages.php";
					modalVideo = "dicydxNC2Cg";
				} else if (BTpodID == 111){ //weddings POD
					modalLink = "/south-carolina-wedding-offers.php";
					modalVideo = "dicydxNC2Cg";
				}					
			} else if (classes.indexOf("video-pavillion") > -1){
				linkCtaTxt = "The Sweetgrass Pavilion Video";
				filterID = "";
				modalCtaTxt = "View Meeting Packages";
				modalLink = "/sc-meeting-packages.php";
				modalVideo = "ex7XH1CNVPk";
			} else if (classes.indexOf("video-paviweddings") > -1){
				linkCtaTxt = "The Sweetgrass Pavilion Video";
				filterID = "";
				modalCtaTxt = "View Wedding Packages";
				modalLink = "/south-carolina-wedding-offers.php";
				modalVideo = "ex7XH1CNVPk";
			}else if (classes.indexOf("video-croquet") > -1){
				linkCtaTxt = "Croquet Lawn Video";
				filterID = "";
				modalCtaTxt = "View Wedding Packages";
				modalLink = "/south-carolina-wedding-offers.php";
				modalVideo = "FS4DbmvNiNc";
			} else if (classes.indexOf("video-catering") > -1){
				linkCtaTxt = "Catering Video";
				filterID = "";
				modalCtaTxt = "View Wedding Packages";
				modalLink = "/south-carolina-wedding-offers.php";
				modalVideo = "osl9MUb8h1Y";
			} else if (classes.indexOf("video-business") > -1){
				linkCtaTxt = "Catering Video";
				filterID = "";
				modalCtaTxt = "View Meeting Packages";
				modalLink = "/sc-meeting-packages.php";
				modalVideo = "osl9MUb8h1Y";
			} else if (classes.indexOf("video-facility") > -1){
				linkCtaTxt = "Boardwalk Inn Facility";
				filterID = "";
				modalCtaTxt = "View Meeting Packages";
				modalLink = "/sc-meeting-packages.php";
				modalVideo = "CJgu3utRyk8";
			}
			
			if (linkCtaTxt != undefined){
				$("#content-wrapper").prepend("<div id='video-cat'><a href='http://www.youtube.com/watch?v="+modalVideo+"' type='"+filterID+"'  modalLink='"+modalLink+"' name='"+modalCtaTxt+"' class='video-launch'>"+linkCtaTxt+"</a></div>");
			}
	}
	
	if($("#video-modal-wrapper").get(0)){
		$("#content-wrapper").prepend("<div id='video-cat'><a href='#' class='video-modal-launch'>Charleston Video</a></div>");
	}
	
	
	// Packages Filter
	
	var currentIds = new Array("136", "134", "130", "131", "135", "132", "419", "219", "418", "696");
	var currentFilter = new Array();
	//var activeChannel = "<?php echo $pod_map[$PAGECONTENT->pod_id]['specials']?>"; MOVED TO js.php
	
	function filterPackages(id){
		
	}
	
	if($("#package-filter").get(0)){
		
		var podClass = $("#package-filter").attr("class");
		var podID = podClass.split(' ');
		
		if(podClass){
		// POD
			
			$("#packages-wrapper .package-wrapper").hide();
			$("#package-filter ul li").removeClass("active");
			
			$.each(podID, function(index, value) {
				$("#packages-wrapper ."+value).show();
				$("#package-filter a."+value).parent().addClass("active");
			});
			
		}else{
		//Default
		
			$("#packages-wrapper .package-wrapper").not('.permalink').hide();
			$("#package-filter ul li").removeClass("active");
			$("#packages-wrapper .136").show();
			$("#package-filter a.136").parent().addClass("active");
		}
	}
	
	// Filter pseudo check box
	$("#package-filter a").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		
		$(this).parent().toggleClass("active");
		$("#packages-wrapper .package-wrapper").hide(); // hide all packages
		
		// We add or remove this ID from the filter
		if($.inArray($(this).attr("class"), currentFilter) == "-1"){
			currentFilter.push($(this).attr("class"));
		}else{
			currentFilter[$.inArray($(this).attr("class"), currentFilter)] = null;
		}	
		
		$.each(currentIds, function(index, value) { 
			if($.inArray(value, currentFilter) == "-1"){
				$("."+value).show();
			}else{
			}
		});
	});

	/* Unit search results */
	$('.ratings-modal').click(function(e) {
	    e.preventDefault();
	    var $modal = $($(this).attr('href'));
	    if ( $modal.length ) {
			$modal.show();
	    }
	    $modal.siblings().hide()
	});

	$('#modal-wrapper .close').click(function(e) {
	    e.preventDefault()
	    $('#modal-wrapper div:visible').hide();
	});
	
	/* My Wild Dunes */
	
	if($("#my-gallery-wrapper").get(0)){
			
		$("#my-gallery-tabs ul li a").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			
			$("#my-gallery-tabs ul li").removeClass("active");
			$(this).parent().addClass("active");
			$("div.my-gallery").removeClass("active");
			$($(this).attr("href")).addClass("active");
			
		});
	}
	
	
	/* Quaternary nav fix */
	if($("div#sidebar div#sidenav ul li ul li.current ul li.current ul li.current a").get(0)){
		$("div#sidebar div#sidenav ul li ul li.current ul li.current ul li.current a").parent().parent().prev().css("background-color", "white");
	}
	
	//Help text
	$(".form-help").hide();
	$(".help-text").each( function() {
		var x = $(this);
		$("a.help-link", x).toggle( function() {
			$(".form-help", x).slideDown("slow");
		}, function () {
			$(".form-help", x).slideUp("slow");
		});
	}); 

	if($("#my-video-playlist").get(0)){
			
		$("#my-video-playlist ul li a").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			
			$("#my-video-playlist ul li").removeClass("current-video");
			$(this).parent().addClass("current-video");
			$("#my-video-player").empty();
			$('#my-video-player').html('<iframe width="590" height="436" src="http://www.youtube.com/embed/'+$(this).attr("id")+'" frameborder="0" allowfullscreen></iframe>');
		});
	}
	
	
	/* resort map */
	
	$('#resort-map-modal').jqm({toTop: true, trigger: 'a.resort-map-open'});	
	
	/* video modal*/
	
	$('.video-modal-wrapper').jqm({toTop: true, trigger: 'a.video-modal-launch'});
	$('.video-modal-single').jqm({toTop: true});
	$('.video-modal-wrapper').jqmAddClose($('a.close-video'));
	$('.video-modal-single').jqmAddClose($('a.close-video'));
			
	$(".video-playlist ul li a").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		$(".video-playlist ul li").removeClass("current-video");
		$(this).parent().addClass("current-video");
		$("#video-player-iframe").empty();
		$('#video-player-iframe').html('<iframe width="640" height="396" src="http://www.youtube.com/embed/'+$(this).attr("id")+'" frameborder="0" allowfullscreen></iframe>');
	});
	
	$('a.close-video').click(function(e){
		$("body.home div#backdrop").cycle("resume");
	});
	
	$('a.video-launch').click(function(e){
		e.preventDefault();
		
		var modalText = null;
		var modalLink = null;
		
		if($(this).is('[name]')){
			//$("#video-modal-cta").text($(this).attr("name")).attr("href", "/south-carolina-packages.php?filter="+$(this).attr("type"));
			modalText = $(this).attr("name");
		}else{
			//$("#video-modal-cta").text("View Packages").attr("href", "/south-carolina-packages.php");
			modalText = "View Packages";
		}
		
		
		if($(this).is('[modalLink]')){
			modalLink = $(this).attr("modalLink");
		}else{
			if($(this).is('[type]')){
				modalLink = "/south-carolina-packages.php?filter="+$(this).attr("type");
			}else{
				modalLink = "/south-carolina-packages.php";
			}
		}
		
		
		$("#video-modal-cta").text(modalText).attr("href", modalLink);
		
		$("#video-player-single-iframe").empty();
		
		var videoURL = $(this).attr("href");
		var youtubeID = videoURL.split("=",2);
		var youtubeDesc = $(this).attr("rel");
		
		if (youtubeID[1] == undefined){ //new format
			youtubeID = videoURL.split("/");
		}
		
		var thisvideoID = youtubeID[(youtubeID.length - 1)];
		
		$('#video-player-single-iframe').attr('title',youtubeDesc).html('<iframe width="640" height="390" src="http://www.youtube.com/embed/'+thisvideoID+'" frameborder="0" allowfullscreen></iframe>');
		$("body.home div#backdrop").cycle("pause");
		$('.video-modal-single').jqmShow();
	});
	
	$('.video-modal .close-video').click(function(e) {
	    e.preventDefault()
	    $('#modal-wrapper div:visible').hide();
	});
	

    // Isle of Palms Rental Program form
    $('a#iop-add').click(function(){
        var wrap = $('#iop-wrapper');
        var item = $('#iop-wrapper div:first-child').clone();
        var count = $('#iop-wrapper div').size();
        item.children('input').attr('id','iop-'+count);
        item.children('input').attr('value','');
        item.children('input').attr('class','textfield');
        item.children('a').attr('rel',count);
        
        item.children('a').click(function(){
            count1 = $('#iop-wrapper div').size();
            if(count1>1){
                item.remove();
            }else{
                alert('You must have atleast one rental address');
            }   
            return false;
        });
        
        wrap.append(item);
        return false;
        
    });
    
    $('a#iop-remove').each(function(){
        $(this).click(function(){
            var wrap = $('#iop-wrapper');
            var count = $('#iop-wrapper div').size();
            var item = $(this).parent();
            if(count>1){
                item.remove();
            }else{
                alert('You must have atleast one rental address');
            } 
            return false;
        });  
    });
    
    $('#iop-wrapper div:first-child').children('input').attr('class','textfield required');
	$('#iop-wrapper div:first-child label').addClass('ie-icon-required');
	
	/* resort map */
	
	$('#resort-map-modal').jqm({toTop: true, trigger: 'a.resort-map-open'});
	
	/* Blog Share a Post terms hide/show*/
	
	$('a.toggle').toggle(function(){ 
		$(this).parents('li').children('.terms').show();
		return false;
	},function(){ 
		$(this).parents('li').children('.terms').hide();
		return false;
	});
	
	/* Blog Comment */
	if($(".comment-wrapper").get(0)){
	
		$("a.post-comment-link").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			
			$(".comment-wrapper").toggle();
		});
		
	}
	// Booking console
	
	$('.property_type').change(function(e) {
		e.preventDefault();
		var $console = $(this).parents('form.reservations-console');
		if ( $(this).val() == 'villa' || $(this).val() == 'home' ) {
		    $('.field-gc', $console).hide();
		    $('.field-search', $console).show();
		    $console.attr('action', '/search-results.php');
			$('#booking-promos').hide();
			$('.buttons input', $console).attr('src','/images/buttons/btn-search-now.png')
			$('.button-search input', $console).attr('src','/images/buttons/btn-search.png')
		} else {
		    $('.field-gc', $console).show();
		    $('.field-search', $console).hide();
		    $console.attr('action', 'https://gc.synxis.com/rez.aspx');
			$('#booking-promos').show();
			$('.buttons input', $console).attr('src','/images/buttons/btn-console-book-now.png')
			$('.button-search input', $console).attr('src','/images/buttons/btn-book.png')
		}
	
	});
	
	

	$('.events #content').addClass('clear-float');

	if(getURLParameter("play") == "video"){
		
		//Find out if it is an interior page		
		if($("body.video-cta").get(0)){
		
			var thisVid = $("div#video-cat a.video-launch");
			
			$("#content-wrapper").prepend("<div id='video-cat'><a href='http://www.youtube.com/watch?v="+modalVideo+"' type='"+filterID+"'  modalLink='"+modalLink+"' name='"+modalCtaTxt+"' class='video-launch'>"+linkCtaTxt+"</a></div>");
						
			var modalText = thisVid.attr("name");
			var modalLink = thisVid.attr("modalLink");
					
			$("#video-modal-cta").text(modalText).attr("href", modalLink);
			
			$("#video-player-single-iframe").empty();
			
			var videoURL = thisVid.attr("href");
			var youtubeID = videoURL.split("=",2);
			var youtubeDesc = thisVid.attr("rel");
			
			if (youtubeID[1] == undefined){ //new format
				youtubeID = videoURL.split("/");
			}
			
			var thisvideoID = youtubeID[(youtubeID.length - 1)];
			
			$('#video-player-single-iframe').attr('title',youtubeDesc).html('<iframe width="640" height="390" src="http://www.youtube.com/embed/'+thisvideoID+'" frameborder="0" allowfullscreen></iframe>');
			
			//If interior show .video-modal-single
			$('body.video-cta .video-modal-single').jqmShow();
		}else{
			//if not select first video modal trigger inside #content-wrapper and open video modal 
						
			var modalText = "Packages";
			var modalLink = "/south-carolina-packages.php";
					
			$("#video-modal-cta").text(modalText).attr("href", modalLink);
			
			$("#video-player-single-iframe").empty();
			
			if (getURLParameter("id") != "null"){
				var videoURL = $('a.video-launch[href*="'+getURLParameter("id")+'"]').attr("href");
				var youtubeDesc = $('a.video-launch[href*="'+getURLParameter("id")+'"]').attr("rel");
			} else {
				var videoURL = $('a.video-launch').first().attr("href");
				var youtubeDesc = $('a.video-launch').first().attr("rel");				
			}
			var youtubeID = videoURL.split("=",2);
			
			if (youtubeID[1] == undefined){ //new format
				youtubeID = videoURL.split("/");
			}
			
			var thisvideoID = youtubeID[(youtubeID.length - 1)];
			
			$('#video-player-single-iframe').attr('title',youtubeDesc).html('<iframe width="640" height="390" src="http://www.youtube.com/embed/'+thisvideoID+'" frameborder="0" allowfullscreen></iframe>');
			$("body.home div#backdrop").cycle("pause");
			$('.video-modal-single').jqmShow();
				
		}
	}
	
	
	
	if($("#vacation-wrapper").get(0)){
		$(window)._scrollable(); // When scrolling the window
		var collapsedHeight = "300px";
		
		$("#vacation-wrapper").attr("listing_h", $("#vacation-wrapper").height()).css("height", collapsedHeight).show();
		
		$("a.replaceable-cta").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			
			if($(this).hasClass("expanded")){
			
				$('#vacation-wrapper').animate({
					height: collapsedHeight
					}, 1000, function() {
						$("a.replaceable-cta").removeClass("expanded").css("background-position", "0 0");
						$(".replaceable-title").text("View Complete List of Vacation Homes");
						window.scrollTo("0", "400");
				});
				
			}else{
			
				var expandHeight = $('#vacation-wrapper').attr("listing_h") + "px";
				
				$('#vacation-wrapper').animate({
					height: expandHeight
					}, 1000, function() {
						$("a.replaceable-cta").addClass("expanded").css("background-position", "0 -35px");
						$(".replaceable-title").text("Hide Complete List of Vacation Homes");
						
				});
			

			}
			
		});
	}
	
});
	

