// JavaScript Document

var sema=false;

function getWindowWidth () {
	var myWidth = 0;
	var subWidth = 500;
	
	if( typeof( window.innerWidth ) == 'number' ) {  
		//Non-IE  
		myWidth = window.innerWidth;  
	} else if( document.documentElement && document.documentElement.clientWidth ) {  
		//IE 6+ in 'standards compliant mode'  
		myWidth = document.documentElement.clientWidth;  
	} else if( document.body && document.body.clientWidth ) {  
		//IE 4 compatible  
		myWidth = document.body.clientWidth;  
	} 
	
	while ((subWidth + 600) < myWidth) {
		subWidth += 500;
	}
	return subWidth;
	
}

function getWindowHeight () {
	var myWidth = 0;
	var subWidth = 500;
	
	var ie_offset = 0;
		if( typeof( window.innerWidth ) == 'number' ) {  
		//Non-IE  
		myWidth = window.innerWidth;  
		myHeight = window.innerHeight;  
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {  
		//IE 6+ in 'standards compliant mode'  
		myWidth = document.documentElement.clientWidth;  
		myHeight = document.documentElement.clientHeight;  
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {  
		//IE 4 compatible  
		myWidth = document.body.clientWidth;  
		myHeight = document.body.clientHeight;  
	} 
	
	return myHeight;
	
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

function displayReferences(){
	if (!sema)
	{
		sema = true;	
		opacity('refs', 0, 100, 1000);
		opacity('refslider', 0, 100, 1000);
	}
	return false;
}

function hideReferences(){
	sema = false;
	opacity('refs', 100, 0, 500);
	opacity('refslider', 100, 0, 500);
}


/**********************************/


var pos = 0;
var interval = 0;
var intervalLeft = 0;
var intervalRight = 0;

var posContent = 0;
var scrollWidth = 3360;
var scrolloffset = 0;

var myWidth = 0;
var myHeight;

var stopLeftFlag = false;
var stopRightFlag = false;



function InitSize(){
	var ie_offset = 0;
		if( typeof( window.innerWidth ) == 'number' ) {  
		//Non-IE  
		myWidth = window.innerWidth;  
		myHeight = window.innerHeight;  
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {  
		//IE 6+ in 'standards compliant mode'  
		myWidth = document.documentElement.clientWidth;  
		myHeight = document.documentElement.clientHeight;  
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {  
		//IE 4 compatible  
		myWidth = document.body.clientWidth;  
		myHeight = document.body.clientHeight;  
	} 
	intervalLeft = window.setInterval("move()", 10);
	
	// If not IE add event listener for touchscreen
	if('\v'!='v'){
		var myLinks = document.getElementsByTagName('a');
		for(var i = 0; i < myLinks.length; i++){
			myLinks[i].addEventListener('touchstart', function(){this.className = "hover";}, false);
			myLinks[i].addEventListener('touchend', function(){this.className = "";}, false);
		}
	}


}

function move() {
	posContent = posContent +  scrolloffset;
	if (scrolloffset > -0.01 && scrolloffset < 0.01)
	{
		scrolloffset = 0;
		stopLeftFlag = false;
		stopRightFlag = false;
	}
	else
	{
		if ( scrolloffset < 0 ) {
			if(stopLeftFlag) {
				scrolloffset /= 1.1;
			}
			else {
				
				if(posContent <= myWidth - scrollWidth || stopRightFlag) {
					scrolloffset /= 1.1;
					stopLeft();
				}
				else if (scrolloffset >-4)
						scrolloffset *= 1.5;
			}
		}

		if ( scrolloffset > 0)	{
			if(stopRightFlag) {
				scrolloffset /= 1.2;
			}
			else {
				if(posContent >= 60) {
					scrolloffset /= 1.2;
					stopRight();
				}
				else if (scrolloffset < 4)
						scrolloffset *= 1.5;
			}
		}
		document.getElementById("refs").style.left  = (posContent  + "px");
	}

}

function startLeft() {
	document.getElementById("left").style.backgroundImage  = "url(img/nav_grey_left.png)";
	document.getElementById("right").style.backgroundImage  = "url(img/nav_green_right.png)";
	scrolloffset = -0.25;
}

function stopLeft() {
	if(posContent <= myWidth - scrollWidth)
	{
		document.getElementById("right").style.backgroundImage  = "url(img/nav_grey_right_disabled.png)";
	}
	else
	{
		document.getElementById("right").style.backgroundImage  = "url(img/nav_grey_right.png)";
	}
	stopLeftFlag = true;
}

function startRight() {
	document.getElementById("left").style.backgroundImage  = "url(img/nav_green_left.png)";
	document.getElementById("right").style.backgroundImage  = "url(img/nav_grey_right.png)";
	scrolloffset = 0.25;
}

function stopRight() {
	if(posContent >= 0)
		document.getElementById("left").style.backgroundImage  = "url(img/nav_grey_left_disabled.png)";
	else
	{
		document.getElementById("left").style.backgroundImage  = "url(img/nav_grey_left.png)";

	}
	stopRightFlag = true;
}

