﻿// flash animations

var flashSpeed = 1000;
var flashDelay = 6000;

var toggleFlash = function() { doFlash(false); }

var doFlash = function(init) {
    var flash = $('#flash');
    if (flash) {
        flash.unbind('click', toggleFlash);

        var height = flash.outerHeight();
        var border = parseInt(flash.css('border-bottom-width')) * 2;

        if (init == null || init)
            flash.css("top", -height);

        flash.animate({ top: "0" }, flashSpeed);
        setTimeout(function() {
            flash.animate({ top: border - height }, flashSpeed);
        }, flashDelay + flashSpeed);
        flash.click(toggleFlash);
    }
};


$(document).ready(doFlash);

// select submits
var SubmitOnSelect = function(selector) {
    //console.log("calling SubmitOnSelect");
    $(document).ready(function() {
        //console.log("running SubmitOnSelect");
        var select = $(selector);
        select.change(function() {
            //console.log("change event fired");
            select.parents('form').submit();
        });
    });
};

var ajaxFlash = function(flashData) {
    if (flashData != null && flashData != undefined) {
        $('#flash').remove(); // animate?
        var html = '<div id="flash" class="' + flashData.cls + '">' + flashData.message + '</div>';
        $('body').prepend(html);
        doFlash(true);
    }
};
