﻿// Ponnahdusikkuna:
var popwin=null;
// Hakutoiminnon status:
var searchready=false;
// Pomppivat elementit:
var bouncing=new Array();

function getElementsByClassName(className,tag,elm) {
 var testClass=new RegExp("(^|\\s)"+className+"(\\s|$)");
 var tag=tag || "*";
 var elm=elm || document;
 var elements=(tag=="*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
 var returnElements=[];
 var current;
 var length=elements.length;
 for (var i=0; i<length; i++) {
  current=elements[i];
  if (testClass.test(current.className)) {returnElements.push(current);};
  };
 return returnElements;
 };

function picpop(id,x,y) {
 if (popwin && !popwin.closed) {popwin.close();};
 popwin=window.open('picpop.php?id='+id+'&x='+x+'&y='+y,'','width='+x+',height='+y+',scrollbars=no,resizable=no,menubar=no,status=no,directories=no,location=no,toolbar=no');
 };

function initsearch(txt) {
 o=document.hakulomake.haku;
 o.style.color='#AAAAAA';
 o.value=txt;
 o.blur();
 };

function clearsearch() {
 o=document.hakulomake.haku;
 o.style.color='#000000';
 o.value='';
 return true;
 };

function findPosAndSize(o) {
 var curLeft=curTop=width=height=0;
 if (o.offsetWidth) {width=o.offsetWidth};
 if (o.offsetHeight) {height=o.offsetHeight};
 if (o.offsetParent) {
  curLeft=o.offsetLeft;
  curTop=o.offsetTop;
  while (o=o.offsetParent) {
   curLeft+=o.offsetLeft;
   curTop+=o.offsetTop;
   };
  };

 return {'left':curLeft, 'top':curTop, 'width':width, 'height':height};
 };

function correctHeight(a,b) {
 a=document.getElementById(a);
 b=document.getElementById(b);
 if (a.clientHeight>b.clientHeight) {b.style.height=a.clientHeight+'px';}
 else {a.style.height=b.clientHeight+'px';};
 };

function markPage(lid,kid) {
 var arr=getElementsByClassName(kid);
 if (arr.length<1) {return false;};
 var data=findPosAndSize(arr[arr.length-1]);
 document.getElementById(lid).style.left=data['left']+Math.round(data['width']/2)+'px';
 document.getElementById(lid).style.top=data['top']+data['height']+'px';
 };

function bounce(id,endpoint,speed,gravity,elasticity,delay) {
 var o=document.getElementById(id);
 var ps=findPosAndSize(o);
 var speed=speed+gravity;
 var newtop=Math.min(endpoint,ps.top+Math.round(speed));
 o.style.top=newtop+'px';
 if (newtop==endpoint) {
  speed=-speed*elasticity;
  if (newtop==ps.top) {
   bouncing[id]=false;
   return true;
   };
  };
 bouncing[id]=setTimeout("bounce('"+id+"',"+endpoint+","+speed+","+gravity+","+elasticity+","+delay+")",delay);
 return false;
 };

function drop(id,endpoint,speed,gravity,elasticity,delay) {
 if (bouncing[id]) {clearTimeout(bouncing[id]);};
 bouncing[id]=bounce(id,endpoint,speed,gravity,elasticity,delay);
 return true;
 };

function randord() {return(Math.round(Math.random())-0.5);};

function slideshow(host,frameLength) {
 if (!(this.host=host)) {return false;};
 this.frame=frameLength;
 var p=host.id.split(':');
 this.stay=Math.round(parseFloat(p[1])*1000);
 this.step=100*frameLength/(parseFloat(p[2])*1000);
 this.random=(p[3].toLowerCase()=='rand');
 var arr=host.getElementsByTagName('img');
 if (!arr.length) {return false;};
 this.imgurls=new Array();
 for (x=0; x<arr.length; x++) {if (arr[x].src!='') {this.imgurls[x]=arr[x].src;};};
 if (this.random) {this.imgurls.sort(randord);};
 var cell=host;
 if (host.hasChildNodes()) {while (host.childNodes.length>=1) {host.removeChild(cell.firstChild);};};
 this.img=document.createElement('img');
 this.img.setAttribute('src',this.imgurls[0]);
 host.appendChild(this.img);
 this.fadeIn(0,0);
 };

slideshow.prototype.fadeIn=function(p,n) {
 var self=this;
 if (!this.img.complete) {
  setTimeout(function() {self.fadeIn(0,n);},200);
  return false;
  };
 /*
 var my=Math.round((this.host.offsetHeight-this.img.height)/2);
 var mx=Math.round((this.host.offsetWidth-this.img.width)/2);
 if (p==0) {
  this.img.style.marginTop=my+'px';
  this.img.style.marginLeft=mx+'px';
  };
 */
 var rp=Math.round(p);
 this.img.style.filter='alpha(opacity='+rp+')';
 this.img.style.opacity=''+(rp/100);
 this.img.style.visibility='visible';
 if (p<100) {
  p+=this.step;
  if (p>100) {p=100;};
  setTimeout(function() {self.fadeIn(p,n);},this.frame);
  }
 else {
  this.host.style.backgroundImage='url('+this.imgurls[n]+')';
  // this.host.style.backgroundPosition=mx+'px '+my+'px';
  n++;
  if (n==this.imgurls.length) {n=0;};
  this.img.style.visibility='hidden';
  setTimeout(function() {self.img.src=self.imgurls[n];},this.frame);
  setTimeout(function() {self.fadeIn(0,n);},this.stay);
  };
 return true;
 };

var ssObjects=new Array();
var ssElements=getElementsByClassName('slideshow','*',document);
for (var i=0; i<ssElements.length; i++) {ssObjects.push(new slideshow(ssElements[i],40));};

// IE-korjaukset:
// if (o=document.getElementById('')) {o.allowTransparency=true;};

