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("<h2>" + Application.currentSampleNb + ". " + title + " (<span class='title'  id='" + idButton + "' href='#' onmouseover=\"$('" + idButton + "').addClassName('selected')\" onmouseout=\"$('" + idButton + "').removeClassName('selected')\" onclick=\"Application.evalCode('" + id + "', true)\">click here</span>)</h2>")
	},

	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, page) {
	  idButton = theme + '_theme_button' + page;
		html = "<span class='title2' id='" + idButton + "' onclick=\"Application.openThemeWindow('" + theme + "','','" + page +"')\">View Testimonial " + page + "</span>";
		
    document.write(html)
	},
	
	addViewThemeDialogButton: function(theme, modal) {
	  idButton = theme + '_modal_theme_button';
		html = "<span class='title2' id='" + idButton + "' onclick=\"Application.openThemeDialog('" + theme + "')\">Open a alert dialog</span>";
    document.write(html)
	},
	
	openThemeWindow: function(theme, type, page) {
		alert("Opening Theme Window");
		modal = '';
		if (type != 'gallery') {
			
				if (document.body && typeof(document.body.offsetHeight) == 'number')
				{
					 var cHeight = document.body.offsetHeight;
					 var cWidth = document.body.offsetWidth;
				}
				else if (typeof(window.innerHeight) == 'number')
				{
					 var cHeight = window.innerHeight;
					 var cWidth = window.innerWidth;
				}
				else
				{
					 var cHeight = 400;	
					 var cWidth = 300;
				}		
				
				
				
				var winHeight = cHeight * .8;  /// SIZE OF CALANDER WINDOW BASED ON CLIENT WINDOW PROPORTIONS
				var winWidth = cWidth * .8;
				
				
				var win = new Window(Application.getNewId(), {className: theme, width:winWidth, height:winHeight, title: "Where is Kristi: " + page});
	  
		} else {  /////// GALLERY REQUESTED -------------
			
				var winHeight = 420;  /// SIZE OF CALANDER WINDOW BASED ON CLIENT WINDOW PROPORTIONS
				var winWidth = 620;
				
				
		 		 var win = new Window(Application.getNewId(), {className: theme, width:winWidth, height:winHeight, title: "Kristi's Gallery"});
				
			
		}
	  	
		win.getContent().innerHTML = "<center><br /><br /><br /><br /><br /><br /><img alt='' src='images/global/icons/circleLoadinWhite.gif' /><br /><br />Loading&#8230;</center>";
	 
	  	  switch (page){
		case "January":
			var url = '';
			break;
		case "February":
			var url = 'http://www.kristimorris.com/calendar_files/February_%202007.htm';
			break;
		case "March":
			var url = 'http://www.kristimorris.com/calendar_files/March_%202007.htm';
			break;
		default : 	 
			break;
		}
	
	if (type != 'gallery') {
		win.getContent().innerHTML="<div id='CalanderHolder'></div>";
		var target = "CalanderHolder";
	} else {
		url = page;
		var target = win.getContent();
	}
	
   
    var myAjaxCalander = new Ajax.Updater(target, url, {method: 'get', onComplete: function() { } });

	   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"});
	},
	
}

