/*
	ClassesInterface.js
	
	Basic Pagination setup for the Projects Application. 
	
	AJAX functions will be limited to search results and projects pagination. 
*/

var ClassesPagination = new Class({
	/*
		Options to set rendering locations.
	*/
	
	options: {
		Wrapper:        		'/project_gallery.cfm',		// Page where we run AJAX requests on. Relative to root directory.
		MainContainer: 		'project_listings',    			// Where we replace new Projects AJAX response with
		PaginationInfoContainer:	'pagination_info',				// Where we replace pagination info
		NextPage:      		'NextPage',					// Next Page Button
		PrevPage:      		'PrevPage',        				// Prev Page Button
		Categories:			'.category',					// Class of all category options
		ItemsPerPage:  		 3
	},
	
	initialize: function(options) {
		// Start-Up Code
		this.setOptions(options);

		this.MainContainer 			=  $( this.options.MainContainer );
		this.Categories 			= $$( this.options.Categories );
		this.PaginationInfoContainer 	=  $( this.options.PaginationInfoContainer );
		this.NextPage       		=  $(this.options.NextPage);
		this.PrevPage       		=  $(this.options.PrevPage);
		
		this.Start          		= 0; 
		this.End           			= 0;
		this.startPage      		= 1;
		
		this.InterfaceSetup();
		
	},

	LoadContent: function(sAttributes, oContainer, postFunction) {
		if( postFunction != null )
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer, onComplete: postFunction }).request();
		else
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer }).request();
	},

	 // ///////////////////////////////////////// //
	// /////////////// NextPress /////////////// //
	NextPress: function( event ){
		
		this.currentPage			= parseInt( this.NextPage.name.split('_')[0] );
		this.currentAttribute		= parseInt( this.NextPage.name.split('_')[2] );
		this.currentAttributeValue	= parseInt( this.NextPage.name.split('_')[3] );
		
		if( this.currentAttribute == 0 )
			this.currentAttribute = 'Recent';
		else if( this.currentAttribute == 1 )
			this.currentAttribute = 'Category';
		else if( this.currentAttribute == 2 )
			this.currentAttribute = 'Popular';
		else if( this.currentAttribute == 3 )
			this.currentAttribute = 'User';
		else if( this.currentAttribute == 4 )
			this.currentAttribute = 'Class';
				
		this.SetNextPage( this.currentPage + 1, this.currentAttribute, this.currentAttributeValue );
	
		event.stop();
	},

	 // ///////////////////////////////////////// //
	// /////////////// PrevPress /////////////// //
	PrevPress: function( event ){
		
		this.currentPage = parseInt( this.PrevPage.name.split('_')[0] );
		this.currentAttribute		= parseInt( this.NextPage.name.split('_')[2] );
		this.currentAttributeValue	= parseInt( this.NextPage.name.split('_')[3] );
		
		if( this.currentAttribute == 0 )
			this.currentAttribute = 'Recent';
		else if( this.currentAttribute == 1 )
			this.currentAttribute = 'Category';
		else if( this.currentAttribute == 2 )
			this.currentAttribute = 'Popular';
		else if( this.currentAttribute == 3 )
			this.currentAttribute = 'User';
		else if( this.currentAttribute == 4 )
			this.currentAttribute = 'Class';
		
		this.SetPrevPage( this.currentPage - 1, this.currentAttribute, this.currentAttributeValue );
		
		event.stop();
	},
	  	
	 // ////////////////////////////////////////////// //
	// /////////////// InterfaceSetup /////////////// //
	InterfaceSetup: function() {
				
		this.MainContainer 			=  $( this.options.MainContainer );
		this.Categories 			= $$( this.options.Categories );
		this.PaginationInfoContainer 	=  $( this.options.PaginationInfoContainer );
		this.NextPage       		=  $(this.options.NextPage);
		this.PrevPage       		=  $(this.options.PrevPage);
		
		if( this.NextPage )
			this.Total = this.NextPage.name.split('_')[1];
		else if( this.PrevPage )
			this.Total = this.PrevPage.name.split('_')[1];
		else
			this.Total = null;

		// Setup Navigation
		if( this.NextPage )
			this.NextPage.addEvent('click', this.NextPress.bindWithEvent( this ));
		if( this.PrevPage )
			this.PrevPage.addEvent('click', this.PrevPress.bindWithEvent( this ));
		
		// Setup Categories
		/*
		if( this.Categories )
			for( var i = 0; i < this.Categories.length; i++ )
				this.Categories[i].addEvent( 'click', this.ChangeCategory.bindWithEvent( this, i ) );
		*/
	},

	 // /////////////////////////////////////////// //
	// /////////////// SetNextPage /////////////// //
	SetNextPage: function( page, attribute, attribute_value ) {		
				
		if( page == 1 )
			$( 'Prev_Wrap' ).style.visibility = 'hidden';
		else
			$( 'Prev_Wrap' ).style.visibility = 'visible';
		
		if( page * this.options.ItemsPerPage >= this.Total )
			$( 'Next_Wrap' ).style.visibility = 'hidden';
		else
			$( 'Next_Wrap' ).style.visibility = 'visible';
					
		// Main Class page							
		if( page > 1 )
			this.Start = ( page * this.options.ItemsPerPage ) - this.options.ItemsPerPage + 1;
		else 
			this.Start = 1;
					
		if( this.Total > ( page * this.options.ItemsPerPage ) )
			this.End   = ( page * this.options.ItemsPerPage );
		else
			this.End   = this.Total;
			
		if( this.Start > this.Total )
		{
			this.Start = this.End;
		} else {
			// Load Content
			this.currentAttribute = parseInt( this.NextPage.name.split('_')[2] );

			var Attributes = '/AJAXAction/1/' + attribute + '/' + attribute_value + '/Page/' + page;
			this.LoadContent( Attributes, this.MainContainer, null );
			
			this.PaginationInfoContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
			this.PrevPage.name = page + '_' + this.Total + '_' + this.currentAttribute + '_' + attribute_value;
			this.NextPage.name = page + '_' + this.Total + '_' + this.currentAttribute + '_' + attribute_value;
		}
	},

	 // /////////////////////////////////////////// //
	// /////////////// SetPrevPage /////////////// //
	SetPrevPage: function( page, attribute, attribute_value ) {		
				
		if( page == 1 )
			$( 'Prev_Wrap' ).style.visibility = 'hidden';
		else
			$( 'Prev_Wrap' ).style.visibility = 'visible';

		if( page * this.options.ItemsPerPage >= this.Total )
			$( 'Next_Wrap' ).style.visibility = 'hidden';
		else
			$( 'Next_Wrap' ).style.visibility = 'visible';
			
		// Main classes page
		if( page > 1 )
			this.Start = ( page * this.options.ItemsPerPage ) - this.options.ItemsPerPage + 1;
		else 
			this.Start = 1;
		
		if( this.Total > ( page * this.options.ItemsPerPage ) )
			this.End   = ( page * this.options.ItemsPerPage );
		else
			this.End   = this.Total;
		
		if( page >= 1 )
		{
			// Load Content
			this.currentAttribute = parseInt( this.PrevPage.name.split('_')[2] );
			
			var Attributes = '/AJAXAction/1/' + attribute + '/' + attribute_value + '/Page/' + page;
			this.LoadContent( Attributes, this.MainContainer, null );
			
			this.PaginationInfoContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
			this.PrevPage.name = page + '_' + this.Total + '_' + attribute + '_' + attribute_value;
			this.NextPage.name = page + '_' + this.Total + '_' + this.currentAttribute + '_' + attribute_value;
		}
	}

});

