﻿// Ponnahdusikkuna:
var popwin=null;
// Pomppivat elementit:
var bouncing=new Array();

// Luodaan contains()-metodi Mozillalle:
if (window.Node && Node.prototype && !Node.prototype.contains) {
 Node.prototype.contains=function(arg) {return !!(this.compareDocumentPosition(arg) & 16)};
 };

function trim(s) {return s.replace(/^\s+|\s+$/g,'');};

function picpop(id,x,y) {
 if (popwin && !popwin.closed) {popwin.close();};
 popwin=window.open('pop-'+id+'-'+x+'-'+y,'','width='+x+',height='+y+',scrollbars=no,resizable=no,menubar=no,status=no,directories=no,location=no,toolbar=no');
 };

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 initField(fieldName,txt,c) {
 o=document.getElementsByName(fieldName);
 o=o[0];
 o.style.color=(typeof(c)=='undefined')?'#AAAAAA':c;
 o.value=txt;
 o.blur();
 o.fieldReady=false;
 };

function clearField(o,c) {
 o.style.color=(typeof(c)=='undefined')?'#000000':c;
 o.value='';
 o.fieldReady=true;
 };

function fieldsReady(form,names,msg) {
 if (!names.length) {return false;};
 for (x=0; x<names.length; x++) {
  elms=document.getElementsByName(names[x]);
  for (y=0; y<elms.length; y++) {
   if (form.contains(o=elms[y]) && (!o.fieldReady || (o.value=trim(elms[y].value))=='')) {
    alert(msg+': '+o.title);
    o.focus();
    return false;
    };
   };
  };
 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 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;
 };

