//entries.js for atmhb.com
//03/08 - 12/11

Array.prototype.hasObject = (
	!Array.indexOf ? function (obj)
	{
		var l = this.length + 1;
		while (l -= 1)
		{
			if (this[l - 1] === obj)
				return true;
		}
		return false;
	} : function (obj)
	{
		return (this.indexOf(obj) !== -1);
	});

// Add a class name to an element if it's not already present
function addClassName(obj, className)
{
	if (!obj)
		return;
	
	var classNames = obj.className.split(" ");
	if (!classNames.hasObject(className))
	{
		classNames.push(className);
		obj.className = classNames.join(" ");
	}
}

// Remove a class name from an element if it's present
function removeClassName(obj, className)
{
	if (!obj)
		return;
	
	var classNames = obj.className.split(" ");
	var i = classNames.length;
	var changed = false;
	while (i -= 1)
	{
		if (classNames[i] == className)
		{
			classNames.pop();
			changed = true;
		}
	}
	
	if (changed)
		obj.className = classNames.join(" ");
}

var numEntries = 0; // entry count - to be redefined externally
var entryIndex = -1; // current "open" entry
var entryTypes = ['game', 'video', 'app', 'music', 'website'];

function showEntry(iconObj, colSeq)
{
	if (!iconObj)
		return;

	// get index from id (number after "entryicon")
	var ind = parseInt(iconObj.id.substr(9));

	// check if this entry's already open
	if (entryIndex == ind)
	{
		hideEntry();
		return;
	}
	
	// deselect any other selection
	if (entryIndex > -1)
		hideEntry();
		
	// select icon
	addClassName(iconObj, "entryiconselected");
	
	// show entry
	var contentObj = document.getElementById("entry" + ind);
	removeClassName(contentObj, "entrycontenthidden");

	// set this index
	entryIndex = ind;

	// start blend (StartColourEffect and defCol defined in colourblender.js)
	StartColourEffect(document.body, (colSeq ? colSeq : new Array(defCol)));
	
	// scroll to show entire entry
	var entryInfo = getElementInfo(contentObj);
	var screenInfo = getScreenInfo();
	var entryRight = entryInfo.x + entryInfo.w;
	var screenRight = screenInfo.x + screenInfo.w;
	var scrollOffset = entryRight - screenRight;
	if (scrollOffset > 0)
	{
		//alert(scrollOffset);
		window.scrollBy(scrollOffset, 0);
	}
	
	// "refresh" element for WebKit browsers
	contentObj.style.webkitTransform = 'scale(1)';
}

function hideEntry()
{
	// check for valid index
	if (entryIndex == -1 || entryIndex >= numEntries)
		return;
		
	// reset blend (StartColourEffect and defCol defined in colourblender.js)
	StartColourEffect(document.body, new Array(defCol));

	// deselect icon
	var iconObj = document.getElementById("entryicon" + entryIndex);
	removeClassName(iconObj, "entryiconselected");

	// hide entry		
	var contentObj = document.getElementById("entry" + entryIndex);
	addClassName(contentObj, "entrycontenthidden");
		
	entryIndex = -1; // define no selection
}

// Entry type filter click handler
function filterEntries()
{
	// get a list of checked filters
	var numFilters = entryTypes.length;
	var filtered = new Array();
	var i = 0;
	for(; i<numFilters; i++)
	{
		var filterObj = document.getElementById("cbFilter_" + entryTypes[i]);
		if (!filterObj || !filterObj.checked)
			filtered.push(entryTypes[i]);
	}
	
	// amend entires' styles depending on type
	for(i=0; i<numEntries; i++)
	{
		var iconObj = document.getElementById("entryicon" + i);
		if (!iconObj)
			continue;
		
		// get type from className "entryicon xxx-type"
		var entryType = iconObj.className.substring(10, iconObj.className.indexOf("-type"));
		
		// test for filtering: hide if filtered
		if (filtered.hasObject(entryType))
		{
			// deselect if selected
			if (entryIndex == i)
				hideEntry();
			
			addClassName(iconObj, "entryiconhidden");
		}
		else
			removeClassName(iconObj, "entryiconhidden");
		
	}
}

// Get screen size and scrolling data
// (based on http://www.howtocreate.co.uk/tutorials/javascript/browserwindow Dec. 2011)
function getScreenInfo()
{
	var w, h = 0;
	//Non-IE
	if (typeof( window.innerWidth ) == 'number')
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	//IE 6+ in 'standards compliant mode'
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	//IE 4 compatible
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	var x = 0, y = 0;
	//Netscape compliant
	if (typeof(window.pageYOffset) == 'number')
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
		
	}
	//DOM compliant
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	//IE6 standards compliant mode
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}

	return { "x": x, "y": y, "w": w, "h": h };
}

function getElementInfo(obj)
{
	var pos = getElementPosition(obj);
	var w = 0, h = 0;
	if (obj)
	{
		w = obj.offsetWidth;
		h = obj.offsetHeight;
	}
	return { "x": pos.x, "y": pos.y, "w": w, "h": h };
}

function getElementPosition(obj)
{
	var left = 0, top = 0;
	while (obj)
	{
		left += obj.offsetLeft;
		top += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return { "x": left, "y": top };
}
