function popup() {
	window.open('/popup.htm','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=525,height=480,left=44,top=44');
}

function openwin(theURL,winName,features) {
	xaken=window.open(theURL,winName,features);
}

function ToggleAll(e) {
	if (e.checked) {
		CheckAll();
	}
	else {
		ClearAll();
	}
}

function CheckAll() {
	var cl = document.listing;
	var len = cl.elements.length;
	for (var i = 0; i < len; i++) {
		var e = cl.elements[i];
		if (e.name == 'MailID[]') {
			e.checked = true;
		}
	}
	cl.toggleAllt.checked = true;
	cl.toggleAllb.checked = true;
}

function ClearAll() {
	var cl = document.listing;
	var len = cl.elements.length;
	for (var i = 0; i < len; i++) {
		var e = cl.elements[i];
		if (e.name == 'MailID[]') {
			e.checked = false;
		}
	}
	cl.toggleAllt.checked = false;
	cl.toggleAllb.checked = false;
}

function ShowPicture(Image,Width,Height) {
	openwin('/photo.php?i='+Image,'Pilt','toolbar=0,status=0,menubars=0,scrollbars=0,resizable=0,location=0,directories=0,width='+Width+',height='+Height+',top=50,left=50');
}

function PageNumbers(a) {
	document.fo.start.value=a;
	document.fo.submit();
}

function sort(a) {
	document.fo.sort.value=a;
	document.fo.submit();
}

function Kaart(a,l) {
	openwin('/map.php?i='+a+'&l='+l,'','toolbar=0,status=0,menubars=0,scrollbars=0,resizable=0,location=0,directories=0,width=468,height=340,top=50,left=50');
}

function zoom(a) {
	document.map.noz.value=1;
	document.map.zoom_s.value=a;
	document.map.submit();
}

function move(a) {
	document.map.noz.value=1;
	document.map.nihe.value=a;
	document.map.submit();
}

function LgMap(SObj) {
	if(SObj.options[SObj.selectedIndex].value=="lg"){
		var a=document.map.fullscreen.value;
		openwin(a,'Fullscreen','width=800,height=600,menubar=1')
		document.map.mapsize.value="sm";
	}
}

function show_kaart(x,y,zoom,label) {
	document.map.bbox.value='';
	document.map.adr_x.value=x;
	document.map.adr_y.value=y;
	document.map.zoom_s.value=zoom;
	document.map.adlabel.value=label;
	document.map.submit();
}

function SendMail() {
	var theForm=document.listing;
	var ch=0;
	for(i=0; i < theForm.elements['MailID[]'].length; i++){
		if(theForm.elements['MailID[]'][i].checked) ch++;
	}
	if(theForm.sok.value==1) ch++;
	if(!ch) alert('Midagi pole valitud');
	else document.listing.submit();
}

function SendMail2(a) {
	var theForm=document.listing;
	for(i=0; i < theForm.elements['MailID[]'].length; i++){
		if(theForm.elements['MailID[]'][i].value==a) theForm.elements['MailID[]'][i].checked = true;
	}
	theForm.submit();
}

// arguments: image id, rotation speed, path to images (optional), 
// new (optional) arguments: for transitions, mouse events and random rotation 
function dw_Rotator(id, speed, path, bTrans, bMouse, bRand) {
    var imgObj = document.getElementById(id); 
    if (!imgObj) { // in case name, not id attached to image
        imgObj = document.images[id];
        if (!imgObj) return;
        imgObj.id = id;
    }
    this.id = id; this.speed = speed || 4500; // default speed of rotation
    this.path = path || "";  this.bRand = bRand;
    this.ctr = 0; this.timer = 0; this.imgs = []; 
    this._setupLink(imgObj, bMouse);
    this.bTrans = bTrans && typeof imgObj.filters != 'undefined';
    var index = dw_Rotator.col.length; dw_Rotator.col[index] = this;
    this.animString = "dw_Rotator.col[" + index + "]";
}

dw_Rotator.col = []; // hold instances
dw_Rotator.resumeDelay = 400; // onmouseout resume rotation after delay

// mouse events pause/resume
dw_Rotator.prototype._setupLink = function(imgObj, bMouse) { 
    if ( imgObj.parentNode && imgObj.parentNode.tagName.toLowerCase() == 'a' ) {
        var parentLink = this.parentLink = imgObj.parentNode;
        if (bMouse) {
            dw_Event.add(parentLink, 'mouseover', dw_Rotator.pause);
            dw_Event.add(parentLink, 'mouseout', dw_Rotator.resume);
        }
    }
}

// so instance can be retrieved by id (as well as by looping through col)
dw_Rotator.getInstanceById = function(id) {
    var len = dw_Rotator.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = dw_Rotator.col[i];
        if (obj.id && obj.id == id ) {
            return obj;
        }
    }
    return null;
}

dw_Rotator.prototype.on_rotate = function() {}

dw_Rotator.prototype.addImages = function() { // preloads images
    var img;
    for (var i=0; arguments[i]; i++) {
        img = new Image();
        img.src = this.path + arguments[i];
        this.imgs[this.imgs.length] = img;
    }
}

