﻿$(document).ready(function () {

    //set menu item positions
    $(".menu").children("li").each(function () {
        var $$ = $(this),
            sub = $$.children("ul");

        //has sub?
        if (sub.length > 0) {
            $$.addClass("arrow"); //add arrow class
            $$.bind("mouseover", function () { //bind mouseover
                var pos = $$.position(),
                width = $$.width();
                sub.css("left", pos.left + width).css("top", pos.top)
            });
        }
    });

    //bind confirm deletes
    $("a.modal").live("click", function () {
        var t = $(this);
        rolf.modal({ url: t.attr("href"), title: t.attr("title"), width: parseInt(t.attr("width")), height: parseInt(t.attr("height")), resizable: false });
        return false;
    });

    //auto focus on first "focus" field 
    $(".focus").focus();
});

var growl = {};

growl.show = function (params) {
    //if message
    if (params.msg && params.msg.length > 0) {
        //remove previous messages
        $(".growl").remove();

        //create growl box
        var growlDiv = $("<div class='growl ui-corner-all' />");
        if (params.background) growlDiv.css("background", params.background); //change bg color?
        growlDiv.append(params.msg);

        //fade in. fade out after 5 seconds.
        $(document.body).append(growlDiv);
        growlDiv.fadeIn('slow', function () {
            setTimeout(function () { growlDiv.fadeOut('slow', function () { growlDiv.remove() }) }, 5000);
        });
    }
};

var rolf = {};

rolf.modal = function (options) {
    //defaults
    var target = $("<div/>");
    options.width = options.width || 700;
    options.height = options.height || 450;
    options.close = options.close || function (event, ui) { target.remove() };
    options.modal = true;
    options.resizable = options.resizable == null ? false : options.resizable;

    //if no url, fill html and show dialog
    if (!options.url)
        target.html(options.html).dialog(options);
    else { //ajax call
        rolf.ajax({ target: target, url: options.url, showErrorInTarget: true });
        target.dialog(options);
    }

    return false;
};

rolf.ajax = function (options) {

    var doLoader = true;

    //callbacks
    options.success = options.success || function (data) {
        doLoader = false;
        $(options.target).hide(0, function () { $(this).html(data).show() });
        $(".focus:visible:first").focus();
        if (options.closeModal)
            $(".ui-dialog-content").dialog("close");
        $(options.target).find(".validate").validate({ onkeyup: false, ignore: ':hidden' });
    };
    options.error = options.error || function (xhr, text, err) {
        doLoader = false;
        if (options.showErrorInTarget)
            $(options.target).html(xhr.responseText);
        else
            rolf.modal({ html: xhr.responseText });
    };

    //go
    $.ajax(options);

    //notify user of action after 1000ms
    window.setTimeout(function () {
        if (doLoader && options.target)
            (options.target).html("<img src='/content/images/loading.gif'/>");
    }, 1000);

    return false;
};

rolf.ajaxPost = function (options) {

    var form = $(options.form);

    //if form valid
    if (form.valid({ showErrors: true, focusInvalid: true })) {
        options.data = form.serialize();
        options.type = "POST";
        options.url = options.url || form.attr("action");
        rolf.ajax(options);
    }

    return false;

};

rolf.closeParent = function (e) {
    $(e).parents('.ui-dialog-content').dialog('close');
    return false;
};