﻿/****************************************
/ Animated Scrolling Code
*****************************************/
var jScrollTimer;
var delayTimer;
var HOVER_DELAY = 125; //Milliseconds
var SCROLL_PIXEL_INCREMENT = 2; //Pixels
var SCROLL_TIME_VALUE = 15; //Milliseconds

//Holds the Interval values for multiple Webparts, since there can be 
//more than one Tabbed Webpart on a User's 'My AOS' page.
var aryAutoRotateIntervals = {};

function HandleTabClick(tabId, webPartContainerId) {

    StopTabRotate(webPartContainerId);

    //Get the Active Tab ID for this WebPart only, since there may be multiple Webparts
    var spanActiveTab = document.getElementById('ActiveTab__' + webPartContainerId);
    var activeTabId = spanActiveTab.innerHTML;

    //alert('Clicked Tab: ' + tabId + '\nActive Tab: ' + activeTabId + '\nWebpart ClientID: ' + webPartContainerId);

    //'activeTabId' is declared and initially set on the Server-side
    if (tabId != activeTabId) {

        //Hide the previously Active Tab Content and Tab Header
        var inactiveContentDiv = document.getElementById(activeTabId + '__content');
        inactiveContentDiv.style.display = 'none';
        document.getElementById(activeTabId).className = 'Rss-Tab';

        //Show the new Active Tab Content and Tab Header
        var activeContentDiv = document.getElementById(tabId + '__content');
        activeContentDiv.style.display = 'block';
        document.getElementById(tabId).className = 'Rss-TabActive';

        //Set the Hidden Field with the new Value of the 'Active Tab'
        spanActiveTab.innerHTML = tabId;
    }
}

function HandleTabMouseOver(tabId, activeCssClass, hoverCssClass) {

    //Add 'Hover' styling
    var theTab = document.getElementById(tabId);

    if (theTab.className != activeCssClass) {
        theTab.className = hoverCssClass;
    }
}

function HandleTabMouseOut(tabId, activeCssClass, tabCssClass) {

    //Remove 'Hover' styling
    var theTab = document.getElementById(tabId);

    if (theTab.className != activeCssClass) {
        theTab.className = tabCssClass;
    }
}

function doScrollRight(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) {

    //Make the Right Scroll Button 'Active' again  
    var rs = document.getElementById(leftScrollId);
    rs.className = 'Rss-LeftScrollButton';

    //Get Fixed Header DIV
    var fth = document.getElementById(fixHrdId);

    //Find SlidingTabHeader DIV
    var sth = document.getElementById(slideHdrId);

    //Get the Scroll Buttons DIV
    var scrollers = document.getElementById(scrollerId);

    //If null, initialize it
    if (sth.style.left == '') {
        sth.style.left = '0px';
    }

    if ((sth.offsetLeft + sth.offsetWidth + scrollers.offsetWidth + 5) > fth.offsetWidth) {
        sth.style.left = parseInt(sth.style.left) - SCROLL_PIXEL_INCREMENT + 'px';
        jScrollTimer = setTimeout(function() { doScrollRight(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) }, SCROLL_TIME_VALUE);  // call doMove in 20msec
    } else {
        var ls = document.getElementById(rightScrollId);
        ls.className = 'Rss-RightScrollButtonOff';
    }
}

function HandleMoveRight(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) {

    //Stop tab auto-rotate
    StopTabRotate(wpContainerId);

    //Find SlidingTabHeader DIV
    var sth = document.getElementById(slideHdrId);

    //Get the Scroll Buttons DIV
    var scrollers = document.getElementById(scrollerId);

    //Get Fixed Header DIV
    var fth = document.getElementById(fixHrdId);

    //If null, initialize it
    if (sth.style.left == '') {
        sth.style.left = '0px';
    }

    if ((sth.offsetLeft + sth.offsetWidth + scrollers.offsetWidth + 5) > fth.offsetWidth) {

        //Add 'Hover' styling     
        var ls = document.getElementById(rightScrollId);
        ls.className = 'Rss-RightScrollButtonHover';

        //This is a 'Hover' delay, so if the User quickly mouses over the scroll
        //buttons, its action will not quickly fire.              
        delayTimer = setTimeout(function() { doScrollRight(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) }, HOVER_DELAY);
    }
}


