function fotogallery(conf){
    conf.wrapperSelector=conf.wrapperSelector || '#image_wrap';
    conf.instanceId=conf.instanceId ||'gallery';
    conf.gallerySelector=conf.gallerySelector || '#containerGalleria';
    conf.generaHTML=conf.generaHTML==null?true:conf.generaHTML;
    conf.itemsSelector=conf.itemsSelector || '.items';
    conf.scrollableSelector=conf.scrollableSelector|| '.scrollable';
    conf.jsonUrl=conf.jsonUrl || 'index.php?action=ajaxGalleria';
    conf.timeout=conf.timeout ||4000;
    conf.statica=conf.statica==null?false:conf.statica;
    conf.thumbnailWidth=conf.thumbnailWidth || 100;
    conf.thumbnailPrefix=conf.thumbnailPrefix ||'min-';
    conf.noThumbnail=conf.noThumbnail==null?true:conf.noThumbnail;
    conf.descriptionSelector=conf.descriptionSelector || '#galleryDescrizione';
    conf.linkSelector=conf.linkSelector || '#galleryLink';
    conf.linkAnchorSelector=conf.linkAnchorSelector || '#galleryLinkAnchor';
    if($.tools.fotogallery==null){
        $.tools.fotogallery=new Array();
    }
    $.tools.fotogallery[conf.instanceId]=conf;
    if(conf.statica!=null && conf.statica){
        var data={
            instanceId:conf.instanceId
        };
        galleriaParse(data);
    }
    else{
        jQuery.getJSON(conf.jsonUrl+'&id='+conf.idGalleria+'&instanceId='+conf.instanceId+'&time='+(new Date()).getTime(),galleriaParse);
    }
}
function galleriaParse(data){
    var conf=$.tools.fotogallery[data.instanceId];
    if(conf.timerGallery!=null){
        clearTimeout(conf.timerGallery);
    }
    if(conf.statica!=null && conf.statica){
        
    }
    else{
        if(conf.generaHTML!=null && conf.generaHTML){
            var container=jQuery(conf.gallerySelector);
            container.html("");
            /*Creazione struttura fotogallery*/
            var fg="<div><div ";
            if(conf.wrapperSelector.indexOf("#")!=-1){
                fg+=" id=\""+conf.wrapperSelector.replace("#","")+"\" ";
            }
            else{
                fg+=" class=\""+conf.wrapperSelector.replace(".","")+"\" ";
            }
            fg+="><img src=\"images/blank.gif\" ";
            if(conf.width!=null){
                fg+="width=\""+conf.width+"px\" ";
            }
            if(conf.height!=null){
                fg+="height=\""+conf.height+"px\" ";
            }
            fg+=" alt=\"Immagine\" /></div>";
            if(conf.noThumbnail==false){
                 fg+="<a class=\"prev browse left\"></a>";
            }
            fg+="<div "+((conf.noThumbnail)?" style=\"display:none\" ":" ");
            if(conf.scrollableSelector.indexOf("#")!=-1){
                fg+=" id=\""+conf.scrollableSelector.replace("#","")+"\" ";
            }
            else{
                fg+=" class=\""+conf.scrollableSelector.replace(".","")+"\" ";
            }
            fg+="><div class=\""+conf.itemsSelector.replace("#","").replace(".","")+"\"></div></div></div>";
            if(conf.noThumbnail==false){
                 fg+="<a class=\"next browse right\"></a>";
            }
            container.html(fg);
        }
        jQuery(conf.itemsSelector).html("");
        var contenuto="";
        var contatore=0;
        if(data.galleria!=null && data.galleria.contenuti!=null && data.galleria.contenuti.length>0){
            for(var i=0;i<data.galleria.contenuti.length;i++){
                contenuto+="<img rel=\""+data.instanceId+"cont"+contatore+"\" width=\""+conf.thumbnailWidth+"px\" id=\""+data.instanceId+"imgScr"+contatore+"\" src=\""+data.galleria.contenuti[i].srcThumbnail+"\" style=\"cursor:pointer;\" alt=\"Immagine\"/>";
                contenuto+="<input type=\"hidden\" id=\""+data.instanceId+"imgDesc"+contatore+"\" value=\""+encodeURIComponent(data.galleria.contenuti[i].descrizione)+"\" />";
                contatore++;
            }
        }
        jQuery(conf.itemsSelector).html(contenuto);
    }
    conf.galleria=data.galleria;
    conf.countGallery=$(conf.itemsSelector+" img").size();
    conf.indexGallery=0;
    if(conf.noThumbnail==false){
        $(conf.scrollableSelector).scrollable();
    }
    $(conf.itemsSelector+" img").click(function() {
        if(conf.timerGallery!=null){
            clearTimeout(conf.timerGallery);
        }
        var wrap = $(conf.wrapperSelector);
        var url=$(this).attr("src").replace(conf.thumbnailPrefix,"");
        var contatore=$(this).attr("rel").replace(data.instanceId+"cont","");
        conf.indexGallery=contatore;
        var img = new Image();
        img.onload = function() {
            wrap.find("img").attr("src", url);
            if(conf.descriptionSelector!=null){
                jQuery(conf.descriptionSelector).html(decodeURIComponent(jQuery("#"+data.instanceId+"imgDesc"+contatore).attr("value")));
            }
            if(conf.linkSelector!=null){
                if(!isBlankOrNull(conf.galleria.contenuti[contatore].url)){
                    jQuery(conf.linkSelector).show();
                    jQuery(conf.linkAnchorSelector).attr("href","http://"+decodeURIComponent(conf.galleria.contenuti[contatore].url));
                }
                else{
                    jQuery(conf.linkSelector).hide();
                }
            }
            conf.timerGallery=setTimeout("clickOnImgJs('"+data.instanceId+"')",conf.timeout);
        };
        img.src = url;
    }).filter(":first").click();
    if(conf.overlaySelector!=null){
        eval("jQuery('"+conf.overlaySelector+" .close').filter(':first').click(function (){ stopFotogallery('"+data.instanceId+"') })");
    }
}
function clickOnImgJs(instanceId){
    var conf=$.tools.fotogallery[instanceId];
    conf.indexGallery++;
    if(conf.indexGallery>=conf.countGallery){
        conf.indexGallery=0;
    }
    $("#"+conf.instanceId+"imgScr"+conf.indexGallery).click();
}
function stopFotogallery(id){
    if(id==null){
        id='gallery';
    }
    var conf=$.tools.fotogallery[id];
    if(conf.timerGallery!=null){
        clearTimeout(conf.timerGallery);
    }
}
