/* AJAX ERROR */
$(document).ajaxError(function (request,settings,e) {
    alert('Error requesting URL: '+e.url);
});
/* URL ROUTER */
var Router = function (route,params) {
    //parametre
    if (typeof(params) == 'object') {
        var p = '';
        $.each(params,function (name,value) {
            if (p != '') {
                p += '&';
            }
            p += escape(name)+'='+escape(value);
        });
        return Router(route)+'?'+p;
    }
    else {
        return '/' + route;
    }
};
Router.route = function (route,params) {
    var url = Router(route,params);
    location.href = url;
}

/* Plugin na input hint */
jQuery.fn.inputHint = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('input[type=text]')) {
            jQuery.inputHintShow(self);
            self.focus(function () {
                jQuery.inputHintHide(this);
            }).blur(function () {
                jQuery.inputHintShow(this);
            }).closest('form').submit(function () {
                jQuery.inputHintHide(self);
                return true;
            });
        }
    });
    return this;
};
jQuery.inputHintShow = function (inpt) {
    inpt = jQuery(inpt);
    if (inpt.val() == inpt.attr('title') || inpt.val() == '') {
        inpt.addClass('hint').val(inpt.attr('title'));
    }
}
jQuery.inputHintHide = function (inpt) {
    inpt = jQuery(inpt);
    inpt.removeClass('hint');
    if (inpt.val() == inpt.attr('title')) {
        inpt.val('');
    }
}

jQuery.fn.menuFadeEffect = function ( option ) {
    this.each(function () {
        var self = $(this);
        if (self.is('ul')) {
            
            var options = {
                sWidth   : (option.startWidth !== undefined)   ? option.startWidth   : '100'
               ,eWidth   : (option.endWidth !== undefined)     ? option.endWidth     : '100'
               ,sHeight  : (option.startHeight !== undefined)  ? option.startHeight  : '100'
               ,eHeight  : (option.endHeight !== undefined)    ? option.endHeight    : '100'
               ,sOpacity : (option.startOpacity !== undefined) ? option.startOpacity : 0
               ,eOpacity : (option.endOpacity !== undefined)   ? option.endOpacity   : 1
               ,speed    : (option.speed !== undefined)        ? option.speed        : 250
            };
            
            self.find('li').hover(
                function () {
                    var li = $(this);
                    if(li.has('div').length){
                        $.div = li.find('div').css({
                            opacity : options.sOpacity
                            ,width  : Math.round(li.width() * (options.sWidth / 100))
                           ,height  : Math.round(li.height() * (options.sHeight / 100))
                        });
                    } else {
                        $.div = $('<div></div>').html(li.html()).css({
                            opacity : options.sOpacity
                           ,width   : Math.round(li.width() * (options.sWidth / 100))
                           ,height  : Math.round(li.height() * (options.sHeight / 100)) 
                        });
                        li.append($.div);
                    }
                    $.div.stop().animate({
                            opacity : options.eOpacity
                           ,width   : Math.round(li.width() * (options.eWidth / 100))
                           ,height  : Math.round(li.height() * (options.eHeight / 100))
                        }
                        , options.speed
                    ); 
                }
               ,function () {
                    var li = $(this);
                    $.div.stop().animate({
                            opacity : options.sOpacity
                           ,width   : Math.round(li.width() * (options.sWidth / 100))
                           ,height  : Math.round(li.height() * (options.sHeight / 100))
                        }
                        , options.speed
                        , function(){ $(this).remove() }
                    );
                }
            );
        }
    });
    return this;
}

$(function(){
	$('input.hint').inputHint();
    $('#leftMenu li').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
    
    $("#menu ul").menuFadeEffect({ startHeight : 0 });
    $("#panel ul").menuFadeEffect({ startHeight : 0 });
});

