/*
	scrolling.js
	
	used to make the images on the home page scroll accross the screen.
*/

var scrolling = false;
var timer = setTimeout("",3000);
var interval = 70;

function ProjectScrollInit(initit) {

	if(! initit)
	{
		var f = function() { ProjectScrollInit(true); };
		setTimeout(f, 500);
		return;
	}

	var pdiv = document.getElementById('projects');

	if(! pdiv) return;

	var pchilddivs = pdiv.getElementsByTagName('div');
	
	var pchild = pchilddivs[0];
	var count = pchilddivs.length

	for(var i = 0; i < count; i ++)
	{	
		pchilddivs[i].onmouseover = ProjectFocus; 		// function() { ProjectFocus(); ProjectStopScroll(); };
		pchilddivs[i].onmouseout = ProjectStartScroll;	// function() { ProjectStartScroll(); };
		
		
		pchilddivs[i].style.width = pchilddivs[i].getElementsByTagName("img")[0].width;
	
		var pchildcopy = pchilddivs[i].cloneNode(true);
		pchildcopy.onmouseover = ProjectFocus;
		//pchildcopy.onmouseout = ProjectStartScroll;
		pdiv.appendChild(pchildcopy);
	}

	scrolling = true;
	ProjectScroll();
	
}

function ProjectStartScroll(e) {
	scrolling = true;
	ProjectScroll();
}

function ProjectStopScroll(e) {
	scrolling = false;
}

var fixed = false;
var count = -1;

function ProjectFocus(e) {

	if (!e) var e = window.event;
	
	if(!e) return;
	
	ProjectStopScroll();
	
	
	var el = e.target || e.srcElement;
	
	var x = e.clientX || e.mouseX;
	var y = e.clientY || e.mouseY;

	

	// make sure the current node is the 
	// container div
	while(el.tagName != 'DIV')
		el = el.parentNode;
		
		
	
	// get the indicator element
	var indicator = document.getElementById('indicator');
	indicator.style.visibility = 'hidden';
	indicator.style.display = 'block';
	
	
	
	// using JSON get the value of the content attribute of the current div
	// {'proj':'project title','client':'client name','id':'project_id'}
	// info.proj -> project title
	// info.client -> client name
	var info = eval( '(' + el.getAttribute('content') + ')');
	
	indicator.getElementsByTagName('h3')[0].firstChild.nodeValue = info.proj;
	indicator.getElementsByTagName('span')[0].firstChild.nodeValue = info.client;


	// set the top (and bottom and right)
	indicator.style.top = y - 5 + 'px';
	indicator.style.bottom = 'auto';
	indicator.style.right = 'auto';
	
		
	
	// check to see if the mouse is on the left or right side of the screen;
	
	winwidth = document.getElementsByTagName('body')[0].clientWidth;
	
	if( (x + indicator.clientWidth + 30 ) > (winwidth) )
	{
		indicator.style.left = x - 25 - indicator.clientWidth + 'px';	
		indicator.className = 'right';
	}
	else
	{
		indicator.style.left = x + 35 + 'px';
		indicator.className = 'left';
	}
	
	//return;

	indicator.style.visibility = 'visible';
	
	
	
	this.onmousemove = Follow;
	
}

function Follow(e)
{

	if(!e) var e = window.event;
	
	if(!e) return;

	var x = e.clientX || e.mouseX;
	var y = e.clientY || e.mouseY;
	
	
	
	var indicator = document.getElementById('indicator');
	indicator.style.top = y - 5 + 'px';
	indicator.style.bottom = 'auto';
	indicator.style.right = 'auto';
	
	// check to see if the mouse is on the left or right side of the screen
	
	winwidth = document.getElementsByTagName('body')[0].clientWidth;
	//
	if( (x + indicator.clientWidth + 30 ) > (winwidth) )
	{
		indicator.style.left = x - 15 - indicator.clientWidth + 'px';	
		indicator.className = 'right';
	}
	else
	{
		indicator.style.left = x + 25 + 'px';
		indicator.className = 'left';
	}
}

function ProjectScroll()
{
	clearTimeout(timer);

	if( !scrolling ) return;

	document.getElementById('indicator').style.visibility = 'hidden';
		
	var pdiv = document.getElementById('projects');

	var pchilddivs = pdiv.getElementsByTagName('div');
	
	var pchild = pchilddivs[0];
	var plastchild = pchilddivs[pchilddivs.length - 1];	
	
	if(pchild.offsetWidth - Math.abs(pdiv.offsetLeft) < -5)
	{
		pdiv.style.left = "-6px";
		pdiv.appendChild(pchild);
	}

	var left = (pdiv.style.left)? parseInt(pdiv.style.left) : 0;

	pdiv.style.left = left - 3 + "px";
	timer = setTimeout("ProjectScroll()", interval);
}


if(!document.getElementById && document.all) 
{ 
	document.getElementById = new Function('id', 'return document.all[id]') 
}


function standardizeEvent(event) {

	if( !event.stopPropagation )
	{
		event.stopPropagation = new Function('this.cancelBubble = true');
	}

	if ( !event.preventDefault ) {
		event.preventDefault = new Function('this.returnValue = true')
	}

	if( typeof event.layerX == 'undefined' &&
	    typeof event.offsetX == 'number' ) {
	
		event.layerX = event.offsetX
		event.layerY = event.offsetY
	}

	if( !event.target && event.srcElement ) {
	
		event.target = event.srcElement

		if ( event.type == 'onmouseout' ) {
			event.relatedTarget = event.toElement

		} 
		else if( event.type == 'onmouseover' ) {
			event.relatedTarget = event.fromElement
		}
	}
}
