/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var categorie_vetrina = [18,19,56];
var counter_categorie_vetrina = [0,0,0];
var block_vetrina_items_refresh = [0,0,0,0,0,0];
/*
Array.prototype.max = function() {
    var max = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
    return max;
}
Array.prototype.min = function() {
    var min = this[0];
    var len = this.length;
    for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
    return min;
}*/

function init_body(boxid){
    var k = counter_categorie_vetrina.indexOf(counter_categorie_vetrina.min());
    var j = categorie_vetrina[k];
    counter_categorie_vetrina[k]=counter_categorie_vetrina[k]+1;
    var url = 'ajaxVetrina.jsp?cc='+j+'&box_id='+boxid;
    // notice the use of a proxy to circumvent the Same Origin Policy.
    
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            var el = document.getElementById(boxid);
            el.innerHTML = transport.responseText;            
            jQuery(document).ready(function(){imagePreview();});
        }
    });

}
function begin_updatered(doTimeOut){
    if(block_vetrina_items_refresh[1]==0){
        init_body("annuncio2");
    }
    if(block_vetrina_items_refresh[3]==0){
        init_body("annuncio4");
    }
    if(block_vetrina_items_refresh[5]==0){
        init_body("annuncio6");
    }
    
    if(doTimeOut){
        setTimeout("begin_updatered();",10000);
    }
}
function begin_updateblue(doTimeOut){
    if(block_vetrina_items_refresh[0]==0){
        init_body("annuncio1");
    }
    if(block_vetrina_items_refresh[2]==0){
        init_body("annuncio3");
    }
    if(block_vetrina_items_refresh[4]==0){
        init_body("annuncio5");
    }

    if(doTimeOut){
        setTimeout("begin_updateblue();",10000);
    }
}
function begin_update(firstTime,alt){
    if(firstTime){
        begin_updateblue(false);
        begin_updatered(false);
        setTimeout("begin_update(false,2);",7000);
    }else{
        if(alt==1){
            begin_updateblue(false);
            setTimeout("begin_update(false,2);",7000);
        }
        if(alt==2){
            begin_updatered(false);
            setTimeout("begin_update(false,1);",7000);
        }
    }

}
function update_box(box_name,ctrl_n,mode,pag,jsp,category){
    //mode=1 galleria immagini
    //mode=2 tabella
    var box = document.getElementById(box_name);
    var url = jsp+'?q='+mode+'&pag='+pag+'&cat='+category;

    if(ctrl_n!=null){
        url=url+"&n="+ctrl_n;
    }

    // notice the use of a proxy to circumvent the Same Origin Policy.
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            box.innerHTML = transport.responseText;
            jQuery(document).ready(function(){imagePreview();});
        },
        onFailure: function(transport) {
                box.innerHTML = "Errore";
        }        
    });
}
function update_box2(ctrl,mode){
    //mode=1 galleria immagini
    //mode=2 tabella
    var box = document.getElementById("box_2");
    var url = 'ajaxImageGallery.jsp?q='+mode;
    if(ctrl!=null){
        url=url+"&pag="+ctrl.value;
    }

    // notice the use of a proxy to circumvent the Same Origin Policy.
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            box.innerHTML = transport.responseText;
        }
    });
}