function gebid(id) {
	return document.getElementById(id);
}

function ProcessStateChange() {
	switch(document.lBoxXMLCommand) {
		case 'new item':
			if(req.readyState == 4) {
				setMessage(document.lBoxXMLNewNid, document.lBoxXMLTitle);
				display_box();
			}
			break;
		case 'remove item':
			if(req.readyState == 4) {
				lBoxRemoveItem(document.lBoxXMLNewNid);
			}
			break;
		case 'add vp item':
			if(req.readyState == 4) {
				setAdminMsg(document.lBoxXMLNewNid + ' added to viewing platform');
				if (gebid('vpClick-' + document.lBoxXMLNewNid)){
					gebid('vpClick-' + document.lBoxXMLNewNid).innerHTML = '<a href="#" onclick="javascript:vp_remove_item(' + document.lBoxXMLNewNid + ');return false;">Remove from viewing platform</a>';
				}
			}
			break;
		case 'remove vp item':
			if(req.readyState == 4) {
				setAdminMsg(document.lBoxXMLNewNid + ' removed from viewing platform');
				if (gebid('vpClick-' + document.lBoxXMLNewNid)){
					gebid('vpClick-' + document.lBoxXMLNewNid).innerHTML = '<a href="#" onclick="javascript:vp_add_item(' + document.lBoxXMLNewNid + ');return false;">Add to viewing platform</a>';
				}
			}
			break;
		case 'update active vp':
			if(req.readyState == 4) {
				if (document.oldVp) {
					document.oldVp.sort();
					for(v=0;v<document.oldVp.length;v++) {
						if (gebid('vpClick-' + document.oldVp[v])){
							gebid('vpClick-' + document.oldVp[v]).innerHTML = '<a href="#" onclick="javascript:vp_add_item(' + document.oldVp[v] + ');return false;">Add to viewing platform</a>';
						}
					}
				}
				if (req.responseText == '0') {
					document.Vp = false;
				}
				else {
					document.Vp = req.responseText.split('|');
					document.Vp.sort();
					for(vn=0;vn<document.Vp.length;vn++) {
						if (gebid('vpClick-' + document.Vp[vn])){
							gebid('vpClick-' + document.Vp[vn]).innerHTML = '<a href="#" onclick="javascript:vp_remove_item(' + document.Vp[vn] + ');return false;">Remove from viewing platform</a>';;
						}
					}
				}

				document.oldVp = document.Vp;
			}
			break;
		case 'load box':
			if (req.readyState == 4) {
				if (req.responseText < 0){
					document.lBoxHTML = 'You don\'t have any items in your lightbox.';
					document.lBoxCount = 0;
				}
				else {
					document.lBoxItemsInBox = req.responseText.split('|');
					document.lBoxCount = document.lBoxItemsInBox.length;
				}
				document.lBoxIndicator = '<a href="#" onClick="toggle_box();return false;"><img id="lBoxArrow" width="5" height="7" src="images/lb_down.gif" border="0" />  There are <b><span id="lBoxCount">' + document.lBoxCount + '</span> items</b> in your lightbox.</a>';
				if (gebid('lBoxIndicator')) gebid('lBoxIndicator').innerHTML = document.lBoxIndicator;
				lBoxRefresh();
			}
			break;
	}
}


function add_item(nid, title) {
	if (req.readyState != 4) return;
	gebid('lBoxClick-' + nid).innerHTML = '<div><img width="7" height="7" src="images/add_anim.gif" /> Hold on...</div>';
	if (!document.lBoxUserLoggedIn) {
		window.location = 'http://' + document.lBoxURL + '/user/login';
		return true;
	}
	new_item(nid, title);
}

