$(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";
				modalVideo = "69_crx8tedg";
			} else if (classes.indexOf("video-family") > -1){
				linkCtaTxt = "Family Video";
				filterID = 134;
				modalCtaTxt = "View Family Packages";
				modalVideo = "QJh3ct2yKPs";
			} else if (classes.indexOf("video-romance") > -1){
				linkCtaTxt = "Romance Video";
				filterID = 130;
				modalCtaTxt = "View Romance Packages";
				modalVideo = "9eN93phLB5s";
			} else if (classes.indexOf("video-wedding") > -1){
				linkCtaTxt = "Wedding Video";
				filterID = 418;
				modalCtaTxt = "View Wedding Packages";
				modalVideo = "UEBeUrlDpek";
			} else if (classes.indexOf("video-meeting") > -1){
				linkCtaTxt = "Meeting Video";
				filterID = 419;
				modalCtaTxt = "View Meeting Packages";
				modalVideo = "RoslJGfQnQk";
			} else if (classes.indexOf("video-tennis") > -1){
				linkCtaTxt = "Tennis Video";
				filterID = 132;
				modalCtaTxt = "View Tennis Packages";
				modalVideo = "y1TDvurp7I8";
			} else if (classes.indexOf("video-condo") > -1){
				linkCtaTxt = "Homes & Condos video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "YmzZNH2wSXI";
			} else if (classes.indexOf("video-village") > -1){
				linkCtaTxt = "The Village Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "zVCF4LQ7omw";
			} else if (classes.indexOf("video-board") > -1){
				linkCtaTxt = "The Boardwalk Inn Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "9MAqchriRAY";
			} else if (classes.indexOf("video-resort") > -1){
				linkCtaTxt = "The Resort Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "6mkf8Ah34hE";
			} else if (classes.indexOf("video-pavillion") > -1){
				linkCtaTxt = "The Sweetgrass Pavilion Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "ex7XH1CNVPk";
			} else if (classes.indexOf("video-catering") > -1){
				linkCtaTxt = "Catering Video";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "osl9MUb8h1Y";
			} else if (classes.indexOf("video-facility") > -1){
				linkCtaTxt = "Boardwalk Inn Facility";
				filterID = "";
				modalCtaTxt = "View Packages";
				modalVideo = "CJgu3utRyk8";
			}
			
			if (linkCtaTxt != undefined){
				$("#content-wrapper").prepend("<div id='video-cat'><a href='http://www.youtube.com/watch?v="+modalVideo+"' type='"+filterID+"' 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 podID = $("#package-filter").attr("class");

		if(podID){
		// POD
			$("#packages-wrapper .package-wrapper").hide();
			$("#packages-wrapper ."+podID).show();
			$("#package-filter ul li").removeClass("active");
			$("#package-filter a."+podID).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();
		
		if($(this).is('[name]')){
			$("#video-modal-cta").text($(this).attr("name")).attr("href", "/south-carolina-packages.php?filter="+$(this).attr("type"));
		}else{
			$("#video-modal-cta").text("View Packages").attr("href", "/south-carolina-packages.php");
		}
		
		$("#video-player-single-iframe").empty();
		
		var videoURL = $(this).attr("href");
		var youtubeID = videoURL.split("=",2);
		
		if (youtubeID[1] == undefined){ //new format
			youtubeID = videoURL.split("/");
		}
		
		var thisvideoID = youtubeID[(youtubeID.length - 1)];
		
		$('#video-player-single-iframe').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 ( $type.val() == 'villa' || $type.val() == 'home' ) {
		    $('.field-gc', $console).addClass('hide-field');
		    $('.field-search', $console).addClass('show-field').removeClass('hide-field');
		    $console.attr('action', $console.attr('other'));
	} */	
	
	
});
	