// ///////////////////////////////////////////////////////////////////////////////////////////// // OTHER FUNCTIONS

 // ///////////////////////////////////////////// //
// /////////////// CategorySetup /////////////// //
function CategorySetup() {
	// Main page
	var AllCats    = $$( '#category_container li' );
	var ActiveCats = $$( '#category_container_active li' );
			
	for( i = 0; i < AllCats.length; i++ ) {
		
		//alert( AllCats[i].className );
		
		if( AllCats[i].hasClass( 'default' ) || AllCats[i].hasClass( 'active' )  )
			AllCats[i].setStyle( 'display', 'block' );
		
		AllCats[i].addEvents({
						 
			mouseover: function() {
				
				$( 'category_wrapper' ).setStyle( 'z-index', '250' );
				
				for( var j = 0; j < AllCats.length; j++ ) {
					AllCats[j].setStyle( 'display', 'block' );
					
					if( AllCats[j].hasClass( 'first' ) ){
					    AllCats[j].addClass( 'first_active' );
					}
					
					if( AllCats[j].hasClass( 'last' ) ){
					    AllCats[j].addClass( 'last_active' );
					}
						
				}
					
				$( 'nav_browse' ).addClass( 'active' );
					
			},
			
			click: function() {
				
				for( var j = 0; j < AllCats.length; j++ )
					AllCats[j].removeClass( 'active' );
				
				this.addClass( 'active' );
				
				for( var j = 0; j < AllCats.length; j++ )
					if( !AllCats[j].hasClass( 'active' ) )
						AllCats[j].setStyle( 'display', 'none' );
						
			}
		});
	}
	
	$( 'category_container' ).addEvents({
							
		mouseout: function() {
			for( var j = 0; j < AllCats.length; j++ ) {
				
				if( !AllCats[j].hasClass( 'active' ) && !AllCats[j].hasClass( 'default' ) && !AllCats[j].hasClass( 'first' ) ) 
					AllCats[j].setStyle( 'display', 'none' );
				
				if( AllCats[j].hasClass( 'first_active' ) && !AllCats[j].hasClass( 'default' ) ){
				    AllCats[j].removeClass( 'first_active' );
				    AllCats[j].addClass( 'first' );
				}
				
				if( AllCats[j].hasClass( 'default' ) ) {
					
					$( 'nav_browse' ).removeClass( 'active' );
					
					if( !AllCats[j].hasClass( 'default' ) )
						AllCats[j].removeClass( 'active' );
				}
			}
		}
	});

}

 // /////////////////////////////////////// //
// /////////////// addVote /////////////// //
function addVote ( ProjectID, UserID, UserJoinType, UpdateTo ) {
	var url = '/project_gallery.cfm/AJAXAction/1/AddVote/1/ProjectID/' + ProjectID + '/UserID/' + UserID + '/UserJoinType/' + UserJoinType + '/';
	new Ajax( url, { method: 'get', update: $( UpdateTo ), evalScripts: true }).request();
}

 // ///////////////////////////////////////// //
// /////////////// ChageType /////////////// //
function ChageType ( num ) {
	
	if( num == 1 ) {
		$( 'YouTube_Wrap' ).setStyle( 'display', 'block' );
	} else {
		$( 'YouTube_Wrap' ).setStyle( 'display', 'none' );
	}
	
}

ClassesPagination.implement(new Options);


// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Dom Ready


window.addEvent('domready', function(){
							  
	var Pagination = new ClassesPagination();
	
	CategorySetup();
				
});