$(document).ready(function(){
    $(".anchor").live("hover", function(){
        $(this).children('.anchor-inner').fadeIn();
    });
    
    if ($(".anchorLeft").length == 1)
    {
        $(".anchorLeft").oneTime("2s", function()
        {
            $(this).children('.anchor-inner').fadeIn();
            if ($(".anchorRight").length == 1)
            {
                $(".anchorRight").oneTime("2s", function()
                {
                    $(this).children('.anchor-inner').fadeIn();
                    StartRefreshing();
                });
            }
            else
                StartRefreshing();
        });
    }
});
    
function StartRefreshing()
{
    if ($(".anchorLeft").length == 1)
        $(".anchorLeft").oneTime("5s", StartRefreshing1);
        
    if ($(".anchorRight").length == 1)
        $('.anchorRight').oneTime("10s", StartRefreshing2);
}

function StartRefreshing1()
{
    LeftRefresh($(".anchorLeft"));
    $(".anchorLeft").everyTime("10s", StartLeftRefreshing);
}

function StartRefreshing2()
{
    RightRefresh($(".anchorRight"));
    $(".anchorRight").everyTime("10s", StartRightRefreshing);
}

function StartLeftRefreshing()
{
    LeftRefresh($(this));
}
    
function StartRightRefreshing()
{
    RightRefresh($(this));
}

function RefreshOffer(anchor, type)
{
    var currentAnchor = anchor;
        
    $.post(
        '/ajax/offer/',
        {
            'type'      : type,
            'except'    : currentAnchor.children(".anchor-inner").children('input[@name="offerID"]').val(),
            'countryID' : COUNTRY_ID
        },
        function(data, status)
        {
            if (!OffersAJAXResponseHandler(data, status)) return;
            
            if (data.isNew == false)
            {
                currentAnchor.fadeOut('slow', function(){
                    currentAnchor.fadeIn('slow');
                });
            }
            else
            {
                currentAnchor.fadeOut('slow', function()
                {
                    currentAnchor.children(".anchor-inner").children("a").children("img").attr('src', data.offer.src);
                    currentAnchor.children(".anchor-inner").children("a").attr('href', data.offer.link);
                    currentAnchor.children(".anchor-inner").children('input[@name="offerID"]').attr('value', data.offer.offerID);
                    currentAnchor.css('left', data.offer.coordX + "px");
                    currentAnchor.css('bottom', data.offer.coordY + "px");
                    
                    currentAnchor.fadeIn('slow');
                });
            }
        },
        "json"
    );
}

function LeftRefresh(anchor)
{
    RefreshOffer(anchor, 'left');
}
    
function RightRefresh(anchor)
{
    RefreshOffer(anchor, 'right');
}

function OffersAJAXResponseHandler(data, status)
{
    if (status == 'success')
    {
        if (data.error)
        {
            $.jGrowl(data.error);
            return false;
        }
    }
    else
    {
        $.jGrowl("Ошибка запроса");
        return false;
    }
    return true;
}
