﻿$(document).ready(function () {

    if ($('#wpadder_foofoo').length > 0) {
        $('.slm-edit-ecb').css('padding-left', '40px');
        $('.slm-edit-ecb').last().css('padding-bottom', '30px');
    }
    $("table.gnw_table tr:even:not(:first)").addClass("odd");

    //search behvior
    $("input#search_btn").click(function (e) {
        e.preventDefault();
        if ($("input#query").val() != "") {
            doSearch(searchURL, $("input#query").val());
        }
    });

    $(document).keypress(function (e) {
        if (e.keyCode == 13 || e.charCode == 13) {
            if (e.target.nodeName.toLowerCase() == "input") {
                return false;
            }
        }
    });
    $(document).keypress(function (e) {
        if ((e.keyCode == 13 || e.charCode == 13) && e.target.nodeName.toLowerCase() == "input") {
            e.preventDefault();
            if ($("input#query").val() != "" && $("input#query").hasClass("has-focus")) {
                doSearch(searchURL, $("input#query").val());
            }
        }
    });

    $("input#query").focus(function () { $(this).addClass("has-focus"); });
    $("input#query").blur(function () { $(this).removeClass("has-focus"); });
    //end search behavior

    $("ul.list li a.doc").each(function () {
        var ext = $(this).attr("href");
        if (ext.lastIndexOf(".") > 0) {
            ext = ext.substr(ext.lastIndexOf(".") + 1, ext.length - ext.lastIndexOf("."));
            $(this).parent().append("<span>&nbsp;(" + ext + ")</span>");
        }
    });
    $("span.arrow").html("&raquo;");
    $("div#footer li:not(.nominiwindow) a, a.modal_dialog").click(function (event) {
        event.preventDefault();
        openDialog($(this).attr("href"), undefined, undefined, $(this).text());
    });

    $("a.modal_dialog_paper_finder").click(function (event) {
        event.preventDefault();
        openDialog($(this).attr("href"), 938, 600, $(this).text());
    });

    $("div#header ul li:not(li.right):last, ul#nav_2nd li:not(li.right):last, ul#nav_2nd li.right:first").addClass("last");

    $(".ms-wpTdSpace").empty();

    $(".printgallery a").click(function (event) {
        event.preventDefault();
        var url = "/_layouts/UPM.Internet.Generic.Portal/PrintImage/PrintImage.aspx?ItemId=" + $(this).attr("ID") + "&ListURL=" + $(this).attr("charset"); ;
        openDialog(url, undefined, undefined, $(this).attr("title"));
    });

    $(".dropdown_box").click(function () {
        $("#" + $(this).attr("rel")).toggle();
    });

    $(".dropdown a.close, .dropdown_large a.close").click(function () {
        $(this).parent().parent().toggle();
    });

    //form resetting
    $("a.reset").bind('click', function (e) {
        e.preventDefault();
        var f = $(this).attr("class");
        document.forms[0].reset();
        $("." + f + " select").each(function () {
            $(this).selectedIndex = 0;
        });
        if ($.validator) {
            var validator = $("." + f).validate();
            if (validator != null) {
                validator.resetForm();
            }
        }
    });

    $('#breadcrumb span span:last a').addClass('active');
    $('a.greenlink:last').attr('class', 'greenlink_last');
});

function clearFields(form) {
    if (!form || !form.elements) return;
    var re = /text|password/i;
    for (var i = 0, k = form.elements.length; i < k; i++) {
        if (form.elements[i].type && re.test(form.elements[i].type)) {
            form.elements[i].value = '';
        }
    }
}

function openDialog(url, w, h, title) {
    var options = {
        url: url,
        width: w,
        height: h,
        title: title
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function doSearch(url, q) {
    if (url != undefined) {
        if (url.indexOf(".aspx") > -1 && url.indexOf("http://") > -1) {
            window.location = url + "?k=" + q;
        }
    }
}

function openUnSubscribeDialog(redirectUrl) {
    window.location.replace(redirectUrl);
}
