/*****************************************
* Ticker provides the methods to loop through
* a number of stories (i.e. FeatureBox).
******************************************/
function cwiTicker(container, delay) {
    this.Container = container;
    this.CurrentFrame = 0;
    this.Frames = new Array();
    this.Links = new Array();
    this.FrameDelay = delay;
    this.CurrentTimerID = 0;
    this.Direction = 'forward'; // reverse
    this.Paused = false;
	this.PlayMode = 'dynamic';
    // Style changes
    this.LinkActiveClass = '';
    this.LinkInactiveClass = '';
    this.FrameActiveClass = '';
    this.FrameInactiveClass = '';
}

cwiTicker.prototype.InitializeHeight = function() {
    var height = 0;
    var container = document.getElementById(this.Container);

    for (var i = 0; i < this.Frames.length; i++) {
        var div = document.getElementById(this.Frames[i]);
//        if (container.offsetWidth == 0)
//            div.style.width = "225px"; // for those browsers which are reporting offsetWidth as 0
//        else
//            div.style.width = container.offsetWidth + "px";

        if (div.offsetHeight > height)
            height = div.offsetHeight;
    }

    height += 20;
    container.style.height = height + "px";
}

cwiTicker.prototype.ShowFrame = function(position) {
    if (position > (this.Frames.length - 1)) position = 0;

    jQuery('#'+this.Frames[this.CurrentFrame]).css('display','none');
    if (this.FrameInactiveClass.length != 0)
        jQuery('#'+this.Frames[this.CurrentFrame]).attr('class',this.FrameInactiveClass);
    if (this.LinkInactiveClass.length != 0)
        jQuery('#'+this.Links[this.CurrentFrame]).attr('class',this.LinkInactiveClass);

    jQuery('#'+this.Frames[position]).css('display','block');
    if (this.FrameActiveClass.length != 0)
        jQuery('#'+this.Frames[position]).attr('class',this.FrameActiveClass);
    if (this.LinkActiveClass.length != 0)
        jQuery('#'+this.Links[position]).attr('class',this.LinkActiveClass);

    this.CurrentFrame = position;
}

cwiTicker.prototype.MoveToNextFrame = function() {
    if (this.Direction = 'forward')
        this.ShowFrame(this.CurrentFrame + 1);
    else
        this.ShowFrame(this.CurrentFrame - 1);
        
    if (!this.Paused)
        this.Resume();
}

cwiTicker.prototype.MoveToPreviousFrame = function() {
    if (this.Direction = 'reverse')
        this.ShowFrame(this.CurrentFrame + 1);
    else
        this.ShowFrame(this.CurrentFrame - 1);
        
    if (!this.Paused)
        this.Resume();
}

cwiTicker.prototype.Pause = function() {
    clearTimeout(this.CurrentTimerID);
    this.Paused = true;
}

cwiTicker.prototype.Resume = function() {
	if(this.PlayMode == 'dynamic')
	{
		this.CurrentTimerID = setTimeout(this.Container + '.MoveToNextFrame()', this.FrameDelay);
		this.Paused = false;
	}
}

cwiTicker.prototype.Start = function() {
    this.ShowFrame(0)
    this.Resume();
}
