/*
Copyright (c) 2005 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
    The global object JSON contains two methods.

    JSON.stringify(value) takes a JavaScript value and produces a JSON text.
    The value must not be cyclical.

    JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
    throw a 'JSONError' exception if there is an error.
*/
var JSON = {
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',
/*
    Stringify a JavaScript value, producing a JSON text.
*/
    stringify: function (v) {
        var a = [];

/*
    Emit a string.
*/
        function e(s) {
            a[a.length] = s;
        }

/*
    Convert a value.
*/
        function g(x) {
            var c, i, l, v;

            switch (typeof x) {
            case 'object':
                if (x) {
                    if (x instanceof Array) {
                        e('[');
                        l = a.length;
                        for (i = 0; i < x.length; i += 1) {
                            v = x[i];
                            if (typeof v != 'undefined' &&
                                    typeof v != 'function') {
                                if (l < a.length) {
                                    e(',');
                                }
                                g(v);
                            }
                        }
                        e(']');
                        return;
                    } else if (typeof x.toString != 'undefined') {
                        e('{');
                        l = a.length;
                        for (i in x) {
                            v = x[i];
                            if (x.hasOwnProperty(i) &&
                                    typeof v != 'undefined' &&
                                    typeof v != 'function') {
                                if (l < a.length) {
                                    e(',');
                                }
                                g(i);
                                e(':');
                                g(v);
                            }
                        }
                        return e('}');
                    }
                }
                e('null');
                return;
            case 'number':
                e(isFinite(x) ? +x : 'null');
                return;
            case 'string':
                l = x.length;
                e('"');
                for (i = 0; i < l; i += 1) {
                    c = x.charAt(i);
                    if (c >= ' ') {
                        if (c == '\\' || c == '"') {
                            e('\\');
                        }
                        e(c);
                    } else {
                        switch (c) {
                        case '\b':
                            e('\\b');
                            break;
                        case '\f':
                            e('\\f');
                            break;
                        case '\n':
                            e('\\n');
                            break;
                        case '\r':
                            e('\\r');
                            break;
                        case '\t':
                            e('\\t');
                            break;
                        default:
                            c = c.charCodeAt();
                            e('\\u00' + Math.floor(c / 16).toString(16) +
                                (c % 16).toString(16));
                        }
                    }
                }
                e('"');
                return;
            case 'boolean':
                e(String(x));
                return;
            default:
                e('null');
                return;
            }
        }
        g(v);
        return a.join('');
    },
/*
    Parse a JSON text, producing a JavaScript value.
*/
    parse: function (text) {
        return (/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) &&
            eval('(' + text + ')');
    }
};


