using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Media;
using digioz;
using System.IO;

public partial class ChatWindow : System.Web.UI.Page
{
    public string csRefresh = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        string lsChatText = string.Empty;

        if ((Session["username"] != null))
        {
            csRefresh = "<meta http-equiv=\"refresh\" content=\"5\">";
        }
        else
        {
            csRefresh = "<meta http-equiv=\"refresh\" content=\"300\">";
            return;
        }

        ChatMessageList loChatMessageList = ChatMessageManagement.GetChatMessageList();
        ChatUsersOnlineManagement.SaveChatUsersOnline(Session["username"].ToString());

        for (int liIdx = loChatMessageList.Count - 1; liIdx >= 0; liIdx--)
        {
            lsChatText += "[" + loChatMessageList[liIdx].Username + "]: " + loChatMessageList[liIdx].Message + "<br/>";
        }

        ChatUsersOnlineList loChatUsersOnlineList = ChatUsersOnlineManagement.GetChatUsersOnlineList();

        foreach (ChatUsersOnline loChatUsersOnline in loChatUsersOnlineList)
        {
            lblUsers.Text += "&nbsp;&nbsp;" + loChatUsersOnline.Username + "<br />";
        }

        litMessage.Text = lsChatText;

        //Check to the number of users against what is in the session variable. 
        if (Session["ChatUsersOnlineCount"] == null)
        {
            Session.Add("ChatUsersOnlineCount", loChatUsersOnlineList.Count);
        }
        else if (((int)Session["ChatUsersOnlineCount"]) < loChatUsersOnlineList.Count)
        {
            for (int li = ((int)Session["ChatUsersOnlineCount"]); li < loChatUsersOnlineList.Count; li++)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", @"playSound('Other/alarm.wav');", true);
            }
        }
        Session["ChatUsersOnlineCount"] = loChatUsersOnlineList.Count;

    }
}
