﻿Cufon.replace('.cufon')('h1', { fontFamily: 'HelveticaNeue' })('h2', { fontFamily: 'HelveticaNeue' })('h3', { fontFamily: 'HelveticaNeue' });
Cufon.replace('.HelveticaNeue', { fontFamily: 'HelveticaNeue' });
Cufon.replace('.FrutigerRoman', { fontFamily: 'FrutigerRoman' });
Cufon.replace('.FranklinGothic', { fontFamily: 'FranklinGothicBook' });

String.prototype.startsWith = function (s) { return this.indexOf(s) == 0; };

function bind_form_events() {

    /* SET FORM FOCUS ENTER SUBMIT OF FOCUSED INPUT  */
    var focussed_form;
    $('input').focus(focus_form);
    $('input').blur(unfocus_form);
    /*$('textarea').focus(focus_form);
    $('textarea').blur(unfocus_form);*/
    $('select').focus(focus_form);
    $('select').blur(unfocus_form);
    $('a.btn').focus(focus_form);
    $('a.btn').blur(unfocus_form);
    $('.select_bg').focus(focus_form);

    $(document).keypress(function (e) {
        if (e.keyCode == 13) {
            submit_form();
        }
    });
    $('a.preventDefault').click(function (e) {
        e.preventDefault();
    });

    $('a.btn.forinput').click(function (e) {
        e.preventDefault();
        $(this).closest('form').trigger('submit');
    });

    //footer scrolling/hide-show functionality
    function openSitemap() {
        $('#footer-top-left').removeClass("closed");
        $('#footer-top-left').addClass("open");
        $('#footer #footer-content').show();
        //$('#footer #footer-content').show("slide", { direction: "up" }, 1000);
    }

    function closeSitemap() {
        $('#footer-top-left').removeClass("open");
        $('#footer-top-left').addClass("closed");
        $('#footer #footer-content').hide();
        //$('#footer #footer-content').hide("slide", { direction: "up" }, 1000);
    };

    $('#footer-top-left').click(function (e) {
        e.preventDefault();
        // Remove all cookies to do with the site map first
        $.get("/ajax/removeuserattributetype", { attributetypeid: 5 });

        if ($('#footer-content').css('display') == 'none') {
            openSitemap();
            $.getJSON("/ajax/AddUserAttribute", { attributetypeid: 5, text: 'FOOTERSITEMAPVISIBLE', value: '1' }, function (data) { });
        }
        else {
            closeSitemap();
            $.getJSON("/ajax/AddUserAttribute", { attributetypeid: 5, text: 'FOOTERSITEMAPVISIBLE', value: '0' }, function (data) { });
        }
    });
}

function focus_form() {
    focussed_form = $(this).closest('form');
}

function unfocus_form() {
    focussed_form = null;
}

function submit_form() {
    $(focussed_form).trigger('submit');
}

$(document).ready(function () {

    //$('#home-quick-links a span').fadeTo('fast', 0);
    //$('#home-quick-links a').mouseover(function() { $(this).find('span').fadeTo('slow', 1); });
    //$('#home-quick-links a').mouseout(function() { $(this).find('span').fadeTo('slow', 0); });    

    //To apply specific styling to menu links to current page - first, Get rid of http://
    loc = document.location.href.substring(7);

    // Get rid of domain name
    loc = loc.substring(loc.indexOf("/"));

    // Get rid of fragment identifier
    var i = loc.indexOf("#");
    //if (i > -1) loc = loc.substring(0, i);
    var loc = document.location.pathname;

    // finally, style menu links to this page
    $("#TopMenuInner a[href='" + loc + "']").addClass('top-selected');
    $("#osLowerLinks a[href='" + loc + "']").addClass('left-selected');


    $.preloadCssImages();
    bind_form_events();

    //$('select').SelectCustomizer();

    $('select').JellyfishCustomSelect();
    //$('#search-content').css('display', 'none');
    //$('#next-step-numbers').css('display', 'none');

    $('input:checkbox, input:radio').checkbox();

});


function show_ajax_loader(container) {
    $(container).append('<div class="ajax-loader"></div>');
}

function hide_ajax_loader(container) {
    $(container).find('.ajax-loader').remove();
}

