/*--------------------------------------------------|
| mTicker                                          |
|---------------------------------------------------|
| Copyright (c) 2006-2009 Miroslav ćurčić (mVeliki) |
|                                                   |
| This script can be used freely as long as all     |
| copyright messages are intact.                    |
|                                                   |
| inspired by:                                      |
|  - WebTicker by Mioplanet (www.mioplanet.com)     |
|  - S5 news ticker by www.shape5.com               |
|  - http://tools.gavick.com/demo/highlighter-gk1   |
|--------------------------------------------------*/



Function.prototype.bind = function(context){ // helper function
   var $this = context, fn = this;
   return function(){
         return fn.apply($this, arguments);
   }
};


function mTicker(TickerId, Contents, AnimationType, Speed) {
	// constructor
	// call with: new mTicker("NewsTickerDiv", ["Pera","Mika","Đoka"], 201, 30);
	// AnimationType: 100-marquee, 200..204-crossfade, 300..308-scrollhor, 400..408-scrollvert
	// speed is in range 1..50

	if(Contents.length==0) return;
	this.Ticker= document.getElementById(TickerId);
	if(!this.Ticker) return;
	this.Ticker.TickerRef= this;
	this.Paused= false;
	this.AniFamily= parseInt(AnimationType / 100);
	this.AniVariant= AnimationType % 100;
	this.Speed= Speed;
	this.SpeedDelay= (Speed+2)*1000; // atleast 2 seconds
	this.Contents= Contents;
	this.ContentsLen= Contents.length;
	this.Pointer= (this.ContentsLen>1) ? 1 : 0;
	this.Phase= 1; // will be 0 after launching
	this.Launched= 0;
	this.Curve= [0,0.001,0.0027,0.0052,0.0088,0.013,0.019,0.026,0.034,0.045,0.057,0.07,0.088,0.11,0.13,0.16,0.19,0.23,0.27,0.32,0.38,0.45,0.54,0.68,1];
	// get parent dimensions
	if(document.defaultView && document.defaultView.getComputedStyle) { // FF
		var W= document.defaultView.getComputedStyle(this.Ticker,null).getPropertyValue("width");
		var H= document.defaultView.getComputedStyle(this.Ticker,null).getPropertyValue("height");
	} else {
		var W= this.Ticker.offsetWidth.toString();	//IE
		var H= this.Ticker.offsetHeight.toString();
	}
	this.TickerDims= [parseInt(W),parseInt(H)];
	// common styles
	this.Ticker.style.display= "block";
	this.Ticker.style.overflow= "hidden";
	// set event listeners
	this.Ticker.onmouseover = function () {this.TickerRef.PauseOnOff(0)};
	this.Ticker.onmouseout = function () {this.TickerRef.PauseOnOff(1)};
	// launch ticking
	this.Tick();
	this.TickTimer= setInterval(this.Tick.bind(this), this.SpeedDelay);
}


mTicker.prototype.Tick = function() {
	//JavaScript:window.status= this.Phase +',' + this.Pointer + ',' + this.Ticker.scrollLeft + ' - ' + Number(new Date());
	if(this.Paused) return;
	if(this.AniFamily==1) this.Ani_Marquee();
	if(this.AniFamily==2) this.Ani_CrossFader();
	if(this.AniFamily==3) this.Ani_ScrollHor();
	if(this.AniFamily==4) this.Ani_ScrollVer();
	this.Launched= 1;
	this.Phase= (this.Phase)?0:1;
};


mTicker.prototype.PauseOnOff = function(Enabled) {
	this.Paused= (Enabled) ? false : true;
};


// animation methods

