﻿/// <reference path="~/scripts/jquery-ui-vsdoc.js" />
/// <reference path="~/scripts/jquery-1.4.1-vsdoc.js" />
window.currentLatestAddition = 0;
window.currentTip = 0;
window.pageSetupOpened = false;
swfobject.registerObject("chatSounds", "9.0.115", "expressInstall.swf");
$(document).ready(function () {
    ChangeSelectedLatestAddition(0);
    ChangeNextTip();
    $("#homeDragPanel").sortable({
        handle: '.homePanelTitle',
        placeholder: 'ui-helper',
        dropOnEmpty: true,
        update: function (event, ui) {
            SaveLayout();
        }
    });
    SetupLayoutEvents();
    $('.homeChatMessagesWrapper').scrollTop($('.homeChatMessagesWrapper').attr("scrollHeight"));
    $('#setupPageButton').click(function () {
        if (pageSetupOpened) {
            $('#setupPagePanel').slideUp(1200);
            pageSetupOpened = false;
        }
        else {
            $('#setupPagePanel').slideDown(1200);
            pageSetupOpened = true;
            if ($.browser.opera) {
                $('html, body').scrollTop($('form').height() - $(window).height() + 48);
            } else {
                $('html, body').animate({ scrollTop: $('form').height() - $(window).height() + 48 }, 1200);
            }
        }
    });
    $('#setupPageResetButton').click(ResetPanels);
});
var latestAdditions = window.setInterval(ChangeNextLatestAddition, 4500);
var homeTipsAutoUpdater = window.setTimeout(ChangeNextTip, 5500);
var homeQuickChatUpdater = window.setInterval(UpdateChatMessage, 10000);
function ChangeNextLatestAddition() {
    window.currentLatestAddition++;
    if (window.currentLatestAddition > 8) { window.currentLatestAddition = 0; }
    ChangeSelectedLatestAddition(window.currentLatestAddition);
}
function ChangeSelectedLatestAddition(idx) {
    var r = eval($('#fldLatestAdditions').val());
    if (!r) { return; }
    for (i = 0; i < r.length; i++) {
        $('.latestAddition' + (i + 1)).removeClass('selected');
        $('.latestAddition' + (i + 1)).unbind('click');
        $('.latestAddition' + (i + 1)).unbind('mouseover');
        $('.latestAddition' + (i + 1)).click(function () {
            window.clearInterval(latestAdditions);
            window.setTimeout(function () { window.clearInterval(latestAdditions); latestAdditions = window.setInterval(ChangeNextLatestAddition, 4500); }, 10000);
            for (n = 0; n < $('.latestAdditions > div').length; n++) {
                if ($('.latestAdditions > div').get(n) == this) { currentLatestAddition = n; }
            }
            ChangeSelectedLatestAddition(currentLatestAddition);
        });
        $('.latestAddition' + (i + 1) + ' div').css('background-image', 'url(' + r[i].itemSmallImage + ')').css('opacity', '0.5');
    }
    if (r.length > 0) {
        $('.latestAdditionLargeImage').css('background-image', 'url(' + r[idx].itemHomeImage + ')');
        $('.latestAddition' + (idx + 1)).addClass('selected');
        $('.latestAddition' + (idx + 1) + ' div').css('opacity', '1');
        $('.latestAdditionTitle').text(r[idx].itemName).css('opacity', '1').fadeIn(1500);
        $('.latestAdditionPlatform').html('<img alt="' + r[idx].itemPlatform + '" src="' + r[idx].itemPlatformImage + '" /> ' + r[idx].itemPlatform).animate({ opacity: 0 }, 50).delay(100).animate({ opacity: 1 }, 50);
        $('.latestAdditionDetailLink').html('<a href="' + r[idx].itemDetailLink + '">ver página deste jogo</a>').delay(50).animate({ opacity: 0 }, 50).delay(100).animate({ opacity: 1 }, 50);
        $('.latestAdditionRequestsLink').html(r[idx].usersRequestingItem + ' utilizadores à procura deste jogo').delay(100).animate({ opacity: 0 }, 50).delay(100).animate({ opacity: 1 }, 50);
        $('.latestAdditionAvailabilityLink').html(r[idx].usersWithItem + ' utilizadores têm este jogo para troka').delay(150).animate({ opacity: 0 }, 50).delay(100).animate({ opacity: 1 }, 50);
        $('.latestAdditionTrader').html('<a href="' + r[idx].latestTrader.tradeItemLink + '">última adição por ' + r[idx].latestTrader.sellerName + ' ' + r[idx].latestTrader.insertDate + '</a>').delay(200).animate({ opacity: 0 }, 50).delay(100).animate({ opacity: 1 }, 50);
    }
}
function ChangeNextTip(d) {
    window.clearTimeout(homeTipsAutoUpdater);
    if (d) {
        if (d == 'back') {
            currentTip--; currentTip--;
        }
        homeTipsAutoUpdater = window.setTimeout(ChangeNextTip, 11500);
    }
    else {
        homeTipsAutoUpdater = window.setTimeout(ChangeNextTip, 5500);
    }
    var tips = $(".homeTip");
    if (tips.length > 0) {
        if (currentTip >= tips.length) { currentTip = 0; }
        if (currentTip < 0) { currentTip = tips.length - 1; }
        for (i = 0; i < tips.length; i++) { tips.eq(i).fadeOut(250); }
        tips.eq(currentTip).delay(280).fadeIn(250);
        currentTip++;
    }
    $(".homeTipNavigateLeft").css({ opacity: 0.2 }).unbind('mouseover').unbind('mouseout').mouseover(function () { $(this).fadeTo(200, 1); }).mouseout(function () { $(this).fadeTo(200, 0.2); });
    $(".homeTipNavigateRight").css({ opacity: 0.2 }).unbind('mouseover').unbind('mouseout').mouseover(function () { $(this).fadeTo(200, 1); }).mouseout(function () { $(this).fadeTo(200, 0.2); });
}
function PostChatMessage(e) {
    if (e) { if (e.keyCode != 13) { return; } }
    window.clearInterval(homeQuickChatUpdater);
    UpdateChatMessage($('.homeChatMessageBox').val(), true);
    homeQuickChatUpdater = window.setInterval(UpdateChatMessage, 10000);
    $('.homeChatMessageBox').val('');
    $('.homeChatMessageBox').focus();
}
function UpdateChatMessage(msg, postMsg) {
    if ($('.homeChatMessagesWrapper').length > 0) {
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",
            url: trokasWebsiteRoot() + "chat.asmx/PostQuickChatMessage",
            data: '{ message : "' + (postMsg ? msg : '') + '", lastReceivedMessageId : "' + $('#homeChatLastReceivedMessageId').val() + '" }',
            success: function (response) {
                if (response.d.success == true) {
                    $('#homeChatLastReceivedMessageId').val(response.d.lastMessageID);
                    var isEven = !($('.homeChatMessage').last().hasClass("even"));
                    if (response.d.messages.length > 0) {
                        var chatsounds = swfobject.getObjectById("chatSounds");
                        if (chatsounds.playNewMessage) { chatsounds.playNewMessage(); }
                    }
                    for (i = 0; i < response.d.messages.length; i++) {
                        var m = response.d.messages[i];
                        $('.homeChatMessagesWrapper').append('<div class="homeChatMessage' + (isEven ? " even" : "") + '"><span class="' + (m.Online == true ? 'homeChatOnlineUser' : 'homeChatOfflineUser') + '">' + m.UserName + ':</span>' + m.Message + '</div>');
                        isEven = !isEven;
                    }
                    $('.homeChatMessagesWrapper').scrollTop($('.homeChatMessagesWrapper').attr("scrollHeight"));
                }
            },
            dataType: "json"
        });
    }
}
function SaveLayout() {
    var order = $("#homeDragPanel").sortable("serialize", { key: 'o' });
    $.ajax({
        type: 'POST',
        url: trokasWebsiteRoot() + 'default.aspx?setLayout=true&' + order
    });
}
function SetupLayoutEvents() {
    $('.homePanelTitle').unbind('mouseover').mouseover(function () { $(this).parent().addClass("homePanelHelper"); }).unbind('mouseout').mouseout(function () { $(this).parent().removeClass("homePanelHelper"); });
    $('.homePanelClose').unbind('click').click(function () { $(this).parent().parent().remove(); SaveLayout(); });
}
function OpenClosePanel(id, input) {
    if ($('#' + input).attr('checked')) {
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",
            url: trokasWebsiteRoot() + "webservices/HomeWidgets.asmx/GetPanel",
            data: '{ panelId : "' + id + '" }',
            success: function (response) {
                $("#homeDragPanel .clear").remove();
                $("#homeDragPanel").append(response.d).append('<div class="clear" />');
                SetupLayoutEvents();
                if (id == 1) {
                    ChangeNextTip(0);
                }
                SaveLayout();
            },
            dataType: "json"
        })
    }
    else {
        $('#sort_' + id).remove();
        SaveLayout();
    }
}
function ResetPanels() {
    $.ajax({
        type: "post",
        contentType: "application/json; charset=utf-8",
        url: trokasWebsiteRoot() + "webservices/HomeWidgets.asmx/GetResetedPanels",
        success: function (response) {
            $("#homeDragPanel div").remove();
            $("#homeDragPanel").append(response.d).append('<div class="clear" />');
            SetupLayoutEvents();
            ChangeNextTip(0);
            SaveLayout();
        },
        dataType: "json"
    })
}
