
API.frameControllers=new Object();

API.frameControllers.length=0;

API.frameControllers.New=function(id,type) {
	if(!type)
		type='v1';

	switch(type) {
		case 'v2':
			API.frameControllers[this.length]=new frameController_v2(id);
			break;
		case 'v1':
		default:
			API.frameControllers[this.length]=new frameController_v1(id);
			break;
	}
	this.length++;
	return API.frameControllers[this.length-1];

}
API.frameControllers.Get=function(id) {
	for(i=0;i<API.frameControllers.length;i++) {
		if(API.frameControllers[i].id==id) {
			return API.frameControllers[i];
		}
	}
	return false;
}


// Version 2 frameController - Controls a single frame with multiple content sources. Effect object for show and hide of frame
function frameController_v2(id) {
	// Setting controller version
	this.version=2;

	// Setting id
	this.id=id;

	// Frames
	this.frames=Array();

	// Current frame shown
	this.current=null;

	this.swap=0;
	this.pause=true;
	this.interval=null;

	// setContainer Method to attach container object to controller
	this.setContainer=function(id) {
		// Retrieving container object
		try {
			// BC20080220:	Footsteps dialog support
			//				Get the existing controller
			if(window.name=='frameDialog')
				this.container=opener.window.document.getElementById(this.id);
			else
				this.container=document.getElementById(this.id);
			this.container.controller=this;
			this.container.onmouseover=function() {
				if(this.controller.pause==true)
					window.clearInterval(this.controller.interval);
			}
			this.container.onmouseout=function() {
				if(this.controller.pause==true)
					this.controller.Run();
			}
		}
		catch(e) {
			return false;
		}
		return true;
	}

	this.addFrame=function(oFrame,sClass) {
		if(!oFrame)
			oFrame=new frameController_v2_frame(this.frames.length);
		// BC20080220: 	Footsteps dialog support, we do not support input from the dialog
		// 				but we will return a fake frame to prevent code from breaking
		if(window.name=='frameDialog')
			return oFrame;
		this.frames.push(oFrame);
		oFrame.setController(this);
		return oFrame;
	}

	this.Show=function(id,hide) {
		if(hide!==false)
			hide=true;
		this.current=id;
		for(iFrame=0;iFrame<this.frames.length;iFrame++) {
			if(iFrame==id)
				this.frames[iFrame].Show(true);
			else if(hide===true)
				this.frames[iFrame].Hide(true);
		}
	}

	this.Hide=function(id) {
		this.current=id;
		for(iFrame=0;iFrame<this.frames.length;iFrame++) {
			if(iFrame==id)
				this.frames[iFrame].Hide(true);
		}
	}
	// Controller construction logic

	// Setting default container based upon controller id
	this.Load=function() {
		if(!this.container)
			this.setContainer(this.id);
		for(iFrame=0;iFrame<this.frames.length;iFrame++) {
			this.frames[iFrame].Load();
		}
		if(this.swap) {
			this.current=-1;
			this.Swap();
		}
	}
	this.Swap=function() {
		window.clearInterval(this.interval);
		this.current++;
		if(this.current==this.frames.length)
			this.current=0;
		this.Show(this.current);
		this.Run();
	}
	this.Run=function() {
		if(this.swap) {
			this.interval=window.setInterval("API.frameControllers.Get('"+this.id+"').Swap()",this.swap*1000);
		}
	}
}


