/* DigRotate 4.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.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.FadeStep = 0;
    this.FadeCounter = 0;
    this.ScrollTimeout = window.opera ? 100 : 40;
    this.SetMasterDimensions();
    this.AJAXInitialLoad = 0;
}

DigRotate.prototype.InitData = function() {
    if (this.ContentLoad == 1) {
        this.Transition = 998;
        this.AJAXInitialLoad = 1;
        this.AjaxRotate();
    }
    else {
        if( this.ContentLoad == 2 )
            this.SetCurrentPage(this.CurrentPage);
        dnn.dom.setCookie('ShowAt' + this.ModuleID, '0');
        var id = this.ClientID + "Data" + this.CurrentPage;
        var dataDiv = dnn.dom.getById(id);
        if (dataDiv != null)
            this.CopyHtml(this.Content1Div, dataDiv );
//            this.Content1Div.innerHTML = dataDiv.innerHTML;
    }
    if (this.AutoAdvance == 0)
        this.PauseRotation = 1;
}
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.SetTransition();
    this.InitData();
    this.UpdatePager();
    if (this.AutoAdvance == 1 && this.ContentLoad != 2)
        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 (this.MasterWidthValue == 470 && page == this.ContentCount -1)    /* For Scott's site where the width is forced at 470 */
{}
else
{
    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.SubmitAt = function(page) {
    this.SetCurrentPage(page);
    dnn.dom.setCookie('ShowAt' + this.ModuleID, '1');
    window.location.reload();
}

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() {
    if (this.ContentLoad == 1)
        this.AjaxRotate();
    else {
        var id = this.ClientID + "Data" + this.CurrentPage;
        var dataDiv = dnn.dom.getById(id);
        this.BindContent(dataDiv, null);
    }
}

DigRotate.prototype.BindContent = function(sobj, content) {
    if (this.Transition == 998) {
        if( sobj != null )
            this.CopyHtml( this.Content1Div, sobj );
        else
            this.Content1Div.innerHTML = content;
        this.MoveTo(this.Content1Div, 0, 0);
        this.EnablePages(this.CurrentPage, this.LastPage);
        if (this.PauseRotation == 0) {
            var o1 = this;
            this.RotateTimer = setTimeout(function() { o1.RotateNext(); }, this.AutoAdvanceDelay);
        }
        if (this.AJAXInitialLoad == 1)
            this.SetTransition();
    }
    else {
        if (sobj != null)
            this.CopyHtml(this.Content2Div, sobj);
        else
            this.Content2Div.innerHTML = content;
        this.EnablePages(this.CurrentPage, this.LastPage);
        this.SetDimensions();

        this.PrepareScroll();
        this.SetScrollInterval();
    }
}

DigRotate.prototype.AjaxRotate = function() {
    clearTimeout(this.RotateTimer);
    var o1 = this;
    dnn.xmlhttp.doCallBack(this.CallbackControl, this.CurrentPage + "," + this.ModuleID + "," + this.SortOrder, o1.GetRotateHandler, this.ModuleID, o1.GetRotateError, null, null, null, 0);
}

DigRotate.prototype.GetRotateHandler = function(result, ctx) {
    var o1 = window['' + ctx + ''];
    o1.BindContent(null,result);
}
DigRotate.prototype.GetRotateError = function(result, ctx) {
    // This may hit because page refresh during callback
}

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);
}

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)));
    }
    else if (this.Transition == 32 || this.Transition == 33) {
        this.SetOpacity(this.Content1Div, 100);
        this.SetOpacity(this.Content2Div, 0);
        this.MoveTo(this.Content2Div, 0, -(this.MasterHeightValue));
        this.FadeCounter = 0;
        this.FadeStep = 100.0 / ((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.SetOpacity = function(obj, op) {
    obj.style.opacity = (op / 100);
    obj.opacity = (op / 100);
    obj.MozOpacity = (op / 100);
    obj.KhtmlOpacity = (op / 100);

    if (op == 100)
        obj.style.filter = "";
    else
        obj.style.filter = "alpha(opacity=" + op + ")";
}

DigRotate.prototype.SetTransition = function() {
    var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0 && !window.opera) ? 1 : 0;
    this.Transition = 998;
    if (is_ie && this.IETrans.length > 0) {
        transIndex = Math.floor(Math.random() * this.IETrans.length);
        this.Transition = this.IETrans[transIndex];
    }
    else {
        if (this.XTrans.length > 0) {
            transIndex = Math.floor(Math.random() * this.XTrans.length);
            this.Transition = this.XTrans[transIndex];
        }
    }
}

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.CopyHtml(this.Content1Div, this.Content2Div );
//            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 if (this.Transition == 32) {
        this.FadeCounter = this.FadeCounter + this.FadeStep;
        this.SetOpacity(this.Content1Div, 100 - this.FadeCounter);
        this.SetOpacity(this.Content2Div, this.FadeCounter);
        if (this.FadeCounter >= 100)
            done = 1;
    }
    else if (this.Transition == 33) {
        this.FadeCounter = this.FadeCounter + this.FadeStep;
        var use1 = 100 - (this.FadeCounter * 2);
        if (this.FadeCounter < 50)
            this.SetOpacity(this.Content1Div, use1);
        else {
            this.SetOpacity(this.Content1Div, 0);
            var use2 = (this.FadeCounter - 50) * 2;
            this.SetOpacity(this.Content2Div, (this.FadeCounter - 50) * 2);
        }
        if (this.FadeCounter >= 100)
            done = 1;
    }
    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 (done == 1) {
        clearInterval(this.ScrollTimer);
//        this.Content1Div.innerHTML = this.Content2Div.innerHTML;
        this.CopyHtml( this.Content1Div, this.Content2Div );
        this.MoveTo(this.Content1Div, 0, 0);
        this.MoveTo(this.Content2Div, 0, 0);
        if (this.Transition == 32 || this.Transition == 33) {
            this.SetOpacity(this.Content1Div, 100);
            this.SetOpacity(this.Content2Div, 100);
        }
        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);
    }

    // current
    var oc = dnn.dom.getById(this.ClientID + "Page" + current);
    if (oc == null)
        return;
    oc.className = "current";    
    if (last != -1) {
        var oo = dnn.dom.getById(this.ClientID + "Page" + last);
        oo.className = "";
    }

}

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(curPage) {
    this.LastPage = this.CurrentPage;
    this.CurrentPage = curPage;
    dnn.dom.setCookie("LastRotate" + this.ModuleID, curPage.toString());
}

DigRotate.prototype.ShowDiv = function(o, show) {
    if (o == null)
        return;
    if (o.style == null)
        return;
    o.style.display = show == 0 ? "none" : "inline";
    o.style.visibility = show == 0 ? "hidden" : "visible";
}
DigRotate.prototype.ClearHtml = function(o) {
	while(o.firstChild) o.removeChild(o.firstChild);
}

DigRotate.prototype.CopyHtml = function(dobj, sobj) {
    dobj.innerHTML = sobj.innerHTML;
    /*    this.ClearHtml(dobj);
    for (x = 0; x < sobj.childNodes.length; x++) {
    var newContent = sobj.childNodes[x].cloneNode(true);
    dobj.appendChild(newContent);
    }*/
}