/*
 * SocailPanel beta version 0.0.1
 * http://
 *
 * Copyright (c) 2009 Robert Campbell
 *  
 */
	//Setup Socail Panel with bar and tabs
	document.write('<div id="socialpanel_bar" style="width: 140px;"></div>');
	//Write logo
	document.write('<div id="sp_logo"><img src="images/lcff16.png" /></div>');
	goTab();
	onlineTab();

	//testing
	//tabIcon("<center><a href='http://LiveClient2.net'><img src='images/lcff16.png' border='0'></a></center>");
	ResizeBar();


	getSession();


	//Setting some global variables
	var chatUser2 = "";
	var chatBoxID = "";
	var GlobalJustify = "";
	var TimerID = "1";


	if(spanel_user!="Anonymous" && spanel_user!="") {
		getData();
		TimerID = setInterval ( "getData()", 10000 );
	}


	//maintain relative size
	window.onresize = function(){
		ResizeBar();
    }
	function ResizeBar() {
		var bar_width=document.getElementById("socialpanel_bar");
		bar_width.style.width = (document.body.clientWidth - 30) + "px" 
	}
	window.onscroll = function() {
		var bar_width=document.getElementById("socialpanel_bar");
		bar_width.style.width = (document.body.clientWidth - 30) + "px" 
		bar_width.style.bottom = "0px";
	}

	//Function for first tab "SocailBar" tab (like a start button or menu area)
	function goTab(){

		var divTag = document.createElement("span");
		divTag.id = "goTab";
             
        divTag.setAttribute("align","center");
             
        divTag.style.margin = "0px auto";
             
        divTag.className ="gotab";
        
		var span_id = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );

        divTag.innerHTML = "<span onClick='createPop(\"\", \"goTab\", \"\", \"Social Panel\", \"goTab\");' style='width:100%;'><center>Social Panel</center></span>";
			           
        document.getElementById("socialpanel_bar").appendChild(divTag);
	}

	function tabIcon(html){

		var divTag = document.createElement("span");
		divTag.id = "tabIcons";
             
        divTag.setAttribute("align","center");
             
        divTag.style.margin = "0px auto";
             
        divTag.className ="tabicons";
        
		var span_id = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );

        divTag.innerHTML = html;
			           
        document.getElementById("socialpanel_bar").appendChild(divTag);
	}



	//Function to setup online/offline tab
	function onlineTab(){
		var divTag = document.createElement("span");
		divTag.id = "onlineTab";
             
        divTag.setAttribute("align","center");
             
        divTag.style.margin = "0px auto";
             
        divTag.className ="bartab";

		//phpbb code online
		if(spanel_user!="Anonymous") {
	        divTag.innerHTML = "<span><center>Loading...</center></span>";
		}else{
	        divTag.innerHTML = "<a href='ucp.php?mode=login'><center>Login</center></a>";
		}	
		
        document.getElementById("socialpanel_bar").appendChild(divTag);
	}

	//Function to dynamically create other tabs
	function createTab(TabTitle, OpenPop) {
        var divTag = document.createElement("span");
		var span_id = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );
        divTag.id = span_id;
             
	//TODO: create function to check open tabTitles to avoid opening duplicates

        divTag.setAttribute("align","center");
             
        divTag.style.margin = "0px auto";
             
        divTag.className ="bartab";
             			           
        document.getElementById("socialpanel_bar").appendChild(divTag);
		var new_span_id = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );
		document.getElementById(span_id).innerHTML = "<span style='float:left;' onClick='createPop(\"New\", \""+span_id+"\", \""+new_span_id+"\", \""+ TabTitle +"\", \"dynamicPop\");'>" + TabTitle + "</span>  <a onClick=\"removeElement('"+span_id+"', '"+TabTitle+"');\" style='float:right; padding-right:5px;'>X</a>";
	
		if(OpenPop=="open") {
			createPop("New", span_id, new_span_id, TabTitle, "dynamicPop")
		}

		//Make tab blink on open
		$(divTag).fadeIn(400).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);

		//NEW: set session span_id TabTitle Open
		$.post("sp_receive.php", { func: "sessionSet", tabID: span_id, tabTitle: TabTitle});

	}

	//function to create popups menus over tabs
	function createPop(popTitle, Opener, span_id, TabTitle, getFunc) {
		var span_id = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );
		var span_id2 = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );
		var span_id3 = (String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))+String.fromCharCode(97 + Math.round(Math.random() * 25))   );
		
		//This is where we should close all other popups before opening others.
		removePops();

		if(document.getElementById(span_id)) {
			//Keep from opening a duplicate
		}else{
			var indexOfEl = getIndex("socialpanel_bar", Opener);
			var OpenerObj = document.getElementById("socialpanel_bar").childNodes[indexOfEl];
			var leftPop = ObjectLeft(OpenerObj);
			var rightPop = ObjectRight(OpenerObj);
		    var divTag = document.createElement("div");

			
			var tabHTML = document.getElementById(Opener).innerHTML;
			//alert(tabHTML);
			document.getElementById(Opener).innerHTML = "<center>Loading...</center>";

			divTag.id = span_id;
                
			$.post("sp_receive.php", { func: getFunc, pop: span_id, tab: span_id2 , ChatBoxID: span_id3, leftTab: leftPop, rightTab: rightPop, spanel_user: spanel_user, tabTitle: TabTitle},
			  function(data){
				 
				//create matching tab
				var divTag2 = document.createElement("div");
				divTag2.id = span_id2;
				
				divTag2.setAttribute("align","center");
					 
				divTag2.style.bottom = "0px";
				divTag2.style.border = "1px solid";
				divTag2.style.width = OpenerObj.style.width;

				if(data.justify == "right") {
					divTag2.style.right = (rightPop -132) + "px";
					if(isIE()) {
						divTag2.style.right = (rightPop -122) + "px";
					}
				}else{
					divTag2.style.left = (leftPop + 1) + "px";
				}
				
				divTag2.className ="bartabPop";
				
				if(getFunc != "goTab" && getFunc != "getBuddyList") {
					divTag2.innerHTML = "<span onClick='removePops();' style='cursor:pointer;'><p style='float:left;'>"+TabTitle+"</p><a onClick=\"removeElement('"+Opener+"', '"+TabTitle+"');\" style='float:right; padding-right:5px;'>X</a></span>";
				}else{
					divTag2.innerHTML = "<span onClick='removePops();' style='cursor:pointer;'>"+TabTitle+"</span>";
				}
				
				document.body.appendChild(divTag2);



				if(data.justify == "right") {
					divTag.style.right = data.rightPop + "px";
					if(isIE()){
						divTag.style.right = ((data.rightPop*1) + 10) + "px";
					}
				}else{
					divTag.style.left = data.leftPop + "px";
				}

		
				divTag.style.margin = "0px auto";
				divTag.style.bottom = "25px";
				divTag.style.border = "1px solid black";
				divTag.style.width = data.width + "px";
				//divTag.style.left = data.leftPop + "px";
				divTag.style.height = data.height;
				//divTag.style.position = "fixed";
				//divTag.style.overflow = "auto";
        
				divTag.className ="popupDiv";
				divTag.innerHTML = "<div class='popBar' onClick='removePops();' style='cursor:pointer;'><span>"+TabTitle+"</span><div class='popMin'></div></div>" + data.html
				$(divTag).fadeIn("fast");
				document.body.appendChild(divTag);
				if(getFunc == "dynamicPop") {
					//Loop refresh chat
					chatUser2 = TabTitle;
					chatBoxID = span_id3;
					var ChatLoop = setInterval("refreshChat(chatUser2, chatBoxID);", 5000);
				}
			document.getElementById(Opener).innerHTML = tabHTML;
			}, "json");
			//document.getElementById(span_id).scrollTop = document.getElementById(span_id).scrollHeight;

			
		}
	}

	
	//Get index for childNode elements
	function removePops() {
		for(var index =0; index < document.body.childNodes.length; index++) {

			if(document.body.childNodes[index].className == "bartabPop") {
				//alert (document.body.childNodes[index].id);
				document.body.removeChild(document.body.childNodes[index]);
			}
			if(document.body.childNodes[index].className == "popupDiv") {
				//alert (document.body.childNodes[index].id);
				document.body.removeChild(document.body.childNodes[index]);
			}
		}
		$.post("sp_receive.php", { func: "removePopsSession"});
	}

	function getIndex(parent_id, child_id){
		var parent_obj = document.getElementById(parent_id);
		for ( var index = 0; index < parent_obj.childNodes.length; index++ ) {
			//alert(parent_obj.childNodes[index].id);
			if (parent_obj.childNodes[index].id == child_id)
			{
				return index;
			}
		}
		return -1;
	}
	//Get left position for popup placement relative to tabs
	function ObjectLeft(obj) {
		var curleft = 0;
	      if (obj.offsetParent) {
		        do {
			          curleft += obj.offsetLeft;
				} while (obj = obj.offsetParent);
		}
		return curleft;
	}
	function ObjectRight(obj) {
		var curright = 0;
	      if (obj.offsetParent) {
		        do {
			          curright += obj.offsetLeft;
				} while (obj = obj.offsetParent);
		}

		return (document.body.clientWidth - curright);
	}

	//Detect if browser is IE
	function isIE()
	{
	  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	}


	//Function to remove elements such as dynamicly created tabs
	function removeElement(divNum, tabTitle) {
		$("#" + divNum).remove();
		$.post("sp_receive.php", { func: "sessionKill", elementID:divNum, tabTitle:tabTitle});
	}
	
	function setStatus(status) {
		removePops();
		$.post("sp_receive.php", { func: "updateStatus", spanel_user: spanel_user, status: status.value},
			function(data){
				document.getElementById("onlineTab").innerHTML = "<span onClick='createPop(\"\", \"onlineTab\", \"\", \""+ status.value + "\", \"getBuddyList\");'><center>"+ status.value + "</center></span>";
		}, "json");
		getData();
	}

	function sendChat(e, text, user2, ChatBoxID) {
		var characterCode; //literal character code will be stored in this variable

		if(e && e.which){ //if which property of event object is supported (NN4)
			e = e
			characterCode = e.which //character code is contained in NN4's which property
		}else{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}
		
		//alert(e.shiftKey);
		if(characterCode == 13 && e.shiftKey == false){ //if generated character code is equal to ascii 13 (if enter key)
			$.post("sp_receive.php", { func: "sendChat", spanel_user: spanel_user, text: text.value, user2: user2});
			
			text.value="";
			refreshChat(user2, ChatBoxID);
		}
	}


	function clearChat(chatUser2,ChatBoxID) {
		//alert("Clear chat code to come");
		$.post("sp_receive.php", { func: "clearChat", spanel_user: spanel_user, user2: chatUser2});
		document.getElementById(ChatBoxID).innerHTML = "";
		refreshChat(user2, ChatBoxID);
	}

	function refreshChat(user2, ChatBoxID) {
		//spanel_user  ChatBoxID.id  user2
		if(document.getElementById(ChatBoxID) != null) {
			var OldChat = document.getElementById(ChatBoxID).innerHTML;
			$.post("sp_receive.php", { func: "getChat", spanel_user: spanel_user, user2: user2},
				function(data){
					if(data.newChat != "old") {
						//alert(data.newChat);
						//document.getElementById(ChatBoxID).innerHTML = "";
						//document.getElementById(ChatBoxID).scrollTop = 0;
						document.getElementById(ChatBoxID).innerHTML = data.html;
						//document.getElementById(ChatBoxID).scrollTop = document.getElementById(ChatBoxID).scrollHeight;
					}
					//document.getElementById(ChatBoxID).innerHTML = data.html;
					//alert(data.newChat);
			}, "json");

			//TODO: Setup so this is disabled when user is scrolling and react faster in IE
			//if (document.getElementById(ChatBoxID).scrollTop != document.getElementById(ChatBoxID).scrollHeight - 233)
			//{
			//	alert(document.getElementById(ChatBoxID).scrollTop +"--"+ document.getElementById(ChatBoxID).scrollHeight);
			//}
			//document.getElementById(ChatBoxID).scrollTop = document.getElementById(ChatBoxID).scrollHeight;
			$("#"+ChatBoxID).attr({ scrollTop: $("#"+ChatBoxID).attr("scrollHeight") });
		}
	}

	function getSession() {
		$.post("sp_receive.php", { func: "sessionGet"},
			  function(data){
			if(data.openPop == "goTab") {
				createPop("", "goTab", "", "Social Panel", "goTab");
			}else if(data.openPop == "onlineTab") {
				createPop("", "onlineTab", "","Who's Online", "getBuddyList");
			}

			var tabTitles = new Array();
			tabTitles= data.tab.split(",");
			for(var index =0; index < tabTitles.length; index++) {
				//alert(tabTitles[index]);
				if(tabTitles[index]!="" && tabTitles[index] != "undefined") {
					if(data.openPop == tabTitles[index]) {
						createTab(tabTitles[index], "open");
					}else{
						createTab(tabTitles[index], ""); 
					}
				}
			}
			//Show any openPop
			//alert(data.openPop);

		}, "json");
	}

	function getData() {
		$.post("sp_receive.php", { func: "updateBar", spanel_user: spanel_user},
			function(data){
				document.getElementById("onlineTab").innerHTML = "<span onClick='createPop(\"\", \"onlineTab\", \"\", \""+ data.status + "\", \"getBuddyList\");'><center>"+ data.status + "</center></span>";
				//Checking for waiting
				if(data.waiting != "") {
					createTab(data.waiting, "");
				}
				if(data.flashWho != "") {
					//Make tab blink on open
					$("#" + data.flash).fadeIn(400).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
					//TODO: Need to create tab if it doesn't exist.
					//createTab(data.flashWho, "");
				}
			}, "json");

		}

