﻿$(document).ready(function () {
    /*
    * flags
    */

    var wloc = window.location.toString();
    var lhref = '';
    var setop = true;

    $('a.flag').each(function () {
        $this = $(this);
        lhref = $this.attr('href');
        setop = true;

        if (wloc.length > lhref.length) {
            if (wloc.substr(wloc.length - lhref.length) == lhref) {
                setop = false;
            }
        }

        if (setop) {
            $this.css({ opacity: 0.4 });
            $this.mouseenter(function () { $(this).css({ opacity: 1 }); });
            $this.mouseleave(function () { $(this).css({ opacity: 0.4 }); });
        }
    });

    /*
    * navbar
    */

    var twidth = 970;
    var lcount = 0;
    var lwidth = 0;
    var inbtw = 0;

    $('a.level1link').each(function () {
        lwidth += $(this).outerWidth();
        lcount += 1;
    });

    if (twidth > lwidth && lcount > 1) {
        inbtw = (twidth - lwidth) / (lcount - 1);
    }

    lcount = 0;

    $('a.level1link').each(function () {
        lcount += 1;
        if (lcount > 1) {
            $(this).css({ marginLeft: inbtw });
        }
    });

    /*
    * newsletter
    */

    if (typeof ($.fn.labelover) === 'function') {
        $('input#nlemail').labelover();
    }

    $('a#nlsubscribe').click(function () {
        var retval = "0";
        var animSpeed = 500;
        var email = $('input#nlemail').val();

        $.ajax({
            url: '/ajax/subscribe/' + email,
            type: 'POST',
            async: false,
            success: function (html) {
                retval = html.toString();
            },
            error: function () {
            }
        });

        $('div.nlsuberror').slideUp(animSpeed);
        $('div.nlsubpredone').slideUp(animSpeed);
        $('div.nlsubfailed').slideUp(animSpeed);
        $('div.nlsubsuccess').slideUp(animSpeed);

        if (retval == "-2") {
            $('div.nlsuberror').slideDown(animSpeed);
        }
        else if (retval == "0") {
            $('div.nlsubfailed').slideDown(animSpeed);
        }
        else {
            $('div.nlsub').slideUp(animSpeed, function () {
                if (retval == "-1") { $('div.nlsubpredone').slideDown(animSpeed); }
                if (retval == "1") { $('div.nlsubsuccess').slideDown(animSpeed); }
            });
        }
    });
});
