﻿var currentItem = null;
var currentActiveClass = null;

function setSideItemDropdown()
{
    $('.side-item').css('cursor', 'pointer');
    
    $('.side-item > p').hover(function(e) {
        if($(this).parent().is(':first-child'))
            currentActiveClass = 'activeTop';
        else
            currentActiveClass = 'active';
        
        $(this).parent().addClass(currentActiveClass);    
    }, function(e) {
        if(currentItem != this) 
            $(this).parent().removeClass(currentActiveClass);
    }); 
    
    $('.side-item > p').click(setSideItemsClick);
}

function setSideItemsClick()
{
    var removeCurrentClass = null;
    currentItem = this;
    
    if($(this).parent().is(':first-child'))
        currentActiveClass = 'activeTop';
    else
        currentActiveClass = 'active';
        
    if(!$(this).parent().hasClass("border-bottom"))
    {
        // removes active state from current elements
        if($(this).parent().hasClass('activeTop'))
            removeCurrentClass = "activeTop";
        else
            removeCurrentClass = "active";
        
        $(this).children('span').css('margin-top', '-5px').children('img').css('margin-top', '0px');
        $(this).parent().removeClass(removeCurrentClass).addClass("border-bottom");
        $(this).parent().children('div').children('div:visible').slideUp('fast');
        
        currentItem = null;
    }
    else
    {
        // removes active state from current elements
        if($(this).parent().siblings('div').hasClass('activeTop'))
            removeCurrentClass = "activeTop";           
        else
            removeCurrentClass = "active";
        
        // sets the element slide and sets active state
        if($(this).parent().children("div").length)
        {
            $(this).parent().css('cursor', 'default').removeClass("border-bottom").addClass(currentActiveClass);
            $(this).parent().children('.dropdown-container').children('div').slideDown('fast');
            $(this).children(':eq(0)').css('margin-top', '-5px').children('img').css('margin-top', '-14px');
            
            setItemContainer($(this).parent().children('.dropdown-container').children('div'));
        }
        
        $(this).parent().siblings('div').children('p').children('span').css('margin-top', '-5px').children('img').css('margin-top', '0px');
        $(this).parent().siblings('div').removeClass(removeCurrentClass).addClass("border-bottom");
        $(this).parent().siblings('div').children('div').children('div:visible').slideUp('fast');
       
    }    
}

function setItemContainer(target)
{
    if($(target).children('.watermark').length)
        setWatermark();
        
    if($(target).children('#go-button-container').length)
    {
        $('.go-button').hover(function(){
            $(this).css('margin-top', '-24px');
        }, function() {
            $(this).css('margin-top', '0px');
        });
    }
}

function setWatermark()
{
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    $("#distance .text-zip").focus(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not 
            // something actually entered
            return $(this).val() == "" || $(this).val() == "Zip/Postal Code"

        }).removeClass("watermark").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    $("#distance .text-zip").blur(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("watermark").val("Zip/Postal Code");

    });
}
