
//general dd ac style ajax form
var cc_fx_dasaform = new Class({
	Implements: [Options, Events],
	options:{},
	initialize: function(trigger_class){
		//set the triggers
		this.triggers = $$('.'+trigger_class);
		//set the droppers
		var count = this.triggers.length;
		this.droppers = new Array(count);
		this.slides = new Array(count);
		this.expandedIndex = -1;
		
		this.triggers.each(function(trigger){
			var t_index = trigger.id.substring(trigger.id.lastIndexOf('_')+1, trigger.id.length);
			var t_index2 = this.triggers.indexOf(trigger);
			
			this.droppers[t_index2] = $('d_'+t_index);
			
			//alert(this.droppers[i]);
			//attach events
			/*this.slides[i] = new Fx.Slide(this.droppers[i], {
				duration:700,
				transition: Fx.Transitions.Pow.easeOut
			});
			this.slides[i].hide();
			trigger.addEvent('click', function(event){
				event.preventDefault();
				this.expand(i);
			}.bind(this));
			//set the effect for the droppers
			*/
			
		}.bind(this));
		this.attachTriggers();
		
	},
	attachTriggers: function(){
		this.triggers.each(function(trigger){
			trigger.addEvent('click', function(event){
				event.preventDefault();	
				this.expand(this.triggers.indexOf(trigger));
			}.bind(this));
			//set up slides
			//alert( this.droppers ); 
			this.slides[this.triggers.indexOf(trigger)] = new Fx.Slide(this.droppers[this.triggers.indexOf(trigger)], {
				duration: 700,
		    	transition: Fx.Transitions.Pow.easeOut
			});
			
			this.slides[this.triggers.indexOf(trigger)].hide();
			this.droppers[this.triggers.indexOf(trigger)].style.display = 'block';
		}, this);
		this.droppers.each(function(dropper){
			var the_form = dropper.getElement('form');
			
			the_form.addEvent('submit', function (event){
				event.stop();
				the_form.set('send', {onComplete:function(response){
					this.expand(this.expandedIndex);
					td = $('system_message_ajax');
					
					message = new Element('div', {html:'Email Edited', style:'color:red; font-weight:bold'});
					
					
					message.inject(td, 'top');
					
					
					setTimeout("message.fade(0)", 1000);
					setTimeout('message.destroy()', 1500);
					setTimeout('window.location = window.location', 1600);
				}.bind(this)});
				mail_subject = the_form.subject.value;
				mail_comments = the_form.comments.value;
				mail_content = the_form.content.value;
				the_form.getElement('.email_subj_err').innerHTML = "";
				the_form.getElement('.email_comm_err').innerHTML = "";
				the_form.getElement('.email_cont_err').innerHTML = "";
				if(mail_subject == '')
				{
					the_form.getElementsByClassName('email_subj_err')[0].innerHTML = "Subject must not be empty";
				}
				else if (mail_comments == '')
				{
					the_form.getElementsByClassName('email_comm_err')[0].innerHTML = "Please provide comments for the mail";
				}
				else if (mail_content == '')
				{
					the_form.getElementsByClassName('email_cont_err')[0].innerHTML = "Please provide content for the mail";
				}
				else
				{
					the_form.send();
					
				}
			}.bind(this));
		}, this);
	},
	expand: function(formIndex){
		if(this.expandedIndex == formIndex)
		{
			this.expandedIndex =-1;
			this.contract(formIndex);
			
		}
		else
		{
			var init = this.expandedIndex;
			this.expandedIndex = formIndex;
			
			this.contract(init);
			
			this.slides[formIndex].slideIn();
			//get the contents
			
		}
		
	},
	contract: function(formIndex){
		if(formIndex >- 1)
		{
			this.slides[formIndex].slideOut();
		}
	}
});

var cc_fx_emailForms = new Class({
	Implements: [Options, Events],
	options:{},
	initialize: function (the_trigger, the_dropper){
		this.triggers = $$('.'+the_trigger+' a');
		this.droppers = $$('.'+the_dropper+ ' form')
		this.expandedIndex = -1;
		this.slides = new Array(10);
		this.attachTriggers();
		
	},
	attachTriggers: function(){
	
		this.triggers.each(function(trigger){
			trigger.addEvent('click', function(event){
				event.preventDefault();	
				this.expandForm(this.triggers.indexOf(trigger));
			}.bind(this));
			//set up slides
			this.slides[this.triggers.indexOf(trigger)] = new Fx.Slide(this.droppers[this.triggers.indexOf(trigger)], {
				duration: 700,
		    	transition: Fx.Transitions.Pow.easeOut
			});
			this.slides[this.triggers.indexOf(trigger)].hide();
			
		}, this);
		//attach ajax forms
		this.droppers.each(function(dropper){
			dropper.addEvent('submit', function (event){
				event.stop();
				dropper.set('send', {onComplete:function(response){
					this.expandForm(this.expandedIndex);
					td = dropper.getParent('td');
					
					message = new Element('div', {html:'Mail Sent', style:'color:red; font-weight:bold'});
					
					
					message.inject(td, 'top');
					
					
					//setTimeout("message.fade(0)", 1000);
					setTimeout('message.destroy()', 2800);
				}.bind(this)});
				mail_subject = dropper.email_subject.value;
				mail_content = dropper.email_content.value;
				if(mail_subject == '')
				{
					dropper.getElementsByClassName('email_subj_err')[0].innerHTML = "Subject must not be empty";
				}
				else if (mail_content == '')
				{
					dropper.getElementsByClassName('email_cont_err')[0].innerHTML = "Please provide content for the mail";
				}
				else
				{
					dropper.send();
				}
			}.bind(this));
		}, this);
		
		if($chk($('searh_results_loading')))
		{
			$('searh_results_loading').style.display = 'none';
			$('search-results-table').style.display = 'block';
		}
	},
	expandForm: function(formIndex){
		
		
		
		//expand the form here
		if(this.expandedIndex == formIndex)
		{
			this.contractForm(formIndex);
			this.expandedIndex =-1;
		}
		else
		{
			this.contractForm(this.expandedIndex);
			this.slides[formIndex].slideIn();
			this.expandedIndex = formIndex;
		}
		
		
	},
	contractForm: function(formIndex){
		if(formIndex >- 1)
		{
			this.slides[formIndex].slideOut();
		}
	}
});

window.addEvent('domready', function(){
	
	
	new cc_fx_dasaform('edit_email');
	
	
		setTimeout("new cc_fx_emailForms('email_user', 'email_form');", 2000);
	
	
});
