// JavaScript Document
// Daniel Raquel

//GENERAL CLASSES
	//Doubly Linked List	
	function DLList(){		
		this._first = null;
		this._last = null;
		this._len = 0;
		
		this.getFirst = getFirst;
		function getFirst(){
			return this._first;	
		}
		this.getLast = getLast;
		function getLast(){
			return this._last;	
		}
		this.add = add;
		function add(data){ 
			var node = { 
				data: data, 
				next: null,
				prev: null
			};
			if (this._len == 0) { 
				this._first = node;
				this._last = node;
			} else { 
				this._last.next = node;
				node.prev = this._last;
				this._last = node;
			}        
			this._len++; 
		}
		this.remove = remove;
		function remove(index){
			if (index > -1 && index < this._len){
				var current = this._first,
					i = 0;
				//removing first item
				if (index === 0){
					this._first = current.next;
					if (!this._first){
						this._last = null;
					} else {
						this._first.prev = null;
					}
				//removing last item
				} else if (index === this._len -1){
					current = this._last;
					this._last = current.prev;
					this._last.next = null;
				} else {
					while(i++ < index){
						current = current.next;	
					}
					current.prev.next = current.next;
					current.next.prev = current.prev;
				}
				this._len--;
				return current.data;
			} else {
				return null; // if index is invalid
			}
		}
		this.getNode = getNode;
		function getNode(index){
			if (index > -1 && index < this._len){ 
				current = this._first, i = 0;
				while(i++ < index){
					current = current.next;
				}
				return current; 
			} else {
				return null;  // if index invalid return null
			}
		}
		this.displayForward = displayForward;
		function displayForward(){
			current = this._first;
			while(current != null){
				document.write(current.data+" ");
				current = current.next;
			}
		}
		this.displayBackward = displayBackward;
		function displayBackward(){
			var current = this.last;
			while(current != null){
				document.write(current.data+" ");
				current = current.prev;
			}
		}
		this.size = size;
		function size(){
			return this._len;
		}
	}
	//End DLList
	//Site Class
	function Site(){
		var sid;
		var aid;
		var status;
		var title;
		var domain;
		var gaid;
	
		this.setGlobal = setGlobal;
		function setGlobal(ttf,tts,epp){
			_sid = this.sid; //Site UID
			_gaid = this.gaid; //Google Analytics ID
			document.title = this.title + " " + document.title;
			_domain = "http://"+this.domain; //Site Domain Address
			_status = this.status;
			if(ttf != null && ttf != 0){
				_ttf = ttf; //Time to Fade :: 0 = no fade / 1000 = 1 sec
			}
			if(tts != null && tts != 0){
				_tts = tts; //Time to Slide (Auto Slide Show) :: 1000 = 1 sec
			}
			if(epp != null && epp != 0){
				_epp = epp;
			}
			
		}
		this.getSite = getSite;
		function getSite(sid, hash){
			var xmlhttp, url;
			xmlhttp = ajaxReq();
			url = "post/getSite.php";
			xmlhttp.open("POST",url,false);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			var ins;
			ins = "s="+sid+"&h="+hash;
			xmlhttp.send(ins);
			xmlDoc = loadXML(xmlhttp.responseText);
			this.readXML(xmlDoc);
		}
		this.readXML = readXML;
		function readXML(doc){
			d = doc.documentElement;
			a = d.attributes;
			this.sid = a.getNamedItem("sid").nodeValue;
			this.aid = a.getNamedItem("aid").nodeValue;
			this.gaid = d.getElementsByTagName("gaid")[0].childNodes[0].nodeValue;
			this.title = d.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			this.domain = d.getElementsByTagName("domain")[0].childNodes[0].nodeValue;
			this.status = d.getElementsByTagName("status")[0].childNodes[0].nodeValue;
		}
	}
	//End Site Class
//END GENERAL CLASSES
	
