// copyright 2009, scalp.de

var DOM = (document.getElementById);
var bg = 0;
var bg_play = 0;
var jukebox_modus ="off";

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 toggle_checkbox(id) {		
	if (DOM) {
		node = document.getElementById(id);

		if (node) {					
   			if (node.style.backgroundImage == "url(img/icons/checkbox.gif)") {
				// ausschalten
   				node.style.backgroundImage = "url(img/icons/checked.gif)";
   			} else {
				// einschalten
   				node.style.backgroundImage = "url(img/icons/checkbox.gif)";
  			}
 		}
	}
}
	
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 winopen(url,name,style) { 
	var newWin = window.open(url,name,style);
	newWin.focus();
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function toggleBg() {
	toggle("content");

	if (bg == 0) {
   		setClass("bg_toggle_id", "bg_toggle_on");
		bg = 1;
	} else {
		setClass("bg_toggle_id", "bg_toggle_off");
		bg = 0;
	}

	getFlashMovie("flash_bg").sendToActionscript("toggleCommentMode");
}

function togglePlay() {
	if (bg_play == 0) {
		setClass("play_toggle_id", "play_toggle_on");
		bg_play = 1;
	} else {
		setClass("play_toggle_id", "play_toggle_off");
		bg_play = 0;
	}

	getFlashMovie("flash_bg").sendToActionscript("togglePlay");
}


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 toggleJukebox() {
	if (jukebox_modus == "off") {
		setClass("jukebox_div", "jukebox_on");
		jukebox_modus = "on";
	} else {
		setClass("jukebox_div", "jukebox_off");
		jukebox_modus = "off";		
	}
}

function popupJukebox() {
	playerStop("jukebox");
	var newWin = window.open('jukebox_popup.php','jukebox_popup','width=300,height=256,status=no,location=no,scrollbars=no,resizable=no');
	newWin.focus();
}

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"); 
}

// 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
}

// gibt aktuelle x-scroll position
function scrollLeft() {
	body = document.body
	d = document.documentElement
	if (body && body.scrollLeft) return body.scrollLeft
	if (d && d.scrollLeft) return d.scrollLeft
	if (window.pageXOffset) return window.pageXOffset
	return 0
}

// external link tracking

var internalHosts = ["localhost","www.scalp.de","rhettbrewer.com"]; // 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) {
						// alert("pageTracker._trackPageview('/' + " + lang + "'/external/' + " + onclickUrl[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;
	}

};
// --- peace out ---
