// Web Content Management System
// ---------------------------------------------------------------------
// Copyright (c) 2003 by Jens Clausen, all rights reserved.
// Web:   www.jensclausen.dk
// Email: info@jensclausen.dk
//
// This file is part of the "Web Content Management System" developed by
// the copyright holder. This file, or any part of it, must not be copied,
// modified or used in any other context.
// The copyright holder owns the "Web Content Management System" and it
// must only be installed and used for the website(s) for which it is
// originally licensed.
// This software is provided "as is", without a warranty of any kind.
// ---------------------------------------------------------------------

var imgObjinst = 0;

function imgObj(l,          // a: load all imgs / s: load single img as defined by n
                n,          // single img to load or first img to display r: random /or 0-n
                w,          // image width (pix)
                h,          // image height (pix)
                c)          // Name of the <div> class
                            // followed by one or more image-path/filenames
{                           // to be ascociated with this image
    this.imgs = new Array();
    this.section = '';
    this.w = w ;
    this.h = h;
    this.cln = c;
    this.p = 0;
    this.srcupd = false;
    if ((imgObj.arguments.length-5) > 0) {
        switch (l) {
            case 'a':
            case 'A':
                for (io=0; io < (imgObj.arguments.length-5); io++) {
                    this.imgs[io] = new Image(w,h);
                    this.imgs[io].src = imgObj.arguments[io+5];
                }
                this.p = n % (imgObj.arguments.length-5);
                break;
            default:
                switch (n) {
                    case 'r':
                        io = Math.floor(Math.random() * 100) % (imgObj.arguments.length-5);
                        this.imgs[0] = new Image(w,h);
                        this.imgs[0].src = imgObj.arguments[io+5];
                        break;
                    default:
                        io = n % (imgObj.arguments.length-5);
                        this.imgs[0] = new Image(w,h);
                        this.imgs[0].src = imgObj.arguments[io+5];
                        break;
                }
                break;
        }
    }
    this.n = this.imgs.length;
    this.img_name = "img" + (imgObjinst++);
    this.trtyp = 0;
    this.trdur = 1;
    this.tren = false;
    this.tract = false;
    this.attr = '';
    this.url = '';
    this.alt = '';
    this.total = imgObj_total;          // total_num_imgobj = myImgobj.total()
    this.show = imgObj_show;            // myImgobj.show([imgno])
    this.showfirst = imgObj_showfirst;  // myImgobj.showfirst()
    this.showlast = imgObj_showlast;    // myImgobj.showlast()
    this.shownext = imgObj_shownext;    // myImgobj.shownext()
    this.showprev = imgObj_showprev;    // myImgobj.showprev()
    this.showrand = imgObj_showrand;    // myImgobj.showrand()
    this.html = imgObj_html;            // <body>... <script>myImgobj.html(['additional attributes'])</script>... </body>
    this.trEna = imgObj_trEna;          // myImgobj.trEna([transitiontype, transitionduration])
    this.trDis = imgObj_trDis;          // myImgobj.trdis()
    this.attrib = imgObj_attrib;        // myImgobj.attrib(attribute-text-to-be-added)
    this.setURL = imgObj_setURL;        // myImgobj.setURL('url')
    this.setAlt = imgObj_setAlt;        // myImgobj.setAlt('txt')
    this.replace = imgObj_replace;      // myImgobj.replace(imagename)
    this.getImgName = imgObj_getImgName;// myImgobj.getImgName(imgno)
    this.trUpd = imgObj_trUpd;          // for internal use only
}

function imgObj_getImgName(imgno)
{
    return this.imgs[this.p].src;
}

function imgObj_attrib(attrib)
{
    this.attr = attrib;
}

function imgObj_setURL(url)
{
    this.url = url;
}

function imgObj_setAlt(alt)
{
    this.alt = alt;
}

function imgObj_replace(imgname)
{
    this.trUpd();
    if (this.tract && this.tren) eval('document.images.'+this.img_name).filters[0].Apply();
	if (NS4 && this.section != '') {
	    split = this.section.split("_");
	    dest = "document.";
	    for(si=0; si < split.length; si++) {
	        dest = dest +  split[si] + ".document.";
	    }
	    dest = dest + "images[this.img_name]";
	}
	else {
	    dest = "document.images[this.img_name]";
    }
    if (this.srcupd) {
        eval(dest).src = imgname;
        if (this.tract && this.tren) eval('document.images.'+this.img_name).filters[0].Play()
    }
}