function setMessage(nid, title) {	
	switch(req.responseText) {
		case '0':
			window.location = 'user/login';
		break;
		case '1':
			gebid('lightboxWrapper1').innerHTML ='You already have this item in your lightbox.';
			document.lBoxMessageTimer = 0;
		break;
		case '2':
			gebid('lightboxWrapper1').innerHTML ='Story added to your lightbox.';
			document.lBoxMessageTimer = 0;
		break;
		case '3':
			gebid('lightboxWrapper1').innerHTML ='Story added to your lightbox with attached images.';
			document.lBoxMessageTimer = 0;
		break;
		case '4':
			gebid('lightboxWrapper1').innerHTML ='<img src="image_r/' + nid +'/t" class="pic" border="0" alt=""><div id="lightboxAdded">+ JUST<br />ADDED</div><div class="lightboxName">' + title +'</div><img src="images/see_lb.gif" width="92" height="8" border="0" alt="" align="right"><br>';
			document.lBoxCount++;
			document.lBoxItemsInBox.push(nid + ":" + title);
			lBoxRefresh();
			gebid('lBoxClick-' + nid).innerHTML = '<div><i>Added</i></div>';
			document.lBoxMessageTimer = 0;
		break;
		case '5':
			gebid('lightboxWrapper1').innerHTML ='Image not found.';
			document.lBoxMessageTimer = 0;
		break;

	}
	
}
function display_box() {
	document.lBoxVisibleDiv = 'lightboxWrapper1';
	if (!document.lastClick)	{
		document.lastClick = 0;
	}
	document.lastClick++;
	if (!document.lBoxOpen && !document.lBoxOpenP){
		open_box();
	}
	else {
		gebid('lightboxWrapper2').style.display = 'none';
		gebid('lightboxWrapper1').style.display = 'block';
	}
	
	if (!document.lBoxOpenP){
		setTimeout('fix_timer(' + document.lastClick + ')', 3000);
	}
	else {
		setTimeout('toggleWrapperDisplay()', 3000);
	}
	
}

function toggleWrapperDisplay() {
	if (document.lBoxOpen || document.lBoxOpenP){
		if(gebid('lightboxWrapper1').style.display == 'none') {
			gebid('lightboxWrapper1').style.display = 'block';
			gebid('lightboxWrapper2').style.display = 'none';
		}
		else {
			gebid('lightboxWrapper1').style.display = 'none';
			gebid('lightboxWrapper2').style.display = 'block';
		}
	}
	document.lBoxXmlBusy = false;
}

function fix_timer(lastClick){
	if((lastClick + 0) == document.lastClick) {
		close_box();
	}
}


function new_item(nid, title) {
	document.lBoxXMLCommand = 'new item';
	document.lBoxXMLNewNid = nid;
	document.lBoxXMLTitle = title;
	xml_request('http://' + document.lBoxURL + '/lightbox/add/' + nid);
}

function open_box() {
	if (document.lBoxOpen){
		return true;
	}
	document.lBoxOpen = true;
	gebid('lBoxArrow').src = 'images/lb_up.gif';
	timer = 0;
	millisec = 100;
	speed = Math.round(millisec / 100);
	var i = 0;
	for ( i = 0; i <= 240; i++ ) {
		setTimeout("set_height(" + i + ")", (timer * speed));
		timer++;
	}
}

function close_box() {
	if (document.lBoxOpen){
		gebid('lightboxWrapper1').style.display = 'none';
		gebid('lightboxWrapper2').style.display = 'none';
		document.lBoxOpen = false;
		gebid('lBoxArrow').src = 'images/lb_down.gif';
		timer = 0;
		millisec = 100;
		speed = Math.round(millisec / 100);
		var i = 0;
		for ( i = 240; i >= 0; i-- ) {
			setTimeout("set_height(" + i + ")", (timer * speed));
			timer++;
		}	
	}
	else {
		return true;
	}

	document.lBoxXmlBusy = false;
}

function set_height(h) {
	document.getElementById('daBox').style.height = h;
	if ((h == 240) && (document.lBoxOpenP || document.lBoxOpen)) {
		gebid(document.lBoxVisibleDiv).style.display = 'block';
	}
}

function load_box() {
	if(!document.lBoxItemsInBox) {
		document.lBoxItemsInBox = new Array();
	}
	//gebid('lightboxWrapper2').innerHTML = '';
	document.lBoxIndicator = 'Loading your lightbox, hold on...';
	document.lBoxXMLCommand = 'load box';
	xml_request('http://' + document.lBoxURL + '/lightbox/get');
}

