function toggleLoginBox() {
    if( $("loginbox").hasClassName('show') ) {
        $("loginbox").removeClassName('show');
    } else {
        $("loginbox").addClassName('show');
    }
    return false;
}


var Startpage = Class.create();

Startpage.prototype = {
    initialize : function( all, limit ){
        this.all = all;     // number of total products
        this.limit = limit; // number of shown products
        var invoker = this;
        $('slider').observe('mouseover', function(){ invoker.halt_timer(); });
        $('slider').observe('mouseout' , function(){ invoker.start_timer(); });
        this.start_timer();
    },

    products_iterator   :   0,  // product shift in the slider
    active_product      :   1,
    allow_autoslide     :   1,
    all                 :   0,
    limit               :   0,
    timer               :   null,

    products_next : function(){
        if( this.products_iterator < this.all - this.limit ) {
            this.products_iterator++;
            this.products_update();
        }
        return false;
    },

    products_previous : function(){
        if( this.products_iterator > 0 ) {
            this.products_iterator--;
            this.products_update();
        }
        return false;
    },

    products_update : function(){
        container = "startpage_products";
        iter = 0;
        var invoker = this;
        $(container).select("li.product").each(
            function(item){
                if( (iter>=invoker.products_iterator) && (iter<invoker.products_iterator + invoker.limit) ) {
                    if(!item.hasClassName('show')){
                        item.addClassName('show');
                    }
                }else{
                    if(item.hasClassName('show')){
                        item.removeClassName('show');
                    }
                }
                iter++;
            }
        )
    },

    select_product : function(id) {
        if( id == this.active_product ) return false;
        new Effect.Parallel([
            Effect.Appear('product_details_'+id ,{sync:true}),
            Effect.Fade('product_details_'+this.active_product,{sync:true})
        ], {
            duration: 2
        })
        container = "startpage_products_ul";
        $(container).select("div").each(
             function(item){
                 if( item.hasClassName('show') ){
                     item.removeClassName('show');
                 }
                 if( item.identify() == "product_image_"+id ){
                     item.addClassName('show');
                 }
             }
        )
        this.active_product = id;
        return false; // prevent jump
    },

    start_timer : function() {
        var invoker = this;
        if( this.allow_autoslide ) {
            this.timer = setInterval( function(){ invoker._next();} , 6000 );
        }
    },

    halt_timer : function() {
        if( this.timer ) clearTimeout( this.timer );
        this.timer = null;        
    },


    _next: function() {
        id = this.active_product+1;
        if(id > this.all) {
            id = 1;
            this.products_iterator = 0;
            this.products_update();
        }
        if(id > this.products_iterator+this.limit) {
            this.products_next();
        }
        this.select_product(id);
    }
}


