// tokiohotel.com | 3 | functions.js

// var lang, sections+subsections werden ueber index geliefert
	
var DOM = (document.getElementById);
var fixedDivs = new Array("nav_div:543", "taskbar_div:601", "credits_div:628", "overlay_div:24", "startscreen_div:0");
var xShift = 0; 
var yShift = 0; 
var iw = window.innerWidth || document.documentElement.clientWidth;
var ih = window.innerHeight || document.documentElement.clientHeight;
var iframe = "no";

function onLoadActions() {
}


function onResizeActions() {
	centerStage();
}


function toggleNavSubsections(section, modus) {
	
	if (modus == "off") {
		var classname = "off";
		document.getElementById("nav_subsections_" + section).style.display = "none";
	} else {
		var classname = "on"; 
		document.getElementById("nav_subsections_" + section).style.display = "block";
	}
	
	id = "nav_sections_li_title_" + section;
	setClass(id, classname); 
}


function goSection(section,subsection,subsectionsrc) {	
	// alert("goSection(" + section + "," + subsection + "," + subsectionsrc + ")");

	if (section == "undefined" || section == null || section == "") {
		// navigation ueber subsection-"woerter"
		if (subsection != "" && subsection != "undefined" && subsection != null) {
			// alert("suche nach section anhand von subsection");
			getSectionFromSubsection: for (var a = 0; a < sections.length; ++a) {
				for (var b = 0; b < subsections[a].length; ++b) {
					if (subsections[a][b] == subsection) {
						section = sections[a];
						// alert("section (" + section + ") anhand von subsection (" + subsection + ") gefunden!");					
						break getSectionFromSubsection;
					}
				}
			}

		} else {
			// default laden
			section = "home";
			subsection = "";
			subsectionsrc = "";
		}
	}
	
	if (isNaN(section)) {
		// navigation ueber section-"woerter"
		// new_section_nr bestimmen:
		for (var i = 0; i < sections.length; ++i) {
			if (sections[i] == section) {
				var new_section_nr = i;
			}
		}
	} else {
		// navigation ueber skip-left-right (section-"nummern")
		var new_section_nr = curr_section_nr + section;
		if (new_section_nr < 0) {
			new_section_nr = 0;
		}
		if (new_section_nr >= sections.length) {
			new_section_nr = sections.length -1;
		}
		section = sections[new_section_nr];
	}
	curr_section_nr = new_section_nr;


	// update section-style
	if (new_section_nr <= 0) {
		setClass("nav_left", "nav_left_off");
	} else {
		setClass("nav_left", "nav_left_on");
	}
	if (new_section_nr >= (sections.length -1)) {
		setClass("nav_right", "nav_right_off");
	} else {
		setClass("nav_right", "nav_right_on");
	}

	setClass("nav_sections_li_" + curr_section, "off");
	setClass("nav_sections_li_" + section, "on");
	setClass("nav_subsections_" + curr_section + "_li_" + curr_subsection, "nav_subsection_off");
	
	// position anscrollen
	links = Number(document.getElementById(section + "_div").style.left.replace(/px/, ""));
	if (links > xShift) { 
		links = links - xShift;
	} else {
		links = 0;
	}		
	links = -1 * links;
	new Effect.MoveBy('scroll_container', 0, links, {mode:"absolute", duration:2, beforeStart:goSectionStart, afterFinish:goSectionEnd });

	// init fuer goSectionEnd():
	curr_section = section;
	
	if (subsection != "undefined" && subsection != null && subsection != 0) {
		curr_subsection = subsection;
	} else {
		// suche nach erster subsection von section
		getSubsectionFromSection: for (var a = 0; a < sections.length; ++a) {
			if (sections[a] == curr_section) {
				curr_subsection = subsections[a][0];
				// alert("curr_subsection (" + curr_subsection + ") anhand von curr_section (" + curr_section + ") gefunden!");					
				break getSubsectionFromSection;
			}
		}		
	}
	
	// subsection hiliten falls vorhanden
	setClass("nav_subsections_" + section + "_li_" + curr_subsection, "nav_subsection_on");
	
	if (subsectionsrc != "undefined" && subsectionsrc != null && subsectionsrc != 0) {
		curr_src = subsectionsrc;
	} else {
		curr_src = "";
	}
}


