
function Main () {
	
	this.page;
	this.header;
	this.mainMenu;
	this.content;
	this.footer;
	this.footerClass;
	this.headerHolder;
	this.mainMenuHolder
	this.contentHolder
	this.bg;
	this.homeBg
	this.footerHolder
	this.dataProvider;
	this.currentID = 'Work';
	this.currentTemplate = 'worktemplate';
	this.defaultTag;
	this.currentTags;
	this.template;
	this.isIDevice = false;
	this.isiPad;
	this.isiPhone
	this.isiPod
	this.isIE;
	this.lan = 'de';
	this.changeByTag = false;
	this.hasScroll = false;
	this.contentYPos;
	this.favourites;
	this.favouritesID;
	this.browserHeight;
	this.browserWidth;
	this.pageXPos;
	this.pageYPos = 0;
	this.cookieName = 'elastiqueFavourites'
	this.videoHost = 'http://elastique-video-delivery.s3.amazonaws.com/';
	
	if (window.location.hostname == 'localhost') {
		this.serverRoot = 'http://localhost/';
		this.absolutPath = 'http://localhost/elastique/programmierung/';
	} else {
		this.serverRoot = 'http://www.elastique.de/';
		this.absolutPath = 'http://www.elastique.de/';
	}
	
	if (window.location.hostname == 'elastique.de') {
		window.location.href = 'http://www.elastique.de';
	}

	this.pageWidth = 1000;
	this.menuClass;
	this.footerYPos;
	this.counter = 0;
	this.includes = new Array('js/jquery/jquery-ui-1.8.4.custom.min.js',
							  'js/net/DataProvider.js',
							  'js/page/layout.js', 
							  'js/page/Header.js', 
							  'js/page/MainMenu.js', 
							  'js/page/MainMenuItem.js', 
							  'js/page/ServiceNaviItem.js',
							  'js/page/Footer.js', 
							  'js/templates/HomeTemplate.js',
							  'js/templates/TextTemplate.js',
							  'js/templates/WorkTemplate.js',
							  'js/templates/InfoTemplate.js',
							  'js/templates/NewsTemplate.js',
							  'js/templates/ProjectTemplate.js',
							  'js/templates/FavouriteTemplate.js',
							  'js/components/FavouritePreview.js',
							  'js/components/ProjectPreview.js',
							  'js/components/TopNews.js',
							  'js/components/News.js',
							  'js/components/Award.js',
							  'js/components/RelatedProject.js',
							  'js/components/Formular.js',
							  'js/components/FormularButton.js',
							  'js/components/MediaDisplay.js',
							  'js/video/VideoPlayer.js');
}

Main.prototype.init = function () {
	
	this.isiPad = navigator.userAgent.match(/iPad/i) != null;
	this.isiPhone = navigator.userAgent.match(/iPhone/i) != null;
	this.isiPod = navigator.userAgent.match(/iPod/i) != null;
	if (this.isiPad || this.isiPhone || this.isiPod) {
		this.isIDevice = true;
	}
	this.isIE = navigator.userAgent.match(/IE/i) != null
	this.setDefaults();

	
}

Main.prototype.setDefaults = function () {

	if (location.pathname != '' && location.pathname != '/') {
		var pathArr = location.pathname.split('/');
		this.lan = pathArr[1];
		this.currentID = pathArr[2];
		var hrefArr = top.location.href.split('//');
		var uri = hrefArr[0]+'//'+location.hostname+'/#'+location.pathname;
		top.location.href = uri;
	} else if (location.hash != '' && location.hash != '/'){
		var hashArr = location.hash.split('/');
		
		var checkArr =  hashArr[hashArr.length-1].split('=');
		if (checkArr[0] == 'myFavourites') {
			this.favouritesID = checkArr[1];
		}
		
		this.lan = hashArr[1];
		if (this.favouritesID == undefined) {
			this.currentID = hashArr[2];
			if (hashArr.length >= 4) {
				this.currentTags = new Array();
				for (var i = 0; i < hashArr.length; i++) {
					if (i >= 3) {
						this.currentTags.push(decodeTag(hashArr[i]));
					}
				}
			}
		} else {
			this.currentID = 'myFavourites'
		}
	}
	this.loadIncludes(this.includes[this.counter]);
	
}

