var Products = new Object();

Products.init = function(){
    this.preview    = $('current_product');
    this.curName    = $('current_name');
    this.curMore    = $('current_more');
    this.curHref    = $('current_href');
    this.curHaslo   = $('current_haslo');
    this.products   = new Array();
    this.current    = 0;
    if(this.preview){
        this.getProducts();
    }
}

Products.getProducts = function(){
    var holder = $('products');
    var children = holder.children;
    var a = 0;
    for(i=0;i<children.length;i++){
        if(children[i].children.length && children[i].tagName == 'DIV'){
            this.products[a] = new Array();
            var productChildren = children[i].children;

            for(n=0;n<productChildren.length;n++){
                if(productChildren[n].tagName == 'A' && productChildren[n].className == 'gray')
                    this.products[a]['gray'] = productChildren[n].getAttribute('href');
                    
                if(productChildren[n].tagName == 'A' && productChildren[n].className == 'colour')
                    this.products[a]['colour'] = productChildren[n].getAttribute('href');   
                    
                if(productChildren[n].tagName == 'A' && productChildren[n].className == 'image')
                    this.products[a]['image'] = productChildren[n];            
                    
                if(productChildren[n].tagName == 'SPAN' && productChildren[n].className == 'name')
                    this.products[a]['name'] = productChildren[n].innerHTML;  
                    
                if(productChildren[n].tagName == 'A' && productChildren[n].className == 'more')
                    this.products[a]['more'] = productChildren[n].getAttribute('href');  
                                                                      
                if(productChildren[n].tagName == 'DIV')       
                    this.products[a]['mask'] = productChildren[n];
                
                if(productChildren[n].tagName == 'P')       
                    this.products[a]['haslo'] = productChildren[n];                           
            }
            a++;
        }
    }
}

Products.setActive = function(i){
    if(i == this.current)
        return ;
        
        //alert(this.products[i]['haslo'].innerHTML);
    var href = this.products[i]['image'].getAttribute('href');
    this.preview.style.background = 'transparent url(\''+href+'\') no-repeat center';
    this.products[i]['mask'].className = 'mask red';
    this.products[i]['image'].style.background = 'transparent url(\''+this.products[i]['colour']+'\') no-repeat center';
    this.curName.innerHTML = this.products[i]['name'];
    
    if(this.products[i]['haslo'].innerHTML == '')
    {
       this.curHaslo.style.display = 'none';
       this.curHaslo.innerHTML = '';
    }
    else
    {
      this.curHaslo.style.display = '';
      this.curHaslo.innerHTML = this.products[i]['haslo'].innerHTML;
    }
    
    if(this.products[i]['more'] != '#'){
        this.curMore.setAttribute('href',this.products[i]['more']);
        this.curMore.style.display = '';
        this.curHref.setAttribute('href',this.products[i]['more']);
        this.curHref.setAttribute('onclick',"");
        
    } else
    {
        this.curMore.style.display = 'none';
        this.curHref.setAttribute('href','#');
        this.curHref.setAttribute('onclick',"return false;");
    }    
    this.products[this.current]['mask'].className = 'mask';
    this.products[this.current]['image'].style.background = 'transparent url(\''+this.products[this.current]['gray']+'\') no-repeat center';
    
    this.current = i;
}

Products.next = function(){
    var i = this.current;
    if(i+1 >= this.products.length)
        i = 0;
    else
        i++;   
    this.setActive(i);
}

Products.prev = function(){
    var i = this.current;
    if(i-1 < 0)
        i = this.products.length-1;
    else
        i--;   
    this.setActive(i);
}