function lBoxRefresh() {
	document.lBoxItemsInBox.sort(lBoxCompare);
	var htmldata = '';
	document.lBoxCount = 0;
	for(i=0;i<document.lBoxItemsInBox.length;i++) {
		if (document.lBoxItemsInBox[i] != false)	{
			tArray = document.lBoxItemsInBox[i].split(':');
			htmldata = htmldata + '<div class="lBoxItem" id="lBoxItem-' + tArray[0] + '"><img src="images/spacer.gif" width="100px" height="10px"/><Br /><a href="javascript:var w = window.top.open(\'/lightbox/view&from=' + i + '\',\'requestPopup\',\'status=yes,scrollbars=yes,resizable=yes,width=800,height=570,top=0,left=0\')"><img class="lW2img" src="http://' + document.lBoxURL + '/image_r/' + tArray[0] + '/t"/></a><div class="lW2title">' + tArray[1] + '</div><img src="images/spacer.gif" height="1" width"100" /><a href="#" onClick="javascript:remove_item(' + tArray[0] + ', \'' + tArray[1] + '\');return false;" /><img border="0" src="images/remove.gif" /></a><img src="images/spacer.gif" width="100px" height="2px"/><div class="lW2border"></div></div>';
			document.lBoxCount++;
		}
	}
	if (gebid('lBoxCount'))	{
		gebid('lBoxCount').innerHTML = document.lBoxCount;
	}
	if (document.lBoxCount == 0){
		document.lBoxHTML = 'You don\'t have any items in your lightbox.';
	}
	else {
		document.lBoxHTML = htmldata;
	}
	if (gebid('lW2wrap')) gebid('lW2wrap').innerHTML = document.lBoxHTML;
		
}

function lBoxCompare(a, b) {
	if (a && b) return (a.split(':')[0] + 0)  - (b.split(':')[0] + 0);
	else if (!a && !b) return 0;
	else if (!b) return 1;
	else return -1;
}

function remove_item(nid, title) {
	answer = confirm("Do you really want to remove this photo from your lightbox?");
	if (answer) {
		document.lBoxXMLCommand = 'remove item';
		document.lBoxXMLNewNid = nid;
		document.lBoxXMLTitle = title;
		xml_request('http://' + document.lBoxURL + '/lightbox/remove/' + nid);
	}
	if (field = gebid('lBoxClick-' + nid)) field.innerHTML = '<a href="#" onclick="add_item(' + nid + ', \'' + title + '\');return false">+ add to lightbox</a>';
}

function lBoxRemoveItem(nid) {
	for(h=0;h<document.lBoxItemsInBox.length;h++) {
		if (document.lBoxItemsInBox[h]) {
			tmpNid = document.lBoxItemsInBox[h].split(':');
			if (tmpNid[0] == nid){
				document.lBoxItemsInBox[h] = false;
			}
		}
	}

	timer = 0;
	millisec = 100;
	speed = Math.round(millisec / 50);
	var i;
	for(i=100;i>=0;i--) {
		setTimeout("set_opacity(\"" + 'lBoxItem-' + nid + "\", " + i + ")", (timer * speed));
		timer++;
	}

	
}

function set_opacity(name, value) {
	if (tmpObj = gebid(name)) {
		tmpObj.style.filter = "alpha(opacity=" + value + ")";
		tmpObj.style.opacity = value / 100;
		tmpObj.style.MozOpacity = value / 100;
	}

	if (value <= 0) {
		gebid(name).style.display = 'none';
		lBoxRefresh();
	}
}

function toggle_box() {
	document.lBoxVisibleDiv = 'lightboxWrapper2';
	if (!document.lastClick)	{
		document.lastClick = 0;
	}
	document.lastClick++;
	if (document.lBoxOpen || document.lBoxOpenP){
		document.lBoxOpenP = false;
		close_box();
	}
	else {
		document.lBoxOpenP = true;
		open_box();
	}
}