function frameController_v2_frame(id) {
	this.id=id;
	this.controller=null;
	this.container=null;
	this.content='';
	this.shown=false;
	this.zIncreased=false;

	this.keep=false;
	this.interval=null;

	this.effects=Array();

	this.className='';
	this.zIndex=0;

	this.appendContainerId=null;

	this.classSwapObjects=Array();

	this.onshow=function() { if(this.eventHandler) return this.eventHandler.triggerEvent('onshow',this);}
	this.onhide=function() {  if(this.eventHandler) return this.eventHandler.triggerEvent('onhide',this);}
	this.eventHandler=null;
	this.addClassSwapObjectById=function(id) {
		// Retrieving container object
		try {
			object=document.getElementById(id);
			if(container) {
				return this.addClassSwapObject(object);
			}
			return false;
		}
		catch(e) {
			return false;
		}
	}
	this.addClassSwapObject=function(object) {
		if(object) {
			this.classSwapObjects.push(object);
			return true;
		}
		return false;
	}
	this.appendContainerById=function(id) {
		this.appendContainerId=id;
		if(this.container)
			this.appendContainer(document.getElementById(id));
	}
	this.appendContainer=function(object) {
		if(this.container && object)
			this.container.appendChild(object);
	}
	this.setContainerById=function(id,retain_content) {
		// Retrieving container object
		try {
			container=document.getElementById(id);
			if(container) {
				return this.setContainer(container,retain_content);
			}
			return false;
		}
		catch(e) {
			return false;
		}
	}
	this.setContainer=function(container,retain_content) {
		this.container=container;
		if(!retain_content)
			container.innerHTML=this.content;
		container.frame=this;
		container.style.zIndex=this.zIndex;
		if(this.keep && !this.controller.swap) {
			this.attachEvent(container,'onmouseover','show');
			this.attachEvent(container,'onmouseout','hide',this.keep);
		}
		return true;
	}
	this.setController=function(oController) {
		this.controller=oController;
	}

	this.attachEventById=function(id,event,action,delay) {
		element=document.getElementById(id);
		if(element) {
			return this.attachEvent(element,event,action,delay);
		}
		return false;
	}
	this.attachEvent=function(element,event,action,delay) {
		element.frame=this;
		// BF20080222: If we haven't got a valid controller, return
		if(!this.controller)
			return false;
		switch(action) {
			case 'show':
					if(!this.controller.swap) {
					if(!delay)
						action=function() {
							this.frame.Show();
						}
					else {
						element.showDelay=delay;
						action=function() {
							window.clearInterval(this.frame.interval);
							this.frame.interval=window.setInterval("API.frameControllers.Get('"+this.frame.controller.id+"').frames["+this.frame.id+"].Show();",this.showDelay);
						}
					}
					break;
				}
			case 'swap':
				if(!delay)
					action=function() {
						this.frame.controller.Show(this.frame.id);
					}
				else {
					element.swapDelay=delay;
					action=function() {
						window.clearInterval(this.frame.interval);
						this.frame.interval=window.setInterval("API.frameControllers.Get('"+this.frame.controller.id+"').Show("+this.frame.id+")",this.swapDelay);
					}
				}
				break;
			case 'hide':
				if(!this.controller.swap) {
					if(!delay)
						action=function() {
							this.frame.Hide();
						}
					else{
						element.hideDelay=delay;
						action=function() {
							window.clearInterval(this.frame.interval);
							this.frame.interval=window.setInterval("API.frameControllers.Get('"+this.frame.controller.id+"').frames["+this.frame.id+"].Hide();",this.hideDelay);
						}
					}
					break;
				}
			case 'continue':
				action=function() {
						this.frame.controller.Run();
					}
				break;

			default:
				//return false;
		}
		handler=API.eventHandlers.New(element.id);
		event=handler.attachEvent(event,action);
		event.frame=this;
		/*
		switch(event) {
			case 'onmouseover':
				element.onmouseover=action;
				break;
			case 'onclick':
				element.onclick=action;
				break;
			case 'onmouseout':
				element.onmouseout=action;
				break;
			default:
				return false;
		}
		*/
		return true;
	}
	this.setContent=function(content) {
		// BC20070206: If get "false" as argument, we'll not touch the content
		if(content===false)
			return true;
		this.content=content;
		if(this.container)
			this.container.innerHTML=content;
		return true;
	}
	this.setContentById=function(id) {
		try {
			this.setContent(document.getElementById(id).innerHTML);
		}
		catch(e) {
			return false;
		}
		return true;
	}
	this.setContentByRPC=function(href,oInterpreter) {
		if(href=='return false;')
			return;
		if(!oInterpreter)
			oInterpreter=new frameController_v2_RawInterpreter();

		// BC20070206: The interpreter will now get a reference to the frame by default
		oInterpreter.frame=this;

		this.interpreter=oInterpreter;
		this.RPC=API.RPC.New('fs_rpc_index','get');
		this.RPC.setParameter('href',href);
		this.RPC.frame=this;
		this.RPC.callBack=function(status,xml,text) {
			this.frame.setContent(this.frame.interpreter.interpret(text));
		}
		this.RPC.execute();
	}

	this.addEffect=function(oEffect) {
		if(this.container)
			oEffect.setContainer(this.container);
		this.effects.push(oEffect);
		return oEffect;
	}

	this.Show=function() {
		window.clearInterval(this.interval);
		window.clearInterval(this.controller.interval);
		this.shown=true;
		this.onshow();
		if(!this.zIncreased) {
			this.container.style.zIndex++;
			this.zIncreased=true;
		}
		for(iClassSwapObjects=0; iClassSwapObjects<this.classSwapObjects.length;iClassSwapObjects++) {
			classSwapAttribute=this.classSwapObjects[iClassSwapObjects].getAttribute('_frameShowClass');
			if(classSwapAttribute!==null)
				this.classSwapObjects[iClassSwapObjects].className=classSwapAttribute;
		}
		for(iEffect=0;iEffect<this.effects.length;iEffect++) {
			this.effects[iEffect].Show();
		}
	}
	this.Hide=function() {
		window.clearInterval(this.interval);
		window.clearInterval(this.controller.interval);
		this.shown=false;
		this.onhide();
		if(this.zIncreased) {
			this.container.style.zIndex--;
			this.zIncreased=false;
		}
		for(iClassSwapObjects=0; iClassSwapObjects<this.classSwapObjects.length;iClassSwapObjects++) {
			classSwapAttribute=this.classSwapObjects[iClassSwapObjects].getAttribute('_frameHideClass');
			if(classSwapAttribute!==null)
				this.classSwapObjects[iClassSwapObjects].className=classSwapAttribute;
		}
		for(iEffect=0;iEffect<this.effects.length;iEffect++) {
			this.effects[iEffect].Hide();
		}
	}
	this.Load=function() {
		if(!this.controller || !this.controller.container)
			return;
		if(!this.container) {
			oDiv=document.createElement('DIV');
			oDiv.className=this.className;
			this.setContainer(oDiv);
			this.controller.container.appendChild(oDiv);
			if(this.appendContainerId)
				this.appendContainerById(this.appendContainerId);
		}
		for(iEffect=0;iEffect<this.effects.length;iEffect++) {
			this.effects[iEffect].setContainer(this.container);
		}

	}
}

