// tokiohotel.com | functions.js

var DOM = (document.getElementById);

function onLoadAction() {
	// nix
}


function winopen(url,name,style) { 
	var newWin = window.open(url,name,style);
	newWin.focus();
}


toggle = function(id, toggle_id, toggle_basename) {		
	if (DOM) {
		node = document.getElementById(id);

		if (node) {					
   			if (node.style.display == "") {
				// ausschalten
   				node.style.display = "none";
				if (document.images) {
					document.getElementById(toggle_id).src = "images/icons/" + toggle_basename + "_off.gif";
				}	
   			} else {
				// einschalten
   				node.style.display = "";
				if (document.images) {
					document.getElementById(toggle_id).src = "images/icons/" + toggle_basename + "_on.gif";
				}
  			}
 		}
	}
}


function setClass(id, classname) { 
	// alert("node= " + id + " classname= " + classname);
	document.getElementById(id).setAttribute("class", classname); 
	document.getElementById(id).setAttribute("className", classname); // ie fix 
}


function dynCSS() {
	for (var i = 0; i < document.getElementsByClassName("block_header").length; i++) {
		bg_pos = Math.floor(Math.random() * 100) + "% " + Math.floor(Math.random() * 100) + "%";
		document.getElementsByClassName("block_header")[i].style.backgroundPosition = bg_pos;
  	}
}


document.getElementsByClassName = function(clsName){    
	var retVal = new Array();    
	var elements = document.getElementsByTagName("*");    
	for(var i = 0;i < elements.length;i++){        
		if(elements[i].className.indexOf(" ") >= 0){            
			var classes = elements[i].className.split(" ");            
			for(var j = 0;j < classes.length;j++){                
				if(classes[j] == clsName) {                  
					retVal.push(elements[i]); 
				}
			}        
		}        
		else if(elements[i].className == clsName) {            
			retVal.push(elements[i]);
		}
	}    
	return retVal;
}


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});
}


// 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]);
					}
				}
			}
		}
	}
}


function initItemList(identifier, nr) {
	var itemOptions = {
		// required
		trigger_class:			'item_selector',			// class name for the trigger
		trigger_id:				'item_selector_' + identifier,		// id name of the trigger
		moveObj_id:				'item_selector_holder_' + identifier,		// 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.trigger_id) this.triggerObjName = opt.trigger_id;	// 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;
	}

};



function set_listname() {
	tmp_listname = document.getElementById("lkz_id").value;
	switch (tmp_listname) {
		case "1":
			selected_listname = "tokiohotel";
			break;

		case "3":
			selected_listname = "tokiohotel";
			break;

		case "5":
			selected_listname = "tokiohotel";
			break;

		case "2":
			selected_listname = "tokiohotel_fr";
			break;

		case "217":
			selected_listname = "tokiohotel_fr";
			break;

		case "239":
			selected_listname = "tokiohotel_fr";
			break;

		case "11":
			selected_listname = "tokiohotel_uk";
			break;

		case "14":
			selected_listname = "tokiohotel_it";
			break;

		case "18":
			selected_listname = "tokiohotel_no";
			break;

		case "20":
			selected_listname = "tokiohotel_pt";
			break;

		case "8":
			selected_listname = "tokiohotel_es";
			break;

		case "48":
			selected_listname = "tokiohotel_ca";
			break;

		case "156":
			selected_listname = "tokiohotel_us";
			break;

		case "103":
			selected_listname = "tokiohotel_mx";
			break;

		case "118":
			selected_listname = "tokiohotel_ar";
			break;

		case "40":
			selected_listname = "tokiohotel_br";
			break;

		case "123":
			selected_listname = "tokiohotel_cl";
			break;
			
		default:
			selected_listname = "tokiohotel_en";
			break;

	}
	
	document.getElementById("listname_id_1").value = selected_listname;
	// alert ("listname: " + document.getElementById("listname_id_1").value);
}


function submit_theform(){
	var alerttext = "";

	// email
	var zeichen="/ ;:,δόφ";
	eingabe = document.getElementById("email_id").value;
	laenge = eingabe.length;
	for(i=0; i<laenge; i++) {
		badchar = eingabe.charAt(i);
		if (zeichen.indexOf(badchar)>-1) {
			alerttext += "\nEmail contains bad characters.";
		}
	}   		
		var PosEt = document.getElementById("email_id").value.indexOf("@");
		var PosPt = document.getElementById("email_id").value.indexOf(".", PosEt);
		if ((document.getElementById("email_id").value == '') || (PosPt == -1) || (PosPt - PosEt < 2) || (document.getElementById("email_id").value.length - PosPt < 3) || (PosEt == -1) || (PosEt == 0) || (document.getElementById("email_id").value.indexOf("@", PosEt + 1) > PosEt)) {
		alerttext += "\nEmail is not valid.";	
	}


	var zeichen="/;:,<>";


	// pers_firstname_id   
		if (document.getElementById("pers_firstname_id").value == ""){      
		alerttext += "\nFirst name missing."; 
		}
	eingabe = document.getElementById("pers_firstname_id").value;
	laenge = eingabe.length;
	for(i=0; i<laenge; i++) {
		badchar = eingabe.charAt(i);
		if (zeichen.indexOf(badchar)>-1) {
			alerttext += "\nFirst name contains bad characters.";
		}
	}

	// pers_surname_id   
		if (document.getElementById("pers_surname_id").value == ""){      
		alerttext += "\nLast name missing."; 
		}
	eingabe = document.getElementById("pers_surname_id").value;
	laenge = eingabe.length;
	for(i=0; i<laenge; i++) {
		badchar = eingabe.charAt(i);
		if (zeichen.indexOf(badchar)>-1) {
			alerttext += "\nLast name contains bad characters.";
		}
	}

	// pers_birthday_day_id + pers_birthday_month_id + pers_birthday_year_id
	/*
	if (document.getElementById('pers_birthday_day_id').value == "" || document.getElementById('pers_birthday_month_id').value == "" || document.getElementById('pers_birthday_year_id').value == "") { 
		alerttext += "\nGeburtsdatum nicht korrekt."; 
	}
	*/

	// pers_street_hno_id  
	if (document.getElementById("pers_street_hno_id").value == ""){      
		alerttext += "\nStreet + Number missing."; 
	}
	
	// pers_street_plz_id
	if (document.getElementById("pers_street_plz_id").value == ""){      
		alerttext += "\nPostal code missing."; 
	}
	
	// pers_street_cityname_id  
	if (document.getElementById("pers_street_cityname_id").value == ""){      
		alerttext += "\nCity missing."; 
	}

	// lkz_id  
	if (document.getElementById("lkz_id").value == ""){      
		alerttext += "\nCountry missing."; 
	}

	// phone_id 
	/*
	if (document.getElementById("phone_id").value == ""){      
		alerttext += "\nTelefonnummer fehlt."; 
	}
	*/
	
	// listname_id_1 + listname_id_2
	/*
	if (document.getElementById('listname_id_1').checked==false && document.getElementById('listname_id_2').checked==false) { 
		alerttext += "\n\nTokio Hotel's offiziellen Newsletter abonnieren - JA oder NEIN ?"; 
	}
	*/

	// final check
	if (alerttext != "") {
		alerttext = "Error, please check:\n" + alerttext;
		alert(alerttext);
		return false;
	} else {
		// alerttext = "No errors :)";
		// alert(alerttext);

		document.getElementById("submit_name_id").disabled = true;			
		document.getElementById("submit_name_id").value = 'PLEASE WAIT ...';
		return true;
	}
}
	
	
	// peace out
