// zhark.org | 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 
}


// 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;
	}

};


// peace out
