/* calendar.js - Extends fs_ajax.js */
var Calendar = Class.create();
Object.extend(Object.extend(Calendar.prototype,fs_ajax.prototype), {
	_parent:fs_ajax.prototype,
	debugMode:false,
	tabs:null,
	currentState:"",
	stateSql:"",
	tabs:"",
	initialize: function(ajaxUrl,outputId,calendarName,stateTabsId) {
	    // call the parents initialize with params
 	    this._parent.initialize.call(this,{ajaxUrl:ajaxUrl,outputId:outputId,name:calendarName});
 	    if (stateTabsId) {
 	    	this.tabs=$(stateTabsId);
		}
	},
	setState: function(state,tab) {
	    // This function updates the UI and adjusts the Ajax Pars for the
	    // event filter.
	    // NOTE: some calendars do not have States
	    if (state!=this.currentState) {
		    // Deactivate pervious tab
		    if (this.tabs!="") {
				var previousSelected=this.tabs.getElementsByClassName("aa_active","aa_active");
				previousSelected = $A(previousSelected);
				previousSelected.each( function(currentTab) {
					Element.removeClassName(currentTab,"aa_active");
				});
   			}
			// Activate state tab
			// Only do this if a tab has been specified and if the state has changed
			if (tab!="") {
				selectedTab=$(tab);
				selectedTab.addClassName("aa_active");
				// Set the state (from user interaction) by adjusting an internal var NOT ajax pars
				this.stateSql="&state="+state;
				this.currenState=state;
			}
			// Update the dropdown box on the way out
			$('aa_where_'+state).selected=true;
		}
	},
	setKeyword: function(keywords) {
	    $('aa_keywords').value=keywords;
	},
	setFrom: function(optionValue) {
		// Update the dropdown box for the from field
		$('from_'+optionValue).selected=true;
	},
	setTo: function(optionValue) {
		// Update the dropdown box for the to field
		$('to_'+optionValue).selected=true;
	},
	update: function() {
		this.loading();
 	    // Update the DOM, calling the objects Ajax Url with this.ajaxPars
 	    var fsAjax=new Ajax.Updater({success:this.outputId},this.ajaxUrl,{method:'post',parameters:this.ajaxPars+this.stateSql,onFailure:this.error.bind(this),insertion:this.loaded.bind(this),evalScripts:true});
	},
	submit: function(formId) {
		// Clear out the stateSql for the form submission instead...
		formId.submit=false;
		this.stateSql="";
		// Continue with the rest of our standard ajax stuff
		var pars=Form.serialize($(formId));
		this.debug(pars);
		this.ajaxPars="fs_ajaxSubmit=true&"+pars;
		this.update();
	},
	success: function(formId) {
	    this.animateOut();
	},
	animateOut: function() {
		this.debug("animateOut");
		new Effect.Opacity(this.outputId,{fps:60,duration:0.5,from:1.0,to:0,queue:{position:'end',scope:this.name},afterFinish:this.animateIn.bind(this)});
	},
	animateIn: function() {
		this.debug("animateIn");
		new Effect.Opacity(this.outputId,{fps:60,duration:0.5,from:0,to:1.0,queue:{position:'end',scope:this.name},afterFinish:this.evaluateScripts.bind(this)});
		this._parent.success.call(this);
	},
 	evaluateScripts: function() {
 	    this.debug("evaluating resulting html's scripts");
		this.newHtml.evalScripts();
	}
});

