// JavaScript Document

function hideAllWindows() {
	hideElement("tahestik_full");
	hideElement("tahestik_jutt_full");
	hideElement("portfolio_full");
	hideElement("workers_full");
}

function hideElement(id) {
	var e = document.getElementById(id);
	if(e != null) {
		e.style.display = "none";
	}
}

function showElement(id) {
	var e = document.getElementById(id)
	if(e != null) {
		e.style.display = "block";
	}
}

function setLeft(id) {
	var e = document.getElementById(id);
	var c = document.getElementById("page_container")
	if(e != null && c != null) {
		e.style.left = (mouseX - c.offsetLeft - 50) + "px";
	}
}

function setFooterWidth(newWidth) {
	document.getElementById("footer_moving").style.width = newWidth * 80 +"px";
	document.getElementById("footer_moving").style.left = 0;
}

function getContent(conditions, destination) {
	document.getElementById(destination).style.top = 0;
	send_post(null, "load_content.php?"+ conditions, destination);
}

function send_post(formName, URLto, destination) { 
	var isIE;
	//var loadingElement = document.getElementById("loadingbar");
	//var wholePage = document.getElementById("whole_page");
	
	if(formName != null) {
	    var xmlMessage = buildPOST(formName);
	}

	if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		isIE = true;
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(http) {
	    http.open("GET", URLto, false);
		http.setRequestHeader("Method", "GET " + URLto + " HTTP/1.1");
	    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    //http.setRequestHeader('Content-Type','text/html;charset=UTF-8');
		
		if(formName == null) {
			if(isIE == true) {
			    http.send();
			} else {
			    http.send(null);
			}
		} else {
			http.send(xmlMessage);
		}
	
		display_response(destination);
		//wholePage.style.visibility = "visible";
		//loadingElement.style.visibility = "hidden";
	}
} 

function display_response(destination) { 
    var optionDiv = document.getElementById(destination);

    optionDiv.innerHTML = http.responseText; 
} 
 
 
function taust(a, change) {
	a.style.backgroundColor = change;
	if(change) {
		a.style.color = '#000000';
	} else {
		a.style.color = '';
	}
}

var IE = document.all?true:false

var mouseX = 0;
var mouseY = 0;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e) {
	if (IE) {
		mouseX = event.clientX + document.body.scrollLeft
		mouseY = event.clientY + document.body.scrollTop
	} else {
		mouseX = e.pageX
		mouseY = e.pageY
	}
	if (mouseX < 0) {
		mouseX = 0
	}
	if (mouseY < 0) {
		mouseY = 0
	}
	return true;
}

var topX = 0;
var topY = 0;
var bottomX = 0;
var bottomY = 0;

var mouseOverTop = 0;
var mouseOverBottom = 0;

function changeDivLocation() {
	if(IE) {
		screenX = document.body.offsetWidth;
		screenY = document.body.offsetHeight;
	} else {
		screenX = window.innerWidth;
		screenY = window.innerHeight;
	}

	objTop = document.getElementById("top");
	objBottom = document.getElementById("footer");
	objTopMoving = document.getElementById("top_moving");
	objBottomMoving = document.getElementById("footer_moving");
	
	topX = objTop.offsetLeft;
	topY = objTop.offsetTop;

	bottomX = objBottom.offsetLeft;
	bottomY = objBottom.offsetTop;
	
	topLeft = parseInt(objTopMoving.style.left ? objTopMoving.style.left : "0");
	bottomLeft = parseInt(objBottomMoving.style.left ? objBottomMoving.style.left : "0");

	if(mouseOverTop) {
		newValue = parseInt(topLeft - (screenX/2 - mouseX)/100);
		if(newValue <= 0 && newValue >= -parseInt(objTopMoving.style.width ? objTopMoving.style.width : "0")+900) {
			objTopMoving.style.left = newValue + "px";
		}
	}
	
	if(mouseOverBottom) {
		newValue = parseInt(bottomLeft - (screenX/2 - mouseX)/100);

		if(newValue <= 0 && newValue >= -parseInt(objBottomMoving.style.width ? objBottomMoving.style.width : "0")+900) {
			objBottomMoving.style.left = parseInt(bottomLeft - (screenX/2 - mouseX)/100) + "px";
		}
	}

	setTimeout("changeDivLocation()", 20);
	return true;
}
