﻿$(document).ready(function() {

    $('#mainNav').superfish({

        autoArrows: false,
        speed: 0,
        delay: 500

    });

    $('#mainNav li:not(#mainNav li li)').mouseenter(function() {

        var w = $(this).find('ul').width();

        $(this).find('ul ul').css('marginLeft', w);

        $(this).find('ul a:not(ul ul ul a)').css('width', w - 20);

    });

    $('#mainNav li li').mouseenter(function() {

        var w = $(this).find('ul').width() - 20;

        $(this).find('ul a').css('width', w);

    });

    $('.trnLnk a').each(function() { $(this).parent().addClass('trnLnk') });

    var sChk = $('#mainOuter').attr('class');
    if (sChk == '') sChk = false;
    else sChk = true;

    $('#mainNav li').mouseenter(function() {

        var t = '#9474b4';

        if (sChk) {

            if ($('#mainOuter').attr('class') == 'Vpink') t = '#ef59a1';
            else if ($('#mainOuter').attr('class') == 'Vblue') t = '#00b9f2';
            else if ($('#mainOuter').attr('class') == 'Vgreen') t = '#a3d063';

        }
        else {

            if ($(this).hasClass('empLnk')) t = '#ef59a1';
            else if ($(this).hasClass('trnLnk')) t = '#00b9f2';
            else if ($(this).hasClass('jbsLnk')) t = '#a3d063';
        
        }

        $(this).children('a').stop().animate({ 'backgroundColor': t }, 100);

    });

    $('#mainNav li').mouseleave(function() {

        var t = '#662d91';

        if (sChk) {

            if ($('#mainOuter').attr('class') == 'Vpink') t = '#ec008c';
            else if ($('#mainOuter').attr('class') == 'Vblue') t = '#00aeef';
            else if ($('#mainOuter').attr('class') == 'Vgreen') t = '#8cc63f';

        }
        else {

            if ($(this).hasClass('empLnk')) t = '#ec008c';
            else if ($(this).hasClass('trnLnk')) t = '#00aeef';
            else if ($(this).hasClass('jbsLnk')) t = '#8cc63f';

        }

        $(this).children('a').stop().animate({ 'backgroundColor': t }, 300);

    });

    $('textarea').keyup(function() {

        var max = 350;

        if ($(this).val().length > max) {

            $(this).val($(this).val().substr(0, max));

        }

    });

    $('#emailTxt').Watermark('enter your email');

    $("#searchTxt").bind("keydown", function(e) { if (e.keyCode == "13") doSearch() });
    $("#emailTxt").bind("keydown", function(e) { if (e.keyCode == "13") emailSignup() });

    $('#bannerOuter').cycle({
    
        timeout: 6000,
        speed: 500,
        pause: 1
        
    });

});

function doSearch() {
    document.location = document.getElementsByTagName('base')[0].href + 'search.aspx?q=' + $('#searchTxt').val();
}

var emailWorking = false;

function emailSignup() {

    if (!emailWorking) {
        emailWorking = true;
        $('#emailTxt').blur();
        $('#emailCover').html('please wait...');

        $('#emailCover').fadeIn(function() {

            $.get("ajax-handler.aspx", { mode: "emailsignup", email: $("#emailTxt").val(), ex: '' }, emailHander);

        });
    }
}

function emailHander(data) {

    switch(data) {
        case "ok":
            $('#emailCover').html('thank you');
            break;

        case "2":
            $('#emailCover').html('email already added');
            $('#emailCover').pause(2000).fadeOut(function() { emailWorking = false });
            break;
            
        default:
            $('#emailCover').html('invalid email');
            $('#emailCover').pause(2000).fadeOut(function() { emailWorking = false });
            break
    }
}