var floatMenuPanels = new Array();
var floatMenuAnimator = new Animator();
floatMenuAnimator.setEndCallback(floatMenuAnimEnd);

function floatMenuAnimEnd() {
	for(var i = 0;i<floatMenuAnimator.elements.length;i++) {
		var e = floatMenuAnimator.elements[i];
		if (e.a == 0) e.element.style.visibility = 'hidden';
	}
}

function getFloatMenuPanelById(id) {
	for(var i=0;i<floatMenuPanels.length;i++)
		if(floatMenuPanels[i].id==id) return floatMenuPanels[i];
	return null;
}

function FloatMenuPanel(title,o,e) {
	this.id = floatMenuPanels.push(this) - 1;	
	this.title = title;
	this.div = null;
	this.padding = 4;
	this.magin = 8;
	this.init(e);
	this.animElem = new AnimatedElement(this.div);
	floatMenuAnimator.addElement(this.animElem);
	this.setOptions(o);
}

function FloatMenuOption(text,url,opts) {
	this.text=text;
	this.url=url;
	this.opts=opts;
	this.parent = null;
	if (opts != null)
		for(var i=0;i<opts.length;i++)
			opts[i].parent = this;
	this.id=null;
}

FloatMenuOption.prototype.setId = function(globalId, id) {
	this.id = globalId+'/'+id;
	if (this.opts != null) {
		for(var i=0;i<this.opts.length;i++) {
			this.opts[i].setId(this.id,i);
		}
	}
}

FloatMenuPanel.prototype.init = function(e) {
	if (e != null) this.div = e;
	else {
		var exists = this.div != null;
		if (!exists) {
			this.div = document.createElement("div");
			this.div.style.position = 'absolute';
			this.div.style.left = '0px';
			this.div.style.top = '0px';
			this.div.style.width = '100px';
			this.div.style.height = '100px';
		}
		setOpacity(this.div, 0.0);
		this.div.style.visibility = 'hidden';
		if (!exists) document.body.appendChild(this.div);
	}	
}

FloatMenuPanel.prototype.setOptions = function(opts) {
	this.opts = opts;
	for(var i=0;i<opts.length;i++) opts[i].setId('',i);
	this.setActiveOpts(opts);	
}

FloatMenuPanel.prototype.setSize = function(w, h) {
	this.div.style.width = w+'px';
	this.div.style.height = h+'px';
}

FloatMenuPanel.prototype.setPos = function(x, y) {
	this.div.style.position = 'absolute';
	this.div.style.left = x+'px';
	this.div.style.top = y+'px';
}

FloatMenuPanel.prototype.getContainer = function() {
	return this.div;
}

FloatMenuPanel.prototype.setBackImage = function(url,pos,repeat) {
	this.div.style.backgroundImage = "url('"+url+"')";
	this.div.style.backgroundRepeat = repeat;
	this.div.style.backgroundPosition = pos;
}

FloatMenuPanel.prototype.setBorderColor = function(color) {
	this.div.style.border = '1px solid #'+color;
}

FloatMenuPanel.prototype.setBackColor = function(color) {
	this.div.style.backgroundColor = '#'+color;
}

FloatMenuPanel.prototype.setPadding = function(p) {
	this.padding = p;
	var tab = this.div; //.childNodes[0];
	if (tab != null) tab.style.padding = p+'px';
}
FloatMenuPanel.prototype.setMargin = function(p) {
	var tab = this.div;
	if (tab != null) tab.style.margin = p+'px';
}

FloatMenuPanel.prototype.clear = function() {
	this.div.innerHTML = '';
}

function fmpOptionClicked(fmpId,oId) {
	var fmp = getFloatMenuPanelById(fmpId);
	if (fmp == null) return;
	var o = fmp.getOptionById(oId);
	if (o == null) return;
	fmp.setActiveOpts(o.opts);
}

FloatMenuPanel.prototype.getOptionByIdGlobal = function(id, opts) {
	if (opts == null) return null;
	for(var i=0;i<opts.length;i++) {
		if(opts[i].id==id) return opts[i];
		else {
			var foundOpt = this.getOptionByIdGlobal(id, opts[i].opts);
			if (foundOpt != null) return foundOpt;
		}
	}	
	return null;	
}

FloatMenuPanel.prototype.getOptionById = function(id) {
	for(var i=0;i<this.activeOpts.length;i++) {
		if(this.activeOpts[i].id==id) return this.activeOpts[i];
	}	
	return this.getOptionByIdGlobal(id, this.opts);
}

