/* DigRotate 2.0.0*/
function DigRotate(clientId)
{
	this.Container = dnn.dom.getById(clientId);
	this.Content1Div = dnn.dom.getById(clientId+"Content1");
	this.Content2Div = dnn.dom.getById(clientId+"Content2");
	this.Content1Div.style.zIndex = 1;
	this.Content2Div.style.zIndex = 2;
	this.PagerDiv = dnn.dom.getById(clientId+"Pager1");

	this.IETrans = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
	this.XTrans = [24,25,26,27,28,29,30,31];
	this.TransStart = 0;	
	this.MasterWidth="0px";		
	this.MasterHeight="0px";
	this.MasterHeightValue=0;
	this.MasterWidthValue=0;
	this.PauseRotation = 0;
	this.ClientID = clientId;
	this.CurrentPage = 0;
	this.LastPage = -1;
	this.ScrollStepX = 0;
	this.ScrollStepY = 0;
	this.ScrollTimeout = window.opera ? 100 : 40;
	this.SetMasterDimensions();
}

DigRotate.prototype.InitData = function()
{
	var id = this.ClientID+"Data"+this.CurrentPage;
	var dataDiv = dnn.dom.getById(id);
	this.Content1Div.innerHTML = dataDiv.innerHTML;
}
DigRotate.prototype.SetRotateTimer = function()
{
	if( this.RotateTimer != null )
		clearTimeout( this.RotateTimer);
	var o1 = this;
	this.RotateTimer = setTimeout(function(){ o1.RotateNext();}, this.AutoAdvanceDelay);
}

DigRotate.prototype.SetScrollInterval = function()
{
	if( this.ScrollTimer != null )
		clearInterval( this.ScrollTimer);
	var o1 = this;
	this.ScrollTimer = setInterval( function(){o1.Scroll();}, this.ScrollTimeout );
}

DigRotate.prototype.Start = function()
{
	this.InitData();

	this.UpdatePager();
	if( this.AutoAdvance == 1 )
	{
		this.SetRotateTimer();
	}
}

DigRotate.prototype.Pause = function()
{
	if( this.RotateTimer != null )
		clearTimeout( this.RotateTimer);
	this.PauseRotation = 1;
	this.UpdatePager();
}
DigRotate.prototype.Play = function()
{
	this.RotateNext();
}
DigRotate.prototype.RotatePrev = function()
{
	if( this.RotateTimer != null )
		clearTimeout( this.RotateTimer);
	if( this.ScrollTimer != null )
		clearInterval( this.ScrollTimer);
	
	var page = this.CurrentPage;
	if( page == 0 )
		page = this.ContentCount-1;
	else
		page = page - 1;
	this.SetCurrentPage(page);
	this.PauseRotation = 0;
	this.UpdatePager();
	this.Rotate();
}
DigRotate.prototype.RotateNext = function()
{
	if( this.RotateTimer != null )
		clearTimeout( this.RotateTimer);
	if( this.ScrollTimer != null )
		clearInterval( this.ScrollTimer);

	var page = this.CurrentPage;
	if( page == this.ContentCount-1 )
		page = 0;
	else
		page = page + 1;
	this.SetCurrentPage(page);
	this.PauseRotation = 0;
	this.UpdatePager();
	this.Rotate();
}
DigRotate.prototype.UpdatePager = function()
{
	this.HidePagerObjects(this.PauseRotation);
	this.EnablePages(this.CurrentPage, this.LastPage);
}
DigRotate.prototype.RotateAt = function(page)
{
	if( page != this.CurrentPage )
	{
		this.SetCurrentPage(page);
		this.PauseRotation = 1;
		this.UpdatePager();
		this.Rotate();
	}
	this.Pause();
}

