/**************************************************\
	BandADay Project
	
		A website designed to promote 
		underpopular bands on a daily basis.
	
	js/global.js -
		This file holds globally accessible 
		JavaScript functions, such as those used
		to generate popup messages and dialogs.
	
\**************************************************/

var botdshow = 0;

function doOnLoad()
{
	/* Call child onload, if it exists. */
	if (typeof(onLoadChild) == 'function')
		onLoadChild();
}

function showPopup(title, content)
{
	showBlackout();
	document.getElementById('popup-content').innerHTML = '<h4>'+title+'</h4>\n\r<p>'+content+'</p>\r\n';
	document.getElementById('popup-container').style.display = 'block';
}

function showMessage(title, content)
{
	var icon = 'info';
	if (arguments[2] != null)
		icon = arguments[2];
	showBlackout();
	document.getElementById('popup-content').innerHTML = "<form action='index.php' method='post'><h4 style='background-color: #E0E0E0;'><img alt='"+icon+"' style='border: none;' src='images/forum/"+icon+".png' />&nbsp;"+title+"</h4>\n\r" +
	"<p>"+content+
	"\r\n<div class='centered'><input type='button' class='submit' value='OK' onclick='hidePopup()' /></div>";
	document.getElementById('popup-container').style.display = 'block';
}

function hidePopup()
{
	document.getElementById('blackout').style.display = 'none';
	document.getElementById('popup-container').style.display = 'none';
}

function showDialog(title, content, todo)
{
	var icon = 'info';
	if (arguments[3] != null)
		icon = arguments[3];
	showBlackout();
	document.getElementById('popup-content').innerHTML = "<form action='index.php' method='post'  onsubmit='javascript:return false;'><h4 style='background-color: #E0E0E0;'><img alt='"+icon+"' style='border: none;' src='images/forum/"+icon+".png' />&nbsp;"+title+"</h4>\n\r" +
	"<p>"+content+
	"\r\n<div class='centered'>\r\n" +
	"<input type='submit' class='submit' value='OK' onclick='"+todo+"; return false;' />\r\n" +
	"<input type='button' class='submit' value='Cancel' onclick='hidePopup()' />\r\n" +
	"</div>";
	document.getElementById('popup-container').style.display = 'block';
}

function limitText(field, length) 
{
	if (field.value.length > length)
		field.value = field.value.substring(0, length);
}

