jQuery(document).ready(function ($) {
    //---Search form---//
    var $searchInput = $("#site-search .search-input");
    $searchInput.addClass("empty").attr("value", "keyword search");
    $searchInput.focus(function () {
        $(this).removeClass("empty");
        if ($(this).attr("value") == "keyword search") {
            $(this).attr("value", "");
        }
    });
    $searchInput.blur(function () {
        if ($(this).attr("value") == "") {
            $(this).attr("value", "keyword search");
            $(this).addClass("empty");
        }
    });

    //---Show and hide top destinations---//
    var $destinationsBtn = $("#destinations-btn a");
    var $destinations = $("#hero-destinations");
    var $slideshow = $("#hero-slideshow");

    $destinationsBtn.click(function () {
        if ($destinationsBtn.parent().hasClass("hide-content") == true) {
            hideMap();
        } else {
            showMap();
        }
        return false;
    });

    function hideMap() {
        $destinationsBtn.parent().removeClass("hide-content").addClass("show-content");
        $slideshow.removeClass("off");
        $destinations.animate({
            width: "0px",
            left: "903px"
        }, 500, "easeOutCubic", function () {
            slideshow();
        });
    }
    function showMap() {
        $destinationsBtn.parent().removeClass("show-content").addClass("hide-content");
        $destinations.animate({
            width: "920px",
            left: "0"
        }, 500, "easeOutCubic", function () {
            $slideshow.addClass("off");
            clearInterval(timer);
        });
    }
    //set timer to hide map
    firstTimer = setTimeout(hideMap, 3000);


    //---Home page slideshow---//
    var $slides = $("#hero-slideshow li");
    var tracker = -1;
    var timer = null;

    function slideshow() {
        tracker++;
        //check to make sure your not at the end of the array
        if (tracker >= $slides.length) {
            tracker = 0;
        }
        //fade in the new slide
        $($slides[tracker]).fadeIn(1500);
        //if it's the first slide, fade out the last slide
        if (tracker != 0) {
            $($slides[tracker - 1]).fadeOut(1500);
        } else {
            $($slides[$slides.length - 1]).fadeOut(1500);
        }
        timer = setTimeout(slideshow, 6000);
    }

    //---Fancybox---//
    $("#tour-link-1").fancybox({
        'titleShow': false
    });
    $("#tour-link-2").fancybox({
        'titleShow': false
    });
    $("#find-agent-link").fancybox({
        'titleShow': false
    });

    //---Highlight Nav Items---//
    var pathArray = window.location.pathname.split('/');
    var firstPathArray = "/" + pathArray[1];
    var windowLocation = window.location.protocol + "//" + window.location.host;
    var windowPath = window.location.pathname;
    //alert(windowLocation);
    var mainNav = $("#nav li a");
    for (var i = 0; i < mainNav.length; i++) {
        var href = $(mainNav[i]).attr("href").replace(windowLocation, "");
        if (href == firstPathArray) {
            $(mainNav[i]).parent().addClass("current");
        }
    }
    var subNav = $("#sub-nav li a");
    for (var i = 0; i < subNav.length; i++) {
        var href = $(subNav[i]).attr("href").replace(windowLocation, "");
        if (href == windowPath) {
            $(subNav[i]).parent().addClass("current").parent("ul").parent("li").addClass("current");
        }
    }

    //---Show and hid sub nav on blog template---//
    if ($("body").hasClass("blog-template") == true) {
        if (windowPath.substring(0, 26) == "/_blog/Honeymoons_Articles") {
            $(".honeymoon-sub-nav").removeClass("hide");
        } else if (windowPath.substring(0, 36) == "/_blog/Destination_Weddings_Articles") {
            $(".dw-sub-nav").removeClass("hide");
        }
    }


    //---FO Search Form---//
    function setupFO() {
        //first name
        var firstNameInputs = [$("#find-form #api-find-firstname-one"), $("#find-form #api-find-firstname-two")];
        $(firstNameInputs).each(function () {
            $(this).addClass("empty").attr("value", "first name")
            $(this).focus(function () {
                if ($(this).attr("value") == "first name") {
                    $(this).removeClass("empty").attr("value", "");
                }
            });
            $(this).blur(function () {
                if ($(this).attr("value") == "") {
                    $(this).addClass("empty").attr("value", "first name");
                }
            });
        });
        //last name
        var lastNameInputs = [$("#find-form #api-find-lastname-one"), $("#find-form #api-find-lastname-two")];
        $(lastNameInputs).each(function () {
            $(this).addClass("empty").attr("value", "last name")
            $(this).focus(function () {
                if ($(this).attr("value") == "last name") {
                    $(this).removeClass("empty").attr("value", "");
                }
            });
            $(this).blur(function () {
                if ($(this).attr("value") == "") {
                    $(this).addClass("empty").attr("value", "last name");
                }
            });
        });

        var contentDivs = $("#find-agent .content");

        $("#find-agent #fo-find a").click(function () {
            $("#find-agent #fo-find").addClass("current");
            $("#find-agent #fo-browse").removeClass("current");
            showAndHide("#find-form");
            return false;
        });
        $("#find-agent #fo-browse a").click(function () {
            $("#find-agent #fo-browse").addClass("current");
            $("#find-agent #fo-find").removeClass("current");
            showAndHide("#browse-fos");
            //added by els
            getBrowseData();
            //end els			
            return false;
        });

        $("#find-agent #btn-search-now").click(function () {
            var val = $("#api-find-zip").val();
            if (val == "") {
                alert("You must fill in your zip code");
                return false;
            }

            showAndHide("#form-results");
            //added by els
            getSearchData();
            //end else
            return false;
        });
        /*moved by els
        $("#find-agent .btn-this-is-the-one").click(function () {
        showAndHide("#fo-search-details");
        return false;
        });
        end e*/
        $("#find-agent .btn-choose-this-one").click(function () {
            showAndHide("#fo-search-details");
            return false;
        });      
        $("#find-agent .find-different-link").click(function () {
            $("#find-agent #fo-browse").addClass("current");
            $("#find-agent #fo-find").removeClass("current");
            showAndHide("#browse-fos");
            //added by els
            getBrowseData();
            //end els			
            return false;
        });
        $("#find-agent .btn-save-and-return").click(function () {
            parent.$.fancybox.close();
            return false;
        });
    };

    setupFO();

});

//put here by els
function showAndHide(contentToShow) {
    $("#find-agent .content.show").removeClass("show").addClass("hide");
    $("#find-agent " + contentToShow).removeClass("hide").addClass("show");
    $.fancybox.resize();
}
//end put here by els

$(document).ready(function() {
 $('.api-photo-gallery').each(function() {
  $(this).attr('rel','slideshow1'); 
 });
});  

