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('span.type').text().trim() + ' ' + page.find('span.message').text().trim();
        var file = page.find('span.file:eq(1)').text().trim();
        
        console.group('An error was detected which prevented the loading of ' + url);
        console.error(message);
        console.log('File: ' + file);
        console.group('Stack Trace:');
        page.find('ol.trace').find('span.file').each(function() {
            console.log(jQuery(this).text().trim());
        });
        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();
        }
    );
    
    
    /*****************************************************************
    *   Login and Reg
    ******************************************************************/ 
    var defultVal = {
        'email[0]':     'e-mail',
        'email[1]':     'e-mail',
        'email[2]':     'e-mail',
        '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");
        }
    });
    
    /***************************************************
    *           important news
    ***************************************************/
    $(".important-news-close").click(function() {
        $(this).parent().fadeOut("slow");
        $('#important-news-close').hide();
        $.get("/news/setImportantNews");
    });
    
    /***************************************************
    *           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();
        }
    );

    
    
    /***************************************************
    *           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;
}


/***************************************************
*           Tooltip
***************************************************/
function overTooltip(obj) {
    $("#tooltip").text( $(obj).attr('alt') ).show();
}

function outTooltip() {
    $("#tooltip").hide();
}

function moveTooltip(e) {
    $("#tooltip").css({
        'left': e.clientX + 10 + 'px',
        'top':  e.clientY + 7 + document.documentElement.scrollTop + 'px'
    });
}

/***************************************************
*           flashVersion
***************************************************/
// JavaScript helper required to detect Flash Player PlugIn version information
function flashVersion() {
	var d, n = navigator, m, f = 'Shockwave Flash';
	if((m = n.mimeTypes) && (m = m["application/x-shockwave-flash"]) && m.enabledPlugin && (n = n.plugins) && n[f]) {d = n[f].description}
	else if (window.ActiveXObject) {try {d = (new ActiveXObject((f+'.'+f).replace(/ /g,''))).GetVariable('$version');} catch (e) {}} 
	return d ? d.replace(/\D+/,'') : 0;
}