// inextremo | functions.js
	
var DOM = (document.getElementById);
var iw = window.innerWidth || document.documentElement.clientWidth;
var ih = window.innerHeight || document.documentElement.clientHeight;
var iframe = "no";
var userBox = "on";

function onLoadActions() {
	discoverScript();
}


function onResizeActions() {
}	


function winopen(url,name,style) { 
	// bsp: javascript:winopen('http://www.apple.com','apple_homepage','width=600,height=500,status=no,location=no,scrollbars=no,resizable=no');
	var newWin = window.open(url,name,style);
	newWin.focus();
}


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

		if (node) {					
   			if (node.style.display == "") {
				// ausschalten
   				node.style.display = "none";
   			} else {
				// einschalten
   				node.style.display = "";
  			}
 		}
	}
}

function toggleDesc(id,style) {
	document.getElementById(id).style.display = style;
}

toggleWithHandle = 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.png";
				}	
   			} else {
				// einschalten
   				node.style.display = "";
				if (document.images) {
					document.getElementById(toggle_id).src = "images/icons/" + toggle_basename + "_on.png";
				}
  			}
 		}
	}
}


function toggleUserBox(){
    new Effect.toggle("user_box_body",'Slide', {duration:1, afterFinish:function() { 
		if (userBox == "on") {
			setClass("user_box_bottom", "user_box_bottom_off");
			userBox = "off";
		} else {
			setClass("user_box_bottom", "user_box_bottom_on");
			userBox = "on";
		}
	}  });
}

function toggleLoginPassword() {
	document.getElementById("login_password_text_id").style.display = "none";
	document.getElementById("login_password_id").style.display = "block";
	document.getElementById("login_password_id").focus();
}

// audio / video

function getTrackname(id) {
	return document.getElementById(id).innerHTML;
}

function trackJWPlayer(section, modus, label) {
	_gaq.push(['_trackEvent', section, modus, label]);
	doLog(section + " | " + modus + " | " + label);
}

function playaudio(id, type, player_id) {
	// alert("playaudio(player_id: " + player_id + " id: " + id + ")");
	player_id = "audioplayer" + player_id;
	document.getElementById(player_id + "_div").style.height = "70px";
	var str = type + "," + id;
	callToActionscript(player_id, str);
}

function playerStop(id) {
	var player = getFlashMovie(id);
	player.sendEvent("STOP","true");
}

function playerLoad(id,data) {
	// call with: playerLoad('videoplayer','http://www.youtube.com/watch?v=SkAPW7vEvbs');
	var player = getFlashMovie(id);
	player.sendEvent("LOAD",data);
	player.sendEvent("PLAY","true"); 
}

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

function getElementsByClassName(oElm, strTagName, strClassName){
	// thanks to http://robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function toggleOldEvents() {
	var arrElements = getElementsByClassName(document, '*', 'events_off');	
	for ( var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if (oElement.style.display == 'none') {
			oElement.style.display = '';
		} else {
			oElement.style.display = 'none';
		}
	}
}

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 BDEffect(id){
   	new Effect.BlindDown(id, {duration:2});
}

function BUEffectDelayed(id,sec){
	new Effect.BlindUp(id, {duration:2, delay:sec });
}

function nl_form_submit(){
	var alerttext = "";

	// email
	var zeichen="/ ;:,δόφ";
	eingabe = document.getElementById("nl_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("nl_email_id").value.indexOf("@");
     	var PosPt = document.getElementById("nl_email_id").value.indexOf(".", PosEt);
     	if ((document.getElementById("nl_email_id").value == '') || (PosPt == -1) || (PosPt - PosEt < 2) || (document.getElementById("nl_email_id").value.length - PosPt < 3) || (PosEt == -1) || (PosEt == 0) || (document.getElementById("nl_email_id").value.indexOf("@", PosEt + 1) > PosEt)) {
		alerttext += "\nEmail is not valid.";	
	}

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

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

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

	// nl_gender_id  
    if (document.getElementById("nl_gender_id").value == "0"){      
		alerttext += "\nGender missing."; 
    }
	*/
	
	// final check
	if (alerttext != "") {
		alerttext = "Error, please check:\n" + alerttext;
		alert(alerttext);
		return false;
	} else {
		// alerttext = "No errors :)";
		// alert(alerttext);
		document.getElementById("nl_submit_id").disabled = true;			
		document.getElementById("nl_submit_id").value = '...';
		return true;
	}
}


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

};


// peace out