Main.prototype.loadIncludes = function (file) {
	if (file != undefined) {
		var scope = this;
		var incs = this.includes;
		$.ajax({
			type: "GET",
			url: this.absolutPath+file,
			dataType: "script",
			success: function(msg){ 
				scope.counter++;
				scope.loadIncludes(scope.includes[scope.counter]);
			}
		});
	} else {
		this.loadData();
	}
}

Main.prototype.loadData = function () {
	this.dataProvider = new DataProvider();
	this.dataProvider.init();

}

Main.prototype.getFavourite = function () {
	this.dataProvider.getCookie(this.cookieName);
	
}

Main.prototype.setFavourite = function (value) {
	if (value != undefined && value != '') {
		this.favourites = new Array();
		var strArr = value.split(',');
		for (var i = 0; i < strArr.length; i++) {
			this.favourites.push(strArr[i]);
		}
	}
	this.render();
}

Main.prototype.addFavourite = function (id) {
	if (this.favourites == undefined || this.favourites.length < 1) {
		this.favourites = new Array();
	}
	var isEnable = true;
	for (var i = 0; i < this.favourites.length; i++) {
		if (id == this.favourites[i]) isEnable = false;
	}
	if (isEnable) this.favourites.push(id);
	this.dataProvider.setCookie(this.cookieName, this.favourites);
	
	this.dataProvider.getTitleIDByTemplate('favouriteTemplate', this.dataProvider.dataTree.childNodes);
	this.setCurrentID(this.dataProvider.currentTitleID);
}

Main.prototype.refreshCookie = function () {
	this.dataProvider.setCookie(this.cookieName, this.favourites);
}


Main.prototype.render = function () {

	var htmlBody = getBody();
	var bodyTag = document.body;
	bodyTag.innerHTML = htmlBody;
	this.page = document.getElementById('page');
	
	this.bg = document.getElementById('contentBG');
	this.homeBg = document.getElementById('homeBGContainer');

	this.headerHolder = document.getElementById('headerHolder');
	var htmlHeader = getHeader();
	this.headerHolder.innerHTML = htmlHeader;
	this.header = new Header();
	this.header.init(this);
	
	this.mainMenuHolder = document.getElementById('mainMenuHolder');
	this.mainMenuHolder.style.visibility = 'hidden';
	this.menuClass = new MainMenu();
	this.menuClass.init(this.dataProvider.dataTree, this.mainMenuHolder);
	this.mainMenu = document.getElementById('mainMenu');

	this.contentHolder = document.getElementById('contentHolder');
	this.contentHolder.style.visibility = 'hidden';
	var htmlContent = getContent();
	this.contentHolder.innerHTML = htmlContent;
	this.content = document.getElementById('content');
	
	this.footerHolder = document.getElementById('footerHolder');
	this.footerHolder.style.visibility = 'hidden';
	this.footerClass = new Footer();
	this.footerClass.init(this.footerHolder);

	this.footer = document.getElementById('footer');
	
	this.hideFooter();

	var bg = document.getElementById('mainBack');
	if (window.addEventListener) {
		window.addEventListener('resize', this.onResizeEvent.context(this), false); 
		bg.addEventListener('mousedown', this.onMouseDownEvent.context(this), false); 
		window.addEventListener('scroll', this.onScrollEvent.context(this), false); 
	} else if (window.attachEvent) {
		window.attachEvent('onresize', this.onResizeEvent.context(this));
		bg.attachEvent('onmousedown', this.onMouseDownEvent.context(this), false); 
		window.attachEvent('scroll', this.onScrollEvent.context(this), false); 
	}
	

	
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.handleChange.context(this));
	
	if (this.currentID == undefined || this.currentID == '' || this.currentID == null) {
		this.currentID = 'Home';
	}

	if (this.currentTags == undefined) {
		this.currentTags = new Array();
		this.dataProvider.searchTagName(mainClass.dataProvider.tags.childNodes, '0B52E2B8-80A1-382E-86BF-E7916F97746C');
		this.defaultTag = this.dataProvider.tempTagName;
		this.currentTags = new Array(this.defaultTag);
	}
	this.setCurrentID(this.currentID);
	this.resizePage();

}

