var Current_Tab_ID = 1;
var Table_Coords;
var Tab_Data;

// See comment in initShoveler concerning why this is hard coded
var Display_Width = 653;

function initShoveler(tabData)
{
    try {
        Tab_Data     = tabData;
        _updateTableCoords();

	// IE could not get this value, so hard code it (above) for now
        //Display_Width = Element.getWidth($('shoveler-td-1'));

        var xCoord = Display_Width*(Current_Tab_ID-1);
        Element.setStyle($('shoveler-table'), {"top":xCoord,"left":0});

        _updateTabs(Current_Tab_ID);
        _setContent(Current_Tab_ID);

    } catch (e) {
	//logFatal(e);
    }
}

// Used when a customer clicks on a specific tab
function moveToTab(idx)
{
    try {
        if ( _isInTransition() || idx == Current_Tab_ID || idx < 1 || idx > 6 ) return;

        _onButtonClick();
        _updateTabs(idx,Current_Tab_ID);
        _updateTableCoords();

        var xCoord = Display_Width*(Math.abs(Current_Tab_ID - idx));
        if ( Current_Tab_ID < idx )
            xCoord *= -1;

       	//logDebug("moveToTab: "+ xCoord +" = "+ Display_Width +" * ("+ Math.abs(Current_Tab_ID) +" - "+ idx +")");

        Current_Tab_ID = idx;

         var args = {
             "afterFinish" : _onScrollComplete,
             "x" : xCoord,
             "y" : 0
         };

         new Effect.Move($('shoveler-table'), args);

         _setContent(Current_Tab_ID);

    } catch (e) {
        //logFatal("moveToTab: "+ e);
    }
}

function _updateTabs(selectedTabId,unselectedTabId)
{
    try {
        var selectedTab = $('shoveler-tab-'+ selectedTabId);
        if ( null != selectedTab )
            new Effect.Move(selectedTab,{"x":0,"y":-100});

        var unselectedTab = $('shoveler-tab-'+ unselectedTabId);
        if ( null != unselectedTab )
            new Effect.Move(unselectedTab,{"x":0,"y":100});

    } catch (e) {
        e.message = "_updateTabs: "+ e.message;
        throw e;
    }
}

function _setContent(tabId)
{
    var elm = $('shoveler-td-'+ tabId);
    if ( !elm || ('' != elm.innerHTML) )
        return;

    new Ajax.Request('/flowers/images',
                     {
                         onComplete  : _updatePageContent,
                         method      : 'get',
                         parameters  : 'id=' + tabId,
                         asynchronous: false
                     });
}

//
// Private
//
var _linkStatus = {
    isDisabled : false,
    elm        : null,
    action     : null
};

function _isInTransition() {
    return _linkStatus.isDisabled;
}

function _updateTableCoords() {
    Table_Coords = Element.cumulativeOffset($('shoveler-table'));
}

function _onButtonClick(elm)
{
    try {
        // 1. Mark functionality as disabled
        _linkStatus.isDisabled = true;

        // 2. Disable onclick() for the optional element (elements are
        // passed in for button clicks, but not for tab clicks)
        if ( null != elm ) {
            var oldOnclick = elm.onclick;
            elm.onclick = function(){return false;}

            _linkStatus = {
                isDisabled : true,
                elm        : elm,
                action     : oldOnclick
            };
        }
    } catch (e) {
        e = "_onButtonClick: "+ e;
        throw e;
    }
}

function _onScrollComplete()
{
    try {
        // First, re-enable onclick for the clicked button
        var elm = _linkStatus.elm;

        if ( null != elm )
            elm.onclick = _linkStatus.action;
    
        _linkStatus = {
            isDisabled : false,
            elm        : null,
            action     : null
        };

    } catch (e) {
        //logFatal(e);
    }
}

function _updatePageContent(resp) {
    $('shoveler-td-'+ Current_Tab_ID).innerHTML = resp.responseText;
}