function backClicked(fmpId,oId) {
	var fmp = getFloatMenuPanelById(fmpId);
	if (fmp == null) return;
	var o = fmp.getOptionById(oId);
	if (o == null) return;
	if (o.parent == null) fmp.setActiveOpts(fmp.opts);
	else fmp.setActiveOpts(o.parent.opts);
}

function closeClicked(fmpId) {
	var fmp = getFloatMenuPanelById(fmpId);
	if (fmp == null) return;
	fmp.show(false);
	//Restart photo fading in cover
	startPhotoFading();
}

FloatMenuPanel.prototype.setActiveOpts = function(opts) {
	this.activeOpts = opts;
	if (opts == null) this.div.clear();
	var html = ''; //'<table cellspacing="0" cellpadding="0" width="100%"
	html += '<table cellspacing="0" cellpadding="'+this.padding+'" width="100%" border="0">';
	html += '<tr><td><table cellspacing="0" cellpadding="0" width="100%" border="0">';
	html += '<tr>';
	if (opts.length > 0 && opts[0].parent != null) {
		html += '<td width="4">&nbsp;</td><td class="option-panel-buttons"><a class="option-panel-buttons" href="javascript:backClicked(\''+this.id+'\',\''+opts[0].parent.id+'\')">&laquo; enrere</a></td>';
	}
	html += '<td align="right" class="option-panel-buttons"><a class="option-panel-buttons" href="javascript:closeClicked(\''+this.id+'\')">tancar</a></td>';
	html += '</tr>';
	html += '</table></td></tr>';
	if (opts.length > 0 && opts[0].parent != null) {
		html += '<tr><td class="option-panel-title">'+opts[0].parent.text+'</td></tr>';
	} else if (this.title != null) {
		html += '<tr><td class="option-panel-title">'+this.title+'</td></tr>';
	}
	for(var i=0; i<opts.length;i++) {
		html += '<tr>';
		html += '<td class="normal-text"><span class="option-sep">&nbsp;·&nbsp;</span>';
		if (opts[i].url != null) html += '<a class="normal-text" href="'+opts[i].url+'">';
		else if (opts[i].opts != null && opts[i].opts.length > 0) html += '<a class="normal-text" href="javascript:fmpOptionClicked(\''+this.id+'\',\''+opts[i].id+'\')">';
		html += opts[i].text;
		if (opts[i].url == null && opts[i].opts != null) html += '&nbsp;&raquo;';
		if (opts[i].url != null) html += '</a>';
		else if (opts[i].opts != null && opts[i].opts.length > 0) html += '</a>';
		html += '</td>';
		html += '</tr>';
	}
	html += '</table>';
	this.div.innerHTML = html;
}

FloatMenuPanel.prototype.show = function(s) {
	if (s) {
		for(var i=0;i<floatMenuAnimator.elements.length;i++) {
			floatMenuAnimator.elements[i].setEndOpacity(0.0);
			floatMenuAnimator.elements[i].element.style.zIndex=i+1;
		}
		this.animElem.setEndOpacity(1.0);
		this.animElem.element.style.visibility = 'visible';
		this.div.style.zIndex=floatMenuAnimator.elements.length+1;
	} else {
		this.animElem.setEndOpacity(0.0);
		this.div.style.zIndex=1;
	}
	floatMenuAnimator.play();	
}

var photoFadingStopCounter = 0;

function stopPhotoFading() {
	photoFadingStopCounter++;
	clearTimeout(photoTimerId);
	photoTimerId = 0;
	//Stop photo fading in cover:stop current image-crossing
	if(ixf.count!=1){
		ixf.count = (1 / ixf.resolution)/10;
		ixf.crossfade();
	}
}

function startPhotoFading() {
	photoFadingStopCounter--;
	if (photoFadingStopCounter <= 0) {
		photoFadingStopCounter = 0;
		if( photoTimerId == 0)
			photoTimerId = setTimeout("swapPhoto();", 4000);
	}
}

FloatMenuPanel.prototype.toggle = function() {
	if(this.animElem.a == 0){
		//Stop photo fading in cover:general stop
		stopPhotoFading();
		/*
		clearInterval(ixf.clock);
		ixf.clock = null;
		ixf.count = 1;
		ixf.obj.src = ixf.src;
		if(ixf.newimg.parentNode)
			ixf.newimg.parentNode.removeChild(ixf.newimg);*/
	}
	else{
	
		//Restart photo fading in cover
		if( photoTimerId == 0)
			photoTimerId = setTimeout("swapPhoto();", 4000);
	}
	this.show(this.animElem.a == 0);
}