function doScrollLeft(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) {

    //Make the Left Scroll Button 'Active' again 
    var ls = document.getElementById(rightScrollId);
    ls.className = 'Rss-RightScrollButton';

    //Get Fixed Header DIV
    var fth = document.getElementById(fixHrdId);

    //Find SlidingTabHeader DIV
    var sth = document.getElementById(slideHdrId);

    //If null, initialize it
    if (sth.style.left == '') {
        sth.style.left = '0px';
    }

    if (sth.offsetLeft < 0) {
        sth.style.left = parseInt(sth.style.left) + SCROLL_PIXEL_INCREMENT + 'px';
        jScrollTimer = setTimeout(function() { doScrollLeft(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) }, SCROLL_TIME_VALUE);  // call doMove in 20msec
    } else {
        var rs = document.getElementById(leftScrollId);
        rs.className = 'Rss-LeftScrollButtonOff';
    }
}

function HandleMoveLeft(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) {

    //Stop Tab auto-rotate
    StopTabRotate(wpContainerId);

    //Find SlidingTabHeader DIV
    var sth = document.getElementById(slideHdrId);

    //If null, initialize it
    if (sth.style.left == '') {
        sth.style.left = '0px';
    }

    if (sth.offsetLeft < 0) {

        //Add 'Hover' styling       
        var rs = document.getElementById(leftScrollId);
        rs.className = 'Rss-LeftScrollButtonHover';

        //This is a 'Hover' delay, so if the User quickly mouses over the scroll
        //buttons, its action will not quickly fire.        
        delayTimer = setTimeout(function() { doScrollLeft(wpContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId) }, HOVER_DELAY);
    }
}

function stopScroll(wpContainerId, leftScrollId, rightScrollId) {

    clearTimeout(jScrollTimer);
    clearTimeout(delayTimer);

    //Remove 'Hover' styling
    //Get the Left and Right scroll buttons
    var ls = document.getElementById(leftScrollId);
    var rs = document.getElementById(rightScrollId);

    if (ls.className != 'Rss-LeftScrollButtonOff') {
        ls.className = 'Rss-LeftScrollButton';
    }
    if (rs.className != 'Rss-RightScrollButtonOff') {
        rs.className = 'Rss-RightScrollButton';
    }
}

function TabInit(wpContainerId, tabContainerId, fixHrdId, slideHdrId, scrollerId, leftScrollId, rightScrollId, allowTabRotate, tabRotateInterval) {

    //alert('Webpart.cs TabInit!\n' + wpContainerId + '\n' + tabContainerId + '\n' + fixHrdId + '\n' + slideHdrId + '\n' + scrollerId);


    StopTabRotate(wpContainerId);


    //Only show Scroll Buttons if Sliding Header DIV Width is Greater 
    //than the Fixed Header Offset Width
    //Get Fixed Header DIV    
    var fth = document.getElementById(fixHrdId);

    //Find SlidingTabHeader DIV   
    var sth = document.getElementById(slideHdrId);

    //Get the Scroll Buttons DIV    
    var scrollers = document.getElementById(scrollerId);

    //alert('Sliding Hdr Width ' + sth.offsetWidth + '\nFixedHeader Width: ' + fth.offsetWidth);	

    if (sth.offsetWidth > fth.offsetWidth) {
        scrollers.style.display = 'block';
        scrollers.style.left = (fth.offsetWidth - scrollers.offsetWidth - 2) + 'px';
        scrollers.style.top = '1px';

        //Set the 'Right' scroll button to Inactive       
        var rs = document.getElementById(leftScrollId);
        rs.className = 'Rss-LeftScrollButtonOff';

    } else {
        scrollers.style.display = 'none';
    }

    if (allowTabRotate == 'true') {
        //Start auto-rotate of Tabs
        var RotateInterval = parseInt(tabRotateInterval) * 1000;
        aryAutoRotateIntervals[wpContainerId] = setInterval(function() { StartAutoRotate(wpContainerId, fixHrdId, slideHdrId, leftScrollId, rightScrollId) }, RotateInterval);
    }
}

function StopTabRotate(wpContainerId) {
    //Stop the auto-rotate thru the Tabs
    //alert('Stopping tab rotate...');
    var intervalId = aryAutoRotateIntervals[wpContainerId];
    clearInterval(intervalId);
}

