﻿$(document).ready(function () {

    $('select').JellyfishCustomSelect();
    $('input:checkbox, input:radio').checkbox();
    //$.preloadCssImages();
    bind_form_events();
});
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);
    $("input.submit").after(function () { return $('<a></a>').attr({ "href": "", "class": "submit-btn btn forinput", "id": $(this).attr('id') }).html($(this).attr('value')); }).remove();

    $(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');
    });

}

function focus_form() {
    focussed_form = $(this).closest('form');
}

function unfocus_form() {
    focussed_form = null;
}

function submit_form() {
    $(focussed_form).trigger('submit');
}

$(document).ready(function () {

    //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("#");
    var loc = document.location.pathname;

    // finally, style menu links to this page
    $("li.parent a[href='" + loc + "']").parent().addClass('active-menu');

    // 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.');
        }
    });
});

function show_ajax_loader(container) {
    $(container).append('<div class="ajax-loader"></div>');
}

function hide_ajax_loader(container) {
    $(container).find('.ajax-loader').remove();
}

// Request a call back
$(document).ready(function () {

    var requestcallbacklink = $("a.request-a-call-back");
    $(requestcallbacklink).click(function (e) {
        e.preventDefault();
        e = $(this);
        e.trigger('blur');
        var msg_title = "Request a call back";
        var msg_msg = "";
        toggle_requestcallbackbtn();
        show_confirmationpopup(msg_title, msg_msg, 4);

    });

    function process_requestcallback(dialog) {
        // Enable the btn
        toggle_requestcallbackbtn();

        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.keyCode == 27) {
                    $(document).unbind('keyup');
                    $(document).unbind('keypress');
                    hide_confirmationpopup(dialog);
                }
            });
        });
    }

    function hide_confirmationpopup(dialog) {
        $(dialog).fadeOut().remove();
        $("#overlay").fadeOut().remove();
    }

});
