// Sink.js
// A lot of JavaScript to throw at a small problem.

/* global values */
var selected_curr;
var req;
var queryString;
var seriesNum;
var frame_section;
/* end globals */

window.onload = function(){
	if( isMainPage() )
	{
		if( artl = document.getElementById('artlink') )
			artl.onclick = artlink;
		if( anil = document.getElementById('animationlink') )
			anil.onclick = anilink;
		headMain = document.getElementById('head_main');
		headArt  = document.getElementById('head_art');
		headAni  = document.getElementById('head_animation');
		headSer  = document.getElementById('head_series');
		headWha  = document.getElementById('head_about');
		headers = new Array(headMain, headArt, headAni, headSer, headWha);
		for(i=0;i<headers.length;i++)
		{
			headers[i].onmouseover = function(){ highlightHeader( this ); };	
			headers[i].onmouseout = function(){ clearHeader( this ); };
			headers[i].onclick = function(){ selected( this ); };
		}
	}
	else
	{
		if(headMain = document.getElementById('head_main'))
		{
			headMain.onmouseover = function(){ highlightHeader( this ); };	
			headMain.onmouseout = function(){ clearHeader( this ); };
		}
	}
	
	ims = document.getElementsByTagName('img');
	for(i=0;i<ims.length;i++)
	{
		thumb = /thumb/;
		if(thumb.exec(ims[i].src)) 
		{
			setOpacity( ims[i], 7 );
			ims[i].onmouseover = function(){ setOpacity(this, 10); };
			ims[i].onmouseout = function(){ setOpacity(this, 7); };
		}
	}
	preloadImages('site_images/logo.highlight.jpg', 'site_images/logo.reg.jpg');
}

function isMainPage()
{
	indreg1 = /index.php/;
	indreg2 = /^http:\/\/www\.sinkpotemkin\.com\/$/;
	indreg3 = /^http:\/\/www.sinkpotemkin.com\/\?/;
	if( indreg1.exec(this.location.href) || indreg2.exec(this.location.href) || indreg3.exec(this.location.href))
	{
		return true;
	}
	return false;
}

function setOpacity(obj, val) {
	obj.style.opacity = val/10;
	obj.style.filter = 'alpha(opacity=' + val*10 + ')';
}

function preloadImages()
{
	images = new Array();
	for(i=0;i<arguments.length;i++)
	{
		images[i] = new Image();
		images[i].src = 'http://www.sinkpotemkin.com/' + arguments[i];
	}
}

function headerClick()
{
	url = 'http://www.sinkpotemkin.com';
	location.href = url;
}


function selected( el )
{
	var nodeId = el.id;
	reg = /head_(.+)/;
	if( nodeId != selected_curr )
	{
		// First close the open element;
		if(old = document.getElementById(selected_curr) )
		{
			for(i=0; i<old.parentNode.childNodes.length; i++)
			{
				var oldid = old.parentNode.childNodes[i].id;
				if(reg.exec(oldid) )
				{
					old.parentNode.childNodes[i].className = 'header';
				}
				if(old.parentNode.childNodes[i].id == 'content' )
					old.parentNode.childNodes[i].className = 'hide';
			}
		}
		// Open the new
		selected_curr = nodeId;
		for(i=0; i<el.parentNode.childNodes.length; i++)
		{
			if(el.parentNode.childNodes[i].id == 'content' )
				el.parentNode.childNodes[i].className = 'bodydiv highlighted';
		}
		frame_section = reg.exec(nodeId)[1];
		// Save this page in the sessions
		url = 'http://www.sinkpotemkin.com/savsec.php?sec=' + nodeId;
		loadXMLDoc(url);
		// loadFrameContent(section[1]);
	}
	else
	{
		// User clicked on the already active window, make sure to send them back to default content.
		frame_section = reg.exec(nodeId)[1];
		seriesNum = 0;
		loadFrameContent(frame_section[1]);
	}
}

function newQuery(query)
{
	queryString = query;	
}

function loadFrameContent(section)
{
	if(selection = document.getElementById(section + 'f'))
	{
		query = '';
//		if(queryString.length > 0 ) query = query + queryString + '&';
		if(seriesNum > 0) query = query + 'sid=' + parseInt(seriesNum);
		url = './' + section + 'Frame.php?' + query;
		if( selection.src != url )
		{
			selection.src = url;	
		}
	}
}

function highlightHeader(el)
{
	if( el.id != selected_curr )
	{
		document.getElementById(el.id).className = 'header highlighted';
	}
}

function clearHeader( el )
{
	if( el.id != selected_curr )
	{
		el.className = 'header';
	}
}

function highlightLink(el)
{
	var tags = document.getElementsByName('artlink');
	for(i=0;i<tags.length;i++)
	{	
		tags[i].style.fontWeight = 'normal';
	}
	el.style.fontWeight = 'bold';
}

function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
   try {
	   req = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    } catch(e) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
}

function processReqChange() 
{
	// only if req shows "complete"
    if (req.readyState == 4) {
		if( isMainPage() ) loadFrameContent(frame_section);
	}
}

// Legacy functions
function artlink( event )
{
	window.open( "./recent.php?type=p", "_self" );
}

function anilink( event )
{
	window.open( "./recent.php?type=a", "_self" );	
}
