var HOST_NAME = window.location.hostname;

if(typeof(www_istat_it_popolazione_container) != 'undefined') {
  var elementContainer = '#'+www_istat_it_popolazione_container;
  var superContainer = '#'+www_istat_it_popolazione_container;
} else {
  var elementContainer = '#popolazione_container .content';
  var superContainer = '#popolazione_container';
}

if(typeof(www_istat_it_popolazione_width) != 'undefined') {
  var width = www_istat_it_chart_width;
} else {
  var width = '20em';
}

if(typeof(www_istat_it_popolazione_height) != 'undefined') {
  var height = www_istat_it_chart_height;
} else {
  var height = '14em';
}

var time_elapsed = 0;
//var dataJson = <?php echo  file_get_contents("http://". $_SERVER['SERVER_NAME']. $this->chartUrl);?>;


/* Verifica il caricamento di jquery*/

init_www_istat_popolazione = function() {
  if (typeof $ == "undefined") {
      init_www_istat_popolazione.getScript('/js/jquery.js');
      // do stuff when jQuery finishes loading.
      init_www_istat_popolazione.tryReady(0);
    } else {
      /*implentare jQuery.noConflict();*/
      draw_www_istat_popolazione();
    }
}

// dynamically load any javascript file.
init_www_istat_popolazione.getScript = function(filename) {
  var script = document.createElement('script')
  script.setAttribute("type","text/javascript")
  script.setAttribute("src", filename)
  if (typeof script!="undefined")
  document.getElementsByTagName("head")[0].appendChild(script)
}

init_www_istat_popolazione.tryReady = function(time_elapsed) {
    /* Continually polls for jQuery library. */
    if (typeof $ == "undefined") {
        if (time_elapsed <= 5000) {
            setTimeout("init_www_istat_popolazione.tryReady(" + (time_elapsed + 40) + ")", 40);
        } else {
            alert("Timed out while loading jQuery.")
        }
    } else {
        //jquery è caricato
	draw_www_istat_popolazione();
    }
}

 requestPopoluation = function(loc,code) {
  $.ajax({
  //requestPopoluation.def = true; //registra questa variabile per stoppare la chiamata alle altre cose document.ready
  url: "http://"+HOST_NAME+"/ws/requestPop.php?cerca="+code+"&codice="+loc,
  context: $('#www4_istat_it_popresponse'),
  success: function(data){
    $(this).html(data);
  }
});
}


/*inserisci lo script di init*/
if(window.addEventListener) { //CHROME, MOZILLA, SAFARI
  window.addEventListener('load',init_www_istat_popolazione,false);
} else { //IE, OPERA
  window.attachEvent('onload',init_www_istat_popolazione);

}
/*funzione che disegna il grafico*/

function draw_www_istat_popolazione() {
     draw_www_istat_popolazione.build();
}
function destroyDownloadPanel() {
  $("#popolazioneDownloadPanel").fadeOut('fast');
}

function popolazioneShowEmbed() {
  // $("#popolazioneDownloadPanel").remove();
   $(superContainer).append("<div id='popolazioneDownloadPanel'  style='display: none'><\/div>");    
   $("#popolazioneDownloadPanel").html("<p><a href='javascript:destroyDownloadPanel();'>X<\/a><\/p><textarea rows='4' cols='30'><link href='http://"+HOST_NAME+"/css/popolazione.css'  rel='stylesheet' type='text/css' />\n<script src='http://"+HOST_NAME+"/js/popolazione.js' type='text/javascript'></script><\/textarea>");
   $("#popolazioneDownloadPanel").fadeToggle('fast', 'swing');  
}


draw_www_istat_popolazione.build = function() {
   
  $(elementContainer).html('<form id="pop"><input type="text" id="www_istat_it_zona" size="13" /><input type="hidden" name="loc" value="" /><input type="button" value="&gt;&gt;"  id="www_istat_it_sendRequest"/></form><div id="www4_istat_it_popresponse"><p align="left" style="padding-left:5px;">Scrivi il nome di un Comune, una Provincia o una Regione per conoscere gli ultimi dati sulla popolazione residente e il bilancio demografico</p></div>'); 
  $("<a href='javascript:popolazioneShowEmbed();'><img src='http://"+HOST_NAME+"/img/embed.png' width='25' style='float:left;margin-top:3px' border='0' alt='embed code' title='embed code'\/></a>").appendTo(superContainer);
  $('#www_istat_it_sendRequest').click(function(){ requestPopoluation('',$('#www_istat_it_zona').val());
  });
}





