//Array to hold timer ID for each menu
var hidetimer = new Array();

function rollover(cell){
	cell.style.backgroundColor = "#37539B";
}

function rollout(cell){
	cell.style.backgroundColor = "#1E2D54";
}

//Slide menu out
function menuout(cell, menu){
	var pos = findPos(cell);
	document.getElementById(menu).style.left = (pos[0]+142)+"px";
	document.getElementById(menu).style.top = pos[1]+"px";
	clearTimeout(hidetimer[menu]);
	slideout(menu);
}

//Make/keep menu visible w/ no sliding
function showmenu(menu){
	document.getElementById(menu).style.display='';
	clearTimeout(hidetimer[menu]);
}

//Start timer to hide menu
function menuin(menu, parent){
	hidetimer[menu] = setTimeout("hidemenu('"+menu+"')", 500);
}

//Slide menu in
function hidemenu(menu){
	slidein(menu);
	if(document.getElementById(menu+'_link') != null)
		document.getElementById(menu+'_link').style.backgroundColor = "#1E2D54";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//START MENU SLIDE CODE

var timerlen = 5;
var slideAniLen = 250;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endWidth = new Array();
var moving = new Array();
var dir = new Array();

function slideout(objname){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display != "none")
                return;

        moving[objname] = true;
        dir[objname] = "out";
        startslide(objname);
}

function slidein(objname){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display == "none")
                return;

        moving[objname] = true;
        dir[objname] = "in";
        startslide(objname);
}

function startslide(objname){
        obj[objname] = document.getElementById(objname);

        endWidth[objname] = parseInt(obj[objname].style.width);
        startTime[objname] = (new Date()).getTime();

        if(dir[objname] == "out"){
                obj[objname].style.width = "1px";
        }

        obj[objname].style.display = "";

        timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
}

function slidetick(objname){
        var elapsed = (new Date()).getTime() - startTime[objname];

        if (elapsed > slideAniLen)
                endSlide(objname)
        else {
                var d =Math.round(elapsed / slideAniLen * endWidth[objname]);
                if(dir[objname] == "in")
                        d = endWidth[objname] - d;

                obj[objname].style.width = d + "px";
        }

        return;
}

function endSlide(objname){
        clearInterval(timerID[objname]);

        if(dir[objname] == "in")
                obj[objname].style.display = "none";

        obj[objname].style.width = endWidth[objname] + "px";

        delete(moving[objname]);
        delete(timerID[objname]);
        delete(startTime[objname]);
        delete(endWidth[objname]);
        delete(obj[objname]);
        delete(dir[objname]);

        return;
}

var pane = 2;

function slide(src) { 
	// Image URL
    this.src = src; 
	// Create an image object for the slide
    if (document.images) {
        this.image = new Image();
    } 
	// Flag to tell when load() has already been called
    this.loaded = false; 
	
	// This method loads the image for the slide
    this.load = function() { 
        if (!document.images) {
            return;
        }
        if (!this.loaded) {
            this.image.src = this.src;
            this.loaded = true;
        }
    }
}

function slideshow(showname) {
    this.name = showname;
    this.slides = new Array();
    this.current = 0;
    this.timeoutid = 0;
	this.timeout= 3000;
	
    this.add_slide = function(slide) { 
		// Prefetch the slide image
        slide.load(); //Add slide to the array
        this.slides[this.slides.length] = slide;
    }
	
    this.play = function(timeout) { 
		// If the timeout argument was specified (optional)
        // then make it the new default
        if (timeout) {
            this.timeout = timeout;
        } else {
            this.timeout = 3000;
        } 
		this.timeoutid = setInterval(this.name + ".loop()", this.timeout);
    }
	
    this.pause = function() { 
		// This method stops the slideshow if it is automatically running.
        if (this.timeoutid != 0) {
            clearTimeout(this.timeoutid);
            this.timeoutid = 0;
        }
    } 
	
	//Transition to next photo
    this.update = function() {
		var tPane = pane;
		(pane == 1) ? pane = 2 : pane = 1;
		//set pane img src
		document.getElementById('img'+pane).src = this.slides[this.current].image.src;
		//fade out old
		opacity('pane' + tPane, 100, 0, 1000);
		//fade in new
		opacity('pane' + pane, 0, 100, 1000);
    }
	
	//Loop through all of the slides and prefetch
	this.preload = function() {
        var arLen = this.slides.length;
        for (var i = 0,
        len = arLen; i < len; ++i) {
            this.slides[i].load();
        }
    }
	
    this.next = function() { 
		// This method advances to the next slide.
        // Increment the image number
        if (this.current < this.slides.length - 1) {
            this.current++;
        } else {
            this.current = 0;
        }
        this.update();
    }
	
    this.loop = function() { 
		// This method is for internal use only.
        // This method gets called automatically by a JavaScript timeout.
        // It advances to the next slide, then sets the next timeout.
        // If the next slide image has not completed loading yet,
        // then do not advance to the next slide yet.
        // Make sure the next slide image has finished loading
        if (this.current < this.slides.length - 1) {
            next_slide = this.slides[this.current + 1];
            if (next_slide.image.complete == null || next_slide.image.complete) {
                this.next();
            }
        } else { // we're at the last slide
            this.next();
        } // Keep playing the slideshow
        //this.play();
    }
}

function opacity(id, opacStart, opacEnd, millisec) { //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0; //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
} 

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}