Main.prototype.onMouseDownEvent = function () {
	if (this.currentTemplate.toLowerCase() == 'favouritetemplate') {
		if (this.template.projects.length > 0) {
			history.back();
		}
	}
}

Main.prototype.setCurrentID = function (id) {

	if (id != 'myFavourites') {
		var strArr = id.split('/');
		this.currentID = strArr[0];
		var hashValue = '/'+this.lan;
		var titleValue = '';// = this.lan+' - ';
		
		if (strArr.length > 1) {
			this.currentTags = new Array(strArr[1]);
		}
		
		this.dataProvider.searchTagName(mainClass.dataProvider.tags.childNodes, '0B52E2B8-80A1-382E-86BF-E7916F97746C');
		
		if (this.currentID != undefined && this.currentID != '') {
			hashValue += '/'+this.currentID;
			this.dataProvider.getTemplateByID(this.currentID, this.dataProvider.dataTree.childNodes);
			if (this.dataProvider.currentTemplate == 'workTemplate') {
				if (this.currentTags == undefined) {
					this.currentTags = new Array(this.defaultTag);
				}
				var tagStr = '';
				for (var i = 0; i < this.currentTags.length; i++) {
					var tempTag = encodeTag(this.currentTags[i]);
					tagStr += '[##]'+tempTag;
				}
				hashValue += tagStr.split('[##]').join('/');
				
				// set tags to Title
				//titleValue += tagStr.split('[##]').join(' - ');
			} else {
	            // AUR SELECTED ZURÜCK SETZTEN ?????????
				//this.currentTags = new Array(this.defaultTag);
			}
			titleValue += this.currentID+' - ';
		}
		if(hashValue == SWFAddress.getValue()) {
			this.setContent();
		} else {
			SWFAddress.setValue(hashValue);
			SWFAddress.setTitle(titleValue+' Elastique.');
		}
		track('/'+titleValue);
	} else {
		this.currentTemplate = 'favouriteTemplate';
		this.dataProvider.getTitleIDByTemplate(this.currentTemplate, this.dataProvider.dataTree.childNodes);
		this.currentID = this.dataProvider.currentTitleID;
		this.setContent();
	}
}

Main.prototype.toggleLan = function (newLan) {
	this.lan = newLan;
	this.setCurrentID(this.currentID);
}

Main.prototype.handleChange = function (event) {

	if (!this.changeByTag) {
  		var strArr = event.value.split('/');
  		this.lan = strArr[1];
  		this.currentID = strArr[2];
		
		var checkArr =  strArr[2].split('=');
		if (checkArr[0] == 'myFavourites') {
			this.favouritesID = checkArr[1];
		}
		
		if (this.favouritesID == undefined) {
			if (strArr.length >= 4) {
				this.currentTags = new Array();
				for (var i = 0; i < strArr.length; i++) {
					if (i > 2) {
						var tempTag = decodeTag(strArr[i]);
						this.currentTags.push(tempTag);
					}
				}
			}
		}else {
			this.currentTemplate = 'favouriteTemplate';
			this.dataProvider.getTitleIDByTemplate(this.currentTemplate, this.dataProvider.dataTree.childNodes);
			this.currentID = this.dataProvider.currentTitleID;
		}
  		this.setContent();
	}
	this.changeByTag = false;
}

