var itemClass = 'item';
var itemArray = new Array();
var itemPointer = new Array();

var timer;
var waitSec = 8;
var transSec = 2;

var tellusUpdateCounter = 0;
var gwGlobalHash = '';

function autoTransition()
{
    for( var x = 0; x < autoTransition.arguments.length; x++ )
    {
        var itemParent = $( autoTransition.arguments[ x ] );
        itemArray[ x ] = new Array();
        var childCounter = 0;
        itemPointer[ x ] = 0;

        for( var y = 0; y < itemParent.childNodes.length; y ++ )
        {
            if( itemParent.childNodes[ y ].className == itemClass )
            {
                itemArray[ x ][ childCounter++ ] = itemParent.childNodes[ y ];
            }
        }
    }

    timer = setInterval( 'execTransition()', 1000 * ( waitSec + transSec ) );
}

function execTransition()
{
    for( var x = 0; x < itemArray.length; x++ )
    {
        if( itemArray[x].length > 1 )
        {
            var previousItem = itemPointer[ x ]++;
            if( itemPointer[ x ] >= itemArray[ x ].length )
            {
                itemPointer[ x ] = 0;
            }
            var nextItem = itemPointer[ x ];

            itemArray[ x ][ previousItem ].style.zIndex = 5;
            itemArray[ x ][ nextItem ].style.zIndex = 10;

            Effect.Appear( itemArray[ x ][ nextItem ], { duration: transSec } );
            Effect.Fade( itemArray[ x ][ previousItem ], { duration: 0, delay: transSec } );
        }
    }
}


function toggleCheckboxSelection( name, type )
{
    var status = false;
    var checkboxes = $$('input.' + name ).each( function( e ) {
        if( e.type == 'checkbox' )
        {
            if ( e.checked == 0 )
            {
                status = true;
            }
        }
    } );

    var checkboxes = $$('input.' + name ).each( function( e ) {
        if( e.type == 'checkbox' )
        {
            if ( status == true )
            {
                e.checked = 1;
            }
            else
            {
                e.checked = 0;
            }
        }
    } );
    gwAjaxUpdateForm( false, type );
    return false;
}

/* ajax event list */
function gwAjaxUpdateEventForm()
{
    var url = '/' + gwSiteLanguage + '/tellus/ajax/eventlist';
    var params = $('tellus-filter').serialize();
    var myAjax = new Ajax.Request( url, { method: 'post',
                                          parameters: params,
                                          onLoading: updateTellusEventListLoading,
                                          requestHeaders: { Accept: 'application/json' },
                                          onComplete: updateTellusEventListResponse} );
    tellusUpdateCounter++;
    myAjax.counter = tellusUpdateCounter;
    return false;
}

function updateTellusEventListLoading()
{
    $('event-update-button').disabled = true;
    $('event-update-button').style.color = "#a0a0a0";
    $('update-tellus-loading').style.display = "inline";
}

function updateTellusEventListResponse(oReq, oJSN) {
    if ( oReq.request.counter == tellusUpdateCounter )
    {
        var eventData = oReq.responseText.evalJSON( true );
        $('update-tellus-loading').style.display = "none";
        $('event-update-button').disabled = false;
        $('event-update-button').style.color = "#000000";
        $('tellus-event-list').innerHTML = eventData['tellus_content'];
    }
}

function gwHandleFilterSelectorURL( type )
{
    $('productlist-update-button').style.display = 'none';
    var tellusValue = window.location.hash.substring( 1 );
    gwGlobalHash = tellusValue;
    var newValue = '';
    var tellusAction = '';
    if ( tellusValue != '' )
    {
       var tellusArray = tellusValue.split( "&" );
       var tellusSubCategories = '';
       var tellusDB = '';
       var name = '';
       tellusArray.each( function ( value ) {
           var tempValue = value.split( '=' );
           tempValue.each( function ( newValue ) {
               if ( name == 'tlcc' )
               {
                   tellusSubCategories = newValue;
               }
               else if ( name == 'tldb' )
               {
                   tellusDB = newValue;
               }

               name = '';
               if ( newValue == 'tlcc' )
               {
                   name = 'tlcc';
               }
               else if ( newValue == 'tldb' )
               {
                   name = 'tldb';
               }
           } );
       } );

       gwUpdateTellusCheckedValue( 'tellus-sub-category', tellusSubCategories );
       gwUpdateTellusCheckedValue( 'tellus-destination', tellusDB );

       gwAjaxUpdateForm( true, type );
    }
}

function gwIntervalTest( type )
{
    var tellusValue = window.location.hash.substring( 1 );

    if ( tellusValue != gwGlobalHash )
    {
        gwHandleFilterSelectorURL( type );
    }
}

function gwUpdateTellusCheckedValue( className, values )
{
       $('tellus-filter').select( 'input.' + className ).each( function ( e ){
           if ( values.include( e.value )  )
           {
               e.checked = true;
           }
           else
           {
               e.checked = false;
           }
       } );
}

function gwUpdateTellusInputValues( className )
{
    var isAdded = false;
    var value = '';
    $('tellus-filter').select( 'input.' + className ).each( function( e ) { if ( e.checked ) {
        if ( isAdded == true )
            value += ',';
        else
            isAdded = true;

        value += e.value;
    } } );
    return value;
}

/* ajax product list */
function gwAjaxUpdateForm( doNotUpdateHash, type )
{
    var url =  '/' + gwSiteLanguage + '/tellus/ajax/' + type;
    var value = 'tlcc=' + gwUpdateTellusInputValues( 'tellus-sub-category' ) +
                '&tldb=' + gwUpdateTellusInputValues( 'tellus-destination' );

    var params = $('tellus-filter').serialize();
    if ( doNotUpdateHash == false || doNotUpdateHash == undefined )
    {
        location.href = '#' + value;
    }
    gwGlobalHash = value;
    tellusUpdateCounter++;
    var myAjax = new Ajax.Request( url, { method: 'post',
                                          parameters: params,
                                          onLoading: updateTellusProductListLoading,
                                          requestHeaders: { Accept: 'application/json' },
                                          onComplete: updateTellusProductListResponse} );
    myAjax.counter = tellusUpdateCounter;
    return false;
}

function updateTellusProductListLoading()
{
    $('update-tellus-loading').style.display = "inline";

    $('productlist-invert-button').style.color = "#a0a0a0";
    $('productlist-invert-button').disabled = true;

    $('productlist-update-button').style.color = "#a0a0a0";
    $('productlist-update-button').disabled = true;
}

function updateTellusProductListResponse(oReq, oJSN) {
    if ( oReq.request.counter == tellusUpdateCounter )
    {
        var data = oReq.responseText.evalJSON( true );
        $('update-tellus-loading').style.display = "none";
        $('tellus-product-list').innerHTML = data['tellus_content'];

        $('productlist-invert-button').style.color = "#000000";
        $('productlist-invert-button').disabled = false;

        $('productlist-update-button').style.color = "#000000";
        $('productlist-update-button').disabled = false;
    }
}
