﻿var preloadImages = [
    "prevOn.png",
    "nextOn.png",
    "buttonBG_Over.gif",
    "ddlBG_Over.gif"
];
var themePath = "wp-content/themes/wordfolio/";

function hideAllDropdowns() {
    $(".jselect ul").hide();
    $(".jselect").css("z-index", "80");
    $(".jselect input").addClass("active");
}

function hideDropdown(base) {
    base.find("ul").hide();
    base.css("z-index", "auto");
    base.find("input").addClass("active");
}

function showDropdown(base) {
    base.find("ul").show();
    base.css("z-index", "100");
    base.find("input").removeClass("active");
}


$(function() {

    // preload
    for (var i = 0; i < preloadImages.length; i++)
        jQuery("<img>").attr("src", themePath + "images/001/" + preloadImages[i]);

    // handle search text
    $(".jtext").each(function() {
        $(this).data('caption', $(this).val());
    });

    $(".jtext").focus(function() {
        $(this).removeClass("blur");
        if ($(this).val() == $(this).data('caption'))
            $(this).val("");
        if ($(this).hasClass("password"))
            this.type = "password";
    });
    $(".jtext").blur(function() {
        if ($(this).val() == "" || $(this).val() == $(this).data('caption')) {
            $(this).val($(this).data('caption'));
            $(this).addClass("blur");
            if ($(this).hasClass("password"))
                this.type = "text";
        }
    });

    // search submit functionality
    $(".jsearch").click(function() {
        var searchField = $("#topsearch .keyword .jtext");
        var optionText = $("#topsearch .select input").val();
        var searchText = searchField.val();
        if (searchText == searchField.data('caption') || searchText == "") {
            alert("Please enter a search term first.");
            return false;
        }
        switch (optionText) {
            case 'Products':
                var optionUrl = '&cat=' + blog_id;
                break;
            case 'Hot Projects':
                var optionUrl = '&cat=' + portfolio_id;
                break;
            default:
                var optionUrl = '';
        }
        document.location = "/?s=" + searchText + optionUrl;
        return false;
    });

    // password functionality
    $(".jpassword .placeholder").focus(function() {
        $(this).hide();
        $(this).parent().find(".password")
            .show()
            .focus();
    });
    $(".jpassword .password").blur(function() {
        if ($(this).val() != '')
            return;
        $(this).hide();
        $(this).parent().find(".placeholder")
            .show();
    });

    // dropdown functionality
    $(".jselect input").attr("readonly", "readonly");
    $(".jselect input").click(function() {
        var base = $($(this).parents(".jselect").get(0));
        var isActive = $(this).hasClass("active");
        hideAllDropdowns();
        if (isActive)
            showDropdown(base);
    });
    $(".jselect li").click(function() {
        var base = $($(this).parents(".jselect").get(0));
        base.find("input").val($(this).text());
        hideAllDropdowns();
    });
});
