﻿/// <reference path="../../JavascriptLibrary/jQuery/jquery.1.2.6.js"/>
/********** Constants & Globals **************/
var ELEMENT_HEIGHT = 50;
var objDivCurrentSelectedRoom;
/*********************************************/

function ScrollDown() {
    jQuery('#wrapper').animate({ scrollTop: document.getElementById("wrapper").scrollTop + ELEMENT_HEIGHT }, 250);
}

function ScrollUp() {
    jQuery('#wrapper').animate({ scrollTop: document.getElementById("wrapper").scrollTop - ELEMENT_HEIGHT }, 250);
}


function ChangeWindowTitle(newTitle) {
    document.title = newTitle;
}

/*
Room selection
*/
function SelectRoom(objDivRoomContainer) {

    objDivRoomContainer.className = 'itemSelected';

    if (typeof (objDivCurrentSelectedRoom) == 'undefined')
        objDivCurrentSelectedRoom = objDivRoomContainer;
    else {
        objDivCurrentSelectedRoom.className = 'item';
        objDivCurrentSelectedRoom = objDivRoomContainer;
    }

    var roomId = jQuery('#' + objDivRoomContainer.id).attr('roomid');
    var systemRoomId = jQuery('#' + objDivRoomContainer.id).attr('systemRoomId');
    EmbedChatRoomSWF(roomId);
    SetSelectedRoomIDToCookie(systemRoomId);

    ChangeWindowTitle(jQuery('#' + objDivRoomContainer.id).attr('roomname'));
    setRoomLayout(roomId);
}

function setRoomLayout(roomId) {
    if (typeof RoomLayout != 'undefined') {
        var room = RoomLayout.rooms[roomId];
        if (room == null)
            return;

        jQuery('#' + RoomLayout.titleAreaId).css('background-color',  room.backgroundColor);
        jQuery('#' + RoomLayout.titleLabelId).css('color', room.titleTextColor)
        jQuery('#' + RoomLayout.roomsCellId).css('border-color', room.borderColor); ;
    }
}

/*
Dynamic swf loading
*/
var EmbedChatRoomSWF = function(paramChatRoomID) {
    var params = {};
    params.wmode = "transparent";
    swfobject.embedSWF("http://widget.meebo.com/mcr.swf?showmedia=false&id=" + paramChatRoomID, "swfcont", "100%", "100%", "8.0.0", params);
}


/*
Sets the systemRoomId to the cookie for next time browsing the chat
*/
var SetSelectedRoomIDToCookie = function(systemChatRoomID) {
    if (typeof systemChatRoomID == 'string') {
        EBCookies.SetCookie(ChatCookieName + CTID, systemChatRoomID, 60);
    }
}


function AdjustScrollerHeight() {
    var x = document.getElementById("wrapper");
    if (x.scrollHeight - x.offsetHeight > 1) {
        document.getElementById("ScrollerDiv").style.display = '';
        jQuery('#ScrollerSpacer').height(x.offsetHeight - 30);
    }
    else {
        document.getElementById("ScrollerDiv").style.display = 'none';
    }
}

/*
XHTML workarround to fit the top element to the window height
to prevent scrollbars on IE
*/
var FitTopElementToWindow = function() {

    var h = jQuery(window).height() - 15;
    jQuery("body").css("height", h + "px");
    //30 - the "more rooms" title height
    jQuery("#wrapper").css("height", h - 48 + "px");


}  



//occures once the document is ready
jQuery(document).ready(function() {
    FitTopElementToWindow();
    var objSelectedRoom = jQuery('#room_' + ChatRoomID);
    //room was found on the rooms list
    if (typeof objSelectedRoom[0] == 'object') {
        SelectRoom(objSelectedRoom[0]);
    }
    else {
        //room was not found (may be due to sync issues on BLL objects)
        EmbedChatRoomSWF(ChatRoomID);
        SetSelectedRoomIDToCookie(ChatRoomSystemID);
    }
    AdjustScrollerHeight();
});


jQuery(window).bind('resize', function() {
    FitTopElementToWindow();
    AdjustScrollerHeight();
});    