function frameController_v2_RawInterpreter() {
	this.interpret=function(data) {
		return data;
	}
}

function frameController_v2_fadeEffect(trans,freq,startOpacity,endOpacity,fadeOutFakeOpacity) {

	this.container=null;
	this.fader=null;
	if(trans)
		this.trans=trans;
	else
		this.trans=1;
	if(freq)
		this.freq=freq;
	else
		this.freq=100;


	this.startOpacity=startOpacity;
	this.endOpacity=endOpacity;
	this.fadeOutFakeOpacity=fadeOutFakeOpacity;

	this.setContainer=function(container) {
		this.container=container;
	}
	this.Show=function() {
		if(!this.fader)
			this.fader=API.faders.New(this.container,this.trans,this.freq,this.startOpacity,this.endOpacity,fadeOutFakeOpacity);

		// Make sure container is visible
		this.container.style.display='block';
		this.fader.fadeIn();
	}
	this.Hide=function() {
		if(!this.fader)
			this.fader=API.faders.New(this.container,this.trans,this.freq,this.startOpacity,this.endOpacity,fadeOutFakeOpacity);
		this.fader.fadeOut();
	}
}

function frameController_v2_slideEffect(attribute,trans,freq,basePosition,increment,equation,currentStep,hideEquation) {
	this.container=null;
	this.slider=null;
	if(trans)
		this.trans=trans;
	else
		this.trans=1;
	if(freq)
		this.freq=freq;
	else
		this.freq=100;

	if(attribute)
		this.attribute=attribute;
	else
		this.attribute=null;

	if(basePosition)
		this.basePosition=basePosition;
	else
		this.basePosition=0;
	if(increment)
		this.increment=increment;
	else
		this.increment=0;

	if(equation)
		this.equation=equation;
	else
		this.equation=null;

	if(hideEquation)
		this.hideEquation=hideEquation;
	else if(equation)
		this.hideEquation=equation;
	else
		this.hideEquation=null;


	if(currentStep)
		this.currentStep=currentStep;
	else
		this.currentStep=0;

	this.setContainer=function(container) {
		this.container=container;
	}

	this.Show=function() {
		if(!this.slider) {
			this.slider=API.sliders.New(this.container,this.attribute,this.trans,this.freq,this.basePosition,this.increment,this.equation,this.hideEquation);
			this.slider.step=this.currentStep;
		}
		// Make sure container is visible
		this.container.style.display='block';
		this.slider.slideIn();
	}
	this.Hide=function() {
		if(!this.slider) {
			this.slider=API.sliders.New(this.container,this.attribute,this.trans,this.freq,this.basePosition,this.increment,this.equation,this.hideEquation);
			this.slider.step=this.currentStep;
		}
		this.slider.slideOut();
	}
}