DigRotate.prototype.Rotate = function()
{
	var id = this.ClientID+"Data"+this.CurrentPage;
	var dataDiv = dnn.dom.getById(id);

    if( this.Transition == 998 )
    {
		this.Content1Div.innerHTML = dataDiv.innerHTML;
		this.EnablePages(this.CurrentPage, this.LastPage );
		this.SetDimensions();
		if( this.PauseRotation == 0 )
		{
			var o1 = this;
			this.RotateTimer = setTimeout(function(){ o1.RotateNext();}, this.AutoAdvanceDelay);
		}
	}
	else
	{		
		this.Content2Div.innerHTML = dataDiv.innerHTML;
		this.EnablePages(this.CurrentPage, this.LastPage );
		this.SetDimensions();
		
		this.PrepareScroll();
		this.SetScrollInterval();
	}
}

DigRotate.prototype.SetDimensions = function()
{
	this.Content1Div.style.height = this.Content2Div.offsetHeight;     
	this.Content1Div.style.width = this.Content2Div.offsetWidth;     
	this.Container.style.height = parseInt(this.Content2Div.offsetHeight)+"px";     
	this.Container.style.width = parseInt(this.Content2Div.offsetWidth)+"px";
	this.SetMasterDimensions();
}

DigRotate.prototype.SetMasterDimensions = function()
{
	this.MasterWidth = this.Container.offsetWidth;
	this.MasterHeight = this.Container.offsetHeight;
	this.MasterHeightValue = parseInt(this.MasterHeight);
	this.MasterWidthValue = parseInt(this.MasterWidth);
	if( this.EnablePager == 1 )
		this.PagerDiv.style.width = this.MasterWidth;
}

DigRotate.prototype.PrepareScroll = function()
{
    this.SetTransition();
	this.MoveTo(this.Content1Div, 0, 0 );
    this.ScrollStepX = 0;
    this.ScrollStepY = 0;
    if( this.Transition == 24 || this.Transition == 28) 
    {
		this.MoveTo(this.Content2Div, 0, 0 );
		this.ScrollStepY = -Math.ceil((this.MasterHeightValue / ((this.TransitionSpeed*1000.0)/this.ScrollTimeout)));
    }
    else if( this.Transition == 25 || this.Transition == 29)
    {
		this.MoveTo(this.Content2Div, 0, -(this.MasterHeightValue*2) );
		this.ScrollStepY = Math.ceil((this.MasterHeightValue / ((this.TransitionSpeed*1000.0)/this.ScrollTimeout)));
    }
    else if( this.Transition == 26 || this.Transition == 30)
    {
		this.MoveTo(this.Content2Div, this.MasterWidth, -(this.MasterHeightValue) );
		this.ScrollStepX = -Math.ceil((this.MasterWidthValue / ((this.TransitionSpeed*1000.0)/this.ScrollTimeout)));
    }
    else if( this.Transition == 27 || this.Transition == 31)
    {
		this.MoveTo(this.Content2Div, -this.MasterWidthValue, -(this.MasterHeightValue) );
		this.ScrollStepX = Math.ceil((this.MasterWidthValue / ((this.TransitionSpeed*1000.0)/this.ScrollTimeout)));
    }
   
}
DigRotate.prototype.MoveTo = function(obj,x,y)
{
	obj.style.left = x+"px";
	obj.style.top = y+"px";
}

DigRotate.prototype.SetTransition = function()
{
	var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0 && !window.opera) ? 1 : 0;    
    if( this.RandomTransition == 1 )
    {
        if( is_ie )
        {
            transIndex = Math.floor(Math.random()*this.IETrans.length);
            this.Transition = this.IETrans[transIndex];
        }
        else
        {
            transIndex = Math.floor(Math.random()*this.XTrans.length);
			this.Transition = this.XTrans[transIndex];
		}
    }
    if( this.Transition <=23 && !is_ie )
		this.Transition = 26;
}

