
// HomepagePagination
var HomepagePagination = new Class ({
	
	options: {
		
		Content: 		'news_wrapper',	// ID of the contianing element we will update to
		WebRoot:		'/',				// Webroot for this site
		UpdateURL:	'index.cfm',		// URL to send out AJAX request to
		Prev:		'prev',			// ID of the previous button
		Next:		'next'			// ID of the next button
		
	},
		
	initialize: function( options ) {
		
		this.setOptions( options );
				
		this.Content		= $( this.options.Content );
		this.WebRoot		=	this.options.WebRoot;
		this.UpdateURL		=	this.options.UpdateURL;
		this.Prev			= $( this.options.Prev );
		this.Next			= $( this.options.Next );
		
		if( this.Content ) {
			if( this.Prev )
				this.Prev.addEvent( 'click', this.beginPagination.bindWithEvent( this, this.Prev.name ) );
			
			if( this.Next )
				this.Next.addEvent( 'click', this.beginPagination.bindWithEvent( this, this.Next.name ) );
		}
		
	},
	
	beginPagination: function ( event, name ) {
		
		var page	= parseInt( name.split('_')[3] );
		var limit	= parseInt( name.split('_')[2] );
		var total	= parseInt( name.split('_')[1] );
		
		if( page >= 0 && page < total ) {
			var newURL = this.WebRoot + this.UpdateURL +'?AJAXRequest=true&page=' + page + '&limit=' + limit;
			var myAjax = new Request.HTML( { url: newURL, link: 'cancel', update: $( this.Content ), onSuccess: this.reInitialize.bind( this, [ page, limit ] ) } ).send();
		}
		
		event.stop();

	},
	
	reInitialize: function ( page, limit ) {

		this.Prev			= $( this.options.Prev );
		this.Next			= $( this.options.Next );
				
		if( this.Prev ) {
			if( this.Prev.name.split('_')[3] < 0 ) {
				this.Prev.addClass( 'inactive' );
			} else if ( this.Prev.hasClass( 'inactive' ) ) {
				this.Prev.removeClass( 'inactive' );
			}
			this.Prev.addEvent( 'click', this.beginPagination.bindWithEvent( this, this.Prev.name ) );
		}
		
		if( this.Next ) {
			if( this.Next.name.split('_')[3] == this.Next.name.split('_')[1] ) {
				this.Next.addClass( 'inactive' );
			} else if ( this.Next.hasClass( 'inactive' ) ) {
				this.Next.removeClass( 'inactive' );
			}
			this.Next.addEvent( 'click', this.beginPagination.bindWithEvent( this, this.Next.name ) );
		}
		
	}

});


// ZebraTable
var ZebraTable = new Class({
	/* implements */
	Implements: [Options],

	/* options */
	options: {
		elements:		'#content_cases table',
		cssEven:		'even',
		cssOdd:		'odd',
		cssHighlight:	'highlight',
		cssMouseEnter:	'mo'
	},
	
	/* initialization */
	initialize: function(options) {
		/* set options */
		this.setOptions(options);
		/* zebra-ize! */
		$$(this.options.elements).each(function(table) {
			this.zebraize(table);
		},this);
	},
	
	/* a method that does whatever you want */
	zebraize: function(table) {
		/* for every row in this table... */
		table.getElements('tr').each(function(tr,i) {
			/* check to see if the row has th's
				if so, leave it alone
				if not, move on */
			if(tr.getFirst().get('tag') != 'th') {
				/* set the class for this based on odd/even */
				var self = this, klass = i % 2 ? self.options.even : self.options.odd;
				/* start the events! */
				tr.addClass(klass).addEvents({
					/* mouseenter */
					mouseenter: function () {
						if(!tr.hasClass(self.options.cssHighlight)) tr.addClass(self.options.cssMouseEnter).removeClass(klass);
					},
					/* mouseleave */
					mouseleave: function () {
						if(!tr.hasClass(self.options.cssHighlight)) tr.removeClass(self.options.cssMouseEnter).addClass(klass);
					},
					/* click */
					click: function() {
						tr.toggleClass(self.options.cssMouseEnter).toggleClass(self.options.cssHighlight);
						if(!tr.hasClass(self.options.cssHighlight)) tr.removeClass(self.options.cssMouseEnter);
					}
				});
			}
		},this);
	}
});


// ///////////////////////////////////////////////////////////////////////////////////////////////////////// Initialization //

HomepagePagination.implement( new Options );


// ///////////////////////////////////////////////////////////////////////////////////////////////////////// DOM Ready //

window.addEvent( 'domready', function() { 
	
	if( $( 'content_cases' ) ) { var zebraTables = new ZebraTable(); };
	
});