mTicker.prototype.Ani_Marquee = function() {
	if(!this.Launched) {
		var img = '<img src="'+jsRootDir+'img/interface/spacer.gif" style="width:'+this.TickerDims[0]+'px;height:2px;border:0">';
		this.Contents= this.Contents.join(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ');
		this.Ticker.innerHTML= (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1)
		? "<table  cellspacing='0' cellpadding='0' width='100%'><tr><td nowrap='nowrap'>"+img+"<span width='100%'>"+this.Contents+"</span>"+img+"</td></tr></table>"
		: "<div nowrap='nowrap' style='width:100%;'>"+img+"<span width='100%'>"+this.Contents+"</span>"+img+"</div>";
		this.Ticker.scrollLeft = this.Ticker.scrollWidth - this.Ticker.offsetWidth;
		this.Ticker.style.width= this.TickerDims[0]+'px';
		// set speed vars
		this.SpeedDelay= 10+this.Speed;
		if(this.SpeedDelay<10) this.SpeedDelay= 10; // dont kill machine
		this.SpeedStep= 1;
		if(this.SpeedDelay<40) this.SpeedStep=2;
		if(this.SpeedDelay<20) this.SpeedStep=3;
		if(this.SpeedDelay<15) this.SpeedStep=5;
		return;
	}
	this.Ticker.scrollLeft += this.SpeedStep;
	if(this.Ticker.scrollLeft >= this.Ticker.scrollWidth-this.Ticker.offsetWidth)
	this.Ticker.scrollLeft= 0;
};


mTicker.prototype.Ani_CrossFader = function() {
	if(!this.Launched) {
		var c= this.TickerDims[1];
		this.Ticker.style.width= this.TickerDims[0]+'px';
		this.Ticker.innerHTML= '<div id="'+this.Ticker.id+'_D1" style="display:block;height:'+c+'px;overflow:hidden">'+this.Contents[0]+'</div><div id="'+this.Ticker.id+'_D2" style="display:block;height:'+c+'px;margin-top:-'+c+'px;overflow:hidden"></div>';
		this.Div1= document.getElementById(this.Ticker.id+'_D1');
		this.Div2= document.getElementById(this.Ticker.id+'_D2');
		setTickerProps(this.Ticker.id+'_D2', 0);
		var timeShifts= [0,300,500,800,1200];
		this.TimeShift= (this.AniVariant>4) ? 0 : timeShifts[this.AniVariant];
		return;
	}
	var OldD= (this.Phase) ? this.Div2 : this.Div1;
	var NewD= (this.Phase) ? this.Div1 : this.Div2;
	NewD.innerHTML= this.Contents[this.Pointer];
	this.Pointer++;	if(this.Pointer>=this.ContentsLen) this.Pointer= 0;
	for(var x=0;x<=100;x+=2) {
		setTimeout("setTickerProps('"+OldD.id+"',"+(100-x)+")", x*32);
		setTimeout("setTickerProps('"+NewD.id+"',"+x+")", x*32 + this.TimeShift);
	}
};

mTicker.prototype.Ani_ScrollHor = function() {
	if(!this.Launched) {
		var c= this.TickerDims[1];
		this.Ticker.style.position= "relative";
		this.Ticker.style.overflow= "hidden";
		this.Ticker.style.height= c+'px';
		this.Ticker.innerHTML= '<div id="'+this.Ticker.id+'_D1" style="display:block;height:'+c+'px;position:absolute;white-space:nowrap">'+this.Contents[0]+'</div><div id="'+this.Ticker.id+'_D2" style="display:block;height:'+c+'px;position:absolute;white-space:nowrap"></div>';
		this.Div1= document.getElementById(this.Ticker.id+'_D1');
		this.Div2= document.getElementById(this.Ticker.id+'_D2');
		setTickerProps(this.Ticker.id+'_D2', 0);
		this.TranSpeed= (this.AniVariant % 2) ? 40 : 9;
		this.TimeShift= (this.AniVariant % 2) ? 1100 : 300;
		this.EnterFromRight= (this.AniVariant & 2);
		this.ExitAtLeft= (this.AniVariant & 4);
		return;
	}
	var OldD= (this.Phase) ? this.Div2 : this.Div1;
	var NewD= (this.Phase) ? this.Div1 : this.Div2;
	NewD.innerHTML= this.Contents[this.Pointer];
	this.Pointer++;	if(this.Pointer>=this.ContentsLen) this.Pointer= 0;
	for(var i=1;i<=25;i++) {
		var opacOld= parseInt(100-70*this.Curve[i-1]);
		var	opacNew= 50+i*2;
		var leftNew= - parseInt(this.TickerDims[0] * this.Curve[25-i]);
		var leftOld= parseInt(this.TickerDims[0] * this.Curve[i-1]);
		if(this.EnterFromRight) leftNew= -leftNew;
		if(this.ExitAtLeft) leftOld= -leftOld;
		setTimeout("setTickerProps('"+OldD.id+"',"+opacOld+",'left','"+leftOld+"px')", i*this.TranSpeed);
		setTimeout("setTickerProps('"+NewD.id+"',"+opacNew+",'left','"+leftNew+"px')", i*this.TranSpeed+this.TimeShift);
	}
};


mTicker.prototype.Ani_ScrollVer = function() {
	if(!this.Launched) {
		var c= this.TickerDims[1];
		this.Ticker.style.position= "relative";
		this.Ticker.style.overflow= "hidden";
		this.Ticker.style.height= c+'px';
		this.Ticker.innerHTML= '<div id="'+this.Ticker.id+'_D1" style="display:block;height:'+c+'px;position:absolute;white-space:nowrap">'+this.Contents[0]+'</div><div id="'+this.Ticker.id+'_D2" style="display:block;height:'+c+'px;position:absolute;white-space:nowrap"></div>';
		this.Div1= document.getElementById(this.Ticker.id+'_D1');
		this.Div2= document.getElementById(this.Ticker.id+'_D2');
		setTickerProps(this.Ticker.id+'_D2', 0);
		this.TranSpeed= (this.AniVariant % 2) ? 40 : 10;
		this.TimeShift= (this.AniVariant % 2) ? 900 : 200;
		this.EnterFromTop= (this.AniVariant & 2);
		this.ExitAtBottom= (this.AniVariant & 4);
		return;
	}
	var OldD= (this.Phase) ? this.Div2 : this.Div1;
	var NewD= (this.Phase) ? this.Div1 : this.Div2;
	NewD.innerHTML= this.Contents[this.Pointer];
	this.Pointer++;	if(this.Pointer>=this.ContentsLen) this.Pointer= 0;
	for(var i=1;i<=25;i++) {
		var opacOld= parseInt(100-70*this.Curve[i-1]);
		var	opacNew= parseInt(100-70*this.Curve[25-i]);
		var topNew= parseInt(this.TickerDims[1] * this.Curve[25-i]);
		var topOld= -parseInt(this.TickerDims[1] * this.Curve[i-1]);
		if(this.EnterFromTop) topNew= -topNew;
		if(this.ExitAtBottom) topOld= -topOld;
		setTimeout("setTickerProps('"+OldD.id+"',"+opacOld+",'top','"+topOld+"px')", i*this.TranSpeed);
		setTimeout("setTickerProps('"+NewD.id+"',"+opacNew+",'top','"+topNew+"px')", i*this.TranSpeed+this.TimeShift);
	}
};

function setTickerProps(id, opacity, prop, propvalue) {
	var o = document.getElementById(id);
	if(!o) return;
	var s= o.style;
	s.opacity = (opacity / 100);
	s.MozOpacity = (opacity / 100);
	s.KhtmlOpacity = (opacity / 100);
	s.filter = "alpha(opacity=" + opacity + ")";
	s.zIndex = (opacity>50) ? 2 : 1;
	s.visibility = (opacity) ? "visible" : "hidden";
	if(prop) s[prop]= propvalue;
}

