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;
}


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 tellusLimit = 10;

       var tellusSubCategoriesFound = false;
       var tellusDBFound = false;
       var tellusLimitFound = false;

       var name = '';
       tellusArray.each( function ( value ) {
           var tempValue = value.split( '=' );
           tempValue.each( function ( newValue ) {
               if ( name == 'tlcc' )
               {
                   tellusSubCategories = newValue;
                   tellusSubCategoriesFound = true;
               }
               else if ( name == 'tldb' )
               {
                   tellusDB = newValue;
                   tellusDBFound = true;
               }
               else if ( name == 'tlll' )
               {
                   tellusLimit = newValue;
                   tellusLimitFound = true;
               }

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

           } );
       } );

       if ( tellusLimitFound == true &&
            tellusDBFound == true &&
            tellusLimitFound == true )
       {
           gwUpdateTellusCheckedValue( 'tellus-sub-category', tellusSubCategories );
           gwUpdateTellusCheckedValue( 'tellus-destination', tellusDB );
           gwUpdateTellusLimitValue( 'productlist-limit', tellusLimit );

           gwAjaxUpdateForm( true, type );
       }
    }
}

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

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

function gwUpdateTellusLimitValue( name, value )
{
    $(name).value = value;
}

function gwTellusLimitValue( name )
{
    if ( $(name) != undefined )
        return $(name).value;
    return '';
}

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' ) +
                '&tlll=' + gwTellusLimitValue( 'productlist-limit' );

    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'];

        var map_id = 'google-canvas';
        var map_height = 300;
        var map = document.getElementById( map_id );
        if ( map )
        {
            gwGMarkerMap.setHeight( map_id, map_height );

            gwGMarkerMap.initialize( map_id );
            gwGMarkerMap.addAllMarkers();
            gwGMarkerMap.zoomToFit();
        }

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

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