var CHAT_REQUEST_ID="request_";
var existingRequestes=new HashTable();

function addChatRequest(request)
{
    if(isChatRequestAlreadyExist(request.getCollaborationId(),request.getSessionId())){
        //alert("is exist");
        return;
    }
    addHTMLChatRequest(request);
    divId=getChatDivId(request.getCollaborationId(),request.getSessionId());
    existingRequestes.put(divId,divId);
    modifyContainerStyleBottomToEmpty();
}

function addHTMLChatRequest(request)
{
    sessionId=request.getSessionId();
    collaborationId=request.getCollaborationId();
    name=request.getName();
    invitorId=request.getInvitorId();
    divId=getChatDivId(collaborationId,sessionId);
    var domwindow=document.createElement("div"); //create dhtml div
    domwindow.id=divId;
    domwindow.className="ChatRequest";
    var domwindowdata='';
    domwindowdata+="<br/><div class='Black'><center><b>"+name+" wants to chat with you.</b></center><br/></center></div>";
    domwindowdata+="<div class='Purple'>";
    domwindowdata+="<center><b><a href='javascript:;' onclick='joinChat("+collaborationId+","+sessionId+","+invitorId+");removeChatRequest("+collaborationId+","+sessionId+");'>Accept</a>";
    domwindowdata+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    domwindowdata+="<a href='javascript:;' onclick='cancelChatRequest("+sessionId+");removeChatRequest("+collaborationId+","+sessionId+");'>Decline</a></center>";
    domwindowdata+="</b></div>";
    domwindow.innerHTML=domwindowdata;
    document.getElementById("chatRequestsContainer").appendChild(domwindow);
}
function addRequestsCollection(requests)
{
    //TODO add the requests object using the method of add requests
    //alert("into addRequestsCollection size of requests="+requests.length);
    for(i=0;i<requests.length;i++){
//        sessionId=requests[i].getSessionId();
//        collaborationId=requests[i].getCollaborationId();
//        name=requests[i].getName();
        addChatRequest(requests[i]);
    }  
}

function removeChatRequest(collaborationId,sessionId)
{
    divId=getChatDivId(collaborationId,sessionId);
    removeRequest(divId);
}
function removeRequest(divId)
{
    //alert("div id to remove="+divId);
    child=$(divId);
    if(child==null || child=='null'){
        return;
    }
    document.getElementById("chatRequestsContainer").removeChild(child);
    existingRequestes.remove(divId);
    modifyContainerStyleBottomToEmpty();
}
//toRemove array of divIds
function removeChatRequestCollection(toRemove)
{
    for(i=0;i<toRemove.length;i++){
        removeRequest(toRemove[i]);
    }
}
function removeAllRequests()
{
    existing=existingRequestes.getValues();
    for(i=0;i<existing.length;i++){
        removeRequest(existing[i]);
    }
}
function isChatRequestAlreadyExist(collaborationId,sessionId)
{
    divId=getChatDivId(collaborationId,sessionId);
    isExist=$(divId);
    if(isExist!=null && isExist!='null'){
        return true;
    }
    return false;
}
function getChatDivId(collaborationId,sessionId)
{
    return CHAT_REQUEST_ID+sessionId+"_"+collaborationId;
}
/*
this function compare the existing requestes with the new requests and return the existing requestes that doesn't exist in the new list
*/
function compareExistingWithNew(existingRequestes,newRequests)
{
    toReturn=new Array();
    existing=existingRequestes.getValues();
    for(i=0;i<existing.length;i++){
        for(j=0;j<newRequests.length;j++){
            if(existing[i]==newRequests[j].getRequestKey()){
                break;
            }
        }
        if(j==newRequests.length){
            toReturn.push(existing[i]);    
        }
    }
    return toReturn;
}
//requests is an array of chatRequest objects
function updateUserRequests(requests)
{
    toRemove=compareExistingWithNew(existingRequestes,requests);
    if(toRemove.length>0)
        removeChatRequestCollection(toRemove);
    addRequestsCollection(requests);
}

