var Util = {
	post: function(parameters, success, error, isNotProcessResponse) {
    	var url = window.location.href;
        var i = url.indexOf('#');
        if (i > 0) url = url.substring(0, i);
        if (typeof parameters == "object") parameters = $.param(parameters);
        $.ajax({
            type: 'POST',
            url: url,
            cache: false,
            beforeSend : function (xhr) {
                xhr.setRequestHeader('X-Ajax', '1');
            },
            data: parameters,
            success: function(data, textStatus, xhr) {
                // firefox and chrome native JSON parsers hang on certain JSON data, so use eval
                data = eval(data);
                if(!isNotProcessResponse) {
	                $(data).each(function(i, v) {
	                    if (v.type == 'c') {
	                        $('#' + v.id).html(v.html);
	                    }
	                });
                }
                if (success) success(data, textStatus, xhr);
            },
            error: error
        });
        return false;
    },
    _postForm : function(element, url, parameters, success) {
    	 var form = element.tagName == 'FORM' ? $(element) : $(element).parents("form:eq(0)");
         var params = form[0] ? form.serialize() : "";
         if (parameters) {
             if (params != "") {
                 params += '&';
             }
             params += $.param(parameters);
         }
         Util._post(url, params, success);
         return false;
    },
    /**
	 * Low level post
	 * 
	 * @param url
	 *            url
	 * @param param
	 *            param
	 * @param callback
	 *            callback
	 */
    _post: function(url, param, callback, type) {
    	if (!type) {
    		type = "POST";
    	}
        $.ajax({
            type: type,
            url: url,
            data: param,
            timeout: 30000,
            success: callback,
            error: function(xhr, code) {
                try {
                    if (xhr.status == 403) {
                        window.location = "/";
                    } else {
                    	Util.setStatus("Ошибка: сервер недоступен");
                    }
                } catch (e) {
                	Util.setStatus("Ошибка: сервер недоступен");
                }
            }
        });
    }, 
    reloadCurrentPage: function() {
    	var href = location.href;
    	href = href.replace("#", "");
    	location.href = href;
    },
    setLocation: function(url) {
    	location.href = url;
    },
    randomID: function(length) {
        length = length || 32;
        var id = "";
        for ( i = 0; i < length; i++ ) {
            id += Math.floor(Math.random() * 16).toString(16);
        }
        return id;
    }, 
    showGlass: function(content, onclose) {
        var glass = $("#glass");
        var wnd = $("<div class='window'></div>");
        // var panel = $(content.html());
        wnd.append(content.clone().show());
        glass.resize = function(animate) {
            var width = $(window).width() + $(window).scrollLeft();
            var height = $(window).height() + $(window).scrollTop();
            if (!animate) {
            	glass.css("height", height + 'px')
	                    .css("width", width + 'px');
            	wnd = glass.find(".window");
            	var top = (height - $(window).height() / 2 - wnd.height() / 2);
            	if (top < 0) {
            		top = 0;
            	}
            	wnd.css("left", ((width - wnd.width()) / 2) + 'px');
            	wnd.css("top",  top + 'px');
            } else {
            	var top = wnd.offset().top;
            	var newTop = ((height - wnd.height()) / 2);
            	if (newTop < 0) newTop = 0;
            	if (newTop < $(window).scrollTop()) newTop = $(window).scrollTop();
            	if (Math.abs(top - newTop) > 100) {
    	            glass.css("height", height + 'px')
                    	  .css("width", width + 'px');
	            	wnd.animate({
	            		'left': ((width - wnd.width()) / 2) + 'px',
	            		'top':  newTop + 'px'},
	            		'slow'
	            	);
	            }
            }
        };
        $(window).bind("resize", glass.resize);
        
        $(document).unbind("keydown").keydown(function(e) {
        	if(e.keyCode == 27 /* Esc */)
        	{
        		UtilMini.closeGlass();
        	}
        });
        glass.append(wnd);
        glass.show();
        glass.resize();
        if (onclose) {
        	glass.data('close', onclose);
        }
        return glass;
    },
    closeGlass: function() {
        var glass = $("#glass");
        var onclose = glass.data('close');
        if (onclose) {
        	onclose();
        	glass.data('close', null);
        }
        $(document).unbind('keydown', 'esc');
        glass.find(".window").remove();
        glass.hide();
        $(window).unbind('resize');
        //$(document).unbind("keydown").bind('keydown', 'esc', UtilMini.closeMenu);
        return false;
    },
    showLoadPanel: function() {
    	var loadPanel = $("#load-panel");
    	loadPanel.show();
    	loadPanel.center();
    },
    closeLoadPanel: function() {
    	var loadPanel = $("#load-panel");
    	loadPanel.hide();
    }
}

jQuery.fn.center = function () {
    this.css("position", "absolute");
    var top = ( $(window).height() - this.height() ) / 2 + $(window).scrollTop();
    if (top < 5) {
    	top = 5;
    }
    this.css("top",  top + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + "px");
    return this;
};
