/* list of items, a click makes some items visible
Parameters:
  prefixes: an array of prefixes, something like new Array() {"header_", "body_", "footer_"}
  count: the number of items
  start: the item to show initially, the first is shown by default

Usage:
var my_md = new MasterDetail(new Array() {"header_", "body_", "footer_"}, 10, 0);
my_md.show(2);
my_md.hideCurrent();
-OR-
window["venue_md"] = new MasterDetail(prefixes, prefixes.length, 0);window["venue_md"].show(2);
*/

function MasterDetail(myName,  prefixes, count, start) {

	// preconditions
	if (count < 0) {
		alert("invalid count, can't create MasterDetail control");
	}
	
	// attribute: p_count
	this.myName = myName;
	this.p_count = count;
    this.stopped = false;
    this.fadeOpacityInterval = 10;
    this.cycleDelay = 5000;
    this.fadeInterval = 50;

	// attribute: p_prefixes
	if (typeof prefixes != 'undefined') {
		this.p_prefixes = prefixes;
	} else {
		this.p_prefixes = new Array();
	}
	
	// attribute: p_current
	this.p_current = 0;
	if ((start >= 0) && (start < count)) {
		this.p_current = start;
	}
	
	// attribute: p_currentVisibility
	this.p_currentVisibility = false;
	

	// methods
	this.hideCurrent = function () {
		// hide current item
		this.p_setVisibility(this.p_current, false);
		// current is now invisible
		this.p_currentIsVisible = false;
	}
	
	this.showCurrent = function () {
		// show current item
		this.p_setVisibility(this.p_current, true);
		// current is now visible
		this.p_currentVisibility = true;
	}
	
	this.hide = function (i) {
		// precondition: i >= 0
		if ((i < 0) || (i >= this.p_count)) {
			alert("precondition failed: can't hide invalid item");
		}
		
        // is this the current item?
		if (this.p_current == i) {
		    // just hide the current
		    this.hideCurrent();
		} else {
		    // hide it
            this.p_setVisibility(i, false);
        }
		
		// postcondition
		if ((this.p_current == i) && (this.p_currentVisibility != false)) {
			alert("postcondition failed: hide failed to hide current item.");
		}
	}
	
	this.show = function (i) {
		// precondition: i >= 0
		this.stopped = true;
		this.setOpacity(100, i);
		if ((i < 0) || (i >= this.p_count)) {
			alert("precondition failed: can't hide invalid item");
		}

		// is this the current item?
	    if (this.p_current == i) {
		    // just show the current
		    this.showCurrent();
		} else {
		    // hide the current
		    this.hideCurrent();
		    
		    // update current
		    this.p_current = i;
		    
		    // now show the current
		    this.showCurrent();
		}
		
		// postcondition
		if (this.p_currentVisibility != true) {
		    alert("postcondition failed: show failed to show current item.");
		}
	}
	
	this.p_setVisibility = function (i, b) {
		// set the visibility as required
        var j;
		for (j = 0;j < this.p_prefixes.length;++j) {
			this.p_setPrefixVisibility(j, i, b);
		}
	}
	
	this.p_setPrefixVisibility = function (j, i, b) {
		// set this item's visibility accordingly
		var id = this.p_prefixes[j] + i;
		//alert('getting elem ' + id);
		var elem = document.getElementById(id);
		elem.className = this.p_getClassName(elem.className, b);
	}
	
	this.p_getClassName = function (s, b) {
		return (b ? this.p_toVisible(s) : this.p_toHidden(s));
	}
	
	this.p_toVisible = function (className) {
		return className.replace(/\s*hiddenObj/, '');	
	}
	
	this.p_toHidden = function (className) {
		if (className.match(/hiddenObj/)) {
			return className;
		} else {
			return this.p_trim(className) + ' hiddenObj';
		}
	}
	
	this.p_setOpacity = function (j, i, opacity) {
		// set this item's visibility accordingly
		var id = this.p_prefixes[j] + i;
		//alert("Set " + id + " - opacity:" + opacity);
		//alert('getting elem ' + id);
		var elem = document.getElementById(id);
		if(opacity==0){
		    //if(confirm(elem.className)){return false};
		    elem.className = this.p_toHidden(elem.className);
		    }
		else{
		    //if(confirm(elem.className)){return false};
		    elem.className = this.p_toVisible(elem.className);
		}
		opacity = (opacity == 100)?99.9999:opacity;
		// IE/Win
		elem.style.filter = "alpha(opacity:"+opacity+")";
		// Safari<1.2, Konqueror
		elem.style.KHTMLOpacity = opacity/100;
		// Older Mozilla and Firefox
		elem.style.MozOpacity = opacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		elem.style.opacity = opacity/100;
	}
	
	this.setOpacity = function (opacity, index) {
	
		this.hideCurrent();
        var j;
		for (j = 0;j < this.p_prefixes.length;++j) {
			this.p_setOpacity(j, index, opacity);
		}
	}

	this.fadeIn = function (opacity, index) {
	    this.p_current=index;
        if(this.stopped != false)
            {return false;}
		else if (opacity < 100) {
			this.setOpacity(opacity, index);
			opacity += this.fadeOpacityInterval;
			window.setTimeout(this.myName + ".fadeIn("+opacity+"," + index  + ")", this.fadeInterval);
		} else {
		   // alert(index + " 100% shown");
			window.setTimeout(this.myName + ".fadeOut(" + opacity + "," + index + ")", this.cycleDelay);
		}
	}

	this.fadeOut = function (opacity, index) {
	   // alert(opacity);
        if(this.stopped)
            {return false;}
		else if (opacity > 0) {
			opacity -= this.fadeOpacityInterval;
			this.setOpacity(opacity, index);
			window.setTimeout(this.myName + ".fadeOut("+opacity+ "," + index + ")", this.fadeInterval);
		} else {
		    index +=1;
		    //if( confirm(index) != true ){return false;}
		    if(index > (this.p_count-1)){index=0;}
			this.fadeIn(0,index);
		}
	}
	
	this.run = function () {
	    if(this.p_count>0)
        {
            this.StartSlideshow();
        }
	}
	
	this.StartSlideshow = function()
	{
			window.setTimeout(this.myName + ".fadeOut(100,0)", this.cycleDelay);
			//alert(this.myName + ".fadeOut(100,1)");
	}
	
	
	this.pickImage = function (i) {
		this.current_cycle_id += 1;
		
		// fade from invisible
		var elem = this.getImageElem();
		elem.src = this.preLoad[i].src;
		
		this.setOpacity(elem, 100);
		elem.style.visibility = 'visible';
		this.fadeIn(0, 1, this.current_cycle_id);
	}
	
	this.p_trim = function(s) {
		return s.replace(/^\s+/, '').replace(/\s+$/, '');
	}


	// show current
	this.showCurrent();
	
	// post conditions
	if (this.p_currentVisibility != true) {
		alert("postcondition failed: current item is not visible.");
	} else if (this.p_current < 0) {
		alert("postcondition failed: current is invalid.");
	}
	
	this.run();
}
