﻿//<![CDATA[
$(document).ready(function () {

    /* CHAT */
    $(".chooseSmiley").live("mouseover", function () {
        $(".ChatSmieysHolder").fadeIn(100);
    });

    $(".ChatSmieysHolder").live("mouseleave", function () {
        $(".ChatSmieysHolder").fadeOut(100);
    });

    $(".ChatSmieysHolder img").live("click", function () {
        $(".ChatSmieysHolder").hide();
    });

    var toggleChat = $(".chat")
            .click(function (e) {
                if ($(e.target).hasClass("icon") || $(e.target).hasClass("chat")) {
                    $(".chat-info").toggle();


                    // Show or hide scroll buttons
                    // INIT
                    //checkScrollBtns();
                }
            });

    //    var aTimer;
    //    var mouseisdown = false;

    //    function checkScrollBtns() {

    //        if ($(".chat .scroller").length == 0) {
    //            $(".ScrollBtns").hide();
    //            return;
    //        }



    //        var topVal = $(".chat .scroller").css("top").replace(/[^-\d\.]/g, '');
    //        topVal = parseInt(topVal);
    //        if (Math.abs(topVal) > 0) { $("#chatScrollUp").show(); }
    //        else { $("#chatScrollUp").hide(); }
    //        if (($(".chat .scroller").height() + 60) > $("#ChatRoomWrapper").height()) { $("#chatScrollDown").show(); }
    //        else
    //        { $("#chatScrollDwn").hide(); }
    //    }

    //    function ScrollDown() {
    //        if ($(".chat .scroller").length == 0)
    //            return;

    //        var topVal = $(".chat .scroller").css("top").replace(/[^-\d\.]/g, '');
    //        topVal = parseInt(topVal);

    //        if (Math.abs(topVal) < ($(".chat .scroller").height() - $("#ChatRoomWrapper").height())) { //This is to limit the bottom of the scrolling - add extra to compensate for issues
    //            $("#ChatRoomWrapper").find(".scroller").stop().animate({ "top": topVal - 20 + 'px' }, 'fast');
    //            checkScrollBtns();
    //            if (mouseisdown)
    //                t = setTimeout(ScrollDown, 400);

    //        }
    //        else {
    //            clearTimeout(aTimer);
    //            checkScrollBtns();
    //        }
    //    }

    //    function ScrollUp() {
    //        if ($(".chat .scroller").length == 0)
    //            return;

    //        var topVal = $(".chat .scroller").css("top").replace(/[^-\d\.]/g, '');
    //        topVal = parseInt(topVal);
    //        var fromTop = 20;
    //        if (Math.abs(topVal) <= 20)
    //            fromTop = Math.abs(topVal);
    //        if (Math.abs(topVal) > 0) { //This is to limit the bottom of the scrolling - add extra to compensate for issues
    //            $("#ChatRoomWrapper").find(".scroller").stop().animate({ "top": topVal + fromTop + 'px' }, 'fast');
    //            checkScrollBtns();
    //            if (mouseisdown)
    //                aTimer = setTimeout(ScrollUp, 400);
    //        }
    //        else {
    //            clearTimeout(aTimer);
    //            checkScrollBtns();
    //        }
    //    }

    //    if ($("#chatScrollDwn").length > 0) {
    //        $("#chatScrollDwn").mousedown(function () {
    //            mouseisdown = true;
    //            ScrollDown();
    //        });
    //        $("#chatScrollDwn").mouseup(function () {
    //            mouseisdown = false;
    //        });
    //    }
    //    if ($("#chatScrollUp").length > 0) {
    //        $("#chatScrollUp").mousedown(function () {
    //            mouseisdown = true;
    //            ScrollUp();
    //        });
    //        $("#chatScrollUp").mouseup(function () {
    //            mouseisdown = false;
    //        });
    //    }

    //    $(".chatRoomLink").click(function () {
    //        $(".ScrollBtns").hide();
    //    });
    //    $(".chatBackToRoomLink").click(function () {
    //        $(".ScrollBtns").show();
    //    });


    $("span.chatUsersInRoomLink").live("mouseover mouseout", function (event) {
        if (event.type == "mouseover") {
            $(".chatUsersInRoom").show();
            $(".info-topOver").show();
        } else {
            $(".chatUsersInRoom").hide();
            $(".info-topOver").hide();
        }
    });
});


// Automatically sets the scroller position to the bottom position
// But only when new message arrive, so that one can scroll and read messages
var msgs = 0;
var scrollPosition = 0;

function checkScroller() {
    $(document).ready(function () {
        if ($('.ChatMsg:visible').length > 0) {
            var tmpMsgCnt = $('.ChatMsg').length;

            $('.ChatMsgs').scroll(function () {
                scrollPosition = $('.ChatMsgs').scrollTop();
            });

            if (msgs < tmpMsgCnt) {
                $('.ChatMsgs').scrollTop($('.ChatMsg').height() * (tmpMsgCnt + 1));
                msgs = tmpMsgCnt;
            }
            else {
                $('.ChatMsgs').scrollTop(scrollPosition);
            }
        }
        else
            msgs = 0;
    });
}

//]]>