Main.prototype.setContent = function () {
	
	this.hasScroll = false;
	
	this.hideFooter();
	if (this.currentTemplate == 'workTemplate') {
		this.pageYPos = $(window).scrollTop();
	}
	
	if (this.currentTemplate == 'projectTemplate') {
		this.pageYPos = this.pageYPos;
	} else  if (this.currentTemplate != 'workTemplate') {
		this.pageYPos = 0;
	}
	
	///alert('******** '+this.pageYPos+' - '+this.currentTemplate);
	
	
	$('html,body').animate({scrollTop: 0}, 0);

	mainClass.dataProvider.searchIDInPools(this.currentID);
	
	this.menuClass.searchActiveItem(this.currentID);
	this.footerClass.searchActiveItem(this.currentID);
	if (this.currentID != 'Home') {
		this.dataProvider.getTemplateByID(this.currentID, this.dataProvider[mainClass.dataProvider.currentPool].childNodes);
		this.currentTemplate = this.dataProvider.currentTemplate;
	} else {
		this.currentTemplate = 'worktemplate';
	}
	if (this.currentTemplate == undefined || this.currentTemplate == null ||this.currentTemplate == '') {
		this.setCurrentID('Work');
	}
	
	if (this.template != undefined) {
		
		if (this.currentTemplate.toLowerCase() != 'hometemplate') {
			var scope = this;
			var logoHome = document.getElementById('homeLogo');
			var homeMask = document.getElementById('homeMask');
			if (homeMask != null) {
				logoHome.style.visibility = 'hidden';
				$(this.headerHolder).stop();
				$(this.mainMenuHolder).stop();
				$(homeMask).stop();
				$(this.headerHolder).animate({top: '0px'}, 700, 'easeOutQuart');
				$(this.mainMenuHolder).animate({top: '0px'}, 700, 'easeOutQuart');
				
				$(homeMask).animate({height: '0px'}, 700, 'easeOutQuart', 
					function() {
						scope.initTemplate();
						if (homeMask.parentNode != null) {
							homeMask.parentNode.removeChild(homeMask);
						}
				});
				
			} else {
				this.initTemplate();
				//$(this.content).stop().animate({opacity:0}, 150, 'easeOutQuart', 
					//function() {
					//scope.initTemplate();
				//});
			}
		} else {
			this.initTemplate();
		}
	} else {
		this.initTemplate();
	}

}

Main.prototype.onScrollEvent = function () {
	if (this.currentTemplate == 'workTemplate' && !this.hasScroll) {
		if ($(window).scrollTop() > 0) {
			this.hasScroll = true;
		}
	}
}


Main.prototype.autoScroll = function () {	
	if (!this.hasScroll) {
		$('html,body').animate({scrollTop: this.pageYPos}, 0);
	}
}

Main.prototype.initTemplate = function () {

	this.content.innerHTML = '';
	this.resizePage();
	//this.footer.style.top = '2000px';
	this.content.style.height = '0px';
	$(this.content).animate({opacity:1.00}, 150, 'easeOutQuart');
	
	switch (this.currentTemplate.toLowerCase()) {
		case 'hometemplate':
			this.template = new HomeTemplate();
			break;
		case 'infotemplate':
			this.template = new InfoTemplate();
			break;
		case 'worktemplate':
			this.template = new WorkTemplate();
			break;
		case 'favouritetemplate':
			this.template = new FavouriteTemplate();
			break;
		case 'projecttemplate':
			this.template = new ProjectTemplate();
			break;
		case 'newspagetemplate':
			this.template = new NewsTemplate();
			break;
		case 'texttemplate':
			this.template = new TextTemplate();
			break;
		default: 
			this.template = new TextTemplate();
			break;
	}
	
	if (this.currentTemplate.toLowerCase() == 'favouritetemplate') {
		this.menuClass.setTint('white');
	} else {
	  this.resizeBG(10, false);
		this.menuClass.setTint('black');
	}
	
	this.template.init(this.content, this.currentID);

}

Main.prototype.hideFooter = function () {
	this.footerHolder.style.visibility = 'hidden';
}

Main.prototype.showFooter = function () {
	this.footerHolder.style.visibility = 'visible';
}

Main.prototype.onResizeEvent = function () {
    this.resizePage();
}

Main.prototype.resizePage = function () {
	
	//this.footerHolder.style.visibility = 'visible';
	
	this.setPageSize();

	this.page.style.left = this.pageXPos+'px'; 
	this.contentYPos = this.mainMenu.offsetTop + this.mainMenu.offsetHeight + 58;
	
	if (this.currentTemplate.toLowerCase() != 'hometemplate') {
		
		this.content.style.top = this.contentYPos+'px';

		this.headerHolder.style.top = '0px';
		this.mainMenuHolder.style.top = '0px';
		
		//this.footerHolder.style.visibility = 'visible';
		this.footerHolder.style.display = 'block';
		$(this.footer).stop();
		var xPos = Math.floor((1000 - this.footerClass.navi.offsetWidth) / 2);
	    this.footerClass.navi.style.left = xPos+'px';
		this.setFooterPosition(this.contentYPos);
		//if (this.isIDevice) {
			this.footer.style.top = this.footerYPos+'px';
		//} else {
			//$(this.footer).stop();
			//$(this.footer).animate({top: this.footerYPos+'px'}, 700, 'easeOutQuart');
		//}
		this.headerHolder.style.visibility = 'visible';
		this.mainMenuHolder.style.visibility = 'visible';
		this.contentHolder.style.visibility = 'visible';
		
		this.bg.style.visibility = 'visible';
		this.bg.style.display = 'block';
	
	} else if (this.currentTemplate.toLowerCase() == 'hometemplate') {
		if (this.template != undefined) {
			if (this.template.isInit) {
				this.openHomePage();
			}
		}
	}
	
	/*
	if (this.currentTemplate == 'workTemplate' && this.pageYPos != 0) {
		$('html,body').animate({scrollTop: this.pageYPos}, 0);
	}
	*/
	//else {
		//$('html,body').animate({scrollTop: 0}, 0);
	//}
	
	
	//alert(this.currentTemplate+' - '+this.pageYPos);
}