dw_Rotator.prototype.rotate = function() {
    clearTimeout(this.timer); this.timer = null;
    var imgObj = document.getElementById(this.id);
    if ( this.bRand ) {
        this.setRandomCtr();
    } else {
        if (this.ctr < this.imgs.length-1) this.ctr++;
        else this.ctr = 0;
    }
    if ( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }
    this.swapAlt(imgObj); this.prepAction(); this.on_rotate();
    this.timer = setTimeout( this.animString + ".rotate()", this.speed);   
}

dw_Rotator.prototype.setRandomCtr = function() {
    var i = 0, ctr;
    do { 
        ctr = Math.floor( Math.random() * this.imgs.length );
        i++; 
    } while ( ctr == this.ctr && i < 6 )// repeat attempts to get new image, if necessary
    this.ctr = ctr;
}

dw_Rotator.prototype.doImageTrans = function(imgObj) {
    imgObj.style.filter = 'blendTrans(duration=1)';
    if (imgObj.filters.blendTrans) imgObj.filters.blendTrans.Apply();
    imgObj.src = this.imgs[this.ctr].src;
    imgObj.filters.blendTrans.Play(); 
}

dw_Rotator.prototype.swapAlt = function(imgObj) {
    if ( !imgObj.setAttribute ) return;
    if ( this.alt && this.alt[this.ctr] ) {
        imgObj.setAttribute('alt', this.alt[this.ctr]);
    }
    if ( this.title && this.title[this.ctr] ) {
        imgObj.setAttribute('title', this.title[this.ctr]);
    }
}

dw_Rotator.prototype.prepAction = function() {
    if ( this.actions && this.parentLink && this.actions[this.ctr] ) {
        if ( typeof this.actions[this.ctr] == 'string' ) {
            this.parentLink.href = this.actions[this.ctr];
        } else if ( typeof this.actions[this.ctr] == 'function' ) {
            // to execute function when linked image clicked 
            // passes id used to uniquely identify instance  
            // retrieve it using the dw_Rotator.getInstanceById function 
            // so any property of the instance could be obtained for use in the function 
            var id = this.id;
            this.parentLink.href = "javascript: void " + this.actions[this.ctr] + "('" + id + "')";
        } 
    }
}

dw_Rotator.prototype.showCaption = function() {
    if ( this.captions && this.captionId ) {
        var el = document.getElementById( this.captionId );
        if ( el && this.captions[this.ctr] ) {
            el.innerHTML = this.captions[this.ctr];
        }
    }
}

// Start rotation for all instances 
dw_Rotator.start = function() {
    var len = dw_Rotator.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = dw_Rotator.col[i];
        if (obj && obj.id ) 
            obj.timer = setTimeout( obj.animString + ".rotate()", obj.speed);
    }
}

// Stop rotation for all instances 
dw_Rotator.stop = function() {
    var len = dw_Rotator.col.length, obj;
    for (var i=0; i<len; i++) {
        obj = dw_Rotator.col[i];
        if (obj ) { clearTimeout(obj.timer); obj.timer = null; }
    }
}

// for stopping/starting (onmouseover/out)
dw_Rotator.pause = function(e) {	
    e = dw_Event.DOMit(e);
    var id = e.target.id;
    var obj = dw_Rotator.getInstanceById(id);
    if ( obj ) { clearTimeout( obj.timer ); obj.timer = null; }
}

dw_Rotator.resume = function(e) {
    e = dw_Event.DOMit(e);
    var id = e.target.id;
    var obj = dw_Rotator.getInstanceById(id);
    if ( obj && obj.id ) {
        obj.timer = setTimeout( obj.animString + ".rotate()", dw_Rotator.resumeDelay );
    }
}

/////////////////////////////////////////////////////////////////////
// Use this function to set up when using object literals to hold data 
// it calls constructor, addImages, adds actions, etc.

dw_Rotator.setup = function (rObj) {
    if (!document.getElementById) { return; }
    var r = new dw_Rotator(rObj.id, rObj.speed, rObj.path, rObj.bTrans, rObj.bMouse, rObj.bRand);
    try {
        var imgAr = rObj.images; var len = imgAr.length;
        for (var i=0; i<len; i++) { r.addImages( imgAr[i] ); }
        
        if ( rObj.actions && rObj.actions.length == len ) {
            r.addProp('actions', rObj.actions);
        }
        if ( rObj.alt && rObj.alt.length == len ) {
            r.addProp('alt', rObj.alt);
        }
        if ( rObj.title && rObj.title.length == len ) {
            r.addProp('title', rObj.title);
        }
        if ( rObj.captions ) {
            r.addProp('captions', rObj.captions);
            r.captionId = rObj.captionId;
            dw_Rotator.addRotateEvent(r, function () { dw_Rotator.getInstanceById(rObj.id).showCaption(); } ); 
        }
        dw_Rotator.start();
    } catch (e) { 
        alert(e.message); 
    }
}

// add to on_rotate for specified instance (r)
// see usage above for captions
dw_Rotator.addRotateEvent = function( r, fp ) {
    var old_on_rotate = r.on_rotate;
    r.on_rotate = function() { old_on_rotate(); fp(); }
}

// for adding actions, alt, title
dw_Rotator.prototype.addProp = function(prop, ar) {
    if ( !this[prop] ) {
        this[prop] = [];
    }
    var len = ar.length; 
    for (var i=0; i < len; i++) {
        this[prop][ this[prop].length ] = ar[i]; 
    }
}

