var count = 0;
var max = 0;
var slideshow = document.createElement("div");

$(document).ready(function(){

max = laske_otsikot(); 
var body = document.getElementById("body");
body.insertBefore(slideshow, body.firstChild); 
lisaanav();

/* kommenttilomakkeen lisääminen */

  addEvent(document.getElementById("commentlisaa"), "click", lisaakommenttiloota);

  var div = document.getElementsByTagName("div");
  for(var i=0; i<div.length; i++) {
    if ( div.item(i).getAttribute("class") == "extra" ) {
		hide_extra(div.item(i));
		var strong = document.createElement("strong");
		strong.style.textDecoration = "underline";
		div.item(i).insertBefore(strong, div.item(i).firstChild);
		strong.textContent = "Lisää...";

		$(strong).click(function() {
		  var child = this.parentNode.childNodes;
		     for(var j=0; j<child.length; j++) if (child.item(j).style) child.item(j).style.display = "block"; 
		 	this.parentNode.firstChild.style.display = "none";	
		});

	}
  }


});



function hide_extra(ele) {
  var child = ele.childNodes;
  for(var j=0; j<child.length; j++) if (child.item(j).style) child.item(j).style.display = "none"; 
}






function addEvent( obj, type, fn ) {
  /* onko IE? */
  if ( obj.attachEvent ) {
    obj.attachEvent( 'on'+type, fn );
  } else {
    obj.addEventListener( type, fn, false );
  }
}

function lisaakommenttiloota() {
 
  var lomake =  document.getElementById("commentform");
  /* bottien kiusaksi kahdessa palassa */
  lomake.setAttribute("action","/cgi-bin");
  lomake.setAttribute("action",lomake.getAttribute("action") + "/comment.cgi");
  
  var fs = document.getElementById("commentfieldset");
  fs.removeChild(document.getElementById("commentlisaa"));
  
  var div = document.createElement("div");
  div.setAttribute("class","tasa");
  var label = document.createElement("label");
  label.setAttribute("for", "kommenttinimi");
  var text = document.createTextNode("Nimi");
  label.appendChild(text); 
  div.appendChild(label);
  fs.appendChild(div);

  var p = document.createElement("p");
  var input = document.createElement("input");
  input.setAttribute("id", "kommenttinimi");
  input.setAttribute("type", "text");
  input.setAttribute("name", "kommenttinimi");
  input.setAttribute("value", "");
  input.setAttribute("size", "20");
  p.appendChild(input);
  fs.appendChild(p);

  var div = document.createElement("div");
  div.setAttribute("class","tasa");
  var label = document.createElement("label");
  label.setAttribute("for", "kommenttiemail");
  var text = document.createTextNode("Email");
  label.appendChild(text); 
  div.appendChild(label);
  fs.appendChild(div);

  var p = document.createElement("p");
  var input = document.createElement("input");
  input.setAttribute("id", "kommenttiemail");
  input.setAttribute("type", "text");
  input.setAttribute("name", "kommenttiemail");
  input.setAttribute("value", "");
  input.setAttribute("size", "20");
  p.appendChild(input);
  var small = document.createElement("small");
  text = document.createTextNode("(vapaaehtoinen, ei näy sivulla)");
  small.appendChild(text); 
  p.appendChild(small);
  fs.appendChild(p);

  var div = document.createElement("div");
  div.setAttribute("class","tasa");
  var label = document.createElement("label");
  label.setAttribute("for", "kommenttiroska");
  var text = document.createTextNode("Sanan vilppi monikko?");
  label.appendChild(text); 
  div.appendChild(label);
  fs.appendChild(div);

  var p = document.createElement("p");
  var input = document.createElement("input");
  input.setAttribute("id", "kommenttiroska");
  input.setAttribute("type", "text");
  input.setAttribute("name", "roskatesti");
  input.setAttribute("value", "");
  input.setAttribute("size", "20");
  p.appendChild(input);
  var small = document.createElement("small");
  text = document.createTextNode("(roskapostin välttämiseksi)");
  small.appendChild(text); 
  p.appendChild(small);
  fs.appendChild(p);




  var div = document.createElement("div");
  div.setAttribute("class","tasa");
  var label = document.createElement("label");
  label.setAttribute("for", "kommenttikommentti");
  var text = document.createTextNode("Kommentti");
  label.appendChild(text); 
  div.appendChild(label);
  fs.appendChild(div);

  var p = document.createElement("p");
  var ta = document.createElement("textarea");
  ta.setAttribute("class", "max");
  ta.setAttribute("id", "kommenttikommentti");
  ta.setAttribute("name", "kommentti");
  ta.setAttribute("cols", "32");
  ta.setAttribute("rows", "5");
  var text = document.createTextNode("");
  ta.appendChild(text);
  p.appendChild(ta);
  fs.appendChild(p);

  var p = document.createElement("p");
  p.setAttribute("class","clear");
  var input = document.createElement("input");
  input.setAttribute("type", "submit");
  input.setAttribute("value", "Lisää kommentti");
  addEvent(input, "click", kiitos); 
  p.appendChild(input);
  fs.appendChild(p);

}

