var LiveChats = new Object();
LiveChats = {
	ajaxServletURI: "",
	ajaxActionName: "",
	sequenceNumber: 0,
	chatStatus: 2,
	chatId: 0,
	sequenceNumberParamName: "sequenceNumber",
	chatIdParamName: "chatId",
	chatStateParamaName: "chatState",
	interval: 60000,
	timerHandler: null,
	
	questionClassName: "question",
	answerClassName: "answer",
	STATE_PREVIOUS: 2,
	
	chatGuestName: "",
	questionsContainer: "qa-list",
	
	lastUpdate: '',
	
	init: function(ajaxServletURI, ajaxActionName, chatId, initialSequenceNumber, chatStatus, guestName) {
		LiveChats.ajaxServletURI = ajaxServletURI;
		LiveChats.ajaxActionName = ajaxActionName;
		LiveChats.sequenceNumber = initialSequenceNumber;
		LiveChats.chatStatus = chatStatus;
		LiveChats.chatId = chatId;
		LiveChats.chatGuestName = guestName;
		
		LiveChats.timerHandler = setInterval("LiveChats.doAjaxRequest()", LiveChats.interval);
	},
	doAjaxRequest: function() {
		ajaxHandler = new AjaxHandler();
        ajaxHandler.setUrl(LiveChats.ajaxServletURI);
        ajaxHandler.setQueryString("actionNameAJAX=" + LiveChats.ajaxActionName +
        	"&" + LiveChats.sequenceNumberParamName + "=" + LiveChats.sequenceNumber +
        	"&" + LiveChats.chatIdParamName + "=" + LiveChats.chatId);
        ajaxHandler.send("POST", 'LiveChats.onRecieveQuestions');
	},
	onRecieveQuestions: function(data) {		
		if (data != undefined && data != null) {
			for(var i=0; i < data.length; i++) {
				if (data[i]) {
					if (data[i].__type && data[i].__type == "LiveChatDTO") {
						// the chat state has changed
						LiveChats.chatStatus = data[i].chatState;						
						LiveChats.lastUpdate = data[i].lastUpdate;
						if (LiveChats.chatStatus == LiveChats.STATE_PREVIOUS) {
							clearInterval(LiveChats.timerHandler);
							//TODO: handle this change in the UI
						}
					} else if (data[i].__type && data[i].__type == "LiveChatQADTO") {						
						var id = data[i].questionId;
						var q = LiveChats.createQuestion(data[i]);
						var a = LiveChats.createAnswer(data[i]);
						var qOld = document.getElementById("q_" + id)
						var aOld = document.getElementById("a_" + id)
						if (qOld != undefined) {
							qOld.innerHTML = q.innerHTML;
							aOld.innerHTML = a.innerHTML;
						} else {
							var container = document.getElementById(LiveChats.questionsContainer);
							container.appendChild(q);
							container.appendChild(a);
						}
						if (LiveChats.sequenceNumber < data[i].sequenceNo) {
							LiveChats.sequenceNumber = data[i].sequenceNo;
						}						
						//hide QA form send question confirmation sms						
						var confirmSms = document.getElementById('comments-main');
						if (confirmSms != null) {
							confirmSms.style.display = 'none';
						}	
						//show '7ewar' title bar 
						var titleBar = document.getElementById('live-chat-title-bar');						
						if(titleBar != null) {
							titleBar.style.visibility = 'visible';							
						}
					}
				}
			}
		}
	},
	createQuestion: function(dto) {
		var li = document.createElement("li");
		li.setAttribute("class", LiveChats.questionClassName);
		li.className = LiveChats.questionClassName;
		li.setAttribute("id", "q_" + dto.questionId);
		
		var h5 = document.createElement("h5");
		h5.innerHTML = dto.senderName;
		
		var p = document.createElement("p");
		p.innerHTML = dto.question;
		
		li.appendChild(h5);
		li.appendChild(p);
		return li;
	},
	createAnswer: function(dto) {
		var li = document.createElement("li");
		li.setAttribute("class", LiveChats.answerClassName);
		li.className = LiveChats.answerClassName;
		li.setAttribute("id", "a_" + dto.questionId);
		
		var h5 = document.createElement("h5");
		h5.innerHTML = LiveChats.chatGuestName;
		
		var p = document.createElement("p");
		p.innerHTML = dto.answer;
		
		li.appendChild(h5);
		li.appendChild(p);
		return li;
	}
}



function LiveChatQADTO() {
	this.questionId;
    this.question;
    this.answer;
    this.senderName;
    this.senderProfession;
    this.questionState;
    this.questionTime;
    this.questionChatId;
    this.sequenceNo;
	this.__type = "LiveChatQADTO";
}

function LiveChatDTO() {
	this.chatId;
    this.chatState;
    this.lastUpdate;
	this.__type = "LiveChatDTO";
}