//GENERAL FUNCTIONS
	//--CLOCK--
	function setClock(){
		clock();
		setInterval("clock()",1000.0);
	}
	function clock(){
		var currentTime = new Date ( );
		var currentYear = currentTime.getFullYear();
		var currentMonth = currentTime.getMonth();
		var currentDay = currentTime.getDate();
		var currentHours = currentTime.getHours ( );
		var currentMinutes = currentTime.getMinutes ( );
		var currentSeconds = currentTime.getSeconds ( );
		
		currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
		currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
		currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
		/*var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
		currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;*/
		currentHours = ( currentHours == 0 ) ? "00" : currentHours;
		
		var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds; // + " " + timeOfDay;
		
		document.getElementById("clock").firstChild.nodeValue = currentTimeString;
	}
	//--END CLOCK--
	//--FADE EFFECTS--
		//MUST SET BEFORE CALLING THESE FUNCTIONS
		//EX: var _ttf = 1000.0;

	function fade(eid){
	  var element = document.getElementById(eid);
	  if(element == null){
		return;
	  }
	  if(element.FadeState == null){
		if(element.style.opacity == null
			|| element.style.opacity == ''
			|| element.style.opacity == '1'){
		  element.FadeState = 2;
		}else{
		  element.FadeState = -2;
		}
	  }
	   
	  if(element.FadeState == 1 || element.FadeState == -1){
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = _ttf - element.FadeTimeLeft;
	  }else{
		element.FadeState = element.FadeState == 2 ? -1 : 1;
		element.FadeTimeLeft = _ttf;
		setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
	  }  
	}
	
	function animateFade(lastTick, eid){  
	  var curTick = new Date().getTime();
	  var elapsedTicks = curTick - lastTick;
	  var element = document.getElementById(eid);
	  if(element.FadeTimeLeft <= elapsedTicks){
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = '
			+ (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		return;
	  }
	 
	  element.FadeTimeLeft -= elapsedTicks;
	  var newOpVal = element.FadeTimeLeft/_ttf;
	  if(element.FadeState == 1){
		newOpVal = 1 - newOpVal;
	  }
	  element.style.opacity = newOpVal;
	  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
	 
	  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
	}
	//--END FAFE EFFECTS
	
	//--AJAX FUNCTIONS--
	//--GENERAL AJAX REQUESTS--
	
	//returns XMLHttpRequest Object based on browser
	function ajaxReq(){
		var xmlhttp;
		if (window.XMLHttpRequest){
			//for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}else{
			//for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xmlhttp;
	}
	
	//Sends XMLHttpRequest to url using GET with or without a link posted var, input, and returns
	///responsetext to specified HTML Element, eid.
	//BASE FOR ALL GET-ResponseText calls
	function ajaxGetTxt(eid,url,input){
		var xmlhttp;
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			document.getElementById(eid).innerHTML=xmlhttp.responseText;
			return;
			}
		  }
		  if(input != null && url != null){
			  url += "?i="+input;
		  }
		  xmlhttp.open("GET",url,true);
		  xmlhttp.send();
	}
	//Sends XMLHttpRequest to url using GET with or without a link posted var, input, and returns
	//an XMLResponse Object
	//BASE FOR ALL GET-XMLResponse calls
	function ajaxGetXML(url,input){
		var xmlhttp;
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				return xmlhttp.responseXML;
			}
		}
		if(input != null && url != null){
			url += "?i="+input;
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send();
	}
	
	//Sends XMLHttpRequest to url using POST with or without a posted var, input, and returns
	///responsetext to specified HTML Element, eid.
	//BASE FOR ALL POST-ResponseText calls
	function ajaxPostTxt(eid,url,input){
		var xmlhttp;
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById(eid).innerHTML=xmlhttp.responseText;
				return;
			}
		}
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(input != null){
			var ins;
			ins = "i="+input;
			xmlhttp.send(ins);
		}else{
			xmlhttp.send(null);
		}
	}
	
	//Sends XMLHttpRequest to url using POST with or without a posted var, input, and returns
	//an XMLResponse object
	//BASE FOR ALL POST-XMLResponse calls
	function ajaxPostXML(url,input){
		var xmlhttp;
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				return xmlhttp.responseXML;
			}
		}
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(input != null){
			var ins;
			ins = "i="+input;
			xmlhttp.send(ins);
		}else{
			xmlhttp.send(null);
		}
	}
	//--END GENERAL AJAX REQUESTS--
	
	//--PAGE AJAX REQUESTS--
	function getSitemPage(page){
		ajaxPostTxt('content_sitem',page);
	}
	function searchWork(input){
		ajaxGetTxt('s_res',"post/searchWork.php",input);
	}
	function getVote(int)
	{
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			document.getElementById("poll").innerHTML=xmlhttp.responseText;
			}
		  }
		xmlhttp.open("GET","post/vote.php?i="+int,true);
		xmlhttp.send();
	}
	function getContent(sid,cid){
			
	}
	//--END PAGE AJAX REQUESTS--
	
	//--FORM AJAX REQUESTS--
	function checkUser(input){
		//ajaxGetTxt('u','post/checkUser.php',input);
		var xmlhttp;
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById("u").innerHTML=xmlhttp.responseText;
				if(xmlhttp.responseText != ""){
					_FE = true;
				}
				return;
			}
		}
		xmlhttp.open("POST",'post/checkUser.php',true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(input != null){
			var ins;
			ins = "i="+input;
			xmlhttp.send(ins);
		}else{
			xmlhttp.send(null);
		}	
	}
	function checkPass(input){
		ajaxPostTxt('p','post/checkPass.php',input);
	}
	function checkPassSame(input,input2){
		var xmlhttp, eid, url;
		eid = "p2";
		url = "post/checkPassSame.php";
	  	xmlhttp = ajaxReq();
		xmlhttp.onreadystatechange=function(){
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			document.getElementById(eid).innerHTML=xmlhttp.responseText;
			return;
			}
		  }
		  xmlhttp.open("POST",url,true);
		  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		  if(input != null){
			  var ins;
			  ins = "i="+input+"&ii="+input2;
			  xmlhttp.send(ins);
		  }else{
			  xmlhttp.send(null);
		  }
	}
	function checkEmail(input){
		ajaxGetTxt('e','post/checkEmail.php',input);
	}
	function goto(form) { 
		var index=form.select.selectedIndex;
		if (form.select.options[index].value != "0") {
			location=form.select.options[index].value;
		}
	}
	//--END FORM AJAX REQUESTS--
	//--END AJAX FUNCTIONS--
	
	//--FORM FUNCTIONS--
	
	function formStyleOptional(inputObj,color){
		if(inputObj.value == "[Optional]"){
			formClearElement(inputObj);
			inputObj.style.color = "#"+color;
		}
	}
	function formStyleOptionalReset(inputObj,color){
		if(inputObj.value == ""){
			inputObj.value = "[Optional]";
			inputObj.style.color = "#"+color;
		}
	}
	function formClearElement(inputObj){
		inputObj.value = "";
	}
	function textCounter(field,eid,maxlimit) {
		if (field.value.length > maxlimit){ // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
		}else{ // otherwise, update 'characters left' counter
			document.getElementById(eid).innerHTML = field.value.length;
		}
	}
	//--END FORM FUNCTIONS--
	
	
	//LIGHTBOX GALLERY FUNCTIONS
	function loadLBG(g,num){
		if(num == 0 || num == null){
			loadLBGb(g);
			loadLBGa(g);
		}else{
			loadLBGbt(g,num);
			loadLBGat(g,num);
		}
	}
	function loadLBGb(g){
		var curr = g.content.getFirst();
		document.getElementById("lbg_b").innerHTML = "";
		while(curr.data.getCID() != g.current.data.getCID()){
			if(curr.data.getCID() != g.current.data.getCID()){
				document.getElementById("lbg_b").innerHTML += "<a href='content/img/t/"+curr.data.getPath()+"."+curr.data.getType()+"' title='"+curr.data.getName()+"' rel='lightbox[gallery]' /><br />\n";
			}
 			curr = curr.next;
		}
	}
	function loadLBGa(g){
		var curr = g.current.next;
		document.getElementById("lbg_a").innerHTML = "";
		while(curr != null){
			if(curr.data.getCID() != g.current.data.getCID()){
				document.getElementById("lbg_a").innerHTML += "<a href='content/img/t/"+curr.data.getPath()+"."+curr.data.getType()+"' title='"+curr.data.getName()+"' rel='lightbox[gallery]' /><br />\n";
			}
 			curr = curr.next;
		}
	}
	function loadLBGbt(g,num){
		var curr = g.content.getFirst();
		var end = g.current;
		//for(i = 0; i < num; i++){if(end.prev != null){end = end.prev;}}
		document.getElementById("lbg_b").innerHTML = "";
		while(curr.data.getCID() != end.data.getCID()){
			if(curr.data.getCID() != end.data.getCID()){
				document.getElementById("lbg_b").innerHTML += "<a href='content/img/t/"+curr.data.getPath()+"."+curr.data.getType()+"' title='"+curr.data.getName()+"' rel='lightbox[gallery]' /><br />\n";
			}
 			curr = curr.next;
		}
	}
	function loadLBGat(g,num){
		var curr = g.current;
		for(i = 0; i < num; i++){curr = curr.next;if(curr == null){break;}}
		document.getElementById("lbg_a").innerHTML = "";
		while(curr != null){
			if(curr.data.getCID() != g.current.data.getCID()){
				document.getElementById("lbg_a").innerHTML += "<a href='content/img/t/"+curr.data.getPath()+"."+curr.data.getType()+"' title='"+curr.data.getName()+"' rel='lightbox[gallery]' /><br />\n";
			}
 			curr = curr.next;
		}
	}
	//END LIGHTBOX GALLERY FUNCTIONS
	//XML DOC FUNCTIONS
	function loadXMLDoc(name)
	{
		if (window.XMLHttpRequest){
			xhttp=new XMLHttpRequest();
		}else{
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xhttp.open("GET",name,false);
		xhttp.send();
		return xhttp.responseXML;
	}
	function loadXML(text){
		if (window.DOMParser){
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
		}else{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(text); 
		} 	
		return xmlDoc;
	}
	//END XML DOC FUNCTIONS
	//IMAGE DOM PRELOADER FUNCTIONS
	function preloadImages(imgp){
		var imgs = new Array();
		for(i = 0; i < imgp.length; i++){
			imgs[i] = new Image();
			imgs[i].src = imgp[i];
		}
	}
	function preloadImage(imgp){
		var img = new Image();
		img.src = imgp;
	}
	//END IMAGE DOM PRELOADER FUNCTIONS
	//MISC FUNCTIONS
	function removeElement(parentDiv, childDiv){
		 if (childDiv == parentDiv) {
			  alert("The parent div cannot be removed.");
		 }
		 else if (document.getElementById(childDiv)) {     
			  var child = document.getElementById(childDiv);
			  var parent = document.getElementById(parentDiv);
			  parent.removeChild(child);
		 }
		 else {
			  alert("Child div has already been removed or does not exist.");
			  return false;
		 }
	}
	function isset(varname) {
		if(typeof( window[ varname ] ) != "undefined") return true;
		else return false;
	}
	function include(filename) //NOT USED
	{
		var head = document.getElementsByTagName('head')[0];
		script = document.createElement('script');
		script.src = filename;
		script.type = 'text/javascript';
		
		head.appendChild(script)
	}
	function rand(l,u){
		 return Math.floor((Math.random() * (u-l+1))+l);
	}
	function cleanString(ins){
		var s = ins.replace("\\","!");
		s = s.replace("\"","!");
		s = s.replace("'","!");
		s = s.replace(";","!");
		return s;	
	}
	//END MISC FUNCTIONS
//END GENERAL FUNCTIONS