function ajaxRequest(callback, query)
{
	if (window.XMLHttpRequest) // any modern browser
		xmlhttp=new XMLHttpRequest();
	else // IE ;-;
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	
	xmlhttp.onreadystatechange=callback;
	xmlhttp.open("POST","ajaxcall.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(query);
}

function checkError(str)
{
	return !(str.length > 7 && str.substr(0,7) == "error: ");
}

function getError(str)
{
	return str.substr(7);
}

function hilight(obj)
{
	obj.className = "hilight";
}

function unhilight(obj)
{
	obj.className = '';
}

function toggleSettings()
{
	if (document.getElementById('settingsbox').style.display == 'block')
		document.getElementById('settingsbox').style.display = 'none';
	else
		document.getElementById('settingsbox').style.display = 'block';
}

/* Band of the Day Panel-related */
function toggleBOTD()
{
	var status = (document.getElementById('botd-showhide').src.indexOf('nav/control_eject') != -1);
	var expiry = new Date(); expiry.setTime(expiry.getTime() + 31536000000);
	if (status)
	{
	
		document.cookie = "showbotd=0; expires="+(expiry.toGMTString());
		document.getElementById('botd-top-album').style.display = 'none';
		document.getElementById('ddhr').style.display = 'none';
		document.getElementById('botd-info').style.display = 'none';
		document.getElementById('botd-showhide').src = "images/nav/control_uneject.png";
		document.getElementById('botd-clicktag').innerHTML = "Band of the Day: " + document.getElementById('botd-name').innerHTML;
	}
	else
	{
		document.cookie = "showbotd=1; expires="+(expiry.toGMTString());
		document.getElementById('botd-top-album').style.display = 'block';
		document.getElementById('botd-info').style.display = 'block';
		document.getElementById('botd-showhide').src = "images/nav/control_eject.png";
		document.getElementById('botd-clicktag').innerHTML = "Band of the Day";
		document.getElementById('ddhr').style.display = 'block';
	}
}

function doTheme(color)
{
	var expiry = new Date(); expiry.setTime(expiry.getTime() + 31536000000);
	document.cookie = 'colortheme='+color+'; expires='+(expiry.toGMTString());
	
	document.getElementById('themered').className = '';
	document.getElementById('themeblue').className = '';
	document.getElementById('themeblack').className = '';
	document.getElementById('theme'+color).className = 'sel';
	
	document.getElementById('header').className = color;
	document.getElementById('userbox').className = color;
	document.getElementById('userbox-left').className = color;
	document.getElementById('userbox-center').className = color;
	document.getElementById('userbox-right').className = color;
	document.getElementById('belowlogo').className = color;
	document.getElementById('belowlogotext').className = color;
	document.getElementById('lfm_big').src = 'images/styles/'+color+'/lastfm_custom.png';
	document.getElementById('lfm_small').src = 'images/styles/'+color+'/lastfm_small.gif';
}

function doEditor(edit)
{
	if (edit == 0)
	{
		document.getElementById('editorplain').className = 'sel';
		document.getElementById('editorfull').className = '';
		if (typeof(editorOff) != 'undefined')
			editorOff();
	}
	else
	{
		document.getElementById('editorplain').className = '';
		document.getElementById('editorfull').className = 'sel';
		if (typeof(editorOn) != 'undefined')
			editorOn();
	}
	ajaxRequest(updateEditor, "page=dosettings&editor="+edit);
}

function updateEditor()
{
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		if (!checkError(xmlhttp.responseText))
			showMessage("An error occurred", getError(xmlhttp.responseText), 'error');
}

function doListenLink(ll)
{
	if (ll == 0)
	{
		document.getElementById('listenusinglastfm').className = 'sel';
		document.getElementById('listenusingspotify').className = '';
	}
	else if (ll == 1)
	{
		document.getElementById('listenusinglastfm').className = '';
		document.getElementById('listenusingspotify').className = 'sel';
	}
	else
	{
		showMessage("???", "What exactly are you trying to do?", 'error');
		return;
	}
	ajaxRequest(updateListenLink, "page=dosettings&listenlink="+ll);
}

function updateListenLink()
{
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		if (!checkError(xmlhttp.responseText))
			showMessage("An error occurred", getError(xmlhttp.responseText), 'error');
}		

function nextBand()
{
	botdshow -= 1;
	if (botdshow == 0)
		document.getElementById('botd-next').style.display = 'none';
	ajaxRequest(updateBOTD, "page=getbotd&botd="+botdshow);
}

function priorBand()
{
	botdshow += 1;
	document.getElementById('botd-next').style.display = 'block';
	ajaxRequest(updateBOTD, "page=getbotd&botd="+botdshow);
}

function updateBOTD()
{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		if (checkError(xmlhttp.responseText))
		{
			var data = xmlhttp.responseText.split("<divisor>");
			// check that it has 2 elements, please.
			document.getElementById('botd-info').innerHTML = data[0];
			document.getElementById('botd-top-album').innerHTML = data[1];
			if (document.getElementById('botd-showhide').src.indexOf('nav/control_eject') == -1)
				document.getElementById('botd-clicktag').innerHTML = "Band of the Day: " + document.getElementById('botd-name').innerHTML;
		}
		else
		{
			showMessage("An error ocurred", getError(xmlhttp.responseText), 'error');
			botdshow -= 1;
		}
	}
}

function showSearchSuggestions()
{
	var searchstr = document.getElementById("searchbar").value;
	if (searchstr.length <= 0)
	{
		document.getElementById("searchdiv").style.display = "none";
		document.getElementById("searchdiv").innerHTML = "";
		return;
	}
	
	ajaxRequest(getSearchSuggestions, "page=searchrecs&q="+searchstr);
}

function getSearchSuggestions()
{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		if (checkError(xmlhttp.responseText))
		{
			document.getElementById('searchdiv').innerHTML = xmlhttp.responseText;
			document.getElementById('searchdiv').style.display = 'block';
		}
		else
		{
			hideSearchSuggestions();
		}
	}
}

function hideSearchSuggestions()
{
	document.getElementById("searchdiv").style.display = "none";
}

function searchDiv(e) 
{
	var event = e || window.event;
	if (event.stopPropagation)
		event.stopPropagation();
	else
		event.cancelBubble = true;
}

function showBlackout()
{
	/* 
		Code below adapted from LightBox (Version 1)
		
		Lightbox JS: Fullsize Image Overlays 
		by Lokesh Dhakar - http://www.huddletogether.com
		
		The code below this line is Licensed under the
		Creative Commons Attribution 2.5 License.
		http://creativecommons.org/licenses/by/2.5/
	*/
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight) 
	{ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else 
	{ 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{ 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	} 
	else 
	{
		pageWidth = xScroll;
	}
	// END OF LIGHTBOX CODE


	document.getElementById('blackout').style.width = pageWidth;
	document.getElementById('blackout').style.height = pageHeight;
	document.getElementById('blackout').style.display = 'block';
}
