/****************************************
*                                       *
*          2c-Interactive B.V.          *
*      http://www.2c-interactive.com    *
*                                       *
****************************************/

var model;
var stageControl;

window.onload = function(){
	model = new Model();
	stageControl = new StageControl();
	if(SWFheader.checkVersion()){
		stageControl.startUp();
		stageControl.repositionElements();
		window.onresize = function(){
			stageControl.repositionElements();
		}
	}
}

//Contains all usefull data
function Model()
{
	this.header = document.getElementById("header");
	this.page = document.getElementById("page");
	this.pagePaddingLeft = 108;
	this.footer = document.getElementById("footer");
	this.footerPaddingLeft = 108;
	this.body = document.getElementById("body");
	this.spacer = document.getElementById("spacer");
	this.spacerPaddingLeft = 108;
	this.spacerTd = document.getElementById("spacerTd");

	//Returns display width
	function stageWidth(){
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
		 		width = window.innerWidth;
		 	}
		 	if (navigator.appName.indexOf("Microsoft")!=-1) {
	  			width = document.body.offsetWidth;
		 	}
		}
		return width;
	}

	//Returns display height
	function stageHeight(){
		var height;
		if (window.innerHeight > 0) {
			height = window.innerHeight
		} else if (document.documentElement.clientHeight > 0) {
			height = document.documentElement.clientHeight
		} else {
			height = document.body.clientHeight
		}
		return height;
	}

	//public functions
	this.stageWidth = stageWidth;
	this.stageHeight = stageHeight;
}


//Handles all events
function StageControl()
{
	//Sets styles properties in elements
	function startUp(){
		model.page.style.position = "absolute";
		model.page.style.paddingLeft = 0 + "px";
		model.page.style.marginLeft = 0 + "px";
		model.page.style.marginRight = 0 + "px";
		model.page.style.top = 350 + "px";
		model.page.style.zIndex = 1;
		model.header.style.position = "absolute";
		model.header.style.top = 0 + "px";
		model.header.style.marginLeft = 0 + "px";
		model.header.style.marginRight = 0 + "px";
		model.header.style.zIndex = 2;
		model.footer.style.position = "absolute";
		model.footer.style.paddingLeft = 0 + "px";
		model.footer.style.marginLeft = 0 + "px";
		model.footer.style.marginRight = 0 + "px";
		model.spacer.style.position = "absolute";
		model.body.style.textAlign = "left";
	}

	//Starting point for resize & reposition
	function repositionElements(){
		repositionHeader();
		repositionPage();
		repositionFooter();
		//repositionFooterTop();
	}

	//Repositions header
	function repositionHeader(){
		var stageWidth = model.stageWidth();
		var headerWidth = model.header.offsetWidth;
		var calculated = (stageWidth / 2) - (headerWidth / 2);
		model.header.style.left = (calculated < 0) ? 0 + "px" : calculated + "px";
	}

	//Repositions page
	function repositionPage(){
		var stageWidth = model.stageWidth();
		var calculated = ((stageWidth / 2) - (model.header.offsetWidth / 2));
		model.page.style.left = (calculated < 0) ? 0 + model.pagePaddingLeft + "px" : calculated + model.pagePaddingLeft + "px";
	}

	//Repositions footer
	function repositionFooter(){
		var stageHeight = model.stageHeight();
		var stageWidth = model.stageWidth();
		var calculatedLeft = ((stageWidth / 2) - (model.header.offsetWidth / 2));
		var calculatedTop = stageHeight - model.footer.offsetHeight;
		var topPx = model.page.offsetHeight + model.footer.offsetHeight + 350;
		model.footer.style.left = (calculatedLeft < 0) ? 0 + model.footerPaddingLeft + "px" : calculatedLeft + model.footerPaddingLeft + "px";
		model.footer.style.top = (stageHeight < topPx) ? model.page.offsetHeight + 350 + "px" : calculatedTop + "px";
		repositionSpacer(calculatedTop, topPx);
	}

	function repositionSpacer(calculatedTop, topPx){
		var stageWidth = model.stageWidth();
		var stageHeight = model.stageHeight();
		model.spacer.style.display = (stageHeight < topPx) ? "none" : "block";
		var calculatedLeft = ((stageWidth / 2) - (model.header.offsetWidth / 2));
		model.spacer.style.left = (calculatedLeft < 0) ? 0 + model.spacerPaddingLeft + "px" : calculatedLeft + model.spacerPaddingLeft + "px";
		model.spacer.style.top = model.page.offsetHeight + 350 + "px";
		var calculateHeight = calculatedTop - (model.page.offsetHeight + 350);
		model.spacerTd.style.height = (stageHeight < topPx) ? "1px" : calculateHeight + "px";
	}

	//public functions
	this.startUp = startUp;
	this.repositionElements = repositionElements;
}



/* -- OPEN FUNCTIONS -- */



function setIndex(index){
	if(index){
		model.header.style.zIndex = 2;
	} else {
		model.header.style.zIndex = 2;
	}
}

//alert(document.getElementById("page").offsetHeight);
//flashProxy.call("inbound", "arr");
