var Feedback = {
	initialize: function(){
		this.type = 'B';
		this.code='<div id="Feedback"><div><a id="FeedbackBtn" onclick="Feedback.show(); return false;"><img src="/images/sfeedback_btn.png" width="124" height="18" alt="SEND FEEDBACK" /></a></div></div>'
		this.code+='<div id="FeedBackTool" style="display:none;"><div id="FeedBackForm"></div></div>'; 
		Element.insert('SCION', {top:this.code});
		
		$('FeedbackBtn').onmouseover = function(e){
			$('FeedbackBtn').setStyle({
			  cursor:'pointer'
			});
			//log('FeedbackBtn over');
		}
			
		$('FeedbackBtn').onmouseout = function(e){
			$('FeedbackBtn').setStyle({
			  cursor:'pointer'
			});
			//log('FeedbackBtn out');
		}
	},
	
	buildForm: function(){
		log('bSession:'+bSession);
	
		this.type = 'B';		
		
		this.form='<ul style="width:375px; height:310px; margin: 0 auto; position:absolute; top:0; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/feedbck_frm_bg.png\');"></ul>';
		
		this.form+='<ul style="position:absolute; top:0;">';
		this.form+='<form id="FeedbackFrm" method="POST" action="">';
		this.form+='<li id="FBCloseBtn"><a href="javascript:Feedback.hide();"><img src="/images/feedbck_frm_closebtn.png" alt="*" border="0" /></a></li>';
		this.form+='<li id="FBHeader"><img src="/images/feedbck_frm_header.png" alt="*" /></li>';
		this.form+='<li style="width:296px; color:#999; margin:5px 0 0 0;">We\'re adding more features and updating the landscape regularly, but we want to know what you think so far. Let us know how we can make Scion City even better.</li>';
		this.form+='<li id="FBOptions">';
		this.form+='<ul class="" id="">';
		this.form+='<li><img src="/images/feedbck_frm_labeltype.png" alt="*" /></li>';
		this.form+='<li id="LabelBug"><a class="select" id="Bug"></a></li>';
		this.form+='<li id="LabelSug"><a id="Sugg"></a></li>';		
		this.form+='</ul><br clear="left"/>';		
		this.form+='</li>';
		
		if(!bSession) {
			this.form+='<li class="formfb-label"><img src="/images/feedbck_frm_labelmail.png" alt="*" /></li>';
			this.form+='<li id=""><input name="email" type="" value=""/></li>';	
			this.form+='<li class="formfb-label"><img src="/images/feedbck_frm_labeldescription.png" alt="*" /></li>';
			this.form+='<li id=""><textarea name="message"></textarea></li>';
		}else{		
			this.form+='<li class="formfb-label"><img src="/images/feedbck_frm_labeldescription.png" alt="*" /></li>';
			this.form+='<li id=""><textarea name="message" style="height:90px; "></textarea></li>';
		}
		
		this.form+='<li id="FBSubmit"><a href="javascript:Feedback.send();"><img src="/images/feedbck_frm_submit.png" alt="*" border="0" /></a></li>';
		this.form+='</form>';
		this.form+='</ul>';
		
		
		
		$('FeedBackForm').update(this.form);
				
		$('Bug').observe('mouseup', (function(e){
			var element = e.element();
			if(!element.hasClassName('select')) {
				element.addClassName('select');
				$('Sugg').removeClassName('select');
				this.type = 'B';
			}
		}).bind(this));
		
		$('Sugg').observe('mouseup', (function(e){
			var element = e.element();
			if(!element.hasClassName('select')) {
				element.addClassName('select');
				$('Bug').removeClassName('select');
				this.type = 'S';
			}
		}).bind(this));		
	},

	show: function(){ 
		this.buildForm();
		$('FeedBackTool').show();
		//new Effect.Appear('FeedBackTool', { duration: 0.3, from: 0.0, to: 1 });
	},
	
	hide: function(){ $('FeedBackTool').hide();	},
	
	send: function(){
		this.sendData;
		this.form = $('FeedbackFrm');
		log('this.form:'+this.form);
		
		if(!bSession && !$(this.form['email']).present()) {
			alert('Write your email please');	
		}else if(!$(this.form['message']).present()) {
			alert('Write your message please');	
		}else{
			
			$('FBSubmit').update('<img class="spinner" style="margin:0 0 0 15px;" src="/img/loadinfo.net.gif" alt="loading" />');
			
			this.form=$('FeedbackFrm').serialize(true);		
			
			/*
			if(this.form['email'] != '') this.email = this.form['email'];
			else this.email = '';
			*/
			
			if(!bSession) this.email = this.form['email'];
			else this.email = '';
			
			//log('thisgarage.username:'+user.getUsername());
			//if(thisgarage.isMyGarage()) this.userName = thisgarage.data.username;
			//var sUserName = 
			if(bSession) this.userName = user.getUsername();
			else this.userName = '';
			
			this.sendData = {
				fromemail: this.email,
				username: this.userName,
				ftype: this.type, //('B' o 'S', es requerido), 
				message:	this.form['message']		
			}
			
			log('this.sendData:'+Object.toJSON(this.sendData));
			scion.sendFeedback(this.sendData);			
		}	
	},
	
	confirmation: function(){
		this.conf='<ul style="width:375px; height:310px; margin: 0 auto; position:absolute; top:0; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/feedbck_frm_bg.png\');"></ul>';
		
		this.conf+='<ul style="position:absolute; top:0;">';
		this.conf+='<li id="FBHeader"><img src="/images/feedbck_frm_header_simple.png" alt="*" /></li>';
		this.conf+='<li style="color:#FFF; margin:10px 0 20px 0;">Thank you for your help.<br/>Your feedback has been submitted.</li>';
		this.conf+='<li id="FBSubmit"><a href="javascript:Feedback.hide();"><img src="/images/feedbck_frm_closebtn2.png" alt="*" border="0" /></a><li>';
		this.conf+='</ul>';
		
		$('FeedBackForm').update(this.conf);
	}	
}

/*
new Event.observe(window, "load", function(){
	
});
*/

document.observe('dom:loaded', function () { Feedback.initialize(); });

scion.sendFeedbackSucceeded = function(){Feedback.confirmation();}
scion.sendFeedbackFailed = function(errors){
	$('FBSubmit').update('<a href="javascript:Feedback.send();"><img src="/images/feedbck_frm_submit.png" alt="*" border="0" /></a>');
	log("sendFeedbackFailed:"+errors);
	if(errors.include('Invalid email')) alert('Please type your e-mail address in the format "yourname@domain.com"');		
}