// Version 1 frameController - Controls mutiple frames it detects by itself in its container.
function frameController_v1(id) {
	this.version=1;

	this.id=id;
	this.element=document.getElementById(id);
	this.element.frameController=this;
	this.menu=null;
	this.frames=Array();

	this.fadeMode=this.element.getAttribute('fadeMode');
	this.fadeFreq=this.element.getAttribute('fadeFreq');
	this.fadeTrans=this.element.getAttribute('fadeTrans');

	this.swapFreq=this.element.getAttribute('swapFreq')*1000;
	this.interval=0;
	this.current=null;

	this.Swap=function() {
		this.current++;
		if(this.current==this.frames.length)
			this.current=0;
		this.Show(this.current);
	}

	this.clearInterval=function() {
		if(this.interval)
			window.clearInterval(this.interval);
	}
	this.setInterval=function() {
		if(this.swapFreq) {
			this.interval=window.setInterval("API.frameControllers.Get('"+this.id+"').Swap();",this.swapFreq);
		}
	}
	this.Show=function(id) {
		this.current=id;
		if(this.menu)
			this.menu.setActive(id);
		for(i=0;i<this.frames.length;i++) {
			if(i==id)
				this.frames[i].Show();
			else
				this.frames[i].Hide();
		}
	}
	cElements=this.element.getElementsByTagName('div');
	for(i=0;i<cElements.length;i++) {
		if(cElements[i].getAttribute) {
			frameType=cElements[i].getAttribute('frameType');
			frameTitle=cElements[i].getAttribute('frameTitle');
			frameControllerId=cElements[i].getAttribute('frameController');
			if(frameType && (frameControllerId==this.id || !frameControllerId)) {
				if(frameType=='Menu') {
					this.menu= new frameController_v1_Menu(this,cElements[i]);
				}
				else {
					oFrame= new frameController_v1_Frame(this,frameType,frameTitle,cElements[i]);
					this.frames.push(oFrame);
				}
			}
		}
	}
	if(this.menu) {
		for(i=0;i<this.frames.length;i++) {
			this.menu.Add(this.frames[i].title);
		}
		this.menu.Parse();
	}
	if(this.frames.length) {
		this.current=0;
		this.frames[0].Show();
		if(this.menu)
			this.menu.setActive(0);
	}
	this.element.onmouseover=function() {
		this.frameController.clearInterval();
	}
	this.element.onmouseout=function() {
		this.frameController.setInterval();
	}
	this.setInterval();
}

