/*
	CalendarInterface.js
	
	Basic Pagination setup for the Calendar Application. 
	
*/

var CalendarPagination = new Class({
	/*
		Options to set rendering locations.
	*/
	options: {
		Wrapper:       '/blog.cfm',		// Page where we run AJAX requests on. Relative to root directory.
		Calendar:		'calendar_wrap',	// Where we replace new Calendar AJAX response with
		NextPage:      'NextPage',		// Next Page Button
		PrevPage:      'PrevPage',		// Prev Page Button
		TipContainer:	'search_calendar',	// Page element that contains the calendar tips
		TipClass:		'.calendar_tips'	// name of class the ID's calendar tips
},
	
	initialize: function( options ) {
		this.setOptions( options );
		this.Calendar = $( this.options.Calendar );
		this.LoadingFx = new Fx.Tween( 'loading_overlay', { duration: '200', link: 'chain' } );
		this.LoadingFx.start( 'opacity', '0', '0' );

		this.InterfaceSetup();
	},

	LoadContent: function( sAttributes, oContainer, postFunction ) {
		if( postFunction != null ) {
			var req = new Request.HTML({
				method:		'get',
				update:		 oContainer,
				url:			 this.options.Wrapper + sAttributes,
				evalScripts:	 false,
				link:		'chain',
				onRequest:	 function() {
									
										var StartLoadingFx = new Fx.Tween( 'loading_overlay', { duration: '200' } );
										StartLoadingFx.start( 'opacity', '0', '1');
										//alert('starting');
									 },
				onComplete:	 function() {
										var EndLoadingFx = new Fx.Tween( 'loading_overlay', { duration: '200' } );
										EndLoadingFx.start( 'opacity', '1', '0');
										//alert('ending')
										},
				onSuccess:	 postFunction,
				onFailure:	 function(){ oContainer.set( 'text', 'The request failed.' ); }
			}).send();
		} else {
			var req = new Request.HTML({
				method:		'get',
				update:		 oContainer,
				url:			 this.options.Wrapper + sAttributes,
				evalScripts:	 false,
				link:		'cancel',
				onFailure:	 function() { oContainer.set( 'text', 'The request failed.' ); }
			}).send();
		}
	},
	
	NextPress: function( event ){			
		this.SetNextPage( this.NextPage.name.split('_')[0] );
		event.stop();
	},
	
	PrevPress: function( event ){	
		this.SetPrevPage( this.PrevPage.name.split('_')[0] );
		event.stop();
	},
		
	InterfaceSetup: function() {
		this.NextPage       = $( this.options.NextPage.split('_')[0] );
		this.PrevPage       = $( this.options.PrevPage.split('_')[0] );
		
		if( this.NextPage )
			this.NextPage.addEvent( 'click', this.NextPress.bindWithEvent( this ) );
		if( this.PrevPage )
			this.PrevPage.addEvent( 'click', this.PrevPress.bindWithEvent( this ) );
			
		// Initialize Tool-Tips
		var myTips = new Tips( this.options.TipClass, { className: 'cal_tip_wrap' } );
	},
	
	SetNextPage: function( page ) {		
		if( $( this.options.Calendar ) ) {
			var Attributes = '?AJAXAction=true&CalendarDate=' + page;
			this.LoadContent( Attributes, this.Calendar, this.InterfaceSetup.bind( this ) );
		}
	},
	
	SetPrevPage: function( page ) {		
		if( $( this.options.Calendar ) ) {
			var Attributes = '?AJAXAction=true&CalendarDate=' + page;
			this.LoadContent( Attributes, this.Calendar, this.InterfaceSetup.bind( this ) );
		} 
	}
});

CalendarPagination.implement(new Options);
