function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function css_browser_selector(u) { var ua = u.toLowerCase(), is = function (t) { return ua.indexOf(t) > -1 }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', m = 'mobile', h = document.documentElement, b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3.6') ? g + ' ff3 ff3_6' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('blackberry') ? m + ' blackberry' : is('android') ? m + ' android' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? m + ' j2me' : is('iphone') ? m + ' iphone' : is('ipod') ? m + ' ipod' : is('ipad') ? m + ' ipad' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' + (is('windows nt 6.0') ? ' vista' : '') : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);

$(document).ready(function () {

    $("ul.index>li").hover(function () {
        $(this).addClass("active");
    }, function () {
        $(this).removeClass("active");
    });

    $("a.submit").click(function (e) {
        var form = $("#" + $(this).attr("rel"));
        if (form.length > 0) {
            form.submit();
        } else {
            alert("Warning: Could not find form with ID " + $(this).attr("rel"));
        }
    });

    initSubscriptionForm();

    setTimeout(function () {
        initMatchingHeights();
        initCentered();
    }, 50);
});

function initSubscriptionForm() {
    $("#subscribe-error, #subscribe-info").hide();

    $("#subscribe").click(function (e) {
        e.preventDefault();
        $("#subscribe-error, #subscribe-info").hide();
        $("#subscribe-form input").attr("disabled", "disabled");
        var subscriptions = '';
        $("#subscribe-form input[name='subscriptions']").each(function (idx) {
            if ($(this).is(':checked'))
                subscriptions = subscriptions + $(this).val() + ", ";
        });

        $.post("/handlers/Subscribe.ashx", { nodeId: $("#subscribe-node").val(), name: $("#subscribe-name").val(), email: $("#subscribe-email").val(), subscriptions: subscriptions }, function (resp) {
            if (resp.success) {
                $("#subscribe-form div.form").hide();
                $("#subscribe-info").empty().html(resp.message.replace(/(\r\n|[\r\n])/g, "<br />")).show();
            } else {
                $("#subscribe-error").empty().html(resp.message.replace(/(\r\n|[\r\n])/g, "<br />")).show();
            }

            $("#subscribe-form input").attr("disabled", "");
        }, "json");
    });
}

function initCentered() {
    $(".centered").each(function () {
        var parent = $(this).parent();
        parent.css("position", "relative");
        $(this).css("position", "absolute").css("left", (parent.width() - $(this).width()) / 2 + "px").css("top", (parent.height() - $(this).height()) / 2 + "px");
    });
}

function initMatchingHeights() {
    var maxHeight = 0;
    $(".match-height").children().each(function (idx) {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });

    $(".match-height").children().height(maxHeight);

    maxHeight = 0;
    $(".match-height-each").each(function (idx) {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
    });

    $(".match-height-each").height(maxHeight);

    // this may affect the size of the page, so will have to rescroll to hash if present
    if (location.hash != '') {
        location.hash = location.hash;
    }
}
