
function setImage(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    if (!evt) return;

    var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if (!elem) return;


    if (elem.className == "swappable") {
        switch (evt.type) {
            case "mouseover":
                var src = elem.src;
                if ( !src.match(/_over\.[^\.]+$/) )
                    src = src.replace(/(\.[^\.]+)$/,"_over$1");
                elem.src = src;
                break;
            case "mouseout":
                var src = elem.src;
                src = src.replace(/_over(\.[^.]+)$/,"$1");
                elem.src = src;
                break;
        }
    }
}

function open_close_menu_item(e) {
    while ( !( e.tagName && e.tagName == 'LI' ) && e.parentNode ) {
        e = e.parentNode;
    }

    if ( !( e.tagName && e.tagName == 'LI' ) )
        return;

    if ( e.className.match(/\bclose\b/ ) ) {
        e.className = e.className.replace(/\bclose\b/, 'open');
    } else {
        e.className = e.className.replace(/\bopen\b/, 'close');
    }
    var s = $(e).siblings();
    s.each(function(e){
        if ( e.className.match(/\bclose\b/ ) )
            return;

        e.className = e.className.replace(/\bopen\b/, 'close');
    });
}

function setOpacity(element, value) {
    var opacity = (value == 100 || value === '') ? 100 : (value < 1) ? 0 : value;
    if ( typeof element.style.opacity == 'string' ) {
        element.style.opacity = opacity/100;
    }
    else if ( document.body.filters ) {
        if ( opacity < 99 ) {
            var oAlpha = element.filters['DXImageTransform.Microsoft.alpha']
                || element.filters.alpha;
            if (oAlpha) oAlpha.opacity = opacity;
            else element.style.filter
                += "progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")";
        } else {
            if ( element.filters && element.filters.alpha )
                element.filters.alpha = ''
            else
                element.style.filter = ''
        }
    }
    return element;
}

// define handlers;
document.onmouseover = setImage;
document.onmouseout = setImage;

var fadebgcolor="white"
 
////NO need to edit beyond here/////////////
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
function fadeshow(theimages, delay, displayorder){
    this.running = 0;
    this.base_url = '';
    this.delay = delay
    this.degree = 10 //initial opacity degree (10%)
    this.curimageindex = 0
    this.nextimageindex = 1
    fadearray[fadearray.length]=this
    this.slideshowid=fadearray.length-1
    this.canvasbase = "canvas"+this.slideshowid
    this.curcanvas = this.canvasbase+"_0"
    if (typeof displayorder!="undefined")
        theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
    this.theimages=theimages
    this.postimages=new Array() //preload images
    this.fadewidth = 0;
    this.fadeheight = 0;
    for (var p=0;p<theimages.length;p++){
        if ( theimages[p][1] > this.fadewidth ) this.fadewidth = theimages[p][1];
        if ( theimages[p][2] > this.fadeheight ) this.fadeheight = theimages[p][2];
    }
    this.preloadimages();
    this.fadewidth += 4;
    this.fadeheight += 4;
    document.write('<div id="master'+this.slideshowid+'" class="slide-show-main" style="position:relative;width:'+this.fadewidth+'px;height:'+this.fadeheight+'px;overflow:hidden;margin:auto;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+this.fadewidth+'px;height:'+this.fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+this.fadewidth+'px;height:'+this.fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div></div>')
}

fadeshow.prototype.selectimage = function (index) {
    this.curimageindex = index;
    this.nextimageindex=(this.curimageindex<this.theimages.length-1)? this.curimageindex+1 : 0;
    this.preloadimages();
}

fadeshow.prototype.preloadimages = function () {
    for (var p = this.curimageindex-1; p < this.curimageindex+1; p++ ) {
        var i = p;
        if ( i < 0 ) i = this.theimages.length + i;
        if ( i >= this.theimages.length ) i = i - this.theimages.length;
        this.preloadimage(i);
    }
}