function imgObj_show(imgno)
{
    if (imgObj_show.arguments.length == 0) imgno = this.p;
    this.trUpd();
    if (this.tract && this.tren) eval('document.images.'+this.img_name).filters[0].Apply();
    if (imgno <0) imgno = this.n - 1;
    imgno = imgno % this.n;
	if (NS4 && this.section != '') {
	    split = this.section.split("_");
	    dest = "document.";
	    for(si=0; si < split.length; si++) {
	        dest = dest +  split[si] + ".document.";
	    }
	    dest = dest + "images[this.img_name]";
	}
	else {
	    dest = "document.images[this.img_name]";
    }
    if (this.srcupd) {
        eval(dest).src = this.imgs[imgno].src;
        if (this.tract && this.tren) eval('document.images.'+this.img_name).filters[0].Play()
    }
    this.p = imgno;
}

function imgObj_showfirst()
{
    this.show(0)
}

function imgObj_showlast()
{
    this.show(this.n-1)
}

function imgObj_shownext()
{
    q = this.p + 1;
    this.show(q)
}

function imgObj_showprev()
{
    q = this.p - 1;
    this.show(q)
}

function imgObj_showrand()
{
    var ran = Math.floor(Math.random() * 100) % this.n;
    this.show(ran)
}

function imgObj_trUpd()
{
    if (((this.tract==false) || (this.trtyp == 25)) &&      // must be updated
        (this.tren) &&                                      // if enabled 
        (this.srcupd) &&                                      // and if the image was updated with a 'src'
        (IE4 || IE5)) {                                     // but only for IE4+
        if (this.trtyp == 25) {  // random transition for each update
            tt = Math.floor(Math.random() * 1000) % 25;
        }
        else {
            tt = this.trtyp;
        }
        if (tt < 24) {  // 0-23 are defined transition types for 'revealTrans'
            eval('document.images.'+this.img_name+'.style.filter="revealTrans(transition='+tt+', duration='+this.trdur+')"');
        }
        else { // Blend transition, here defined as type 24
            eval('document.images.'+this.img_name+'.style.filter="blendTrans(duration='+this.trdur+')"');
        }
        this.tract = true;
    }
}

function imgObj_trEna(type, dur)
{
    if (imgObj_trEna.arguments.length > 1) {
        this.trtyp = type;
        this.trdur = dur;
    }
    this.tren = true;
    this.tract = false;
    this.trUpd();
}

function imgObj_trDis()
{
    this.tren = false;
    this.tract = false;
}

function imgObj_total()
{
    return imgObjinst;
}

function imgObj_html(section, txt)
{
    this.section = section;
    if (imgObj_html.arguments.length == 0) txt = '';
	if (NS4 && this.section != '') {
	    split = this.section.split("_");
	    dest = "document.";
	    for(si=0; si < split.length; si++) {
	        dest = (si == split.length-1) ?
	            dest + split[si] + ".document" :
	            dest + split[si] + ".document.";
	    }
	}
	else {
	    dest = "document";
    }
    
    txt = txt + " alt='" + this.alt + "' title='" + this.alt + "' ";
    if (NS4) txt = txt + "border='0' ";
    txt = txt + this.attr;
    if (this.cln != '') eval(dest).write('<div class="'+this.cln+'">');
    if (this.url != '') {
        txt = "border='0' " + txt;
        eval(dest).write("<a href='"+this.url+"'>");
    }
    eval(dest).write("<img id='"+this.img_name+"' name='"+this.img_name+"' src='"+this.imgs[this.p].src+"' width='"+this.w+"' height='"+this.h+"' "+txt+" />");
    if (this.url != '') eval(dest).write('</a>');
    if (this.cln != '') eval(dest).write('</div>');
    this.srcupd = true;
}


//----------------------------------------------------------
// Image objec collection class
// enabling handling of several image objects

function imgCol()   // as parameter: one or more several imgObj's
{
    this.imgArr = new Array();
    for (ic=0; ic < (imgCol.arguments.length); ic++) {
        this.imgArr[ic] = imgCol.arguments[ic];
    }
    this.n = this.imgArr.length;
    this.fnc = imgCol_fnc;          // myImgCollection.fnc('trEna(24,1)');
    this.next = imgCol_next;        // myImgCollection.next();
    this.rand = imgCol_rand;        // myImgCollection.rand();
}

function imgCol_next()
{
    for (icn=0; icn<this.n ;icn++) {
        this.imgArr[icn].shownext();
    }
}

function imgCol_rand()
{
    for (icn=0; icn<this.n ;icn++) {
        this.imgArr[icn].showrand();
    }
}

function imgCol_fnc(act)        // 
{
    for (icn=0; icn<this.n ;icn++) {
        eval('this.imgArr[icn].'+act);
    }
}

//----------------------------------------------------------
// Functions to control the selection of immages

function get_season()
{
    var today = new Date();
    var season;
    var month = today.getMonth() + 1;
    if (month <12 && month >= 9) return 2;      // autumn
    else if (month <9 && month >= 6) return 1;  // summer
    else if (month <6 && month >= 3) return 0;  // spring
    else return 3;                              // winter
}


function get_weekday()
{
    var today = new Date();
    return (today.getDay() + 6) % 7;  // monday = 0, sunday = 6
}