function StartAutoRotate(wpContainerId, fixHrdId, slideHdrId, leftScrollId, rightScrollId) {

    //Get the Cells from the Tabbed Container Table's first and only ROW
    var tabsRow = document.getElementById("TabsRow__" + wpContainerId);
    var tabs = tabsRow.cells;

    //The reason we subtract by 2 is b/c we set an extra 'Hidden' 
    //cell on the Server side to store the initial 
    //'Active_Tab__' value
    var LastCellIndex = tabs.length - 2;  
    
    //Get the Active Tab ID for this WebPart only, since there may be multiple Webparts
    var spanActiveTab = document.getElementById('ActiveTab__' + wpContainerId);

    //Local vars
    var LastTabId = tabs[LastCellIndex].id;
    var FirstTabId = tabs[0].id;
    var CurrentTabId = spanActiveTab.innerHTML; //Current 'Active' tab
    var NextTabId; //The next tab that will be 'Active'

    if (CurrentTabId == LastTabId) {
        //Rotate back to the very FIRST tab
        NextTabId = FirstTabId;
    } else {
        //Get the 'next' Tab after current 'Active' tab
        for (i = 0; i < LastCellIndex; i++) {
            if (tabs[i].id == CurrentTabId) {
                NextTabId = tabs[i + 1].id;
            }                           
        }
    }

    //Hide the previously Active Tab Content and Tab Header
    var inactiveContentDiv = document.getElementById(CurrentTabId + '__content');
    inactiveContentDiv.style.display = 'none';
    document.getElementById(CurrentTabId).className = 'Rss-Tab';

    //Show the newly set Active Tab Content and Tab Header
    var activeContentDiv = document.getElementById(NextTabId + '__content');
    activeContentDiv.style.display = 'block';
    document.getElementById(NextTabId).className = 'Rss-TabActive';

    //Set the Hidden Field with the new Value of the 'Active Tab'
    spanActiveTab.innerHTML = NextTabId;
   
    //Get Fixed Header DIV    
    var fth = document.getElementById(fixHrdId);

    //Find SlidingTabHeader DIV
    var sth = document.getElementById(slideHdrId);

    //Get the TableCell for the 'Next Tab'
    var nextTab = document.getElementById(NextTabId);
      
    //If Slding Header's Left Coord is null, initialize it
    if (sth.style.left == '') {
        sth.style.left = '0px';
    }

    //var rightCoord = nextTab.offsetLeft + nextTab.offsetWidth;
    //alert('Tab Header: ' + document.getElementById(NextTabId).innerHTML + '\nFixedHdr Width: ' + fth.offsetWidth + '\nActive Cell Offset: ' + nextTab.offsetLeft + '\nActive Cell Width: ' + nextTab.offsetWidth + '\nActive Cell Right: ' + rightCoord + '\nSlideHdr Left: ' + sth.style.left);

    //******************************************************************************
    //The following code is used to shift the Sliding Header appropriately so    
    //that a newly set 'Active Tab' will always be visible to the User, even if its
    //initially not visible on the screen.
    //
    //There is slightly different processing for between the very 'Last' Tab and
    //any other Tab
    //******************************************************************************    
    
    if (CurrentTabId != LastTabId) {
        //Checks to see if a Tab is entirely visible in its Container or not
        if (((nextTab.offsetLeft + nextTab.offsetWidth) + parseInt(sth.style.left)) > fth.offsetWidth) {           
            if (NextTabId == LastTabId) {               

                //If this is the Last Tab, do NOT 'shift' it all the way to the Left
                sth.style.left = parseInt(sth.style.left) - (nextTab.offsetLeft + parseInt(sth.style.left) - (nextTab.offsetWidth + 5)) + 'px';
            
                //Set 'Right' scroll button to OFF
                var rs = document.getElementById(rightScrollId);
                rs.className = 'Rss-RightScrollButtonOff';
            } else {
                //Shift the Tab all the way to the left so its fully visible
                sth.style.left = parseInt(sth.style.left) - (nextTab.offsetLeft + parseInt(sth.style.left)) + 'px';
            }
            //Make the Left Scroll Button 'Active' again  
            var ls = document.getElementById(leftScrollId);
            ls.className = 'Rss-LeftScrollButton';
        }
    } else {
        //Rotate back to the very first Tab
        //Adjust the 'Slider Header' so the very first Tab is visible again.
        sth.style.left = '0px';

        //Set the 'Left' scroll button to Inactive       
        var ls = document.getElementById(leftScrollId);
        ls.className = 'Rss-LeftScrollButtonOff';

        //Make the Right Scroll Button 'Active' again  
        var rs = document.getElementById(rightScrollId);
        rs.className = 'Rss-RightScrollButton';
    }
}
//SK - added this call back function for the web method call
function GetTabContent(contentDivId, isRss, channelUrl, hideTitle, hideImage,
                                cmsChannelId, contentType, IsmultipleTab, numberofItems, userid) {
    RssService.GetTabContent(contentDivId, isRss, channelUrl, hideTitle, hideImage,
                                cmsChannelId, contentType,IsmultipleTab, numberofItems, userid, GetTabContentSucceeded);
}

function GetTabContentSucceeded(result) {
    var json = eval("(" + result + ")");
    $('#' + json.TabId).html("<div>" + json.TabContent + "</div>");
}