fadeshow.prototype.preloadimage = function (index) {
    if ( this.postimages[index] )
        return;
    this.postimages[index] = new Image(this.theimages[index][1], this.theimages[index][2])
    this.postimages[index].src = this.theimages[index][0]
}

function fadepic(obj) {
    if (obj.degree<100){
        obj.degree += 10
        setOpacity( obj.tempobj, obj.degree );
    }
    else {
        if ( fadeclear[obj.slideshowid] )
            clearInterval(fadeclear[obj.slideshowid])
        fadeclear[obj.slideshowid] = '';
        if ( obj.running == 0 ) return;

        var nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
        obj.tempobj= document.getElementById(nextcanvas)
        obj.populateslide(obj.tempobj, obj.nextimageindex)
        obj.nextimageindex=(obj.nextimageindex<obj.theimages.length-1)? obj.nextimageindex+1 : 0
        setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
    }
}
 
fadeshow.prototype.populateslide = function(picobj, picindex) {
    var slideHTML = '<img src="'+ this.postimages[picindex].src +'"'
        +' width="'+ this.theimages[picindex][1] +'"'
        +' height="'+ this.theimages[picindex][2] +'"'
        +' />';

    if ( this.theimages[picindex][3] != "" ) //if associated link exists for image
        slideHTML = '<a href="'+ this.theimages[picindex][3] +'"'
            +' target="'+ this.theimages[picindex][4]+ '">'
            + slideHTML
            + '</a>';
    picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage = function() {
    if (!this.running && this.stopped) {
        this.stopped = 0;
        return;
    }
    this.resetit()
    this.tempobj = document.getElementById(this.curcanvas)
    this.tempobj.style.zIndex++
    var tmp = document.getElementById('images-counter');
    if ( tmp ) {
        tmp.innerHTML = (this.curimageindex+1) +'/'+ this.theimages.length;
    }
    tmp = document.getElementById('image-link-prev');
    if ( tmp ) {
        tmp.href = this.base_url + '/' + (this.curimageindex - 1 < 0? this.theimages.length - 1 : this.curimageindex - 1);
    }
    tmp = document.getElementById('image-link-next');
    if ( tmp ) {
        tmp.href = this.base_url + '/' + (this.curimageindex + 1 >= this.theimages.length? 0: this.curimageindex + 1);
    }
    this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
    this.curimageindex=(this.curimageindex<this.theimages.length-1)? this.curimageindex+1 : 0;
    this.preloadimages();
    if ( fadeclear[this.slideshowid] )
        clearInterval(fadeclear[this.slideshowid]);
    fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
}
 
fadeshow.prototype.resetit=function(){
    this.degree=10
    var crossobj = document.getElementById(this.curcanvas)
    setOpacity( crossobj, this.degree );
}
 
fadeshow.prototype.startit=function(){
    this.running = 1;
    this.populateslide(
        document.getElementById(this.curcanvas), this.curimageindex
    );
    this.rotateimage();
}
fadeshow.prototype.stopit=function(){
    this.running = 0;
    this.stopped = 1;
}
fadeshow.prototype.startstop=function(){
    if ( this.running == 1 )
        this.stopit()
    else
        this.startit()
}

fadeshow.prototype.showit=function(){
    this.populateslide(
        document.getElementById(this.curcanvas), this.curimageindex
    );
    this.rotateimage();
}

fadeshow.prototype.shownext=function(){
    this.populateslide(
        document.getElementById(this.curcanvas), this.curimageindex
    );
    this.rotateimage();
}

fadeshow.prototype.showprev=function(){
    this.curimageindex = this.curimageindex - 2;
    if ( this.curimageindex < 0 )
        this.curimageindex = this.theimages.length + this.curimageindex;
    this.preloadimages();
    this.populateslide(
        document.getElementById(this.curcanvas), this.curimageindex
    );
    this.rotateimage();
}