function goSectionStart() {
  	// alert("starting move: disable all iframes");
	for (var i = 0; i < sections.length; ++i) {
		document.getElementById(sections[i] + "_iframe_id").src = "";
		document.getElementById(sections[i] + "_iframe_id").style.display = "none";
	}
	document.getElementById("kyte_iframe_id").src = "";
	document.getElementById("kyte_iframe_id").style.display = "none";
}


function goSectionEnd(){
    // alert("loading curr_section: " + curr_section + " | curr_subsection: " + curr_subsection + " | curr_src: " + curr_src);	
	document.getElementById(curr_section + "_iframe_id").src = "/" + lang + "/" + curr_subsection + "/" + curr_src;
	document.getElementById(curr_section + "_iframe_id").style.display = "block";
	if (curr_section == "home") {
		document.getElementById("kyte_iframe_id").src = "/" + lang + "/home/kyte";
		document.getElementById("kyte_iframe_id").style.display = "block";
	}
}


// # navigation

function getHash() {
	var reqString = "";
	var hashString = window.location.hash;
	/*
	if (hashString){
		hashString = hashString.slice(hashString.indexOf('#') +1);			
		var hashArray = hashString.split('&');	
		if (hashArray.length > 0){
			for (var i=0; i<hashArray.length; i++ ) {
				var hashPair = hashArray[i].split( '=' );				
				var hashName = (escape(hashPair[0])).toLowerCase();
				var hashValue = escape(hashPair[1]);
				if (hashName && hashValue && hashName != undefined && hashValue != undefined) {
					reqString += "&" + hashName + "=" + hashValue;
				}
			}				
		}
	}	
	return reqString;
	*/
	if (hashString){
		hashString = hashString.slice(hashString.indexOf('#') +1);
	}
	return hashString;
}


function getTopHash() {
	var hashString = parent.frames.top.document.location.hash;
	if (hashString){
		hashString = hashString.slice(hashString.indexOf('#') +1);
	}
	return hashString;
}


function setHash(str) {
	// alert("setHash(" + str + ")");
	parent.frames.top.document.location.hash = str;
}


function setTopHash(str) {
	// alert("setTopHash(" + str + ")");	
	strParts = str.split("/",4);
	var hashString = strParts[2] + "/" + strParts[3];
	if (top.page == "main") {
		// current page is viewed as iframe
		if (hashString != getTopHash()) {
			parent.frames.top.document.location.hash = hashString;
		}
		iframe = true;
	} else {
		// reload top with #-str
		parent.frames.top.document.location = "/" + strParts[1] + "/#" + hashString;
	}
}


function detectHash(lang) {
	var str = getHash();
	// alert("deeplinkHash: " + "/" + lang + "/" + str);
	if (str != "" && str != "undefined") {
		strParts = str.split("/",2);
		var tmp_subsection = strParts[0];
		if (strParts[1] != "undefined" && strParts[1] != null && strParts[1] != 0) {
			tmp_src = strParts[1];
		} else {
			tmp_src = "";
		}
		// alert("detectHash: tmp_subsection: " + tmp_subsection + " | tmp_src: " + tmp_src);
		goSection("",tmp_subsection,tmp_src);
	} else {
		// load default-home
		goSection("home","","");
	}
}


function initMain() {
	// center nav exactly
	var nav_width = 0;
	nav_width += Number(document.getElementById('nav_left').offsetWidth);
	nav_width += Number(document.getElementById('nav_logo').offsetWidth);
	nav_width += Number(document.getElementById('nav_center').offsetWidth);
	nav_width += Number(document.getElementById('nav_right').offsetWidth);
	nav_width += 5; // add more space
	document.getElementById('nav').style.width = nav_width + "px";
	document.getElementById('nav').style.left = "50%";
	document.getElementById('nav').style.marginLeft = (-1 * Math.round(nav_width / 2)) + "px"; 

	// detect screensize - let user scroll if screen too small
	if (screen.width < 1024) {
		document.getElementById('body_main').style.overflowX = "scroll";
	}
	
	// detect if user scrolls -> make position corrections
	detectScroll();
}


