/*
 * gf.js - Global functions
 * 
 * [Subversion-Info: $Id: gf.js 9229 2009-04-07 10:02:11Z int168 $ ]
 * author: Tallence GmbH - www.tallence.com
 *   
 */

/*
 * EVENT LISTENING
 */
/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
var EventCache = function(){
	var listEvents = [];
	
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				}
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				}
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				}
				
				item[0][item[1]] = null;
			}
		}
	};
}();

function addListener(event, target, func) {
	if( target.attachEvent ) {
		target.attachEvent( 'on'+event,func );

	} else {
		target.addEventListener( event,func,true );
	}
	
	EventCache.add(target, event, func, true);
}
window.onunload = EventCache.flush;


/*
 * function calling first occuring element/html-tag by classname :
 */
function getFirstElementWithClassName( tag,className,parent ) {
	var elements = parent.getElementsByTagName(tag);
	for( var i = 0; i < elements.length; i ++ ) {
		if( elements[i].className==className ) {
			return elements[i];
		}
	}
	return null;
}


/*
 * Newsticker class
 */ 
function Newsticker(data, interfaceId) {
	/* Begin customization - adjust fade */
  
	this.fadeSteps       =   10;
	this.fadeDuration    =  750;     // value in milliseconds
	this.delayBeforeFade = 4000;     // value in milliseconds
	this.delayAfterFade  =  750;     // value in milliseconds
	
	/* End of customization - do not modify from here */
	
	this.currentIndex  = 0;
	this.data          = data;
	
	this.opacity       = 100;
	this.isPlaying     = false;
	this.fadeInterval  = 100 / this.fadeSteps;
	this.fadeTimeout   = this.fadeDuration / this.opacity * this.fadeInterval;
	this.timeoutObject = null;
	
	/* changed: */
	this.fadeColors = ["#fcfdf5", "#e6e6e6", "#d5d5d5", "#c4c4c4", "#b2b2b2", "#a1a1a1", "#909090", "#7d7d7d", "#6c6c6c", "#5a5a5a", "#5a5a5a"];
	
	// use getFirstElementWithClassName() function from slideshow
	
	this.view = document.getElementById(interfaceId);
	this.text = getFirstElementWithClassName('div', 'li red tickertext', this.view); // TODO maybe "li red tickertext"
	
	// use addListener() function from slideshow
	
	var thisRef = this;
	addListener('mouseover', this.text, function() { thisRef.pause(); });
	addListener('mouseout', this.text, function () { thisRef.play(); });
	
	this.play();
}

Newsticker.prototype.next = function () {

	var thisRef = this;
	
	this.currentIndex++;
	if (this.currentIndex >= this.data.length) {
		this.currentIndex = 0;
	}
	
	// build the ticker message
	var msg;
	if (this.data[this.currentIndex].link !== null) {
		msg = '<a href="' + this.data[this.currentIndex].link + '">';
		msg += this.data[this.currentIndex].text;
		msg += '</a>';
	} else {
		msg = this.data[this.currentIndex].text;
	}
	if (this.data[this.currentIndex].edit !== null) {
		msg += ' ' + this.data[this.currentIndex].edit;
	}
	
	// replace current message and finally redraw
	this.text.innerHTML = msg;
	
	this.opacity = 100;
	this.draw();
	
	clearTimeout(this.timeoutObject);
	this.timeoutObject = setTimeout(function() { thisRef.fade(); }, this.delayBeforeFade);
};

Newsticker.prototype.play = function() {

	var thisRef = this;
	
	this.isPlaying = true;
	this.opacity = 100;
	clearTimeout(this.timeoutObject);
	this.timeoutObject = setTimeout(function() { thisRef.fade(); }, this.delayBeforeFade);
};

Newsticker.prototype.pause = function() {

	clearTimeout(this.timeoutObject);
	
	this.isPlaying = false;
	this.opacity = 100;
	this.draw();
};

Newsticker.prototype.fade = function() {
	var thisRef = this;
	
	if (this.isPlaying) {
		this.opacity = this.opacity - this.fadeInterval;
		if (this.opacity <= 0) {
			clearTimeout(this.timeoutObject);
			this.timeoutObject = setTimeout(function() { thisRef.next(); }, this.delayAfterFade);
		} else {
			clearTimeout(this.timeoutObject);
			this.timeoutObject = setTimeout(function() { thisRef.fade(); }, this.fadeTimeout);
		}
	}
	
	this.draw();
};

/* changed: */
Newsticker.prototype.draw = function() {
	/* int050 [07.10.2008]: Added fix for Firefox 2: When the first newsticker text is rendered with 
	 * line breaks then this.text.childNodes[0] is a line break which has no style attribute.
	 * In this case you cannot assign a value to el.style.color and an error occurs.
	 * To prevent this error we loop through all childNodes until we find one with an style attribute defined.
	 */
	var childIndex = 0;
	var el = this.text.childNodes[childIndex];
	while(el.style === undefined && this.text.childNodes.length > (childIndex + 1)) {
		childIndex++;
		el = this.text.childNodes[childIndex];
	}
	if(el.style !== undefined) {
		el.style.color = this.fadeColors[this.opacity / 10];
	}
};

/* int168: Appends the loop through parameters to the url if found in request URL */
function appendLoopThroughParams(url, params) {
  paramarr = params.split(",");
  for (var iParam = 0; iParam < paramarr.length; iParam++){
    param = paramarr[iParam].replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]" + param + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results !== null) {
      if (url.indexOf("?") > -1){
        url = url + "&" + param + "=" + results[1];
      } else {
        url = url + "?" + param + "=" + results[1];
      }
    } 
  }
  return url;
}

/* int168: Reads the value from the 'page' parameter and returns it if it is a number, else returns 1 */
function getCurrentPage() {
  var regexS = "[\\?&]page=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results !== null) {
    if (isNaN(results[1])) {
      return 1;
    } else {
      return parseInt(results[1], 10);
    }
  } else {
    return 1;
  }
}
