jQuery.fn.logAjaxRequestError = function(url, responseText) {
    if (responseText.length < 500) {
        console.group('An error was detected which prevented the loading of ' + url);
        console.log(responseText);
        console.groupEnd();
    } else {
        var page = jQuery(responseText);
        var message = page.find('h3:eq(0)').text();
        var file = page.find('p:eq(1)').text();
        var error = page.find('p:eq(2)').text();
        console.group('An error was detected which prevented the loading of ' + url);
        console.error(message);
        console.log('>' + file);
        console.log(error);
        console.groupCollapsed('Stack Trace:');
        page.find('ul.backtrace>li').each(function() {
            console.log(jQuery(this).text());
        });
        console.groupEnd();
        console.groupEnd();
    }
};

jQuery().ajaxError(function(event, request, settings){
    jQuery().logAjaxRequestError(settings.url, request.responseText);
});

/*****************************************************************
*   
*       jQuery wraper
*
******************************************************************/

$(document).ready(function() {
    $("#btnSubmit").click(function() {
        var error = false;
        var countEmple = 3;
        
        $("#mail input:text").each(function () {            
            var mail = $(this).val();
            
            if (mail == 'e-mail') {
                mail = "";
                $(this).val('');
            }
            
            if (mail != "") {
                if (! /([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,4})/.test(mail)) {
                    $(this).css('backgroundColor', '#F9DF78');
                    error = true;
                } else {
                    $(this).css('backgroundColor', '#53DF73');
                }
            } else {
                --countEmple;
                $(this).css('backgroundColor', '#fff');
            }
        });

        if (error) {
            $("#msg-mail").text(general.email.valide);
        } 
        
        if (countEmple == 0) {
            $("#msg-mail").text(general.email.emply);
            error = true;
        }
        
        if (! error) {      
            $("#msg-mail").text(general.email.send);
            $.ajax({
                type: "POST",
                url: "/ajax/mail/linkfile",
                data: $(this).parent().serialize(),
                success: function(msg){
                    $("#msg-mail").text(general.email.ok);
                    
                    $("#mail input:text").each(function () {            
                        $(this).css('backgroundColor', '#fff');
                        $(this).val();
                    });
                }
            });
        }

    });
    
    var globalVar = {
        loginEnable: true,
        regEnable: true
    };
    
    $(".login").hover(
        function() {
            if (globalVar.loginEnable) {
                $(this).children(".login-title").addClass("hover");
                $(this).children(".login-tab").show();
            }
        },
        function() {
            if (globalVar.loginEnable) {
                $(this).children(".login-title").removeClass("hover-error");
                $(this).children(".login-title").removeClass("hover");
                $(this).children(".login-tab").hide();
            }
        }
    );
    
    $(".login input").focus(function() {
        globalVar.loginEnable = false;
    });
    
    $(".login input").blur(function() {
        globalVar.loginEnable = true;
    });
    
    
    
    $(".reg").hover(
        function() {
            if (globalVar.regEnable) {
                $(this).children(".reg-title").addClass("hover");
                $(this).children(".reg-tab").show();
            }
        },
        function() {
            if (globalVar.regEnable) {
                $(this).children(".reg-title").removeClass("hover-error");
                $(this).children(".reg-title").removeClass("hover");
                $(this).children(".reg-tab").hide();
            }
        }
    );
    
    $(".reg input").focus(function() {
        globalVar.regEnable = false;
    });
    
    $(".reg input").blur(function() {
        globalVar.regEnable = true;
    });
    
    
    $(".panel-local").hover(
        function() {
            $(".panel-local li:not(.selected)").show();
        },
        function() {
            $(".panel-local li:not(.selected)").hide();
        }
    );
    
    var defultVal = {
        'user[login]':	'e-mail',
        'user[pass]':	'pass',
		'reg[login]':		'e-mail',
        'reg[pass]':		general.reg.pass		
    };
	
    
    $(".text").focus(function() {
        var val = $(this).val();
        var key = $(this).attr("name");
        
        if (defultVal[key] == val) {
            $(this).val("");
        }
    });
    
    $(".text").blur(function() {
        var val = $(this).val();
        var key = $(this).attr("name");
        
        if (val == "") {
            $(this).val(defultVal[key]);
        }
    });
    
    $(".panel-select-upload :radio").click(function() {
        if ($.browser.msie) {
            window.location.href = $(this).parent().attr("href");
        }
    });
    
    $("#button").click(function() {
    
    });
    
    /***************************************************
    *           important news
    ***************************************************/
    $(".important-news-close").click(function() {
        $(this).parent().fadeOut("slow");
        $.get("/news/setImportantNews");
    });
    
    /***************************************************
    *           ftp
    ***************************************************/
    $("#create-new-ftp").click(function() {
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/create",
            dataType: "json",
            success: function(msg){
                if (msg['is_success']) {
                    redrawFtpUsersArea(msg['respont']);
                } else {
                    alert(msg['error']);
                }
            }
        });
    });
    
    function redrawFtpUsersArea(respont) {
        $('#ftp-usrs-area').empty();
        $.each(respont, function() {
            var li = "<tr id='ftp"+this.num+"' name='"+this.login+"'><td>"+this.login+" / "+this.pass+"</td><td><img class='ftp-del' src='/img/icon/cancel_small.png'></td></tr>";
            $('#ftp-usrs-area').append(li);
        });
        $('.ftp-del').bind('click', function(){
            deleteFtpUsr($(this).parent().parent().attr('id'), $(this).parent().parent().attr('name'));
        });
    }
    
    $('.ftp-del').click(function() {
        deleteFtpUsr($(this).parent().parent().attr('id'), $(this).parent().parent().attr('name'));
    });
    
    function deleteFtpUsr(fuid, funame) {
        if (!confirm(general.ftp.deleteAccount + funame + '?')) {return;}       
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/delete/"+fuid,
            dataType: "json"
        });
        $("#"+fuid).remove();
    }
    
    $(".ftp-move").click(function() {
        $(this).text(general.ftp.moveBegin);
        
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/moveFiles/"+ $(this).attr('id'),
            dataType: "json",
            success: function(res) {
                $('#'+res.id).text(general.ftp.moveEnd);
                
                if (res.is_success) {
                    alert(general.ftp.moveSuccessfully + res.data);
                } else {
                    alert("Error: "+res.error);
                }
            }
        });

    });
    
    /***************************************************
    *           Rule box
    ***************************************************/
    $("#panel-upload-rule").toggle(
        function () {                       
            $(this).prev().attr('src', '/img/main/upload-minus.gif');
            $("#panel-upload-rule-text").show();
        },
        function () {
            $(this).prev().attr('src', '/img/main/upload-plus.gif');
            $("#panel-upload-rule-text").hide();
        }
    );

    /***************************************************
    *           Premium account
    ***************************************************/
    $("#memory-link").click(function() {
        if ($("#upload-url").val() == "") {
            alert(general.premium.emply);
        } else {
            $(".authorization").load(
                "/user/urlfiles/memoryForm",
                {'url' : $("#upload-url").val()},
                function(){
                    $(this).show();
                    $('.memory-link').hide();
                    $(".ui-dialog").css({
                        'height': 85 + $('.ui-dialog').height()  + "px",
                        "overflow": "hidden"
                    });
                    $("#upload-add-url").css({
                        'height': 85 + $('#upload-add-url').height()  + "px",
                        "overflow": "hidden"
                    });
                }
            );
        }
    });
    
    /***************************************************
    *           Short url
    ***************************************************/
    $(".shorturl-box").click(function() {
        $(".shorturl-title").hide();
        $(".shorturl-link").show()
        $(".shorturl-link input").focus().select();
    });
    
});
	/***************************************************
    *           radiobuttons links
    ***************************************************/    
	function go(loc) {
		window.location.href = loc;
	}