function centerStage() {
	// alert("centerStage()");
	iw = window.innerWidth || document.documentElement.clientWidth;
	ih = window.innerHeight || document.documentElement.clientHeight;
	// equal x-margin from content to windowWidth to content (960)
	if (iw > 1200) { iw = 1200; }
	if (iw < 960) { iw = 960; }
	xShift = Math.ceil((iw - 960) / 2); 
	// equal y-margin from content to windowHeight to content (626)
	yShift = Math.ceil((ih - 626) / 2);
	if (xShift < 0) { xShift = 0; }
	if (yShift < 0) { yShift = 0; }
	var yScroll = scrollTop();

	for (var i = 0; i < fixedDivs.length; ++i) {			
		tmp = fixedDivs[i].split(':');
		obj = tmp[0];
		objY = Number(tmp[1]);
		node = document.getElementById(obj);
		if (node) {
			node.style.position = "absolute";
			if (yScroll > 0) {
				node.style.top = ((-1 * yScroll) + objY + yShift) + "px";
			} else {
				node.style.top = (objY + yShift) + "px";
			}
			node.style.position = "fixed";
		}
	}

	document.getElementById('scroll_wrapper').style.top = (yShift) + "px";
	document.getElementById('scroll_wrapper').style.width = iw + "px";
	document.getElementById('taskbar_div').style.width = iw + "px";
}


function detectScroll() {
	// user-scrollen abfragen, nur fuer main
	if (window.addEventListener) {
  		window.addEventListener("scroll", centerStage, false);
	} else if (window.attachEvent){
 		window.attachEvent("onscroll", centerStage);
	}
}
	
	
function winopen(url,name,style) { 
	var newWin = window.open(url,name,style);
	newWin.focus();
}


function toggle_plain(id) {		
	if (DOM) {
		node = document.getElementById(id);

		if (node) {					
   			if (node.style.display == "") {
				// ausschalten
   				node.style.display = "none";
   			} else {
				// einschalten
   				node.style.display = "";
  			}
 		}
	}
}

	

function playaudio(id, player_id) {
	// alert("playaudio(player_id: " + player_id + " id: " + id + ")");
	// reload ad ?
	player_id = "audioplayer" + player_id;
	callToActionscript(player_id, id);
}


function playvideo(id, lang) {
	// alert("playvideo(" + id + ", " + lang + ")");
}


function playlyrics(id, lang) {
	// alert("playlyrics(" + id + ", " + lang + ")");
}


function setClass(id, classname) { 
	// alert("node= " + id + " classname= " + classname);
	node = document.getElementById(id);
	if (node) {
		document.getElementById(id).setAttribute("class", classname); 
		document.getElementById(id).setAttribute("className", classname); // ie fix
	}
}


// gibt aktuelle y-scroll position
function scrollTop() {
	body = document.body
	d = document.documentElement
	if (body && body.scrollTop) return body.scrollTop
	if (d && d.scrollTop) return d.scrollTop
	if (window.pageYOffset) return window.pageYOffset
	return 0
}

function show(id) {
	document.getElementById(id).style.display = "";
}
	
function ShowEffect(element){
	obj = document.getElementById(element);
    new Effect.Appear(obj, {duration:1, from:0, to:1.0});
}
   
function hide(id) {
	document.getElementById(id).style.display = "none";
}

function HideEffect(id){
    new Effect.Appear(id, {duration:1, from:1.0, to:0, afterFinish:function() { hide(id); }  });
}

function BUEffect(id){
	new Effect.BlindUp(id, {duration:2});
}