DigRotate.prototype.Scroll = function()
{
    var done = 0;
    if( this.Transition >=0 && this.Transition <= 23 )
    {
		if( this.TransStart == 0 )
		{
			this.Content1Div.style.filter="revealTrans(duration="+this.TransitionSpeed+",transition="+this.Transition+")";
	        this.Content1Div.filters.revealTrans.Apply();
		    this.Content1Div.innerHTML = this.Content2Div.innerHTML;
			this.Content1Div.filters.revealTrans.Play();
			this.TransStart = 1;
		}
		else
		{
			if( this.Content1Div.filters.revealTrans.status == 0 )
			{
		        done = 1;
				this.TransStart = 0;
			}
		}
     }
     else
     {        
		var divY = parseInt(this.Content2Div.style.top);
        var divX = parseInt(this.Content2Div.style.left);
        
        if( this.Transition == 24 || this.Transition == 28) 
		{
			if( divY+this.ScrollStepY <= -this.MasterHeightValue )
			{
				this.ScrollStepY = -(this.MasterHeightValue-divY);
				done = 1;
			}
		}
	    else if( this.Transition == 25 || this.Transition == 29)
	    {
			if( (divY+this.ScrollStepY) >= -this.MasterHeightValue )
			{ 
				this.ScrollStepY = -this.MasterHeightValue-divY;
				done = 1;
			}
		}
	    else if( this.Transition == 26 || this.Transition == 30)
	    {
			if( (divX+this.ScrollStepX) <= 0 )
			{
				this.ScrollStepX = -divX;
				done = 1;
			}
		}
	    else if( this.Transition == 27 || this.Transition == 31)
	    {
			if( (divX+this.ScrollStepX) >= 0 )
			{
				this.ScrollStepX = divX;
				done = 1;
			}
		}

		this.Content2Div.style.left = (divX+this.ScrollStepX)+"px";
		this.Content2Div.style.top = (divY+this.ScrollStepY )+"px";
		
        if( this.Transition == 24 || this.Transition == 25 || this.Transition == 26 || this.Transition == 27)
		{ 
			this.Content1Div.style.left = (parseInt(this.Content1Div.style.left)+this.ScrollStepX)+"px";
			this.Content1Div.style.top = (parseInt(this.Content1Div.style.top)+this.ScrollStepY)+"px";
        }
	}
    if( this.AutoAdvance == 1 && done == 1)
	{		
		clearInterval( this.ScrollTimer );
    	this.Content1Div.innerHTML = this.Content2Div.innerHTML;
    	this.MoveTo( this.Content1Div, 0, 0 );
    	this.MoveTo( this.Content2Div, 0, 0 );
        if( this.PauseRotation == 0 )
			this.SetRotateTimer();
	}
}


DigRotate.prototype.EnablePages = function(current,last)
{
	var o1 = dnn.dom.getById(this.ClientID+"ispage"+current);
	var o2 = dnn.dom.getById(this.ClientID+"notpage"+current);	
	this.ShowDiv(o1,1);
	this.ShowDiv(o2,0);
	if( last != -1 )
	{
		var o3 = dnn.dom.getById(this.ClientID+"ispage"+last);
		var o4 = dnn.dom.getById(this.ClientID+"notpage"+last);
		this.ShowDiv(o3,0);
		this.ShowDiv(o4,1);
	}
}

DigRotate.prototype.HidePagerObjects = function(pause)
{
	var o1 = dnn.dom.getById(this.ClientID+"ispause");
	var o2 = dnn.dom.getById(this.ClientID+"notpause");	
	this.ShowDiv(o1,pause);
	this.ShowDiv(o2,pause==1?0:1);
}

DigRotate.prototype.SetCurrentPage = function(page)
{
	this.LastPage = this.CurrentPage;
	this.CurrentPage = page;
	dnn.dom.setCookie("LastRotate"+this.ModuleID,page,1);
}

DigRotate.prototype.ShowDiv = function(dobj,show )
{
	if( dobj == null )
		return;
	if( dobj.style == null )
		return;
		
	dobj.style.display = show == 0 ? "none" : "inline";
	dobj.style.visibility = show == 0 ? "hidden" : "visible";
}