function cancelChatRequest(sessionId){
    //alert("inside cancel");
    $("header:ChatRequestForm:ChatRequestSessionId").value=sessionId;
    $("header:ChatRequestForm:ChatRequestActions").click();
    //alert("still inside cancel after completion");
}

function modifyContainerStyleBottomToEmpty()
{
    if(existingRequestes.size()==0)
        $("chatRequestsContainer").style.bottom='-20px';
    else
        $("chatRequestsContainer").style.bottom="0px";
}
function ChatRequest(collaborationId,sessionId,name,invitorId){
    this.collaborationId=collaborationId;
    this.name=name;
    this.sessionId=sessionId;
    this.invitorId=invitorId;
    this.setSessionId=function (value){
        sessionId=value;
    };
    this.setCollaborationId=function (value){
        collaborationId=value;
    };
    this.setName=function (value){
        name=value;
    };
    this.setInvitorId=function (value){
        invitorId=value;
    };
    this.getSessionId=function (){
        return sessionId;
    }
    this.getCollaborationId=function (){
        return collaborationId;
    }
    this.getName=function (){
        return name;
    }
    this.getInvitorId=function (){
        return invitorId;
    }
    this.getRequestKey=function(){
        return  getChatDivId(this.collaborationId,this.sessionId);
    }
}
function HashTable(){
    this.hash = new Array();
    this.keys = new Array();
    
    this.put = function (key, value){
        if (value == null)
            return;
        
        if (this.hash[key] == null)
            this.keys[this.keys.length] = key;
        
        this.hash[key] = value;
    }
    
    /**
    * get
    * Return an element
    * param: key - String, key name
    * Return: object - The requested object
    */
    this.get = function (key){
        return this.hash[key];
    }
    
    /**
    * remove
    * Remove an element
    * param: key - String, key name
    */
    this.remove = function (key){
        for (var i = 0; i < this.keys.length; i++){
            //did we found our key?
            if (key == this.keys[i]){
                //remove it from the hash
                p=this.hash[this.keys[i]];
                this.hash[this.keys[i]] = null;
                //and throw away the key...
                this.keys.splice(i ,1);
                return p;
            }
        }
        return null;
    }
    
    /**
    * size
    * Return: Number of elements in the hashtable
    */
    this.size = function (){
        return this.keys.length;
    }
    
    
    this.contains = function(key) {
        for (var i = 0; i < this.keys.length; i++){
            //did we found our key?
            if (key == this.keys[i]&&this.hash[this.keys[i]] != null){
                return true;
            }
        }
        return false;
    }
    
    this.removeAll=function(){
        this.keys = new Array();
        this.hash = new Array();
        
    }
    
    /**
    * next
    * Return: true if theres more items
    */
    
    this.getValues = function() {
        var values = new Array();
        for (var i = 0; i < this.keys.length; i++){
            values[i]=this.hash[this.keys[i]] ;
        }
        return values;
    }    
}

function testChatRequests()
{
    chatRequests=new Array();
    chatRequest=new ChatRequest(1,2,"Nabil Test 1");
    chatRequests.push(chatRequest);
    chatRequest=new ChatRequest(2,3,"Nabil Test 2");
    chatRequests.push(chatRequest);
    chatRequest=new ChatRequest(3,4,"Nabil Test 3");
    chatRequests.push(chatRequest);
    chatRequest=new ChatRequest(4,5,"Nabil Test 4");
    chatRequests.push(chatRequest);
    chatRequest=new ChatRequest(5,6,"Nabil Test 5");
    chatRequests.push(chatRequest);
    chatRequest=new ChatRequest(6,7,"Nabil Test 6");
    chatRequests.push(chatRequest);
    //alert("into test size="+chatRequests.length);
    updateUserRequests(chatRequests);
}