// event listener on
function addEvent(elm, evType, fn, useCapture){
	if (elm.addEventListener){
	elm.addEventListener(evType, fn, useCapture);
	return true;
	}else if (elm.attachEvent){
	var r = elm.attachEvent('on' + evType, fn);
	return r;
	}else{elm['on' + evType] = fn}
}
// event listener off

// ticker on
function Ticker(id, backId, fwId, typeTimeout, pauseTimeout)
{
	var headlines=document.getElementById(id).getElementsByTagName('li');
	for(var i=0;i<headlines.length;i++) hideHeadline(i);
    var links=document.getElementById(id).getElementsByTagName('a');
	for(var i=0;i<links.length;i++) links[i].originalText = links[i].innerHTML;
	var forward= document.getElementById(fwId);
	var back= document.getElementById(backId);
	var timeOut, startTimeOut;
	var count=0;
	var currentHeadline='';
	var linkedList=[];
	var nextLetter=0;
	var semiColonYet=true;
   
    forward.onclick=function(){tickerPause(1)}
    back.onclick=function(){tickerPause(-1)}

    startTicking();
	
	function startTicking(){
  	nextLetter=0;
	  currentHeadline=links[count].originalText;
	  setInnerText('');
		showHeadline(count);
		timeOut = setInterval (tick, typeTimeout);
	}
	
	function tick(){
		if(currentHeadline.length>=nextLetter){
    	setInnerText(currentHeadline.substring(0,nextLetter));
			nextLetter++;
    }else{
      nextLetter=0;
      clearInterval(timeOut);
      startTimeOut=window.setTimeout(pauseThenTick, pauseTimeout);
    }
	}
	
	function pauseThenTick(){
    clearTimeout(startTimeOut);
   	hideHeadline(count);
		if(count<headlines.length-1){
			count++;
    }else{
    	count=0;
    }
		startTicking();
	}

  function tickerPause(direction){
  	hideHeadline(count);
    if(direction==0 && timeOut!=false){
			clearInterval(timeOut);
			clearTimeout(startTimeOut);
		  timeOut=false;
		  showHeadline(count);
		}else{
			clearInterval(timeOut);
			clearTimeout(startTimeOut);
      if(direction==0) count++;
      count = getMovedIndex(count,direction);
      startTicking();
		}
  }
    
  function setInnerText(value){links[count].innerHTML=value}
    
  function hideHeadline(index){headlines[index].style.display="none"}
	
  function showHeadline(index){headlines[index].style.display="block"}
    
  function getMovedIndex(currentIndex, direction){
  	var nextIndex=currentIndex+direction;
    if(nextIndex>=headlines.length){   
    	nextIndex=0;
		}else if(nextIndex<0){
      nextIndex=headlines.length-1;
		}  
		return nextIndex;  
  }
}
// ticker off
addEvent(window, "load", function(){
	if(document.getElementById("ticker"))	{new Ticker('ticker','ticker-back','ticker-forward', 100, 2500)}
}, false);








// widget scroll on
//jQuery.noConflict();
function ScrollFadeWidget(slideContainerId, itemListContainerId, prevBtnId, nextBtnId, fakeLayerId,slideDuration, autoSlideDuration, hoverDuration){
jQuery(function( $ ){
	$.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	$(document).ready(function(){
		var menuTimeout = null;
		
		$('#'+slideContainerId).serialScroll({
				items:'li',
				prev:'#'+prevBtnId,
				next:'#'+nextBtnId,
				axis:'x',
				offset:0,
				start:0,
				duration:slideDuration,
				force:true,
				stop:true,
				lock:false,
				cycle:true,
				easing:'easeOutQuart'
		});
		$("#"+slideContainerId+" ul").width($("#"+slideContainerId+" ul li").length * $("#"+slideContainerId).width());
		$("#"+itemListContainerId+" ul li").each(function(i){
			this.relativeTo = $("#"+slideContainerId+" ul li").eq(i);
			this.index = i;
		});

		$("#"+prevBtnId).click(function(){
			setActiveItem(getActiveIndex());
			restartAutoScrollTimer();
		});
		$("#"+nextBtnId).click(function(){
			setActiveItem(getActiveIndex());
			restartAutoScrollTimer();
		});

		function setActiveItem(i){
			$("#"+itemListContainerId+" ul li").removeClass("active");
			$("#"+itemListContainerId+" ul").find("li:eq("+i+")").addClass("active");
		}
		
		function getActiveIndex(){
			return $("#"+slideContainerId).attr("activeItemIndex");
		}
		
		$("#"+itemListContainerId+" ul li").mouseover(function(){
			_this = this;
			menuTimeout = setTimeout(function(){
				$("#"+fakeLayerId).html($("#"+slideContainerId+" ul li").eq(getActiveIndex()).html());
				$("#"+fakeLayerId).css("display","block");
				setActiveItem(_this.index);
				$("#"+slideContainerId).scrollTo(_this.relativeTo, 0, {
					axis:'x',
					offset:0,
					duration:1
				}).trigger('notify.serialScroll',[_this.index]);
				$("#"+fakeLayerId).css('visibility','hidden');
//				$("#"+fakeLayerId).fadeOut("normal");
				restartAutoScrollTimer();
			},hoverDuration);
		});

		$("#"+itemListContainerId+" ul li").mouseout(function(){
			clearTimeout(menuTimeout);
		});
		
		var autoScrollTimer = null;
		
		function restartAutoScrollTimer(){
			clearTimeout(autoScrollTimer);
			autoScrollTimer = setTimeout(function(){$("#"+nextBtnId).click(); restartAutoScrollTimer()}, autoSlideDuration);		
		}
		restartAutoScrollTimer();
	});
});

}
// widget scroll off



