// Redirect to Container Site

//Concatenate a URL to to index-state
function concatURL()
{
	var protocol = window.location.protocol;
	var host = window.location.host;
	var urlPathArray = window.location.pathname.split( '/' );
	var startSite ='index.php';

	var url = protocol + '//' + host;

	for ( i = 0; i < urlPathArray.length - 1; i++ ) 
	{
		if(urlPathArray[i] != "")
		{
			url += '/';
			url += urlPathArray[i];
		}

	}
	
	url += '/' + startSite + '?section=';
	
	return url;
}

//Redirects to the requested section with body id
function redirect(section) 
{
	var url = concatURL();

	window.location = url + section + "";
}

//Checks if there's a manual access of a single site
//Returns TRUE when accessing direct, else FALSE (of course)
function isDirectAccess()
{
	url = window.location.href;
	//alert(url.substring(url.length - 9, url.length) + "\n");
	
	//alert((url.substring(url.length - 9, url.length) == "index.php"));
	//alert(url.indexOf('section') == -1);
	
	return url.indexOf('section') == -1 && !(url.substring(url.length - 9, url.length) == "index.php");
}

function loaded()
{	
	var isDirect = isDirectAccess();
	//alert("isDirect :: " + isDirect);
	
	//Check if there's a redirection required
	if(isDirect)
	{
		//Grab the actual body id
		//var bodyID = document.body.getAttribute('id');
		var bodyID = getElementsByClass('subPage', 'div')[0].getAttribute('id');
		//alert("redirect");
		redirect(bodyID);
	}
}

//--------------------
//	Context menu blocking
//--------------------

function dummy()
{
	return false;	
}

function disableContext()
{
	document.body.oncontextmenu = dummy;
}

//--------------------
//	Div Anchors
//--------------------

function divAnchor()
{
	var classElements = getElementsByClass('listItem', 'div');
	
	for(i = 0; i < classElements.length; i++) 
	{
		if(classElements[i] != null)
		{
			classElements[i].onclick = function() { redirect(this.id); };
		}
	} 
}


function getElementsByClass(className, tagName) 
{ 
	var node = document;
	
	if (tagName === null) 
	{
		tagName = '*';
	}

	var tags = node.getElementsByTagName(tagName);
	var elements = new Array(tags.length);
	var tcl = " " + className + " ";
	
	var j = 0;
	
	for(i = 0; i < tags.length; i++) 
	{ 
		var temp = " " + tags[i].className + " ";
		
		if (temp.indexOf(tcl) != -1)
		{
			elements[j] = tags[i];
			
			j++;
		}
	} 
	
	return elements;
} 


//--------------------
//	Distribute
//--------------------

function process()
{		
	//alert(5);
	loaded();

	disableContext();

	divAnchor();	
	//alert('done');
}

//Add onload function
window.onload = process;	
