﻿$.fn.pause = function(n) {
    n = n || 1000;
    return this.queue(function() {
        var el = this;
        setTimeout(function() {
            return $(el).dequeue();
        }, n);
    });
};

String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }

$(document).ready(function() {

    $("#email_cover").css("opacity", "0.75");

    var mouseDown = false;

    $("#slider_handle").mousedown(function() {

        mouseDown = true;
        $("body").css("-moz-user-select", "none");
        $("body").bind("selectstart", function() { return false })

    });

    $(document).mouseup(function() {

        mouseDown = false;
        $("body").unbind("selectstart");
        $("body").css("-moz-user-select", "");

        if (!$.browser.msie) $("body").focus();

    });

    $(document).bind("mousemove", function(e) {
        
        if (mouseDown) {
            var x = e.pageX - $("#slider_holder").offset().left - 5;

            if ((x <= 80) && (x >= 10)) {
                $("#slider_handle").css("margin-left", x);
                initVista(false, null);
            }
        }

    });

    initVista(true, null);

    $("#search_txt").bind("keydown", function(e) {

        if (e.keyCode == "13") doSearch();

    });

    $("#email_txt").bind("keydown", function(e) {

        if (e.keyCode == "13") signup();

    });

    $(".socialIco").hover(
        function() {
            $(this).css("opacity", 1);
        },
        function() {
            $(this).fadeTo("fast", 0.5);
        }
    );

    $('#bannerHolder').cycle({
        delay: 3000,
        speed: 500,
        pause: 1
    });

});

function preFormatCmd(mode) {
    initVista(false, mode);
}

function openTextOnly() {
    $("#menuOuter, #header_controls, #bannerHolder").fadeOut("fast");
    $("#textOnlyOffHolder").fadeIn("fast");
}

function openTextOnlyOff() {
    $("#menuOuter, #header_controls, #bannerHolder").fadeIn("fast");
    $("#textOnlyOffHolder").fadeOut("fast");
}

function manualZoom(mode) {
    var v = parseInt($("#slider_handle").css("margin-left"));

    v = Math.round(v / 10) * 10;
    
    if(mode=='m') v = v - 10;
    else if(mode=='p') v = v + 10;

    if ((v < 90) && (v > 0)) {
        $("#slider_handle").css("margin-left", v);
        initVista(false, null);
    }
}

var mode = "nm";

function initVista(init, cmode) {
    var t = null;

    if (cmode != null) mode = cmode;

    if (init) {
        var cookie = $.cookie('henvista');

        if (cookie != null) {
            var a = cookie.split(';');
            t = a[0];
            mode = a[1];

            $("#slider_handle").css("margin-left", (t - 2) * 10);
        }
        else t = 6;
    }
    else {
        t = $("#slider_handle").css("margin-left");
        t = t.replace("px", "");
        t = Math.round(t / 10) + 2;

        $.cookie('henvista', null);
        $.cookie('henvista', t + ';' + mode, { expires: 10 });
    }

    switch (mode) {
        case "hc":
            setVista(t, "black", "#f7e01e", "underline");
            break;

        case "bw":
            setVista(t, "white", "black", "underline");
            break;

        case "nm":
            setVista(t, "", "", "");
            break;
    }
}

function setVista(x, col, bg, uh) {
    var gc = x * 2 + "pt";
    var lh = (x * 2) + 4 + "pt";
    var v = ((x * 2) + 4) * 1.1 + "pt";

    $("#main_content p, #main_content span,#main_content .footer, #main_content div").css("font-size", gc);
    
    $("#main_content p, #main_content span, h2, #main_content .footer, #main_content div").css("line-height", lh);

    $("#main_content h2").css("font-size", gc);

    $("#main_content h1").css("font-size", lh);
    $("#main_content h1").css("line-height", v);

    $("#main_content p, #main_content span, #main_content h2, #main_content h1, #main_content a, #main_content .footer, #main_content div").css("color", col);

    $("#main_content h2, #main_content h1").css("text-decoration", uh);

    $("body").css("background-color", bg);

    calendarFix();
}

function calendarFix() {
    $(".calendar .month span, .calendar .year span").css("font-size", "9pt");
    $(".calendar .date span").css("font-size", "22pt");
    $(".calendar div").css("line-height", "16pt");
}

