  dojo.require("dojo.io.*");
  dojo.require("dojo.widget.ContentPane");
  dojo.require("dojo.widget.Form");
  dojo.require("dojo.widget.*");
  dojo.require("dojo.lfx.html");
  dojo.require("dojo.undo.browser");
  dojo.require("dojo.widget.TitlePane");
  var content;
  var currentpage="";

  var state = { }; 

  function sendForm() {
   var form=dojo.widget.byId('form1');
   var btn=dojo.byId('form1btn');
   btn.disabled=true;
   var values=form.getValues();
   values["spam"]="nospam";
   if (form.isValid()) {
     btn.value="Lähetetään tietoja!";
     dojo.io.bind({
	url: "sendMail.php",
	content: values,
        method: "post",
	load: function(a,data) {
   	    var fo=dojo.lfx.fadeOut(content.domNode, 500);
            dojo.event.connect(fo, "onEnd", function() { 
	    	content.setContent(data);
            });
   	    fo.play();
	},
        error:  function (a,b) {
		alert ("Tapahtui virhe lähetettäessä lomaketta!");
	}
     });
   } else {
     var value=btn.value;
     btn.value="Pakolliset kentät eivät ole täytettty!";
     setTimeout(function() { btn.disabled=false; btn.value=value; }, 2000);
   }
   return false;
  }

  function showUrl(page, init) {
   if (currentpage == page) { return; }
   if (currentpage != "") {
        dojo.html.setStyle("MENU"+currentpage, "textDecoration", "none");
   }
   currentpage=page;

   var fo=dojo.lfx.html.fadeOut(content.domNode, 250);

   var self=this;
   if (!dojo.render.html.safari) {
     content.bindArgs={
	changeUrl: page,
	back: function() { self.getPage(); },
	forward: function() { self.getPage(); } 
     };
   }
   var fo=dojo.lfx.html.fadeOut(content.domNode, 150);
   dojo.event.connect(fo, "onEnd", function() {
   	content.setUrl(page);
   });
   fo.play();
  }

  function gotoUrl(page) {
   if (currentpage == page) { return; }
   currentpage=page;

   var fo=dojo.lfx.html.fadeOut(content.domNode, 250);

   var self=this;
   if (!dojo.render.html.safari) {
     content.bindArgs={
	changeUrl: page,
	back: function() { self.getPage(); },
	forward: function() { self.getPage(); } 
     };
   }
   var fo=dojo.lfx.html.fadeOut(content.domNode, 150);
   dojo.event.connect(fo, "onEnd", function() {
	window.location=page;
   });

   fo.play();
  }


  function showPage(page, init) {
   if (currentpage == page) { return; }
   if (currentpage != "") {
        dojo.html.setStyle("MENU"+currentpage, "textDecoration", "none");
   }
   currentpage=page;
  	var fo=dojo.lfx.html.fadeOut(content.domNode, 250);

   var self=this;
   if (!dojo.render.html.safari) {
     content.bindArgs={
	changeUrl: page,
	back: function() { self.getPage(); },
	forward: function() { self.getPage(); } 
     };
   }
   var fo=dojo.lfx.html.fadeOut(content.domNode, 150);
   dojo.event.connect(fo, "onEnd", function() {
   	content.setUrl(page + ".html");
   });
if(!init) {
  fo.play();
		currentPage="";
}
  }

  function getPage() {
   var fo=dojo.lfx.html.fadeOut(content.domNode, 150);

   dojo.event.connect(fo, "onEnd", function() {
     var bookmarkId = location.hash;
     bookmarkId = bookmarkId.substring(1, bookmarkId.length);
     dojo.io.bind({
       url: bookmarkId+".html",
       mimetype: "text/html",
       load: function(a,page) {
         content.setContent(page);
       },
       error: function() {
         content.setContent("virhe");
       }
      });
    });

   fo.play();
  }

  function init() {

   content=dojo.widget.byId("primaryContent");

   dojo.undo.browser.setInitialState(state);

   dojo.event.connect(content, "onLoad", function() {
	if (currentPage == "") {
   		fo=dojo.lfx.html.fadeIn(content.domNode, 150);
   		fo.play();
	}
        dojo.html.setStyle("MENU"+currentpage, "textDecoration", "underline");
   });
   dojo.event.connect(content, "onDownloadError", function(e) {
    e.preventDefault();
    content.setContent("<h2>Sivua ei löytynyt! (404)</h2>");
   });

   var bookmarkId = location.hash;
    if (bookmarkId) {
       bookmarkId = bookmarkId.substring(1, bookmarkId.length);
     } else {
       bookmarkId = currentPage;
        dojo.html.setStyle("MENU"+bookmarkId, "textDecoration", "underline");
    }
   showPage(bookmarkId, true);
   }
 
  dojo.addOnLoad(init);