function HideOverlay(url){
	var iframe_div = "overlay_div";
	var iframe_id = "overlay_iframe_id";
		
   	new Effect.Appear(iframe_div, {duration:1, from:1.0, to:0, afterFinish:function() { 
		document.getElementById(iframe_id).src = url;
		document.getElementById(iframe_id).style.width = "0px";
		document.getElementById(iframe_id).style.height = "0px";
		document.getElementById(iframe_id).style.display = "none";

		document.getElementById(iframe_div).style.width = "0px";
		document.getElementById(iframe_div).style.height = "0px";
		document.getElementById(iframe_div).style.display = "none";
		document.getElementById(iframe_div).style.left = "-9000px";
	}  });
}
	
function overlay(modus, url) {
	var iframe_div = "overlay_div";
	var iframe_id = "overlay_iframe_id";
	
	if (modus == "open") {
		document.getElementById(iframe_div).style.width = "442px";
		document.getElementById(iframe_div).style.height = "524px";
		// document.getElementById(iframe_div).style.display = "block";
		document.getElementById(iframe_div).style.left = "50%";

		document.getElementById(iframe_id).style.width = "442px";
		document.getElementById(iframe_id).style.height = "524px";
		document.getElementById(iframe_id).style.display = "block";
		document.getElementById(iframe_id).src = url;
			
		ShowEffect(iframe_div);
	} else {
		parent.frames.top.HideOverlay(url);
	}		
}

function showComments(type, id) {
	alert("showComments(" + type + "," + id + ")");
}


// external link tracking

var internalHosts = ["localhost","www.scalp.de"]; // domains die nicht getrackt werden sollen

function trackExternalLinks(lang) {	
	var links = document.getElementsByTagName("a");
	var internalHostsLength = internalHosts.length;
	for (var i=0; i < links.length; i++) {
		if (links[i].getAttribute("onclick") == null) {
			var dotrack = 0;
			for (var b = 0; b < internalHostsLength; b++) {					
				if (links[i].href.indexOf(internalHosts[b]) == -1) {
					dotrack++;
				}
			}
			if (dotrack == internalHostsLength) {
				links[i].onclick = function() { 
					var onclickUrl = this.href.split("//");
					if (this.href.indexOf('javascript') == -1) {
						pageTracker._trackPageview("/" + lang + '/external/' + onclickUrl[1]);
					}
				}
			}
		}
	}
}
	

// item-nav + nav

function initTypeList(nr) {
	var typeOptions = {
		// required
		trigger_class:			'type_selector',			// class name for the trigger
		moveObj_id:				'type_selector_holder',		// id name of the item that has to be moved (contains a number of sub-items)
		// optional
		currItem:				nr,							// selected type - set dynamically via php
		autoScroll:				false,						// enable/disable auto scrolling
		backBtn_class:			'previous_button',			// class name of the left (back) button
		back_disabled_class:	'previous_button_disabled',	// class name of the left (back) button disabled
		nextBtn_class:			'next_button',				// class name of the right (next) button
		next_disabled_class:	'next_button_disabled'		// class name of the right (next) button disabled
	};
	var typeList = new scroller(typeOptions);
}


function initItemList(nr) {
	var itemOptions = {
		// required
		trigger_class:			'item_selector',			// class name for the trigger
		moveObj_id:				'item_selector_holder',		// id name of the item that has to be moved (contains a number of sub-items)
		// optional
		currItem:				nr,							// selected item - set dynamically via php
		autoScroll:				false,						// enable/disable auto scrolling
		backBtn_class:			'previous_button',			// class name of the left (back) button
		back_disabled_class:	'previous_button_disabled',	// class name of the left (back) button disabled
		nextBtn_class:			'next_button',				// class name of the right (next) button
		next_disabled_class:	'next_button_disabled'  	// class name of the right (next) button disabled
	};
	var itemList = new scroller(itemOptions);
}