function doSearch() {
    var q = $("#search_txt").val();
    window.location = "/search.aspx?q=" + encodeURI(q);
}

function signup() {
    $("#email_cover").show();
    $("#email_disp").html("");

    var href = document.location.pathname;

    $.get("/assets/includes/ajax-handler.aspx", { mode: "emailsignup", email: $("#email_txt").val(), ex: href },

    function(data) {

        $("#email_cover").fadeOut("fast");

        $("#email_p1").fadeOut(function() {
                
            if (data == "ok") {
                $("#email_disp").html("Thank you.");
                $("#email_p2").fadeIn();
            }
            else if (data == "e1") {
                $("#email_disp").html("Invalid email.");
                $("#email_p2").fadeIn().pause(1000).fadeOut(function() { $("#email_p1").fadeIn() });
            }
            else if (data == "e2") {
                $("#email_disp").html("Email already added.");
                $("#email_p2").fadeIn().pause(1000).fadeOut(function() { $("#email_p1").fadeIn() });
            }

        });
    });
}

////
//MENU SCRIPT
////

$(document).pngFix();

var menuPopup;
var isIE6 = ($.browser.msie && ($.browser.version == "6.0"));
var moving = 0;

$(document).ready(function() {

    $("#menu li").hover(function() {
        $(this).addClass('menuHover');
    }, function() {
        $(this).removeClass('menuHover');
    });

    $("#menu ul li.more a").hover(function() {
        $(this).next().show();
    }, function() {
        $(this).next().hide();
    });

    $("#menu ul li.more .children").hover(function() {
        $(this).show();
    }, function() {
        $(this).hide();
    });
});

/*
    $("#menu ul li a.more").mouseenter(
    function() {
        menuRemovePopup();
        if (moving == 0) {
            $(this).css('color', 'white');
            menuPopup = $(this).next();
            menuPopup.css('display', 'block');
            if (isIE6) menuPopup.parent().addClass('ie6Fix');
            menuPopup.find('a').css('width', menuPopup.width() - 20 + 'px');
        }
    }
    );

    $("#menu").mouseleave(menuRemovePopup);
    $("#menu a:not(.more, #menu ul ul li a)").mouseenter(menuRemovePopup);

    $("#menu a:not(#menu ul a)").mousemove(
        function(e) {
            var cx = e.clientX - $(this).offset().left;
            if (cx >= 250 && cx <= 278) menuDropOk = true;
            else menuDropOk = false;
        }
    );

    $("#menu a:not(#menu ul a, #menu ul ul li a)").click(function() {

        if (moving == 0 && menuDropOk) {

            var itemClicked = $(this).next();

            if (itemClicked.css('display') == 'block') {
                moving++;
                itemClicked.slideToggle(movinMinus);
            }
            else {

                $("#menu ul:not(#menu ul ul)").each(function() {

                    if ($(this).css('display') == 'block') {
                        moving++;
                        $(this).slideToggle(movinMinus);
                    }

                });

                moving++;
                itemClicked.slideToggle(movinMinus);
            }

            return false;
        }
        else return true;

    });

});

function movinMinus() {
    moving--;
}

function menuRemovePopup() {
    if (menuPopup) {
        if (isIE6) menuPopup.parent().css('marginBottom', '0px');
        menuPopup.prev().css('color', '#25247b');
        menuPopup.css('display', 'none');
    }
}
*/

////
//MENU SCRIPT - END
////

var signupHignlightRunning = false;
var signupHignlightI = 0;

function signupHignlightCmd() {

    if(!signupHignlightRunning)
    {
        signupHignlightIN();
        signupHignlightRunning = true;
        signupHignlightI = 0;
    }

}

function signupHignlightIN() {
    signupHignlightI++;
    $('#signupHighlight').fadeIn("fast", signupHignlightOUT);
}

function signupHignlightOUT()
{
    if (signupHignlightI < 1) {
        $('#signupHighlight').fadeOut("fast", signupHignlightIN);
    }
    else {
        $('#signupHighlight').fadeOut("fast");
        signupHignlightRunning = false;
    }
}
