function init() {
    $("#cbo_view").change(function() {
      load_view();
    });
    $("#img_view").load(function(){
      hide_wait();
    });
    $("#dlg_event").dialog({ autoOpen: false });
    $("#dlg_event").dialog({ width: 700 });
    $("#dlg_event").dialog('option', 'width', 700); 
    $("#dlg_event").dialog('option', 'height', 500); 
    $("#dlg_event").dialog('option', 'position', '0,0'); 
    $('#dlg_event').bgiframe();
}

function load_views(tid, uname, pwd) {
    /* $("#div_info").text("Loading views..."); */
    show_wait();
    /* uname = "pinky"; ### uname here */
    /* pwd = "perky"; ### pwd here */   
    $.getJSON("http://www.timelinedata.net/main/api/view/getviews2.php?user="+uname+"&pwd="+pwd+"&tmlnid="+tid+"&jsoncallback=?", function(data){
      /* alert("1"); */
      $("#cbo_view").empty();
      $.each(data.items, function(i, item){
        id = item.ID;
        name = item.NAME;
        content222 = '<option value="'+id+'">'+name+'</option>\n';
        $("#cbo_view").append(content222);
      });
      load_view();
    });
    load_view();
}

function load_view() {
  load_view_pic();
  $("#map_main").empty();
    /* $.getJSON("http://www.timelinedata.net/main/feeds/"+$("#cbo_view").val()+".json", */
  $url = "http://www.timelinedata.net/main/ui2/getctdata.php?view="+$("#cbo_view").val()+"&jsoncallback=?";
  $.getJSON($url,
        function(data){
          $.each(data.items, function(i,item){
              var thisArea = document.createElement('area');
              thisArea.href = "javascript: show_event('"+item.url+"');";
              thisArea.coords = item.coords;
              thisArea.shape = "rect";
              $('#map_main').append(thisArea);
          });
        });
}

/*
function show_event(url) {
  $("#img_event").attr("src", url);
  $("#dlg_event").dialog('open');
}
*/

function show_event(url) {
  /* alert(url); */
  /* $("#img_event").attr("src", url); */
  /* get html from url, set html of div to it */
  file_ext = get_file_ext(url);
  /* alert(file_ext); */
  $("#div_event").html('<div style="width: 300px; height: 200px; display:block; margin:0px auto;"><img src="pb.gif" /></div>');
  $("#dlg_event").dialog('open');
  /* alert("yo"); */
  if (file_ext == ".html") {
   /* alert("yy");  */

    /* $.get(url, {},
    function(data){
      alert(data); 
      $("#div_event").html(data);
    }); */
    
    $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
      "q=select%20*%20from%20html%20where%20url%3D%22"+
      encodeURIComponent(url)+
      "%22&format=xml'&callback=?",
      function(data){
        /* alert(data.results[0]); */
        $("#div_event").html(data.results[0]);
      }
    );

  } else {
    /* alert("eee"); */
    $("#div_event").html('<img id="img_event" src="'+url+'" style="display:block; margin:auto;" />');
  }
}

function get_file_ext(url) 
{ 
/* inc dot, result is lowercase */
 var filename = url;
 if( filename.length == 0 ) return ""; 
 var dot = filename.lastIndexOf("."); 
 if( dot == -1 ) return ""; 
 var extension = filename.substr(dot,filename.length); 
 return extension.toLowerCase(); 
} 

function load_view_pic() {
  /* $("#div_info").text("Loading pic..."); */
  show_wait();
  do_load_view_pic($("#cbo_view").val());
}

function do_load_view_pic(view_id) {
  $("#img_view").attr("src", "http://www.timelinedata.net/main/img/uploaded/"+view_id+".jpg");
  
}

function show_wait() {
  /* $("#div_info").text("Loading pic..."); */
  $("#div_wait").show();
}

function hide_wait() {
  $("#div_info").text(""); /* needed to clear eg deleted msg */
  $("#div_wait").hide();
}