$(document).ready(function () {

    //$('#bookmark-page').jFav();

    // Add Printing Functionality    
    $('a.print-page').click(function (e) {
        e.preventDefault();
        window.print();
    });

    // add a "rel" attrib if Opera 7+
    if (window.opera) {
        if ($("a.bookmark-page").attr("rel") != "") { // don't overwrite the rel attrib if already set
            $("a.bookmark-page").attr("rel", "sidebar");
        }
    }

    $("a.bookmark-page").click(function (event) {
        event.preventDefault();
        var url = window.location;
        var title = document.title;

        if (window.sidebar != null) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if (window.external != null) { // IE Favorite
            window.external.AddFavorite(url, title);
        } else if (window.opera != null) { // Opera 7+
            return false;
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Press "CTRL + D" or "CMD + D" (for Mac) to add this page to your bookmarks.');
        }
    });
});


// Request a call back
$(document).ready(function () {

    // Request a call back
    var requestcallbacklink = $("a.request-a-call-back");
    $(requestcallbacklink).click(function (e) {
        //alert("show something!!!");
        e.preventDefault();
        e = $(this);
        e.trigger('blur');
        var msg_title = "Request a call back";
        var msg_msg = "";
        //if ((e).hasClass(':not(.disabled)')) {
            toggle_requestcallbackbtn();
            show_confirmationpopup(msg_title, msg_msg, 4);
        //}
    });

    function process_requestcallback(dialog) {
        // Enable the btn
        toggle_requestcallbackbtn();
        //bind_form_events();

        var ajax_loader_container = $(dialog).find('a.btn').parent();
        var error = $('<span />').html('Please complete all required fields correctly.').addClass('field-validation-error').fadeTo('fast', 0);

        $(dialog).find("input:hidden[name='msg.pagetitle']").val(document.title);
        $(dialog).find("input:hidden[name='msg.pageurl']").val(window.location);

        $('#frequestcallback').submit(function () {
            $(error).fadeTo('fast', 0);
            show_ajax_loader(ajax_loader_container);
            $.post("/ajax/requestcallback",
                $(this).serialize(),
	                function (data) {
	                    hide_ajax_loader(ajax_loader_container);
	                    if (data == "-1") {
	                        $(ajax_loader_container).append(error);
	                        $(error).fadeTo('slow', 1);
	                    }
	                    else {
	                        $("#frequestcallback").slideUp('slow', function () {
	                            var h = $("<p />").html('You have requested a callback.  We will get back to you as soon as possible.').hide();
	                            $(this).before(h);
	                            $(h).slideDown();
	                        });
	                    }
	                }
                 );

            return false;
        });
    }

    function toggle_requestcallbackbtn() {
        var title = "Request a call back";
        $(requestcallbacklink).fadeOut('slow', function () { $(this).html(title).attr('title', title).fadeIn('slow').toggleClass('disabled'); });
    }

    // Pop Up Boxes
    function show_confirmationpopup(title, message, type) {
        $.get("/ajax/ConfirmationPopUp", { title: title, message: message, type: type }, function (data) {

            $('#window-wrapper').append(data);
            var dialog = $("#confirmation-popup");

            bind_form_events();

            if (type == 4) process_requestcallback(dialog);
            if (type == 5) process_registerforupdates(dialog);

            var winH = $(window).height();
            var winW = $(window).width();

            //Set the popup window to center
            $(dialog).css('top', winH / 2 - $(dialog).height() / 2);
            $(dialog).css('left', winW / 2 - $(dialog).width() / 2);

            $("#overlay").fadeTo("slow", 0.5).fadeIn("slow", function () { $(dialog).fadeIn(500); });

            Cufon.replace("#confirmation-popup h2");

            $(dialog).find("a.close-btn").click(function (e) {
                e.preventDefault();
                hide_confirmationpopup(dialog);
            });
            $(dialog).click(function () {
                //hide_confirmationpopup(dialog);
            });
            $("#overlay").click(function () {
                hide_confirmationpopup(dialog);
            });
            $(document).keyup(function (e) {
                /*if (e.which == 13) {
                $(document).unbind('keyup');
                $(document).unbind('keypress');
                hide_confirmationpopup(dialog);
                }*/
                if (e.keyCode == 27) {
                    $(document).unbind('keyup');
                    $(document).unbind('keypress');
                    hide_confirmationpopup(dialog);
                }
            });
        });
    }

    function hide_confirmationpopup(dialog) {
        $(dialog).fadeOut().remove();
        $("#overlay").fadeOut().remove();
    }

});