function frameController_v1_Frame(frameParent,frameType,frameTitle,frameElement) {
	this.parent=frameParent;
	this.type=frameType;
	this.title=frameTitle;
	this.element=frameElement;
	this.element.parent=this;
	this.visible=false;

	this.zIncreased=0;
	if(this.parent.fadeMode)
		this.fader=API.faders.New(this.element,this.parent.fadeTrans,this.parent.fadeFreq);
	else
		this.fader=null;

	this.Show=function() {
		if(this.visible==false) {
			if(this.zIncreased==0) {
				this.element.style.zIndex=this.element.style.zIndex+2;
				this.zIncreased=2;
			}
			if(this.fader)
				this.fader.cancel();
			this.visible=true;
			if(this.parent.fadeMode=='in' || this.parent.fadeMode=='in2' || this.parent.fadeMode=='both') {
				this.fader.fadeIn();
			}
			else {
				this.element.style.display='inline' ;
			}
		}
	}
	this.Hide=function() {
		if(this.zIncreased>0) {
			this.zIncreased--;
			this.element.style.zIndex--;
		}
		else
			this.element.style.display='none';
		if(this.visible==true) {
			if(this.fader)
				this.fader.cancel();
			this.visible=false;
			if(this.parent.fadeMode=='out' || this.parent.fadeMode=='both') {
				this.fader.fadeOut();
			}
			else if(this.parent.fadeMode!='in2')
				this.element.style.display='none' ;

		}
	}
}

function frameController_v1_Menu(menuParent,menuElement) {
	this.element=menuElement;
	this.selectMode=this.element.getAttribute('selectMode');
	if(!this.selectMode)
		this.selectMode='click';
	this.options=Array();
	this.parent=menuParent;
	this.buttonTemplate='buttonTitle';
	this.buttons=Array();

	this.classDefault='';
	this.classDown='';
	this.classHover='';
	this.classActive='';
	for(i2=0;i2<this.element.childNodes.length;i2++) {
		if(this.element.childNodes[i2].getAttribute) {
			menuItem=this.element.childNodes[i2].getAttribute('menuItem');
			if(menuItem) {
				if(menuItem=='menuButton') {
					this.buttonTemplate=this.element.childNodes[i2].innerHTML;
					this.classDefault=this.element.childNodes[i2].getAttribute('classDefault');
					this.classDown=this.element.childNodes[i2].getAttribute('classDown');
					this.classHover=this.element.childNodes[i2].getAttribute('classHover');
					this.classActive=this.element.childNodes[i2].getAttribute('classActive');
				}
			}
		}
	}
	this.element.innerHTML='';
	this.Add=function(optionTitle) {
		this.options.push(optionTitle);
	}
	this.setActive=function(id) {
		for(i2=0;i2<this.buttons.length;i2++) {
			if(i2==id) {
				this.buttons[i2].className=this.buttons[i2].classActive;
				this.buttons[i2].active=true;
			}
			else {
				this.buttons[i2].className=this.buttons[i2].classDefault;
				this.buttons[i2].active=false;
			}
		}
	}
	this.Parse=function() {
		this.element.innerHTML='';
		this.buttons=Array();
		for(i=0;i<this.options.length;i++) {
			oElement=document.createElement('div');

			oElement.innerHTML=this.buttonTemplate.replace(/\{buttonTitle\}/,this.options[i]);
			oElement.classDefault=this.classDefault;
			oElement.classDown=this.classDown;
			oElement.classHover=this.classHover;
			oElement.classActive=this.classActive;
			oElement.className=oElement.classDefault;
			oElement.active=false;
			oElement.onclick=function(e) {
				optionId=this.getAttribute('optionId');
				this.parent.parent.Show(optionId);
			}
			oElement.onmouseout=function() {
				if(!this.active)
					this.className=this.classDefault;
			}
			oElement.onmouseover=function() {
				if(!this.active)
					this.className=this.classHover;
				if(this.parent.selectMode=='hover') {
					optionId=this.getAttribute('optionId');
					this.parent.parent.Show(optionId);
				}
			}
			oElement.onmousedown=function() {
				if(!this.active)
					this.className=this.classDown;
			}
			oElement.setAttribute('optionId',i);
			oElement.parent=this;
			this.element.appendChild(oElement);
			this.buttons.push(oElement);
		}
	}
}
