var ProductSupport = {
	
	root: 'http://livesupport.slideshop.com/',
	ajaxDir: 'v2/ajax/',
	location: window.location,
	doShow: '1',
	inAnimation: '0',
	shown: '0',
	z: '0',
	timeOuts: new Array,
	iniTimeout: '',
	shiftDown: false,
	state: 'default',
	
	// File Requests
	killScript : function (theID) {
		child = document.getElementById(theID);
		child.parentNode.removeChild(child);
		return false;
	},
	
	loadRequest : function (file) {
		var newDate = new Date;
		var uniqueID = newDate.getTime();
		var id = 'rtSupport_' + uniqueID;
		var arr = file.split('?');
		var symbol = (arr.length == 1) ? '?' : '&';
		var url = ProductSupport.root + ProductSupport.ajaxDir + file + symbol + 'scriptElementID=' + id + '&product=1';
		var script = document.createElement('script');
		script.src = url;
		script.type = 'text/javascript';
		script.id = id;
		document.getElementsByTagName('head')[0].appendChild(script);
		return false;
	},
	
	// Manipulation
	scrollToBottom : function (objID) {
		var objDiv = document.getElementById(objID);
		objDiv.scrollTop = objDiv.scrollHeight;
		return false;
	},
	
	keyDown : function (event, type, object) {
		if(event.keyCode == 16){
			ProductSupport.shiftDown = true;
		}
		if(event.keyCode == 13){
			if(!ProductSupport.shiftDown){
				object.disabled = true;
				if(type == "question"){
					ProductSupport.sendQuestion();
				} else {
					ProductSupport.sendReply();
				}
			}
		}
		return false;
	},
	
	keyUp : function (event) {
		if(event.keyCode == 16){
			ProductSupport.shiftDown = false;
		}
	},
	
	clearAll : function () {
		Ext.get('LPSProductSupport').update('');
		return false;
	},
	
	checkFocus : function (obj, text) {
		if(Ext.get(obj).dom.innerHTML == text){
			Ext.get(obj).update('');
		}
		return false;
	},
	
	// Actions using Requests
	initialize : function () {
		var header = document.getElementsByTagName("head").item(0);
		var obj = document.createElement("link");
		obj.rel = "stylesheet";
		obj.type = "text/css";
		obj.href = "http://livesupport.slideshop.com/v2/livesupport_product.css";
		header.appendChild(obj);
		ProductSupport.loadRequest('initialize.php?LSCountry=' + ProductSupport.country + '&LSHref=' + location.href);
		return false;
	},
	
	sendReply : function () {
		var form = document.getElementById('LPSWriteReply');
		var textarea = escape(form.textarea.value);
		if(textarea.length > 0){
			Ext.get('LPSWriteReplyTextArea').dom.disabled = true;
			ProductSupport.loadRequest('send_reply.php?textarea=' + textarea);
		}
		return false;
	},
	
	sendQuestion : function () {
		var form = document.getElementById('LPSWriteMessage');
		var textarea = escape(form.textarea.value);
		var location = ProductSupport.location;
		if(textarea.length > 0){
			Ext.get('LPSWriteMessageTextArea').dom.disabled = true;
			ProductSupport.loadRequest('send_question.php?textarea=' + textarea);
		}
		return false;
	},
	
	sendEmail : function () {
		var form = document.getElementById('LPSEmailForm');
		var name = escape(form.name.value);
		var email = escape(form.email.value);
		var question = escape(form.question.value);
		var href = ProductSupport.location.href
		ProductSupport.loadRequest('email_submit.php?name=' + name + '&email=' + email + '&question=' + question + '&href=' + href);
		return false;
	},
	
	forwardEmail : function () {
		var recipient = prompt('Enter recipient email address.');
		if(recipient){
			if(recipient.length > 0){
				ProductSupport.loadRequest('send_email_conversation.php?recipient=' + recipient);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	},
	
	printConversation : function (uniqid) {
		var name = "SlideShop";
		var url = "http://livesupport.slideshop.com/" + uniqid + ".print";
		window.open(url, name);
		return true;
	},
	
	loadWelcomeMessage : function (set) {
		ProductSupport.loadRequest('welcome_message.php?set=' + set);
		return true;
	},
	
	loadSupporters : function() {
		ProductSupport.loadRequest('supporters.php');
		return true;
	},
	
	loadConversation : function(a) {
		ProductSupport.z = "1";
		ProductSupport.loadRequest('conversation.php?a=' + a);
		return true;
	},
	
	loadQueue : function () {
		ProductSupport.loadRequest('queue.php');
		return true;
	},
	
	setShow : function () {
		if(ProductSupport.doShow == '0'){
			ProductSupport.loadRequest("set_show.php");
			ProductSupport.doShow = '1';
			return true;
		} else {
			return false;
		}
	},
	
	selectSupporter : function (id) {
		ProductSupport.loadRequest('select_supporter.php?id=' + id);
		return true;
	},
	
	closeSession : function () {
		ProductSupport.loadRequest('close_session.php');
		ProductSupport.doShow = '0';
		return true;
	},
	
	closeInstance : function () {
		if(confirm('Are you sure you want to close the support?')){
			ProductSupport.loadRequest('close_instance.php');
		}
		return true;
	},
	
	// AJAX
	performRequest : function (id, data) {
		if (!id) return false;
		else {
			// close_instance.php
			if (id == 1) {
				history.go(0);
			}
			// close_session.php
			if (id == 2) {
				ProductSupport.closeInstance();
			}
			// conversation.php
			if (id == 4) {
				var thisHtml = "";
				thisHtml += "<span class='LPSSupporterField'>";
				thisHtml += "<div style='height: 70px; overflow: hidden;'>";
				thisHtml += "<img src='http://livesupport.slideshop.com/images/supporters/" + data["supporterImage"] + "' alt='' id='LPSSupporterImage' />";
				thisHtml += "<span class='LPSTitle'>" + data["title"] + "</span><span class='LPSMessage'>" + data["message"] + "</span>";
				thisHtml += "<br clear='all' />";
				thisHtml += "</div>";
				thisHtml += "<ul id='LPSMessageContainer' style='max-height: none;'></ul>";
				thisHtml += "<form id='LPSWriteReply' class='LPSWriteMessage' onsubmit='ProductSupport.sendReply(); return false'>";
				thisHtml += "<textarea id='LPSWriteReplyTextArea' name='textarea' rows='3' onfocus='ProductSupport.checkFocus(this, \"" + data["textarea"] + "\")' onkeydown='ProductSupport.keyDown(event, \"reply\", this)' onkeyup='ProductSupport.keyUp(event)'>" + data["textarea"] + "</textarea>";
				thisHtml += "<div class='LPSButtons'><a href='javascript:void(0)' onclick='ProductSupport.forwardEmail()'>Email</a> or <a href='javascript:void(0)' onclick='ProductSupport.printConversation(\"" + data["uniqid"] + "\")'>Print</a><input type='image' src='http://livesupport.slideshop.com/design/chat_button.png' style='cursor: pointer;' class='LPSRight' /></div>";
				thisHtml += "</form>";
				thisHtml += "</span>";
				Ext.get('LPSProductSupport').update(thisHtml);
			}
			if (id == 5) {
				var el = new Array;
				for(var i=0; i<data["amount"]; i++){
					var doDisable = false;
					var thisMessage = data.messages["msg" + i];
					if(thisMessage["message"] == "SupporterEnded"){
						thisMessage["message"] = "The supporter ended the conversation.";
						doDisable = true;
					}
					if(thisMessage["message"] == "CustomerEnded"){
						thisMessage["message"] = "The customer ended the conversation.";
						doDisable = true;
					}
					if(doDisable == true){
						Ext.get('LPSWriteReplyTextArea').setStyle('background-color', '#efefef');
						document.getElementById('LPSWriteReplyTextArea').disabled = 'disabled';
						document.getElementById('LPSWriteReplyTextArea').value = '';
					}
					if(thisMessage["system"] == "true") var thisHtml = "<div style='text-align: center; font-style: italic;'>" + thisMessage["message"] + "</div>";
					else var thisHtml = "<span class='LPSName' style='color: " + thisMessage["color"] + ";'>" + thisMessage["name"] + "</span>" + thisMessage["message"];
					el[i] = document.createElement('li');
					el[i].innerHTML = thisHtml;
					document.getElementById('LPSMessageContainer').appendChild(el[i]);
				}
				return true;
			}
			// queue.php
			if (id == 9) {
				ProductSupport.state = 'queue';
				var thisHtml = "";
				thisHtml += "<div class='LPSWaitText'>Please wait while " + data["supporterName"] + " is answering your question.</div>";
				thisHtml += "<div class='LPSNumLine'>You are now <strong>" + data["numOut"] + "</strong> in line, please wait..</div>";
				thisHtml += "<div class='LPSLoadingCont'><img src='" + data["image"] + "' alt='Loading..' /></div>";
				Ext.get('LPSProductSupport').update(thisHtml);
				setTimeout("ProductSupport.loadQueue()", 7000);
				if (typeof(LiveSupport) == 'object'){
					if (LiveSupport.state != 'queue'){
						LiveSupport.state = 'queue';
						var thisHtml = "";
						thisHtml += "<div class='LSWaitText'>Please wait while " + data["supporterName"] + " is answering your question.</div>";
						thisHtml += "<div class='LSNumLine'>You are now <strong>" + data["numOut"] + "</strong> in line, please wait..</div>";
						thisHtml += "<div class='LSLoadingCont'><img src='" + data["image"] + "' alt='Loading..' /></div>";
						Ext.get('LSContent').update(thisHtml);
						setTimeout("LiveSupport.loadQueue()", 7000);
					}
				}
				return true;
			}
			if(id == 14) {
				ProductSupport.state = 'conversation';
				ProductSupport.loadConversation('true');
				if (typeof(LiveSupport) == 'object'){
					if (LiveSupport.state != 'conversation'){
						LiveSupport.state = 'conversation';
						LiveSupport.loadConversation('true');
					}
				}
				return true;
			}
			// send_reply.php
			if (id == 10) {
				var el = Ext.get('LPSWriteReplyTextArea').dom;
				el.disabled = false;
				el.value = '';
				for(key in ProductSupport.timeOuts){
					clearTimeout(ProductSupport.timeOuts[key]);
				}
				ProductSupport.loadConversation('false');
				return true;
			}
			// send_email_conversation.php
			if (id == 12) {
				if(data["message"].length > 0){
					alert(data["message"]);
				}
				return true;
			}
			// send_question.php
			if (id == 13) {
				ProductSupport.clearAll();
				ProductSupport.loadQueue(data["uniqid"]);
				return true;
			}
			// initialize.php
			if (id == 15) {
				if(data.online == "true"){
					if(data.isSet == "true") ProductSupport.loadQueue();
					else ProductSupport.loadWelcomeMessage('product');
				} else {
					ProductSupport.state = 'offline';
					Ext.get('LPSProductSupport').update("");
				}
			}
			// welcome_message.php
			if (id == 16) {
				var thisHtml = "<a name='ProductSupportAnchor'></a>";
				thisHtml += "<span class='LPSSupporterField'>";
				thisHtml += "<form id='LPSWriteMessage' class='LPSWriteMessage' onsubmit='ProductSupport.sendQuestion(); return false'>";
				thisHtml += "<div style='height: 70px; overflow: hidden; font-family: Arial; font-size: 11px;'>";
				thisHtml += "<img src='http://livesupport.slideshop.com/images/supporters/" + data["image"] + "' alt='' id='LPSSupporterImage' />";
				thisHtml += "<span class='title'>" + data["msgSet"]["title"] + "</span><span class='message'>" + data["msgSet"]["message"] + "</span>";
				thisHtml += "<textarea id='LPSWriteMessageTextArea' name='textarea' rows='2' onfocus='ProductSupport.checkFocus(this, \"" + data["msgSet"]["textarea"] + "\")' onkeydown='ProductSupport.keyDown(event, \"question\", this)' onkeyup='ProductSupport.keyUp(event)' style='width: 254px; margin-top: 5px;'>" + data["msgSet"]["textarea"] + "</textarea>";
				thisHtml += "<input type='image' src='http://livesupport.slideshop.com/design/chat_button.png' style='cursor: pointer; margin: 0 0 -2px 10px;' />";
				thisHtml += "</div>";
				thisHtml += "</form>";
				thisHtml += "</span>";
				thisHtml += "<br clear='all' />";
				Ext.get('LPSProductSupport').update(thisHtml);
			}
			// initialize.php
			if (id == 17) {
				//alert('Domain not found: ' + data.domain + '.');
			}
		}
	}
	
}

Ext.onReady(function(){
	ProductSupport.initialize();
});