function kiitos() {
  alert("Kiitos kommentista! Kommentti lisätään tarkistuksen jälkeen.");
}


function projection() {

	if ( count < 1 ) { count = 1; return; }
	if ( count > max ) { count = max; return; }

	for( x=0,s=document.styleSheets;s[x];x++) {
		p=s[x].media;
		if (p.mediaText=="projection") p.appendMedium("screen");
	}

/*
     var tyyli = document.getElementById("appro");
     tyyli.setAttribute("media", "foobar");
     var tyyli = document.getElementById("projection");
     tyyli.setAttribute("media", "screen");
var body = document.getElementsByTagName("div");

for(var i=0; i<body.length; i++) {
  if ( body.item(i).getAttribute("class") == "navbar" ) body.item(i).style.display = "none";
  if ( body.item(i).getAttribute("class") == "navbartop" ) body.item(i).style.display = "none";
  if ( body.item(i).getAttribute("class") == "breadcrumb" ) body.item(i).style.display = "none";
  if ( body.item(i).getAttribute("class") == "logo" ) body.item(i).style.display = "none";
}

body = document.getElementsByTagName("ul");

for(var i=0; i<body.length; i++) {
  if ( body.item(i).getAttribute("class") == "breadcrumb" ) body.item(i).style.display = "none";
}
*/
        slideshow.setAttribute("class", "main");
        nodes = slideshow.childNodes;
	var i = 0;
	while ( nodes.item(i) ) { 
			slideshow.removeChild(nodes.item(i)); 
	}

        main = document.getElementById("main");
        nodes = main.childNodes;

        i = 0;
        var apu = 0;
        while ( nodes.item(i) ) {
	        var node = nodes.item(i);
		if ( otsikko(node) ) { 
			apu++; 
		}
	        i++;
		if ( apu == count  ) {
			var kopio = node.cloneNode(1); 
			slideshow.appendChild(kopio); 
		} 
        }

	main.style.display = "none"; 
	lisaanav();	
	scrollTo(0,0)

var body = document.getElementById("logo");
body.style.display = "none";
body = document.getElementById("navbartop");
body.style.display = "none";
body = document.getElementById("author");
body.style.display = "none";
body = document.getElementById("breadcrumb");
body.style.display = "none";
body = document.getElementById("prevbot");
body.style.display = "none";
body = document.getElementById("nextbot");
body.style.display = "none";
body = document.getElementById("nexttop");
body.style.display = "none";
body = document.getElementById("otsake");
body.style.display = "none";
body = document.getElementById("prevtop");
body.style.display = "none";
body = document.getElementById("body");
body.style.margin = "0px";
body.style.padding = "0px";
body = document.getElementById("main");
body.style.margin = "0px";
body.style.padding = "0px";
body.style.display = "none";

//slideshow.style.fontsize = "2em";

}

function otsikko(node) {

 if ( node.nodeName.toLowerCase() == "h1" ||
	node.nodeName.toLowerCase() == "h2" ||
	node.nodeName.toLowerCase() == "h3" )
  return true;
}

function lisaanav() {

var a = document.createElement("a")
a.setAttribute("accesskey", "w");
a.setAttribute("id", "next");
a.setAttribute("href", "#");
a.setAttribute("onclick", "count=count+1;projection();return false;");
a.setAttribute("class", "hide");
slideshow.appendChild(a);

a = document.createElement("a")
a.setAttribute("accesskey", "q");
a.setAttribute("id", "prev");
a.setAttribute("href", "#");
a.setAttribute("onclick", "count=count-1;projection();return false;");
a.setAttribute("class", "hide");
slideshow.appendChild(a);

}

function laske_otsikot() {
        var main = document.getElementById("main");
        var nodes = main.childNodes;  
        var lkm = 0;

       for (var i=0; i<nodes.length; i++) {
         var node = nodes.item(i);
		if ( otsikko(node) ) { 
			lkm++; 
		}
        }

        return lkm;
}


