var Formulario=function(options)
{this.init(options);};Formulario.prototype={init:function(options)
{var defaults=Object.extend({container:'form',form:'',template:'default',emoticon:'normal',site:'',step:1,currentName:"current",animate:false,animateFrame:'imagem',saveID:'saver',onComplete:false,onSubmit:function(){alert('Obrigado pela sua ajuda')},tree:{container:"treeContainer",uses:true,hoverElem:'otherTopicsContainer'},topics:{container:"topics-container",question:"li",type:"class",atomic:"topic-"},welcome:{exists:true,container:"welcome-msg",hiddenContainer:"toshow",headers:"titulos",headersClass:"titulos-bg"}},arguments[0]||{});this.options=defaults;this.topics=new Hash();this.topicHits=new Hash();this.changeStyle();this.endLoad.bindAsEventListener(this);this.run();},highlight:function(eid,bcolor,fcolor)
{$(eid).style.backgroundColor=bcolor;$(eid).style.color=fcolor;},completeFirstAnimation:function(counter,bean,ref)
{if(counter>0)
{var less=counter-1;var grow=bean+272;$(ref.options.animateFrame).style.backgroundPosition="0px "+grow+"px";ref.completeFirstAnimation.delay(0.05,less,grow,ref);}},showFirstAnimation:function(counter,bean,ref)
{if(counter>0)
{var less=counter-1;var grow=bean-272;$(ref.options.animateFrame).style.backgroundPosition="0px "+grow+"px";ref.showFirstAnimation.delay(0.1,less,grow,ref);}else{ref.completeFirstAnimation(4,(bean-272),ref);}},endLoad:function()
{if(this.options.animate)
{this.showFirstAnimation.delay(0.3,3,0,this);}
if(this.options.onComplete)
{this.options.onComplete();}
this.updateTopicsInfo();if(this.options.tree.uses)
{var ref=this;Event.observe($(ref.options.tree.container),'mouseover',function(){$(ref.options.tree.hoverElem).show();});Event.observe($(ref.options.tree.container),'mouseout',function(){$(ref.options.tree.hoverElem).hide();});}},updateTopicsInfo:function()
{var ref=this;$$('*['+this.options.topics.type+'="'+this.options.topics.container+'"]').each(function(el){var total=$$("#"+el.id+" "+ref.options.topics.question).length;ref.topics.set(el.id,total);ref.topicHits.set(el.id,0);});},run:function()
{var starter=new Ajax.Updater({success:this.options.container,failure:this.options.container},'services.sapo.pt/form/get/',{parameters:{form:this.options.form,site:this.options.site,template:this.options.template},onComplete:function(){this.formulario.endLoad();}});},growElement:function(eid,nChilds,oriHeight,navId)
{var childsHeight=nChilds*70;var barHeight=$(navId).offsetHeight+40;var incHeight=childsHeight;if(barHeight>childsHeight)
{incHeight=barHeight;}
if(incHeight>oriHeight)
{$(eid).style.height=incHeight+"px";}else{$(eid).style.height=oriHeight+"px";}},showTopicQuestions:function(elem,rootContainer,topicID)
{if($(this.options.currentName))
{$(this.options.currentName).id='zero';}
$(elem).id=this.options.currentName;Effect.Pulsate(elem,{duration:1,pulses:3});if(this.options.step===1)
{if(this.options.welcome.exists)
{if($(this.options.welcome.container))
{$(this.options.welcome.container).hide();}
if($(this.options.welcome.hiddenContainer))
{$(this.options.welcome.hiddenContainer).show();}
if($(this.options.welcome.headers))
{$(this.options.welcome.headers).writeAttribute('class',this.options.welcome.headersClass);Effect.Pulsate(this.options.welcome.headers,{duration:0.5,pulses:2});}}
this.options.step+=1;}
$(rootContainer).childElements().each(function(el)
{$(el).hide();});$(topicID).slideDown();},showOtherTopicQuestions:function(elem,rootContainer,topicID,dropBox,dropElement,theContainer)
{var numberOfElements=-1;if($(this.options.currentName))
{$(this.options.currentName).id='zero';}
$(elem).id=this.options.currentName;Effect.Pulsate(elem,{duration:1,pulses:3});if(this.options.step===1)
{if(this.options.welcome.exists)
{if($(this.options.welcome.container))
{$(this.options.welcome.container).hide();}
if($(this.options.welcome.hiddenContainer))
{$(this.options.welcome.hiddenContainer).show();}
if($(this.options.welcome.headers))
{$(this.options.welcome.headers).writeAttribute('class',this.options.welcome.headersClass);Effect.Pulsate(this.options.welcome.headers,{duration:0.5,pulses:2});}}
this.options.step+=1;}
$(rootContainer).childElements().each(function(el)
{$(el).hide();numberOfElements++;});$(topicID).slideDown();if($(theContainer))
{$(theContainer).hide();}
var subMenu=$$('#'+dropBox+' ul').first();if(subMenu.childElements().length<=1)
{$('otherTopics').hide();}
$(dropBox).insert({top:$(dropElement)});},changeStyle:function()
{var cssLink=new Element('link',{type:'text/css',id:'css-formulario-boss',rel:'stylesheet',href:'services.sapo.pt/form/style/'+this.options.template+'/'});if($('css-formulario-boss'))
{$$('head').first().removeChild($('css-formulario-boss'));}
$$('head').first().appendChild(cssLink);},changeTheme:function(theme)
{this.options.template=theme;this.changeStyle();this.run();},save:function(elem)
{$(this.options.saveID).innerHTML="a enviar...";var vals=this.topicHits.values();var totalAnswered=0;for(var i=0;i<vals.length;i++)
{totalAnswered+=vals[i];}
if( totalAnswered == 0 ) {
	$(this.options.saveID).innerHTML="Sem dados";
	setTimeout( "changeLabel();" , 3000 );
	return false;
}
var theValueFields=Form.serialize(elem).split("&");var theValues=0;for(var j=0;j<theValueFields.length;j++)
{if(theValueFields[j].match('form-question'))
{var tmp=theValueFields[j].split("=");if(parseInt(tmp[1]))
theValues+=parseInt(tmp[1]);}}
var average=theValues/totalAnswered;var face="";if(average<3)
{face="zangado";}else if(average>3){face="contente";}else{face="normal";}
var ref=this;var saver=new Ajax.Request('services.sapo.pt/form/save/',{parameters:Form.serialize(elem),onComplete:function()
{ref.options.emoticon=face;ref.options.onSubmit();$(ref.options.saveID).parentNode.remove();}});return false;},hidePreviewIconVote:function(eid)
{Effect.Appear(eid);},showPreviewIconVote:function(eid)
{},updateTopicHits:function(topicID,newValue)
{this.topicHits.set(topicID,newValue);if(this.topics.get(topicID)>0)
{$$("."+this.options.topics.atomic+topicID+"")[0].addClassName("done");}
else
{$$("."+this.options.topics.atomic+topicID+"")[0].removeClassName("done");}},hideOtherIconVotes:function(elem,icon,field,value)
{var args=field.split("-");var topicID=args[2];if($(icon).style.display==='none')
{$(icon).show();$(field).value=value;var newValue=this.topicHits.get(topicID)+1;}else{$(icon).hide();$(field).value="";var newValue=this.topicHits.get(topicID)-1;}
this.updateTopicHits(topicID,newValue);var list=['vote-red-icon-'+field,'vote-light-red-icon-'+field,'vote-yellow-icon-'+field,'vote-light-green-icon-'+field,'vote-green-icon-'+field];list.splice(list.indexOf(icon),1);var ref=this;list.each(function(el){if($(el).visible())
{$(el).hide();var spl=el.split("-");var secTopicID=spl[spl.length-2];var secNewValue=ref.topicHits.get(secTopicID)-1;ref.updateTopicHits(secTopicID,secNewValue);}});}};
function changeLabel(eid)
{
	$("saver").innerHTML="Submeter"
}