var scroller = Class.create();
scroller.prototype = {
	
	triggerObjName:		null,
	overflowObj:		null,
	movingObjName:		null,
	btnLeft:			null,
	btnLeftDisabled:	null,
	btnRight:			null,
	btnRightDisabled:	null,
	autoScroll:			true,
	autoScrollTimer:	5,	
	animationSpeed:		1,
	itemsWidth:			null,
	visibleWidth:		null,
	numItems:			0,
	currItem:			1,
	currItemOffset:		0,
	currDirection:		1,
	animating:			false,
	iv:					null,
	
	initialize: function(opt) {
		
		// initialize options
		if(opt) {
			if(opt.trigger_class) this.triggerObjName = opt.trigger_class;	// required
			if(opt.moveObj_id) this.movingObjName = opt.moveObj_id;			// required
			if(opt.backBtn_class) this.btnLeft = opt.backBtn_class;
			if(opt.back_disabled_class) this.btnLeftDisabled = opt.back_disabled_class;
			if(opt.nextBtn_class) this.btnRight = opt.nextBtn_class;
			if(opt.next_disabled_class) this.btnRightDisabled = opt.next_disabled_class;
			if(opt.autoScroll == true || opt.autoScroll == false) this.autoScroll = opt.autoScroll;
			if(opt.autoScrollTimer) this.autoScrollTimer = opt.autoScrollTimer;
			if(opt.animationSpeed) this.animationSpeed = opt.animationSpeed;
			if(opt.currItem) this.currItem = opt.currItem;
		}
		
		// check if exists
		if(!this.movingObjName.blank() && $(this.movingObjName)) {
				
			// set first settings
			this.numItems = $$('.'+this.triggerObjName+' #'+this.movingObjName+' li').size();
			if($(this.movingObjName).down()) this.itemWidth = $(this.movingObjName).down().getWidth();
			this.itemsWidth = 2;
			var lis = $$('.'+this.triggerObjName+' #'+this.movingObjName+' li');
			for (var i = 0; i < lis.length; i++) {
				if (i == (this.currItem -1)) { this.currItemOffset = this.itemsWidth; }
				this.itemsWidth += lis[i].getWidth();
			}
			this.overflowObj = $(this.movingObjName).up();
			this.visibleWidth = this.overflowObj.getWidth()+1; // ie fix
			$(this.movingObjName).style.width = this.itemsWidth +'px';
				
			// has to scroll?
			if(this.hasToScroll()) {
						
				// add left event listener
				this.disableLeftBtn();
				$$('.'+this.triggerObjName+' .'+this.btnLeft).each(function(e) {
					Event.observe(e, 'click', function(){
						if (!this.animating) this.moveScrollerFromButton(-1);
					}.bind(this));
				}.bind(this));
						
				// add right event listener
				$$('.'+this.triggerObjName+' .'+this.btnRight).each(function(e) {
					Event.observe(e, 'click', function(){
						if (!this.animating) this.moveScrollerFromButton(1);
					}.bind(this));
				}.bind(this));
				
				// show currItem
				this.moveScrollerBy('init', 0, ((-1 * this.currItemOffset) + 100));
				
				// start autoscroll
				if (this.autoScroll) {
					var msSpeed = this.autoScrollTimer * 1000;
					this.iv = setInterval(function(){ this.moveScrollerFromAnimation(); }.bind(this), msSpeed);
				}
						
			} else {
				this.disableLeftBtn();
				this.disableRightBtn();
			}
				
		}
		
	},
	moveScrollerFromButton: function(direction) {
		this.moveScrollerBy('button', direction, 0);
	},
	moveScrollerFromAnimation: function() {
		this.moveScrollerBy('animation', this.currDirection, 0);
	},
	moveScrollerBy:function(from, direction, targetOffset) {

			// initial setup
			if(from == 'button') clearInterval(this.iv);
			var dist = Math.round(0.75 * this.visibleWidth); // Math.round(this.visibleWidth / 2);
			var scrollMax = -1 * (this.itemsWidth - this.visibleWidth -1);
			if (scrollMax > 0) { scrollMax = 0 };
	 		var scrollMin = 0;		
			var oldPos = Number($(this.movingObjName).style.marginLeft.replace(/px/, ""));
			if (from == "init") {
				var newPos = targetOffset;
			} else {
				var newPos = oldPos + (-1 * direction * dist);
			}
			if (newPos <= scrollMax) { newPos = scrollMax; }
			if (newPos >= scrollMin) { newPos = scrollMin; }
			
			// animate
			if(newPos >= scrollMax && newPos <= scrollMin) {
				
				new Effect.Morph($(this.movingObjName), {
					duration: this.animationSpeed,
					beforeStart: function(){ this.animating = true; }.bind(this),
					afterFinish: function(){ this.animating = false; }.bind(this),
					style: 'margin-left: ' + newPos + 'px'
				});
				
				// save current position
				this.currPos = newPos;
			}
			
			// reloop if animation is playing
			if(from == 'animation' && newPos == oldPos) {
				this.currDirection = (this.currDirection == 1) ? -1 : 1;
					this.moveScrollerFromAnimation();
			}
		
			// disable buttons
			if(this.currPos >= 0) this.disableLeftBtn(); else this.enableLeftBtn();
			if(this.currPos <= scrollMax) this.disableRightBtn(); else this.enableRightBtn();
		
	},
	disableLeftBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnLeft)[0].className = this.btnLeft+' '+this.btnLeftDisabled;
	},
	enableLeftBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnLeft)[0].className = this.btnLeft;
	},
	disableRightBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnRight)[0].className = this.btnRight+' '+this.btnRightDisabled;
	},
	enableRightBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnRight)[0].className = this.btnRight;
	},
	hasToScroll: function() {
		return (this.itemsWidth > this.visibleWidth) ? true : false;
	}

};


