/*
  Javascript Web Library
  @desc Libreria di funzioni utili per lo sviluppo Web
	@author Andrea Quinteri ('Keeper')
  @author Alessio M. Santin ('Lex')
  @version 0.6
*/
//Recupero di info di base

// WITH PROTOTYPE.JS
/*
* Parte per la gestione del caricamento di elementi, script, modifica , traduzioni
*/

function tradElement(id,lang){
    for(var k in CKEDITOR.instances) CKEDITOR.remove(CKEDITOR.instances[k]);
    var url = base+'amministrazione/ajax_translate.php?id='+id+'&t='+section+'&l='+lang;
    new Ajax.Request(url,{
        method:'get',
        onSuccess:function(transport){
            $('frm').innerHTML = transport.responseText;
            $$('#m_form h1.tit')[0].style.backgroundColor = '#FFFF99';
            $('tit_azione').update("Traduci "+secname+" ("+lang+")").style.color = '#993';
            CKEDITOR.replaceAll();
        },
        onFailure:function(transport){
            alert('Load Failed');
        }
    });
}
	
function newElement(){
    var url = base+'amministrazione/ajax_new.php?t='+section;
    new Ajax.Request(url,{
        method:'get',
        onSuccess:function(transport){
            $('frm').innerHTML = transport.responseText;
            $$('#m_form h1.tit')[0].style.backgroundColor = '#EFEFFF';
            $('tit_azione').update("Inserisci "+secname).style.color = '#339';
        },
        onFailure:function(transport){
            alert('Load Failed');
        }
    });
}
	
function delElement(id){
    var url = base+'amministrazione/ajax_remove.php?id='+id+'&t='+section;
	
    if(confirm('Confermi la cancellazione?')){
        new Ajax.Request(url,{
            method:'get',
            onSuccess:function(transport){
                ret = transport.responseText.evalJSON();
                if(eval(ret['result'])) $('row_'+id).hide();
                else alert("Cancellazione fallita");
            },
            onFailure:function(transport){
                alert('Load Failed');
            }
        });
    }
    return false;
}
	
function loadElement(id){
    for(var k in CKEDITOR.instances) CKEDITOR.remove(CKEDITOR.instances[k]);
    var url = base+'amministrazione/ajax_elements.php?id='+id+'&t='+section;
    new Ajax.Request(url,{
        method:'get',
        onSuccess:function(transport){
            $('frm').innerHTML = transport.responseText;
            $$('#m_form h1.tit')[0].style.backgroundColor = '#EFFFEF';
            $('tit_azione').update("Modifica "+secname).style.color = '#393';
            CKEDITOR.replaceAll();
        },
        onFailure:function(transport){
            alert('Load Failed');
        }
    });
}
/*
*  Parte per la gestione dei commenti nei blog (Utilizza Prototype 1.6.0)
*/

function setWait(id){
    $(id).innerHTML = '<img src="img/ajax-loader.gif" />';
}
			
function showComments(id){
    var comments = 'blog-comm-list-'+id;
    $(comments).show();
    setWait(comments);
	
    var url = "ajax_commenti.php?id="+id;
	
    new Ajax.Request(url,{
        method:'post',
        onSuccess: function(transport){
            arr = transport.responseText.evalJSON();
			
            html = "<ul>";
            for(var i=0,len=arr.length; i<len; ++i){
                el = arr[i];
                html += '<li><span class="blog-comm-list-data">'+el[0]+'</span><h3><a href="'+el[3]+'">'+el[1]+'</a></h3><div class="blog-comm-list-testo">'+el[2]+'</div></li>';
            }
            html += "</ul>";
            $(comments).innerHTML=html;
        },
        onFailure: function(transport){
            $(comments).innerHTML="Error "+transport.responseText;
        }
	
    });
}
			
function writeComment(id){
    ($(id).style.display=='none'?$(id).show():$(id).hide());
}

function delComment(id){
    var url = base+'amministrazione/ajax_remove.php?comm='+id+'&t='+section;
	
    if(confirm("Confermi la cancellazione?")){
        new Ajax.Request(url,{
            method:'get',
            onSuccess:function(transport){
                ret = transport.responseText;
                if(eval(ret)) $('comment_'+id).hide();
            },
            onFailure:function(transport){
                alert('Load Failed');
            }
        });
        return true;
    }
    return false;
}

/* Funzioni per la gestione della parte amministrativa, immagini e simili. */

function closeThis(el){
    var i = 0;
    new PeriodicalExecuter(	function(pe){
        if(++i>1)
            pe.stop();
        else try {
            $('m_err').hide();
        } catch(e) {}
    },5);
}

function change(id,img){
    var el = document.getElementById(id);
    el.src = img;
}

// NO PROTOTYPE.JS

/*
* Popup Resizable Windows including a page url containing an image
*/
function popupImg(url,commento){	
    w = h = 0;
    if(navigator.userAgent.toLowerCase().indexOf('msie') == -1)
    {
        w = 20;
        h = 80;
    }
	
    var func = "<script language=\"javascript\">"+
    "function imposta() {"+
    "var NS = (navigator.appName==\"Netscape\")?true:false;"+
    "iWidth = (NS)?window.innerWidth:document.body.clientWidth;"+
    "iHeight = (NS)?window.innerHeight:document.body.clientHeight;"+
    "iWidth = document.images[0].width - iWidth;"+
    "iHeight = document.images[0].height - iHeight;"+
    "window.resizeBy(iWidth, iHeight);"+
    "}"+
    "</scr"+"ipt>";
    var win = window.open('','viewimg','status=no,resizable=yes,width=250,height=250');
    win.document.write('<html><head><title></title>'+func+'</head>');
    win.document.write('<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onload="imposta()">');
    win.document.write('<img id="img" src="'+url+'" border="0" onClick="window.close()" align="absmiddle" alt="'+commento+'">');
    win.document.write('</body></html>');
    win.document.close();
}

function popupPage(url,w,h){
    window.open(url,'viewimg','status=no,resizable=yes,width='+w+',height='+h);
}
