var Application =  {
  lastId: 0,
  currentSampleNb: 0,

  getNewId: function() {
    Application.lastId++;
    return "window_id_" + Application.lastId;
  },
  
  showCode: function(a, id) {
    a.innerHTML = $(id + "_codediv").visible() ? "View source" : "Hide source"
    $(id + "_codediv").toggle();
  },

  editCode: function(id) {
    var pre = $(id);
    // First time
    if (!pre.textarea) {
      var textarea = document.createElement("textarea");
      var dim = pre.getDimensions();
      textarea.setAttribute('id', id + "_edit");
      textarea.setAttribute('class', 'listing');
  		
  		pre.textarea = textarea
  		
  		pre.parentNode.insertBefore(textarea, pre);
    } 
    // Show text area
    if (pre.visible()) {
      var dim = pre.getDimensions();      
      
      pre.textarea.value = pre.innerHTML;
      pre.hide();
      pre.textarea.style.height = dim.height + "px"
      pre.textarea.style.width = "100%"
      pre.textarea.style.display = "block";
  
  		pre.textarea.focus();
  		$(id+'_edit_button').innerHTML = "Stop editing";
    }
    // Remove text area
    else {
      pre.update(pre.textarea.value);
      pre.textarea.style.display = "none";
      pre.show();
  		$(id+'_edit_button').innerHTML = "Edit Source";
    }
  },
  
  evalCode: function(id) {
    var pre = $(id);
    var code;
    if (pre.textarea && pre.textarea.visible)
      code = pre.textarea.value;
    else
      code = pre.innerHTML;
    
    code = code.gsub("&lt;", "<");
    code = code.gsub("&gt;", ">");
    
    try {
      eval(code);
    }
    catch (error) {
      Dialog.alert(" error accurs while interprating your javascript code <br/>" + error, {windowParameters: {width:300, showEffect:Element.show}, okLabel: "close"});
    }
  },

	addTitle: function(title, id) {
	  Application.currentSampleNb++;
	  idButton = id + '_click_button';
	  
	  document.write(title + " (<span class='title'  id='" + idButton + "' href='#' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" onclick=\"Application.evalCode('" + id + "', true)\">click here</span>)")
	},

	addShowButton: function(id) {
	  idButton = id + '_show_button';
	  document.write("<span class='title2' id='" + idButton + "' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" onclick=\"Application.showCode(this, '" + id + "')\">View source</span>")
	},

	addEditButton: function(id) {
	  idButton = id + '_edit_button';
		html = "<p class='buttons'><span class='button' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" id='" + idButton + "' onclick=\"Application.editCode('" + id + "')\">Edit Source</span> </p>";
    document.write(html)
	},
	
	addViewThemeButton: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<span class='title2' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" id='" + idButton + "' onclick=\"Application.openThemeWindow('" + theme + "')\">View a window</span>";
    document.write(html)
	},
	
	addViewThemeDialogButton: function(theme, modal) {
	  idButton = theme + '_modal_theme_button';
		html = "<span class='title2' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" id='" + idButton + "' onclick=\"Application.openThemeDialog('" + theme + "')\">Open a alert dialog</span>";
    document.write(html)
	},
	
	openThemeWindow: function(theme, modal) {
	  var win = new Window(Application.getNewId(), {className: theme, width:300, height:200, title: "Theme : " + theme});
	  win.getContent().innerHTML= "Lorem ipsum dolor sit amet, consectetur adipiscing elit, set eiusmod tempor incidunt et labore et dolore magna aliquam. Ut enim ad minim veniam, quis nostrud exerc.";
	  win.showCenter(modal);
	},
	
	openThemeDialog: function(theme, modal) {
    Dialog.alert("Lorem ipsum dolor sit amet, consectetur adipiscing elit, set eiusmod tempor incidunt et labore et dolore magna aliquam. Ut enim ad minim veniam, quis nostrud exerc", 
                {windowParameters: {className: theme, width:350}, okLabel: "close"});
	},
	
	insertDocOverview: function() {
	  var div = $('Overview');
	  var html = "<table class='overview''><tr>";
	  
	  // Window
	  html += "<td>Window Class <ul>";
	  $$(".window").each(function(element){html += "- <a href='#" + element.title + "'>" + element.title + "</a><br/>"});
	  html += "</ul></td>";
	  
	  // Dialog
	  html += "<td>Dialog Module <ul>";
	  $$(".dialogmodule").each(function(element){html += "- <a href='#" + element.title + "'>" + element.title + "</a><br/>"});
	  html += "</ul></td>";
	  
	  // Windows
	  html += "<td>Windows Module <ul>";
	  $$(".windows").each(function(element){html += "- <a href='#" + element.title + "'>" + element.title + "</a><br/>"});
	  html += "</ul></td>";
		  
	  html += "</tr></table>"
	  div.innerHTML = html;
	},
	
	insertNavigation: function(selected) {
	  document.write('\
	  <table border=0 height=100 CELLSPACING=0 CELLPADDING=0>\
	  <td width="200"><h2><font color="#000099" size="3"><center><b>Bioinformatics@UC <br> Meller Lab</b></center></font></h2></td>\
	  <td><h2><a href="http://cinteny.cchmc.org"><img border=0 src="main_bar/dog_vs_human.jpg"/></a></h2></td>\
	  <td><h2><a href="empty.html"><img border=0 src="main_bar/snpp_logo.jpg"/></a> </h2></td>\
	  <td><h2><a href="http://polyview.cchmc.org"><img border=0 src="main_bar/phd_3d.gif"/></a></h2></td>\
	  <td><h2><a href="http://sppider.cchmc.org"><img border=0 src="main_bar/rna_pol2.png"/></a></h2></td>\
	  <td><h2><a href="http://sable.cchmc.org"><img border=0 src="main_bar/plk1_vs_sable.jpg"/></a></h2></td></table>\
	    <div class="navigation">\
      <ul>\
        <li><a id="menu_overview" href="overview.html"><font color="#FFFF99" size="3"><b>Overview</b></font></a></li>\
        <li><a id="menu_servers" href="servers.html"><font color="#FFFF99" size="3"><b>Servers</b></font></a></li>\
        <li><a id="menu_publications" href="publications.html"><font color="#FFFF99" size="3"><b>Publications</b></font></a></li>\
        <li><a id="menu_people" href="people.html"><font color="#FFFF99" size="3"><b>People</b></font></a></li>\
	  <li><a id="menu_links" href="my.html"><font color="#FFFF99" size="3"><b>My Info</b></font></a></li>\
	  <li><a id="menu_courses" href="courses.html"><font color="#FFFF99" size="3"><b>Courses</b></font></a></li>\
	  <li><a id="menu_projects" href="projects.html"><font color="#FFFF99" size="3"><b>Projects</b></font></a></li>\
	  <li><a id="menu_gallery" href="gallery.html"><font color="#FFFF99" size="3"><b>Galleries</b></font></a></li>\
        <li><a id="menu_prot_info_core" href="prot_info_core.html"><font color="#FFFF99" size="3"><b>Protein Core</b></font></a></li>\
	  <li><a id="menu_contact" href="contact.html"><font color="#FFFF99" size="3"><b>Contact Us</b></font></a></li>\
      </ul>\
    </div>');
    $('menu_' + selected).addClassName("selected");
	}
}