Main.prototype.openHomePage = function (total, part) {
	
	this.setPageSize();
	
	
	this.headerHolder.style.visibility = 'visible';
	this.mainMenuHolder.style.visibility = 'visible';
	this.contentHolder.style.visibility = 'visible';
	
	var headY = this.browserHeight - 15;
	var headerDiv = document.getElementById('header');
	$(headerDiv).stop();
	headerDiv.style.height = '15px';
	
	var mainY = this.browserHeight - 150;
	
	$(this.mainMenu).stop();
	this.mainMenu.style.top = '61px';
	
	this.footerHolder.style.visibility = 'hidden';
	this.footerHolder.style.display = 'none';
	
	this.bg.style.visibility = 'hidden';
	this.bg.style.display = 'none';

	this.homeBg.style.left = -this.pageXPos+'px'; 
	var mask = document.getElementById('homeMask');

	mask.style.width = this.browserWidth+'px'; 
	mask.style.height = '0px'; 
	var logoHome = document.getElementById('homeLogo');
	
	
	if(!this.template.isInit) {
		if (!this.isIE) {
			$(logoHome).animate({opacity:0.00}, 0, 'easeOutQuart');
		}
		$(this.headerHolder).stop();
		$(this.mainMenuHolder).stop();
		$(mask).stop();
		$(this.headerHolder).animate({top: headY+'px'}, 700, 'easeOutQuart');
		$(this.mainMenuHolder).animate({top: mainY+'px'}, 700, 'easeOutQuart');		
		$(mask).animate({height: this.browserHeight+'px'}, 700, 'easeOutQuart', 
			function() {
				if (!mainClass.isIE) {
					$(logoHome).animate({opacity:1.00}, 700, 'easeOutQuart');
					
				}
				logoHome.style.visibility = 'visible';
			}
		);
	} else {
		this.headerHolder.style.top = headY+'px';
		this.mainMenuHolder.style.top = mainY+'px';		
		mask.style.height = this.browserHeight+'px';
	}

	var mediaContainer = document.getElementById('homeMediaContainer');
	var media = document.getElementById('homeMedia');

	if (mediaContainer != null && media != null) {
		
		var p = this.browserWidth / this.browserHeight;
		var m = this.template.orginWidth / this.template.orginHeight;
		if (p == m) {
			media.width = this.browserWidth;
			media.height = this.browserHeight;
		} else if (p <= m) {
			media.height = this.browserHeight;
			var p = this.getPercent(this.template.orginHeight, this.browserHeight)
			var tempWidth = this.getSize( this.getPercent(this.template.orginHeight, this.browserHeight), this.template.orginWidth);
			media.width = tempWidth;
		} else if (p > m) {
			media.width = this.browserWidth;
			var tempHeight = this.getSize(this.getPercent(this.template.orginWidth, this.browserWidth), this.template.orginHeight);
			media.height = tempHeight;
		}
		var pWidth;
		pWidth = this.browserWidth;
		var mediaX = Math.floor((media.width - pWidth) / 2);
		var mediaY = Math.floor((media.height - this.browserHeight) / 2);
		mediaContainer.style.left = -mediaX+'px';
		mediaContainer.style.top = -mediaY+'px';
	}
	this.template.isInit = true;
}

