// JavaScript Document
if( !edi ){ var edi = {}; }
if( !edi.util ) { alert('this package requires edi/util.js'); }
if( !edi.gui ){ edi.gui = {}; }


edi.gui.controls = new edi.util.Scope(null);
edi.gui.getControlById = function( id )
{
	return edi.gui.controls.lookup(id);
}

edi.gui.setOpacity = function( obj, opacity )
{
	obj.style.opacity = opacity/100; 
	obj.style.MozOpacity = opacity/100;
	obj.style.KhtmlOpacity = opacity/100; 
	obj.style.filter = 'alpha(opacity=' + opacity + ')' ;
}

edi.gui.HTMLSwapper = function( id )
{	
	this._id = id;
	this._elem = document.getElementById( id );
	this._contents = new Array();
	this._state = 0;
	edi.gui.controls.add( this._id, this );
}

// returns a handle
edi.gui.HTMLSwapper.prototype.addContent = function( content )
{
	
	this._contents.push( content );
	return this._contents.length-1;
}

edi.gui.HTMLSwapper.prototype.setState = function( obj, id )
{	
	obj._elem.innerHTML = obj._contents[id];
}

edi.gui.Carousel = function( id )
{
	this._id = id;
}



/* New Spinner Banner 
 * This is a newer and a bit more cleaner version of spinner banner 
 * 
 * Author: Ali Goheer (mag97)
 * Created: May 26, 2009
 * Last Modified: May 26, 2009
 */

/* Instantiate a new spinner banner class */
edi.gui.BlendImageSpinner = function(imageID, captionID){
	var This = this; 	
	var _inUse = new edi.util.SimpleLock();
	var _id = imageID;
	var _elem = document.getElementById(_id);
	var _captionElement = document.getElementById(captionID); 	
	var _images = new Array();
	var _links = new Array();
	var _alts = new Array();
	var _captions = new Array(); 
	var _nstates = 0;
	var _state = null;
	_elem.className = 'blendContainer';

	var _fromImage = 0;
	var _toImage = 0;
	var _topID = imageID + '_blend1'; 
	var _bottomID = imageID + '_blend2'; 
	
	// add the two image containers that will be used to display transitions
	var _top = document.createElement("div"); 
	_top.id = _topID;
	_top.className = "blendImageContainer"; 
	_top.style.zIndex = 100;
	_elem.appendChild(_top);
	
	var _bottom = document.createElement("div"); 
	_bottom.id = _bottomID;
	_bottom.className = "blendImageContainer"; 
	_bottom.style.zIndex = 50;
	_elem.appendChild(_bottom);

	// add it to the collection
	edi.gui.controls.add(_id, This);

	This.transitionStartingCallbacks = new CallManager(); 	// callback manager that will notify other controls when the image transition is completed
	This.transitionHalfCompletedCallbacks = new CallManager(); 	// callback manager that will notify other controls when the image transition is half-way through
	This.transitionCompletedCallbacks = new CallManager(); 	// callback manager that will notify other controls when the image transition is completed	
	
	
	/* add a new image */
	this.addImage = function(img, href, alt, caption){
		_images.push(img);
		_links.push(href);
		_alts.push(alt);
		_captions.push(caption); 
		_nstate = _images.length;		
	}
	
	/* set the top image */
	this.setTopImage	= function(id){
		if(id >= 0 && id < _images.length){
			var img_href = _images[id];
			var img_link = _links[id];
			var img_alt = _alts[id];			
			_top.innerHTML = '<a href="' + img_link + '" style="text-decoration: none;"><img src="' + img_href + '" alt="' + img_alt + '" title="' + img_alt + '"border="0"/>' + '</a>';
		}
		
	}
	
	/* set the bottom image */ 
	this.setBottomImage = function(id){
		if(id >= 0 && id < _images.length){		
			var img_href = _images[id];
			var img_link = _links[id];		
			var img_alt = _alts[id];
			_bottom.innerHTML = '<a href="' + img_link + '" style="text-decoration: none;"><img src="' + img_href + '" alt="' + img_alt + '" title="' + img_alt + '" border="0"/></a>';			
		}
	}
	
	/* start a transition in a blender banner */
	this.startTransition = function(dest){	
		// fire the appropriate events
		This.transitionStartingCallbacks.callback(true); 
	
		_state = dest; 
		_fromImage = _state;
		_toImage = dest;
		This.setBottomImage(dest);
		This.setT(0);
		This.inTransition(0);
	}
	
	/* set the opacity of the top & bottom images */
	this.setT = function(t){
		edi.gui.setOpacity(_bottom, t);
		edi.gui.setOpacity(_top, 100-t);
		
		// change the caption at the 50% mark
		if(t == 50){
			This.transitionHalfCompletedCallbacks.callback(true); 
			_captionElement.innerHTML = _captions[_state]; 		
		}
		
		if( t>=100 ){ 
			This.complete(); 
		}
	}
	
	/* take whtever steps you need to take at end of transition */
	this.complete = function(){	
		// switch the top & bottom images 
		var temp = _top;
		_top = _bottom;
		_bottom = temp;

		// release the lock 
		_inUse.release();
				
		// fire the appropriate events
		This.transitionCompletedCallbacks.callback(true); 
		
	}	

	/* start a transition in a blender banner */
	this.inTransition = function(t){	
		This.setT(t);				
		if( t < 100 ){
			setTimeout('edi.gui.getControlById("' + _id + '").inTransition(' + (t + 5) + ')', 50 );
		}
	}
	
	/* set the state of the blender banner */
	this.setState = function(s){
		// if this is the very first transition (i.e. trying to set the image on page load) then just set the image without any transition 
		// otherwise go through the transition 
		if(_state == null){
			_state = s; 
			This.setTopImage(s);
			This.setBottomImage(s);
			_captionElement.innerHTML = _captions[s]; 
		}else{	
			if(_inUse.obtain()){				
				This.startTransition(s % _nstate);
			}
		}
	}


}



