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() {

    var globalVar = {
        loginEnable: true,
        regEnable: true
    };

    $(".login").hover(
        function() {
            if (globalVar.loginEnable) {
                $(this).children(".login-title").addClass("hover");
                $(this).children(".login-tab").show();
            }
        },
        function(event) {
            if (globalVar.loginEnable && $(event.target).attr('class')!='tabInside') {
                $(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(event) {
            if (globalVar.regEnable && $(event.target).attr('class')!='tabInside') {
                $(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");
    });

	/***************************************************
    *           unread_messages
    ***************************************************/
    $(".unread-messages-close").click(function() {
        $(this).parent().fadeOut("slow");
        $('#unread-messages-close').hide();
        $.get("/user/messages/setRead");
    });
	
    /***************************************************
    *           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
***************************************************/
$('.torrent_tool').bind('onmouseover',function () {
	$("#tooltip").text( $(this).attr('alt') ).show();
});

$('.torrent_tool').bind('onmouseout', function () {
    $("#tooltip").hide();
});

$('.torrent_tool').bind('onmousemove', function (e) {
    $("#tooltip").css({
        'left': e.clientX + 10 + 'px',
        'top':  e.clientY + 7 + document.documentElement.scrollTop + 'px'
    });
});

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;
}

/***************************************************
*           Работа с location
***************************************************/
/**
 * Получение get-параметров из адресной строки
 * @return Object Hash, где ключи - имена get-параметров, а значения - их величины
 */
function getVars() {
	var tmp = new Array();
	var param = {};
	var get = location.search; // строка GET запроса
	if (get == '') {
		return {};
	}
	tmp = (get.substr(1)).split('&'); // Разделяем переменные
	for(var i=0; i < tmp.length; i++) {
		var subArr = tmp[i].split('=');
		param[subArr[0]] = subArr[1]; // пары ключ(имя переменной)->значение
	}
	return param;
}