Main.prototype.setPageSize = function () {
	if (window.innerWidth) {
  	   this.pageXPos = Math.floor((window.innerWidth - this.page.offsetWidth) / 2);
  	   this.browserHeight = window.innerHeight;
	   this.browserWidth = window.innerWidth;
  	} else if (document.body.offsetWidth) {
       this.pageXPos = Math.floor((document.body.offsetWidth - this.page.offsetWidth) / 2);
       this.browserHeight = document.body.offsetHeight;
	   this.browserWidth = document.body.offsetWidth;
    }
	if (this.pageXPos <= 12) {
		this.pageXPos = 12;
	}
}


Main.prototype.getPercent = function (total, part) {
	var p = (part * 100) / total;
	return p;
}
		
Main.prototype.getSize = function (percent, total) {
	var v = Math.floor((total / 100) * percent);
	return v;
}

Main.prototype.resizeBG = function (h, fadeIn) {
	var scope = this
	$(this.bg).animate({
		height: h+'px'
		}, {
		duration: 400, 
		specialEasing: {
	 		top: 'easeOutBack'
		}, 
		complete: function() {
	  		if (fadeIn) {
				scope.template.showProjects();
	  		}
		}
	});
}

Main.prototype.getValue = function (str) {
	var strArr = str.split('px');
	var value = Number(strArr[0]);
	return value;
}

Main.prototype.toggleDivs = function (yPos) {
	
	if (this.currentTemplate.toLowerCase() != 'hometemplate') {
		var menuY = yPos + 46;
		this.contentYPos = menuY + this.mainMenu.offsetHeight + 58;
		
		$(this.mainMenu).stop();
		$(this.mainMenu).animate({top:menuY+'px'}, {queue:false, duration: 600, specialEasing:{ top: 'easeOutBack'}});
		
		$(this.content).stop();
		$(this.content).animate({top:this.contentYPos+'px'}, {queue:false, duration: 500, specialEasing:{ top: 'easeOutBack'}});
		
		this.setFooterPosition(this.contentYPos);
		$(this.footer).stop();
		$(this.footer).animate({top:this.footerYPos+'px'}, {queue:false, duration: 400, specialEasing:{ top: 'easeOutBack'}});
		
		if (this.currentTemplate.toLowerCase() == 'favouritetemplate') {
			var bgHeight = this.contentYPos + this.content.offsetHeight - 75;
			$(this.bg).animate({height:bgHeight+'px'}, {queue:false, duration: 400, specialEasing:{ top: 'easeOutBack'}});
		}
	}
}

Main.prototype.setFooterPosition = function (contentY) {
	this.setPageSize();
	if (contentY + this.content.offsetHeight + this.footer.offsetHeight +10 <= this.browserHeight) { 
		this.footerYPos = this.browserHeight - this.footer.offsetHeight -10;
	} else {
		this.footerYPos = contentY + this.content.offsetHeight;
	}
} 


Function.prototype.context = function(obj){
	var method = this, temp;
	temp = function(){
		return method.apply(obj, arguments);
	}
	return temp;
} 

function setDeeplink (id) {
	mainClass.setCurrentID(id);
}

function encodeTag (str) {
	var split1 = str.split(' & ').join('+');
	split1 = split1.split('/').join('|');
	split1 = split1.split('-').join('_');
	split1 = split1.split(' ').join('-');
	return split1;
}


function decodeTag (str) {
	var split1 = str.split('+').join(' & ');
	split1 = split1.split('|').join('/');
	split1 = split1.split('-').join(' ');
	split1 = split1.split('_').join('-');
	return split1;
}

function setTag (id) {
	
	var tagArray = id.split('/');
	mainClass.currentTags = new Array();
	for (var i = 0; i < tagArray.length; i++) {
		var tempTag = decodeTag(tagArray[i]);
		mainClass.currentTags.push(tempTag);
	}
	mainClass.changeByTag = true;
	document.location.hash = '/'+mainClass.lan+'/'+mainClass.currentID+'/'+tagArray.join('/');
	mainClass.template.selectProjects();
	mainClass.hideFooter();

}

function setProjectTag (id) {
	mainClass.currentTags = new Array();
	mainClass.currentTags.push(id);
	mainClass.dataProvider.getTitleIDByTemplate('workTemplate', mainClass.dataProvider.dataTree.childNodes);
	mainClass.setCurrentID(mainClass.dataProvider.currentTitleID);
}