/* LEDs for Spinner Banner 
 * This is a newer and a bit more cleaner version of LED controls  
 * 
 * Author: Ali Goheer (mag97)
 * Created: May 26, 2009
 * Last Modified: May 26, 2009
 */

/* constructor for LEDs class */
edi.gui.LEDs = function(ledsID, dummyID, count){
	var This = this; // create a copy of 'this' reference for use in callback functions
	
	This.statusChangedCallbacks = new CallManager(); 	// callback manager that will notify other controls when the LEDs are change 		
	var _id = ledsID;
	var _dummyID = dummyID; 
	var _elem = document.getElementById(_id);	
	var _dummyElement = document.getElementById(_dummyID);
	var _nstates = 0;
	var _state = 0;
	This.enabled = true; 

	/* insert a new LED */
	this.addLED = function(ledBody){
		newLED(_nstates, ledBody); 
	}

	_dummyElement.style.display = "none"; // make dummy LED invisible 
	// add the requested number of LEDs
	for(var j=0; j<count; j++){				
		this.addLED(); 
	}

 	

	// add this isntance to the list of controls 
	edi.gui.controls.add(_id, This);
	
	/* set the state of the LEDs control */
	this.setState = function(s){
		if(This.enabled){ // only perform the transition if the control is enabled 
			_state = s;
			var i = 0;
			
			for(i=0; i<_nstates; i++){
				var e1 = document.getElementById(_id +'_led_' + i);
				if(s==i){
					e1.className = 'led_active';
				}else{
					e1.className = 'led';
				}
			}
			
			// now call all the registered callback functions
			This.statusChangedCallbacks.callback(s); 				
		}
	}

	/* return the current state of the LEDs control */
	this.getState = function(){	
		return _state;
	}

	/* set next LED as selected in LEDs control */
	this.next = function(message){
		This.setState((_state + 1) % _nstates);
	}
	
	/* set the state of the blender banner */
	this.previous = function(message){
		if(_state > 0){
			This.setState(_state -1);
		}else{
			This.setState(_nstates - 1);
		}
	}
	
	/* Enable the LED control */
	this.enable = function(){
		This.enabled = true; 
	}
	
	/* Disable the LED control */
	this.disable = function(){
		This.enabled = false; 
	}

	/* internal functions */
	function newLED(i, ledBody){				
		var newElement = _dummyElement.cloneNode(true); 
		if(ledBody != null){
			newElement.innerHTML = ledBody; 
		}
			
		newElement.id = _id + '_led_' + j;
		newElement.onclick = function(){This.setState(i)};		
		newElement.style.display = "block"; 
		_elem.appendChild(newElement); 		
		
		_nstates++; 	
	}	
		
}





/* New Spinner LeftRight control
 * This is a newer and a bit more cleaner version of LeftRight controls for spinner banner
 * 
 * Author: Ali Goheer (mag97)
 * Created: May 26, 2009
 * Last Modified: May 26, 2009
 */
edi.gui.LeftRight = function(leftID, rightID){
	var This = this; 
	var _leftID = leftID;
	var _rightID = rightID; 
	var _leftElement = document.getElementById(_leftID); 
	var _rightElement = document.getElementById(_rightID);

	// the two callback manager for this control 
	This.leftPressedCallbacks = new CallManager(); 
	This.rightPressedCallbacks = new CallManager(); 

	// wire the js events properly
	_leftElement.onclick = function(){This.leftPressedCallbacks.callback('left button clicked')};
	_rightElement.onclick = function(){This.rightPressedCallbacks.callback('right button clicked')}; 	
	
			
	edi.gui.controls.add(_leftID, This);
	edi.gui.controls.add(_rightID, This);
	
}
