/* vanhat sivut
--------------- */

/* Valmistelut
----------------------------------*/

// Ulos kehyksistä

if (self != top) {
	top.location.href = self.location.href
}

// popup-ikkunat

function mediaPop(media) {
	NewWindow = window.open(media, 'media', 'width=250, height=170, menubar=no, status=no, resizable=no, toolbar=no, location=no, scrollbars=no')
	NewWindow.focus()
}

function esimerkkiPop(esimerkki) {
	NewWindow = window.open(esimerkki, 'esimerkki', 'width=400, height=600, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}

function esimerkkiPopIso(esimerkkiIso) {
	NewWindow = window.open(esimerkkiIso, 'esimerkkiIso', 'width=750, height=600, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}

function loadinparent(url){
	self.opener.location = url;
}

/* MEDIA PLAYER
--------------------------------- */

function playPop(url) {
	var NewWindow = window.open(url, 'playpop', 'width=710, height=250, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}
function presentationPop(url) {
	var NewWindow = window.open(url, 'presentationpop', 'width=900, height=700, menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}
function customPop(url,width,height) {
	var NewWindow = window.open(url, 'attachmentpop', 'width='+width+', height='+height+', menubar=no, status=no, resizable=yes, toolbar=no, location=no, scrollbars=yes')
	NewWindow.focus()
}
/* Apufunktioita
----------------------------------*/

function showItem(id) {
	if (!document.getElementById) return false;
	var select = document.getElementById(id);
	select.style.display="block"; //.setAttribute("style", "display:block");
	//select.setAttribute("class", "show");
}
function hideItem(id) {
	if (!document.getElementById) return false;
	//document.getElementById(id).setAttribute("style", "display:none");
	var select = document.getElementById(id);
	select.style.display="none"; //.setAttribute("style", "display:none");
	//select.setAttribute("class", "hide");
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}
/*
function teeSposti() {
	if (!document.createElement) return false;
	if (!document.createTextNode) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("sposti")) return false;
	var teeSpostiOs = document.createElement("a");
	var tee = "mailto:";
	var alku = "info";
	var loppu = "raulihaverinen.fi";
	var spostiOsoite = alku + "@" + loppu;
	teeSpostiOs.setAttribute("href",tee + spostiOsoite);
	var spostiTeksti = document.createTextNode(spostiOsoite);
	teeSpostiOs.appendChild(spostiTeksti);
	var finaali = document.getElementById("sposti");
	finaali.firstChild.style.display="none";
	finaali.appendChild(teeSpostiOs);
}
*/
function aikaNyt() {
	var hae_aika = new Date();
	var v = hae_aika.getYear()
	var v = v + 1900;
	var kk = hae_aika.getMonth()
	var kk = kk + 1;
	var kk = kk + "";
	if (kk.length == 1) { kk = "0" + kk; }
	var pv = hae_aika.getDate()
	var pv = pv + "";
	if (pv.length == 1) { pv = "0" + pv; }
	var h = hae_aika.getHours()
	var h = h + "";
	if (h.length == 1) { h = "0" + h; }
	var m = hae_aika.getMinutes()
	var m = m + "";
	if (m.length == 1) { m = "0" + m; }
	var s = hae_aika.getSeconds()
	var s = s + "";
	if (s.length == 1) { s = "0" + s; }
	var aika_nyt = (v + "-" + kk + "-" + pv + " " + h + ":" + m + ":" + s);
	return aika_nyt;
}


/* Audio player
----------------------------------*/
var ap_instances = new Array();

function ap_stopAll(playerID) {
	for(var i = 0;i<ap_instances.length;i++) {
		try {
			if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
			else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
		} catch( errorObject ) {
			// stop any errors
		}
	}
}

function ap_registerPlayers() {
	var objectID;
	var objectTags = document.getElementsByTagName("object");
	for(var i=0;i<objectTags.length;i++) {
		objectID = objectTags[i].id;
		if(objectID.indexOf("audioplayer") == 0) {
			ap_instances[i] = objectID.substring(11, objectID.length);
		}
	}
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );

/* Taustalla kuvagalleriascripti kirjasta Jeremy Keith: "DOM Scripting 
- Web Design with JavaScript and the Document Object Model"
---------------------------------------------------------- */

function haeMuuttujaURL() {
/*	var koko = new String(document.location);
	var pala = koko.split("http://raulihaverinen.fi/");
	pala = pala[1].split("/");*/
	var hashhh = document.location.hash.replace("#",'');
	hashhh = hashhh.replace("/",'');
//	alert(hashhh);
//	pala = hashhh.split("/");
	//	alert(hashh);


//	var i = pala.length;
	var auki = hashhh;
	if (auki.length < 1) {
		auki = '01';
	}
	if (auki.length > 2) {
		auki = '01';
	}
	if (isNaN(auki)) {
		auki = '01';
//		document.location.href = "http://raulihaverinen.fi/valokuva/"
	}
	if (auki.length < 2) {
		auki = '0' + auki;
	}
	var kuvalinkit = document.getElementById("kuvalinkit");
	var links = kuvalinkit.getElementsByTagName("a");
	if (links.length < auki) {
		auki = '01';
	}
	return auki;
}

function haeMuuttujaSessio() {  // TODO ei toimi
	if (!document.getElementById("oletusid")) { 
		return false;
	} else {
		var oletusid = document.getElementById(oletusid);
		oletusid = oletusid.getAttribute("class");
		if (oletusid.length < 1) {
			oletusid = '01';
		}
		if (oletusid.length > 2) {
			oletusid = '01';
		}
		if (oletusid.length < 2) {
			oletusid = '0' + auki;
		}
		return oletusid;
	}
}

function prepareKuvalaatikko() { // tehdään img-kuvaavoin ja kuvaus, sijoitetaan kuvalinkkien perään xhtml:ssä
	if (!document.createElement) return false;
	if (!document.createTextNode) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("kuvalinkit")) return false;
	var kuvalaatikko = document.createElement("div"); // tehdään div#kuvalaatikko
	kuvalaatikko.setAttribute("id","kuvalaatikko");
	
	// oletusasetukset
	
	var oletus_id = "kuva" + haeMuuttujaURL();
	
	var oletus = document.getElementById(oletus_id);
	oletus.focus(); // focus ensimmäiseen linkkin että saadaan aktiivisen css
	var oletus_src = oletus.getAttribute("href");
	var kuvaavoin = document.createElement("img"); // img#kuvaavoin
	kuvaavoin.setAttribute("id","kuvaavoin");
	kuvaavoin.setAttribute("src",oletus_src);
	kuvaavoin.setAttribute("class",oletus_id);

//	kuvaavoin.setAttribute("alt","oma kuvalinkit");
	kuvalaatikko.appendChild(kuvaavoin); // kuva laatikkoon
	var kuvaus = document.createElement("ul"); // ul#kuvaus
	kuvaus.setAttribute("id","kuvaus");
	
	var oletus_tiedot = oletus.getAttribute("title");
	var oletus_tiedot = oletus_tiedot.split("; ");
	
	var paikka = document.createElement("li");
		paikka.setAttribute("id","paikka");
		kuvaus.appendChild(paikka).appendChild(document.createTextNode(oletus_tiedot[0]));
	var aika = document.createElement("li");
		aika.setAttribute("id","aika");
		kuvaus.appendChild(aika).appendChild(document.createTextNode(oletus_tiedot[1]));
	var kamera = document.createElement("li");
		kamera.setAttribute("id","kamera");
		kuvaus.appendChild(kamera).appendChild(document.createTextNode(oletus_tiedot[2]));
	var objektiivi = document.createElement("li");
		objektiivi.setAttribute("id","objektiivi");
		kuvaus.appendChild(objektiivi).appendChild(document.createTextNode(oletus_tiedot[3]));
	var ohjelma = document.createElement("li");
		ohjelma.setAttribute("id","ohjelma");
		if (oletus_tiedot[4]) {
			kuvaus.appendChild(ohjelma).appendChild(document.createTextNode(oletus_tiedot[4]));
		} else {
			kuvaus.appendChild(ohjelma).appendChild(document.createTextNode(' '));
		}
	var kuvalinkit = document.getElementById("kuvalinkit"); //haetaan kuvalinkit
	insertAfter(kuvalaatikko,kuvalinkit); // kuvaboxi perään
	insertAfter(kuvaus,kuvalaatikko); // kuvateksti kuvan jälkeen
	
	oletus.setAttribute("class","katsottu");
}

function prepareGallery() { // haetaan listaus kuvista
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("kuvalinkit")) return false;
	var kuvalinkit = document.getElementById("kuvalinkit");
	var links = kuvalinkit.getElementsByTagName("a");
	for ( var i=0; i < links.length; i++ ) {
		links[i].onclick = function() {
			jokuvalittu = true;
			return naytaKuva(this);
		}
	}
}

function naytaKuva(valittu) {
	if (!document.getElementById("kuvaavoin")) return true;
	var source = valittu.getAttribute("href");
	valittu.setAttribute("class","katsottu");
	var kuvaavoin = document.getElementById("kuvaavoin");
	kuvaavoin.setAttribute("src",source);

	// siirretään id jotta seuraavaa klikattaessa löydetään edellinen
	valittu_id = valittu.getAttribute("id");
	kuvaavoin.setAttribute("class",valittu_id);

	if (!document.getElementById("kuvaus")) return false;
	if (valittu.getAttribute("title")) {
		var tiedot = valittu.getAttribute("title");
//	if (valittu.nextSibling.childNodes[0].nodeValue) {
	//	var tiedot = valittu.nextSibling.childNodes[0].nodeValue;
		var tiedot = tiedot.split("; ");
		var paikka = document.getElementById("paikka");
		if (paikka.firstChild.nodeType == 3) {
			paikka.firstChild.nodeValue = tiedot[0];
		}
		var aika = document.getElementById("aika");
		if (aika.firstChild.nodeType == 3) {
			aika.firstChild.nodeValue = tiedot[1];
		}
		var kamera = document.getElementById("kamera");
		if (kamera.firstChild.nodeType == 3) {
			kamera.firstChild.nodeValue = tiedot[2];
		}
		var objektiivi = document.getElementById("objektiivi");
		if (objektiivi.firstChild.nodeType == 3) {
			objektiivi.firstChild.nodeValue = tiedot[3];
		}
		var ohjelma = document.getElementById("ohjelma");
		if (ohjelma.firstChild.nodeType == 3) {
			if (tiedot[4]) {
				ohjelma.firstChild.nodeValue = tiedot[4];
			} else {
				ohjelma.firstChild.nodeValue = " ";
			}
		}
		var altteksti = document.getElementById("kuvaavoin");
		kuvaavoin.setAttribute("alt",tiedot[0]);
/*		if (document.getElementById("galleria_intro")) {
			document.getElementById("galleria_intro").setAttribute("style","display:none;");
		}*/
	}
	return false;
}

/* Ajo sivun latautuessa
----------------------------------*/

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(prepareKuvalaatikko);
addLoadEvent(prepareGallery);


/* uusi koodi */



function prepareSitemapLink() {
	if (!document.getElementById("sitemaplink")) return false;
	var sitemap_link = document.getElementById("sitemaplink");

	sitemap_link.onclick = function() {
		var sidebar_structure = document.getElementById("sidebar");
		var scructure_links = sidebar_structure.getElementsByTagName("li");
		for ( var i=0; i < scructure_links .length; i++ ) {
			if(scructure_links[i].getAttribute("class") != "structureopen" && scructure_links[i].getAttribute("class") != "pagelink" && scructure_links[i].getAttribute("class") != "pageopen")
				scructure_links[i].setAttribute("class","sitemapopen");
		}
		prepareRemoveSitemapLink();
		return false;
	}
}
function prepareRemoveSitemapLink() {
	if (!document.getElementById("sitemaplink")) return false;
	var sitemap_link = document.getElementById("sitemaplink");
	sitemap_link.onclick = function() {
		var sidebar_structure = document.getElementById("sidebar");
		var scructure_links = sidebar_structure.getElementsByTagName("li");
		for ( var i=0; i < scructure_links .length; i++ ) {
			if(scructure_links[i].getAttribute("class") != "structureopen"&& scructure_links[i].getAttribute("class") != "pagelink" && scructure_links[i].getAttribute("class") != "pageopen")
				scructure_links[i].setAttribute("class","structurehidden");
		}
		prepareSitemapLink();
		return false;
	}
}
/*
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
*/
addLoadEvent(prepareSitemapLink);