var boxes = Class.create();
boxes.prototype = {
	
	objName:			'box',
	handleName: 		'box_top_right_off',
	handleNameActive:	'box_top_right_on',
	contentName:		'box_content',
	contentNameOuter:	'box_body',
	contentTriggerOpen:	'open',
	animationSpeed:		1,
	eventName:			'click',
	animating:			false,
	allowAllClosed:		true,
	
	initialize: function() {
		if($$('.'+this.objName)) {
			$$('.'+this.objName+' .'+this.handleName).each(function(e) {
				
				// add event listener
				// alert("node: " + e.up().next(1).className);	
				Event.observe(e, this.eventName, function(){
					if(!this.animating && e.up().next(1).className.include(this.contentNameOuter)) this.toggleElement(e);
				}.bind(this));
				
				// check for open content
				// alert("node: " + e.up().next(1).down().className);
				var nextContent = e.up().next(1).down();
				if (nextContent.className.include(this.contentTriggerOpen)) {
					e.className = this.handleNameActive;
					nextContent.style.marginTop = '0px';
				} else nextContent.style.marginTop = '-' + nextContent.getHeight() + 'px';
				
			}.bind(this));
		}
	},
	
	toggleElement: function(handle) {
		var content = handle.up().next(1).down();
		if ($(content).style.marginTop <= '0') {
			$$('.'+this.objName+' .'+this.handleName, '.'+this.objName+' .'+this.handleNameActive).each(function(e) {
				if (e == handle) {
					new Effect.Morph(content, {
						duration:		this.animationSpeed,
						beforeStart:	function() { this.animating = true; }.bind(this),
						afterFinish:	function() { this.animating = false; content.style.overflowY = "auto"; }.bind(this),
						style:			'margin-top: 0px;'
					});
					e.className = this.handleNameActive;
				} else {
					var nextContent = e.next().down();
					nextContent.style.overflow = "hidden";
					new Effect.Morph(nextContent, {
						duration:		this.animationSpeed,
						style:			'margin-top: -'+nextContent.getHeight()+'px;',
						afterFinish:	function(){ e.className = this.handleName; }.bind(this)
					});
				}
			}.bind(this));
		} else if(this.allowAllClosed && this.eventName == 'click') {
			content.style.overflow = "hidden";
			new Effect.Morph(content, {
				duration:		this.animationSpeed,
				beforeStart:	function() { this.animating = true; }.bind(this),
				afterFinish:	function() { this.animating = false; handle.className = this.handleName; }.bind(this),
				style:			'margin-top: -'+content.getHeight()+'px;'
			});
		}
	}
	
};


// peace out