// jquery.linkscrubber
$(document).ready(function(){$("a, button, input[type='submit']").bind("focus",function(){if(this.blur)this.blur()});});

// jquery.lazyload
(function($) {

    $.fn.lazyload = function()
    {
        /* ДВИГАЕМ СКРОЛЛ */
        var elements = this;
        $(window).bind("scroll", function(event)
        {
            var counter = 0;
            elements.each(function()
            {
                if ($(this).is(":visible") && !$.belowthefold(this) && !$.rightoffold(this)) $(this).trigger("appear");
            });
            /* Remove image from array so it is not looped next time. */
            var temp = $.grep(elements, function(element)
            {
                return !element.loaded;
            });
            elements = $(temp);
        });

        
        return this.each(function()
        {
            var self = this;
        
            /* TODO: use .data() instead of .attr() */
            $(self).attr("original", $(self).attr("src"));

            // Не загружаем изображения, которые скрыты или находятся вне экрана
            if ($(self).is(":hidden") || $.belowthefold(self) || $.rightoffold(self))
            {
                $(self).attr("src", "/img/diz/pixel.png");
                self.loaded = false;
            }
            else
            {
                self.loaded = true;
            }
            
            /* When appear is triggered load original image. */
            $(self).one("appear", function()
            {
                if (!this.loaded)
                {
                    $("<img>")
                        .attr("src", $(self).attr("original"))
                        .bind("load", function()
                        {
                            $(self).attr("src", $(self).attr("original"));
                            self.loaded = true;
                        });
                };
            });
        });

    };
    // Если изображение находится вне экрана возвращаем FALSE
    $.belowthefold = function(element)
    {
        var fold = $(window).height() + $(window).scrollTop();
        return fold <= $(element).offset().top;
    };
    $.rightoffold = function(element)
    {
        var fold = $(window).width() + $(window).scrollLeft();
        return fold <= $(element).offset().left;
    };
    
})(jQuery);

// Для загрузки картинки при открытии шторки клипа
function showImg(img)
{
    $(img).attr("src", $(img).attr("original"));
    $(img).css("visibility", "visible");
}

/** Логин **/
function openWindow()
{
    $("body").append("<div id='disabled'></div>");
    $('#windowb').fadeTo("fast", 0.5).fadeIn();
    $('#window').fadeIn();
}

function closeWindow()
{
    $('#windowb').fadeOut();
    $('#window').fadeOut();
    $("#disabled").remove();
    $("#loginmsg").remove();
}

function initLogin()
{
	var name = document.getElementById("a_name").value;
	if (name === '' || typeof(name) == "undefined")
	{
        alert("Введите имя пользователя!");
        $("#a_name").focus();
        return;
	}

	var pass = document.getElementById("a_pass").value;
	if (pass === '' || typeof(pass) == "undefined")
	{
        alert("Введите пароль!");
        $("#a_pass").focus();
        return;
	}
    $('#loginmsg').remove();
    $("#window").append('<div id="disabled-loading"></div>');

    $.post('/ajax/login.php', {a_name:name, a_pass:pass}, function(response)
    {
		switch(response)
		{
            case "enter":
                setTimeout("window.location = '"+document.location+"'", 400);
                break;

			case "limit":
                $('#disabled-loading').remove();
                $("#window form").html('<p>Вы исчерпали максимальное количество попыток входа.<br />Попробуйте зайти позже...</p><a href="#" onclick="closeWindow();return false" class="btn">Закрыть окно</a>');
                break;

			case "error":
                $('#disabled-loading').remove();
                $("#window").append('<div id="loginmsg">Неверный логин или пароль!</div>');
                break;
		}
	});
}