// client-side agent implementation 
this_url = "<?php echo htmlentities($_GET['this_url']) ?>";
function Agent() {
  this.debug = false; // default
  this.call = function () {
    var aa_sfunc = "";
    var aa_cfunc = "";
    var result = "";
    var xmlHttpObject;
    if(arguments.length<3) {
      alert("Incorrect number of parameters. Please check your function call");
      return;
    } 
    aa_url=arguments[0];
    aa_sfunc=arguments[1];
    aa_cfunc=arguments[2];

    if((aa_url==null)||(aa_url=="")) aa_url = this_url;
    
    aa_url = "/admin/lib.ajax.inc.php";
    
    var aa_poststr = "aa_afunc=call&aa_sfunc=" + encodeURI(aa_sfunc) +
        "&aa_cfunc=" + encodeURI(aa_cfunc);
    for(var i=3; i<arguments.length; i++) {
      if(typeof(arguments[i])=='object') {
        aa_poststr += "&aa_sfunc_args[]="+encodeURI(JSON.stringify(arguments[i]));
      } else {
        aa_poststr += "&aa_sfunc_args[]="+encodeURI(arguments[i]);
      }
    }
    
    xmlHttpObject = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
       xmlHttpObject = new XMLHttpRequest();
       if (xmlHttpObject.overrideMimeType) {
          //xmlHttpObject.overrideMimeType('text/xml');
       }
    } else if (window.ActiveXObject) { // IE
       try {
          xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          try {
             xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
       }
    }
    if (!xmlHttpObject) {
       alert('Agent unable to establish communication  :( ');
       return false;
    }
    
    if((aa_sfunc==null)||(aa_sfunc=="")) {
      if(arguments[3]) aa_poststr=arguments[3];
    }
    
    if((aa_cfunc==null)||(aa_cfunc=="")) {
      xmlHttpObject.open('POST', aa_url, false);
      xmlHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttpObject.setRequestHeader("Content-length", arguments.length+1);
      //xmlHttpObject.setRequestHeader("Connection", "close"); // not needed
      xmlHttpObject.send(aa_poststr);
      return xmlHttpObject.responseText;
    } else {
      xmlHttpObject.onreadystatechange = function () {
        if (xmlHttpObject.readyState == 4) {
           if (xmlHttpObject.status == 200) {
              result = xmlHttpObject.responseText;
              result = result.replace(/\\\"/g,'"');
              if(document.getElementById(aa_cfunc)) {
                try {
                  document.getElementById(aa_cfunc).innerHTML=result;
                }
                catch (e) {
                  document.getElementById(aa_cfunc).value=result;
                }               
              } else {
                if (JSON.parse(result)) 
                  eval(aa_cfunc+"(JSON.parse(result));");
                else 
                  eval(aa_cfunc+"(result);");
              }
           } else {
              if(xmlHttpObject.status!=0) {
                alert('There was a problem with the request.');
              }
           }
        }
      }
      xmlHttpObject.open('POST', aa_url, true);
      xmlHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttpObject.setRequestHeader("Content-length", arguments.length+1);
      //xmlHttpObject.setRequestHeader("Connection", "close"); // not needed
      xmlHttpObject.send(aa_poststr);
      return xmlHttpObject;
    }
  }
  this.listen = function (aa_event, aa_cfunc) {
    // listener function will come here
  }
}
var agent = new Agent();

<!--//
function openComments(baseurl, blogid, eid, snd, pwd, ancora)
{
    if (snd==1)
    {
        if (pwd != "")
        {
            pwd = "&pwd="+pwd;
        }
        window.open(baseurl+'snd_comments.php?blogid='+blogid+'&snd_key='+eid+pwd,'snd_comments'+blogid+eid, 'width=480,height=480,scrollbars=yes,resizable');
    }
    else
    {
        if (pwd != "")
        {
            pwd = "&pwd="+pwd;
        }
	    window.open(baseurl+'comments.php?blogid='+blogid+'&eid='+eid+pwd+ancora,'comments'+blogid+eid, 'width=480,height=480,scrollbars=yes,resizable');
    } 
}
function openTrackback(baseurl, blogid, eid)
{
    window.open(baseurl+'trackback.php?blogid='+blogid+'&eid='+eid,'trackback'+blogid+eid, 'width=480,height=480,scrollbars=yes,resizable');
}

function page(n, keyw, archivio)
{
    var nuovosearch = document.location.search
    re = new RegExp('start=[^\&]+','gi')
    if (keyw != "")
    {
        nuovosearch = "?keyw="+keyw+"&act=search";
    }
    if (re.test(nuovosearch))
    {
        nuovosearch = nuovosearch.replace(re,'start='+n)
    }
    else
    {
        if (nuovosearch == "") nuovosearch = '?start=' + n;
        else nuovosearch = nuovosearch + '&start=' + n;
    }
    if (archivio == 1)
    {
        window.location = (document.location.pathname.toString()+'/archive.php').replace('//archive', '/archive') + nuovosearch;
    }
    else
    {
        if (archivio == "2" && document.location.pathname.toString().indexOf("archive.php/")!=-1)
        {
            tag = document.location.pathname.toString().split("/");
            tag = "&tag="+tag[tag.length-1];
            window.location = "http://"+(document.location.host.toString()+'/archive.php').replace('//archive', '/archive') + nuovosearch + tag;
        }
        else
        {
            if (document.location.pathname.toString().indexOf("php")==-1) {var dsa = document.location.pathname.toString().split("\/?"); window.location = dsa+"/"+nuovosearch;}
            else window.location.search = nuovosearch;
        }
    }
}


function pagina(n, keyw, archivio, pageurl)
{
    var nuovosearch = document.location.search
    re = new RegExp('start=.*','gi')
    if (keyw != "")
    {
        nuovosearch = "?keyw="+keyw+"&act=search";
    }
    if (re.test(nuovosearch))
    {
        nuovosearch = nuovosearch.replace(re,'start='+n)
    }
    else
    {
        if (nuovosearch == "") nuovosearch = '?start=' + n;
        else nuovosearch = nuovosearch + '&start=' + n;
    }
    if (pageurl != "" && pageurl != "undefined" && archivio!=1) document.location = pageurl+nuovosearch
    else
        if (archivio == 1)
            if (pageurl.indexOf("?")!=-1)
                document.location = pageurl + "&" + nuovosearch.substr(1);
            else
                document.location = pageurl + nuovosearch;
        else
            document.location.search = nuovosearch;
}

function pageCm(n, pagina)
{
    var nuovosearch = pagina;
    re = new RegExp('&comment_start=.*','gi')
    if (re.test(nuovosearch))
    {
        nuovosearch = nuovosearch.replace(re,'&comment_start='+n)
    }
    else
    {
        nuovosearch = nuovosearch + '&comment_start=' + n;
    }
    blogurl = document.location.host.toString();
    if (document.location.toString().indexOf("archive.php")==-1 && document.location.toString().indexOf("comments.php")==-1)
        document.location = "http://"+blogurl+"/archive.php"+nuovosearch+"#commenti_start";
    else
        document.location = "http://"+blogurl+"/comments.php"+nuovosearch+"#commenti_start";
}

/*******************************************************************************
showLastFmPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showLastFmPreview(blogid, lastfmusername, chart, showButton)
{
    document.getElementById("lastFmPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showLastFmPreview', 'callback7', blogid, lastfmusername, chart, showButton, 1);
}
function callback7(str)
{
    document.getElementById("lastFmPreview").innerHTML = str;
}

function showAlbumImage(row, image, el)
{
    if (document.getElementById(row))
    {
        imageEl = document.getElementById(row);
    }
    else
    {
        imageEl = document.createElement("IMG");
        imageEl.src = image;
        imageEl.id = row;
        el.style.position="relative";
        el.appendChild(imageEl);
    }
    imageEl.style.display = "block";
    imageEl.style.position = "absolute";
    imageEl.style.top = "-5";
    imageEl.style.right = "0";
    imageEl.style.width = "40px";
    imageEl.style.height = "40px";
}

function hideAlbumImage(row)
{
    document.getElementById(row).style.display="none";
}

/*******************************************************************************
showDeviantartPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showDeviantartPreview(blogid, deviantusername, add, showButton)
{
    document.getElementById("deviantartPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showDeviantartPreview', 'callback11', blogid, deviantusername, add, showButton, 1);
}
function callback11(str)
{
    document.getElementById("deviantartPreview").innerHTML = str;
}

/*******************************************************************************
showFlickrPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showFlickrPreview(blogid, flickrusername, showButton, username, count)
{
    document.getElementById("flickrPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showFlickrPreview', 'callback8', blogid, flickrusername, showButton, username, 1, count);
}
function callback8(str)
{
    str = str.split("|$|");
    document.getElementById("flickrPreview").innerHTML = str[0];
    if (document.getElementById('flickrID')) document.getElementById('flickrID').value=str[1];
}

/*******************************************************************************
showTwitterPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showTwitterPreview(blogid, Twitterusername)
{
    document.getElementById("twitterPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showTwitterPreview', 'callback10', blogid, Twitterusername, 0, 1);
}
function callback10(str)
{
    document.getElementById("twitterPreview").innerHTML = str;
}

/*******************************************************************************
showTumblrPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showTumblrPreview(blogid, tumblrUrl, tumblrNo)
{
    document.getElementById("tumblrPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showTumblrPreview', 'showTumblrPreviewEnd', blogid, tumblrUrl, tumblrNo, 0);
}
function showTumblrPreviewEnd(str)
{
    document.getElementById("tumblrPreview").innerHTML = str;
}

/*******************************************************************************
share
Mostra la finestra di social share 
*******************************************************************************/
function share(type, postid, blogid, t_nome, t_mail, t_destmail, t_messaggio)
{
    agent.call('', 'share', 'callbackShare', type, postid, blogid, t_nome, t_mail, t_destmail, t_messaggio);
}
function callbackShare(str)
{
    $("#ajaxblog").html(str);
    $("#ajaxblog").dialog("open");
}

/*******************************************************************************
showTwitterPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showTwitterPreview(blogid, Twitterusername)
{
    document.getElementById("twitterPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showTwitterPreview', 'callback10', blogid, Twitterusername, 0, 1);
}
function callback10(str)
{
    document.getElementById("twitterPreview").innerHTML = str;
}

/*******************************************************************************
favePost
Aggiunge un post ai preferiti 
*******************************************************************************/
var inputObj = false;
$(document).ready(function(){$("<div id=ajaxblog></div>").appendTo("body"); $("#ajaxblog").dialog( {modal:true, resizable:false, width:350, height:250, autoOpen : false, overlay: { opacity: 0.7, background: "black" }} );});
$(document).ready(function(){$("<div id=ajaxblogsmall></div>").appendTo("body"); $("#ajaxblogsmall").dialog( {modal:true, resizable:false, width:300, height:100, autoOpen : false, overlay: { opacity: 0.7, background: "black" }} );});
function favePost(blogid, postid, inputObjParameter)
{
    inputObj = $(inputObjParameter).children("IMG");
    agent.call('', 'favePost', 'callback9', blogid, postid);
}
function callback9(str)
{
    str = str.split("|");
    if (str[1]!="")
    {
        $("#ajaxblog").dialog("close");
        $("#ajaxblogsmall").html(str[0]);
        $("#ajaxblogsmall").dialog("open");
        inputObj.src = str[1];
    }
    else
    {
        $("#ajaxblog").html(str[0]);
        $("#ajaxblog").dialog("open");
    } 
}

/*******************************************************************************
richiediAccesso
Richiede la lettura di un post protetto 
*******************************************************************************/
function richiediAccesso(postid, blogid)
{
    agent.call('', 'richiediAccesso', 'richiediAccessoEnd', postid, blogid);
}
function richiediAccessoEnd(str)
{
    $("#ajaxblogsmall").html(str);
    $("#ajaxblogsmall").dialog("open"); 
    
}

/*******************************************************************************
showAnobiiPreview
Mostra l'anteprima di last.fm 
*******************************************************************************/
function showAnobiiPreview(blogid, anobiiid, count, tipo, addstr)
{
    document.getElementById("anobiiPreview").innerHTML = "<div style=\"text-align:center;\"><img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\"></div>";
    agent.call('', 'showAnobiiPreview', 'showAnobiiPreviewEnd', blogid, anobiiid, tipo, 0, 1, count, addstr.replace(/\&/g, "%26"));
}
function showAnobiiPreviewEnd(str)
{
    document.getElementById("anobiiPreview").innerHTML = str;
}

function replyComment(editor, nick, avatar, plink)
{
    if (editor == 1)
    {
        oEditor = FCKeditorAPI.GetInstance('comment') ;
        if (avatar == "") oEditor.InsertHtml("<a href=\""+plink+"\" title=\"Risposta a "+nick+"\" class=\"reply\">"+nick+"</a> ");
        else              oEditor.InsertHtml("<a href=\""+plink+"\" title=\"Risposta a "+nick+"\" class=\"reply\"><img src=\""+avatar+"\" alt=\"\" border=\"0\" style=\"margin:0 5px 0px 0;\"></a> ");
        document.getElementById("comment___Frame").focus();
        oEditor.Focus();
    }
    else
    {
        if (avatar == "") document.forms[0].comment.value="<a href=\""+plink+"\" title=\"Risposta a "+nick+"\" class=\"reply\">"+nick+"</a> ";
        else              document.forms[0].comment.value="<a href=\""+plink+"\" title=\"Risposta a "+nick+"\" class=\"reply\"><img src=\""+avatar+"\" alt=\"\" border=\"0\" style=\"margin:0 5px 0px 0;\"></a> ";
    }
}
/*******************************************************************************
getTestimonial()
Mostra un testimonial a caso
*******************************************************************************/
function getTestimonial(uid, tot, last)
{
    document.getElementById("testimonial").innerHTML = "<img src=\"http://static.iobloggo.com/static/img/admin/wait.gif\" alt=\"\">";
    agent.call('', 'getTestimonial', 'getTestimonialEnd', uid, tot, last);
    if (tot == -1) window.setTimeout("getTestimonial(927, -1, 0)", 5000);
}
function getTestimonialEnd(str)
{
    document.getElementById("testimonial").innerHTML = str;
}
