Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

$(document).ready(function(){
    //zajaxovatění formulářů
    $(function () {
        // odeslání na formulářích
        $("form.ajax").livequery("submit",function (e) {
            
            $(this).ajaxSubmit(e);

            // zobrazení spinneru a nastavení jeho pozice
            e.preventDefault();
            if(e.pageX != undefined){
                $("#ajax-spinner").show().css({
                    position: "absolute",
                    left: e.pageX + 20,
                    top: e.pageY + 40
                });
            }
        });

        $("form.ajax :submit").livequery("click",function (e) {
            $(this).ajaxSubmit(e);

            // zobrazení spinneru a nastavení jeho pozice
            e.preventDefault();
            $("#ajax-spinner").show().css({
                position: "absolute",
                left: e.pageX + 20,
                top: e.pageY + 40
            });
        });
    });

    //efekt updatu snippetu
    jQuery.nette.updateSnippet = function (id, html) {
        $("#" + id).fadeTo("fast", 0.01, function () {
            $(this).html(html).fadeTo("fast", 1);
        });
    };

    //tocici se "ajax" kolecko
    $(function () {
        // vhodně nastylovaný div vložím po načtení stránky
        $('<div id="ajax-spinner"></div>').appendTo("body").ajaxStop(function () {
            // a při události ajaxStop spinner schovám a nastavím mu původní pozici
            $(this).hide().css({
                position: "fixed",
                left: "50%",
                top: "50%"
            });
        }).hide();
    });

    // zajaxovatění odkazů
    $("a.ajax").live("click", function (event) {
        event.preventDefault();

        $.get(this.href);

        // zobrazení spinneru a nastavení jeho pozice
        $("#ajax-spinner").show().css({
            position: "absolute",
            left: event.pageX + 20,
            top: event.pageY + 40
        });
    });

    //grid
    grid();
    
});

//zobrazeni hlasek
function showMessage(text, type)
{
    $.jGrowl(text, {theme: type});
}

//grid
function grid()
{
    //schovam vsechny ikony menu a menu
    $('.grid div.items').hide();
    $('.grid div.menu').hide();
    //menu posunu doprava
    $('.grid div.menu').css('margin-left', $('.grid td.menu').width() - 16 + 'px');
    //navesim handlery na sloupecky oznacene class="menu"
    $('.grid tr').hover(function(){
        //zobrazim ikonu menu
        if($(this).attr('class') != ''){
            $('.grid tr.' + $(this).attr('class') + ' div.menu').show();
        }
    }, function(){
        //skryju ikonu menu
         $('.grid div.menu').hide();
    });

    //zobrazeni menu
    $('.grid div.menu').hover(function(){
        $('.grid tr.' + $(this).parent().parent().attr('class') + ' div.items').show();
    }, function(){
        $('.grid div.items').hide();
    });
}

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
    //
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {                return true;
            }
        }
    }
     return false;
}
