/*
	ClassesInterface.js
	
	Basic Pagination setup for the Classes Application. 
	
	AJAX functions will be limited to search results and classes 
	pagination. 
*/

var ClassesPagination = new Class({
	/*
		Options to set rendering locations.
	*/
	
	options: {
		Wrapper:        		'/classes.cfm',	// Page where we run AJAX requests on. Relative to root directory.
		ClassContainer: 		'ClassMainContent',    	// Where we replace new Class AJAX response with
		ClassDetailContainer: 	'class_menu_right',		// Where we replace new Class Details AJAX response with 
		HowToContainer: 		'content_wrap',    		// Where we replace new HowTo AJAX response with
		InfoContainer: 		'PaginationInfo',  		// Where our pagination div is at
		InfoDetailContainer: 	'PaginationDetailInfo',	// Where our detail pagination div is at
		NextPage:      		'NextPage',			// Next Page Button
		PrevPage:      		'PrevPage',        		// Prev Page Button
		Favorite:				'favorite',			// ID of the favorite icon link
		ClassCategories:		'.class_category',		// class of all class category options
		ClassDetailPerPage:  	 3,
		ClassPerPage:  		 3,
		HowToPerPage:  		 6,
		CategoryList: 			'Search_Category',
		KeywordsList:  		'Search_Keywords',
		SearchButton: 			'Search_Btn'
	},
	
	initialize: function(options) {
		// Start-Up Code
		this.setOptions(options);

		this.ClassContainer 		=  $(this.options.ClassContainer);
		this.ClassDetailContainer	=  $(this.options.ClassDetailContainer);
		this.InfoContainer  		=  $(this.options.InfoContainer);
		this.InfoDetailContainer  	=  $(this.options.InfoDetailContainer);
		this.HowToContainer 		=  $(this.options.HowToContainer);
		this.ClassCategories 		= $$(this.options.ClassCategories);
		
		this.Keywords      			= '';
		this.Category       		= '';
		
		this.Start          		= 0; 
		this.End           			= 0;
		this.startPage      		= 1;
		
		this.InterfaceSetup();
		
	},
	//
		//sAttributes : Attributes in the form of /Page/1/Request/Test Request Response or ?Page=1&Request=Test Request Response
		//oContainer  : Container in the form of 'test'
		//postFunction: Function to execute after the response has been received.
	//	
	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.SetNextPage( this.currentPage + 1 );
	
		event.stop();
	},

	 // ///////////////////////////////////////// //
	// /////////////// PrevPress /////////////// //
	PrevPress: function( event ){
		this.currentPage = parseInt( this.PrevPage.name.split('_')[0] );
		
		this.SetPrevPage( this.currentPage - 1 );
		
		event.stop();
	},
	
	 // ////////////////////////////////////////////// //
	// /////////////// ChangeCategory /////////////// //
	ChangeCategory: function( event ){
		this.Keywords       = '';
		this.Category       = this.CategoryList.value;
		
		sAttributes         = '/HowTo/1/Page/1/Category/' + this.Category;
		this.LoadContent( sAttributes, this.HowToContainer, this.InterfaceSetup.bind(this) );
		
		event.stop();
		
	},
	
	 // ////////////////////////////////////////////// //
	// /////////////// ChangeKeywords /////////////// //
	ChangeKeywords: function( event ) {
		this.Keywords       = this.SearchKeywords.value;
		this.Category       = '';
		
		var sAttributes     = '/HowTo/1/Page/1/Keywords/' + this.Keywords;
		this.LoadContent( sAttributes, this.HowToContainer, this.InterfaceSetup.bind(this) );
		
		event.stop();
	},

	 // /////////////////////////////////////////// //
	// /////////////// AddFavorite /////////////// //
	AddFavorite: function ( event ) {
		
		this.cID			= this.Favorite.name.split('_')[1];
		this.uID			= this.Favorite.name.split('_')[0];
		var sAttributes	= '/Favorite/1/cID/' + this.cID + '/uID/' + this.uID + '/';
		
		new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: $( 'favorite_responce' ), evalScripts: true }).request();
		
		event.stop();
		
	},
	  
	 // /////////////////////////////////////////////////// //
	// /////////////// ChangeClassCategory /////////////// //
	ChangeClassCategory: function ( event, i ) {
		
		this.catID = this.ClassCategories[i].name;
		
		var page = 1; // When going to a new category, always start on page 1
		
		if( this.NextPage )
			this.Total = this.NextPage.name.split('_')[1];
		else
			this.Total = null;
			
		this.Start = page;
					
		if( this.Total > ( page * this.options.ClassDetailPerPage ) )
			this.End   = ( page * this.options.ClassDetailPerPage );
		else
			this.End   = this.Total;

		var Attributes = '/DetailPage/1/ClassCat/' + this.catID + '/';
		this.LoadContent( Attributes, this.ClassDetailContainer, this.ChangeClassPost.bind( this, this.catID ) );
	},
	
	ChangeClassPost: function( catID ) {
		if( $( 'new_total' ) )
			var newTotal = $( 'new_total' ).name;
		//alert(newTotal);
		
		if($(this.InfoDetailContainer))
			this.InfoDetailContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + newTotal;
		this.PrevPage.name = '1_' + newTotal + '_' + catID;
		this.NextPage.name = '1_' + newTotal + '_' + catID;
		
		//$( 'browse_btn' ).removeClass( 'active' );
		//$( 'category_nav' ).setStyle( 'display', 'none' );
		
	},
	
	  // ////////////////////////////////////////////// //
	 // /////////////// InterfaceSetup /////////////// //
	InterfaceSetup: function() {
				
		this.CategoryList   	=  $(this.options.CategoryList);
		this.SearchButton   	=  $(this.options.SearchButton);
		this.SearchKeywords 	=  $(this.options.KeywordsList);
		this.NextPage       	=  $(this.options.NextPage);
		this.PrevPage       	=  $(this.options.PrevPage);
		this.Favorite       	=  $(this.options.Favorite);
		this.ClassCategories	= $$(this.options.ClassCategories)
		
		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;
		// End Initial Setup

		// Setup HowTo Search
		if( this.CategoryList )
			this.CategoryList.addEvent('change', this.ChangeCategory.bindWithEvent( this ));
		if( this.SearchButton )
			this.SearchButton.addEvent('click', this.ChangeKeywords.bindWithEvent( this ));

		// 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 Favorites
		if( this.Favorite )
			this.Favorite.addEvent('click', this.AddFavorite.bindWithEvent( this ));
			
		if( this.ClassCategories )
			for( var i = 0; i < this.ClassCategories.length; i++ )
				this.ClassCategories[i].addEvent( 'click', this.ChangeClassCategory.bindWithEvent( this, i ) );
	},

	 // /////////////////////////////////////////// //
	// /////////////// SetNextPage /////////////// //
	SetNextPage: function( page ) {		
		
		var cPage  = $(this.options.ClassContainer);
		var cdPage = $(this.options.ClassDetailContainer);
		
		if( page == 1 )
			$( 'prev_wrap' ).style.visibility = 'hidden';
		else
			$( 'prev_wrap' ).style.visibility = 'visible';
		
		if( cPage ) {
			if( page * this.options.ClassPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		} else if(cdPage) {
			if( page * this.options.ClassDetailPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		} else {
			if( page * this.options.HowToPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		}
					
		if( cPage ) {
			// Main Class page							
			if( page > 1 )
				this.Start = ( page * this.options.ClassPerPage ) - this.options.ClassPerPage + 1;
			else 
				this.Start = 1;
						
			if( this.Total > ( page * this.options.ClassPerPage ) )
				this.End   = ( page * this.options.ClassPerPage );
			else
				this.End   = this.Total;
				
			if( this.Start > this.Total )
			{
				this.Start = this.End;
			} else {
				// Load Content
				var Attributes = '/Page/' + page;
				this.LoadContent( Attributes, this.ClassContainer, null );
				// Set Info Bar
				this.InfoContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
				this.PrevPage.name = page;
				this.NextPage.name = page;
			}
		} else if( cdPage ) {
			// Class Detail page
			this.Total = $( 'new_total' ).name;
			
			if( page > 1 )
				this.Start = ( page * this.options.ClassDetailPerPage ) - this.options.ClassDetailPerPage + 1;
			else 
				this.Start = 1;
						
			if( this.Total > ( page * this.options.ClassDetailPerPage ) )
				this.End   = ( page * this.options.ClassDetailPerPage );
			else
				this.End   = this.Total;
				
			if( this.Start > this.Total )
			{
				this.Start = this.End;
			} else {
				// Load Content
				var CurrentCat = this.NextPage.name.split('_')[2];
				var Attributes = '/DetailPage/' + page + '/ClassCat/' + CurrentCat + '/';
				this.LoadContent( Attributes, this.ClassDetailContainer, null );
				// Set Info Bar
				if($(this.InfoDetailContainer))
					this.InfoDetailContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
				
				
				this.PrevPage.name = page + '_' + this.Total + '_' + CurrentCat + '/';
				this.NextPage.name = page + '_' + this.Total + '_' + CurrentCat + '/';
			}
		} else {
			// How-Tos
			if( page > 1 )
				this.Start = ( page * this.options.HowToPerPage ) - this.options.HowToPerPage + 1;
			else 
				this.Start = 1;
						
			if( this.Total > ( page * this.options.HowToPerPage ) )
				this.End   = ( page * this.options.HowToPerPage );
			else
				this.End   = this.Total;
				
			if( this.Start > this.Total )
			{
				this.Start = this.End;
			} else {
				// Load Content
				if( this.Keywords != '' && this.Category == '')
					sAttributes         = '/HowTo/1/Page/' + page + '/Keywords/' + this.Keywords;
				else
					sAttributes         = '/HowTo/1/Page/' + page + '/Category/' + this.Category;
					
				this.LoadContent( sAttributes, this.HowToContainer, this.InterfaceSetup.bind(this) );
				// Set Info Bar
				this.InfoContainer.innerHTML = "How-To's " + this.Start + ' - ' + this.End + ' of ' + this.Total;
				this.PrevPage.name = page;
				this.NextPage.name = page;
			}
		}
	},

	 // /////////////////////////////////////////// //
	// /////////////// SetPrevPage /////////////// //
	SetPrevPage: function( page ) {		
		
		var cPage = $(this.options.ClassContainer);
		var cdPage = $(this.options.ClassDetailContainer);
		
		if( page == 1 )
			$( 'prev_wrap' ).style.visibility = 'hidden';
		else
			$( 'prev_wrap' ).style.visibility = 'visible';

		if( cPage ) {
			if( page * this.options.ClassPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		} else if(cdPage) {
			if( page * this.options.ClassDetailPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		} else {
			if( page * this.options.HowToPerPage >= this.Total )
				$( 'next_wrap' ).style.visibility = 'hidden';
			else
				$( 'next_wrap' ).style.visibility = 'visible';
		}
			
		if( cPage ) {
			// Main classes page
			if( page > 1 )
				this.Start = ( page * this.options.ClassPerPage ) - this.options.ClassPerPage + 1;
			else 
				this.Start = 1;
			
			if( this.Total > ( page * this.options.ClassPerPage ) )
				this.End   = ( page * this.options.ClassPerPage );
			else
				this.End   = this.Total;
			
			if( page >= 1 )
			{
				// Load Content
				var Attributes = '/Page/' + page;
				this.LoadContent( Attributes, this.ClassContainer, null );
				// Set Info Bar
				this.InfoContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
				this.PrevPage.name = page;
				this.NextPage.name = page;
			}
		} else if( cdPage ) {
			
			this.Total = $( 'new_total' ).name;
			
			// detail class pagination
			if( page > 1 )
				this.Start = ( page * this.options.ClassDetailPerPage ) - this.options.ClassDetailPerPage + 1;
			else 
				this.Start = 1;
			
			if( this.Total > ( page * this.options.ClassDetailPerPage ) )
				this.End   = ( page * this.options.ClassDetailPerPage );
			else
				this.End   = this.Total;
			
			if( page >= 1 )
			{
				// Load Content
				var CurrentCat = this.NextPage.name.split('_')[2];
				var Attributes = '/DetailPage/' + page + '/ClassCat/' + CurrentCat + '/';
				this.LoadContent( Attributes, this.ClassDetailContainer, null );
				// Set Info Bar
				if($(this.InfoDetailContainer))
					this.InfoDetailContainer.innerHTML = this.Start + ' - ' + this.End + ' of ' + this.Total;
				
				
				this.PrevPage.name = page + '_' + this.Total + '_' + CurrentCat + '/';
				this.NextPage.name = page + '_' + this.Total + '_' + CurrentCat + '/';
			}
			
		} else {
			// How-Tos
			if( page > 1 )
				this.Start = ( page * this.options.HowToPerPage ) - this.options.HowToPerPage + 1;
			else 
				this.Start = 1;
			
			if( this.Total > ( page * this.options.HowToPerPage ) )
				this.End   = ( page * this.options.HowToPerPage );
			else
				this.End   = this.Total;
			
			if( page >= 1 )
			{
				// Load Content
				if( this.Keywords != '' && this.Category == '')
					sAttributes         = '/HowTo/1/Page/' + page + '/Keywords/' + this.Keywords;
				else
					sAttributes         = '/HowTo/1/Page/' + page + '/Category/' + this.Category;
					
				this.LoadContent( sAttributes, this.HowToContainer, this.InterfaceSetup.bind(this) );
				// Set Info Bar
				this.InfoContainer.innerHTML = "How-To's " + this.Start + ' - ' + this.End + ' of ' + this.Total;
				this.PrevPage.name = page;
				this.NextPage.name = page;
			}
		}
	}

});

// ///////////////////////////////////////////////////////////////////////////////////////////// // OTHER FUNCTIONS

 // ///////////////////////////////////////////// //
// /////////////// CategorySetup /////////////// //
function CategorySetup() {
	// Main page
	var AllCats = $$( '#category_nav .menu_item' );
	var i;
		
	for( i = 0; i < AllCats.length; i++ ) {
		
		//alert( AllCats[i].id + ' : ' + AllCats[i].hasClass( 'default' ) );
		
		if( AllCats[i].hasClass( 'default' ) || AllCats[i].hasClass( 'active' )  )
			AllCats[i].setStyle( 'display', 'block' );
		
		AllCats[i].addEvents({
						 
			mouseover: function() {
				
				for( var j = 0; j < AllCats.length; j++ )
					AllCats[j].setStyle( 'display', 'block' );
					
			},
			
			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_nav' ).addEvents({
							
		mouseout: function() {
			for( var j = 0; j < AllCats.length; j++ ) {
				if( !AllCats[j].hasClass( 'active' ) && !AllCats[j].hasClass( 'default' ) ) {
					AllCats[j].setStyle( 'display', 'none' );
				}
			}
		}
	});

}

 // /////////////////////////////////////////////////// //
// /////////////// DetailCategorySetup /////////////// //
function DetailCategorySetup() {
	// Detail page	
	var AllCats = $$( '#category_nav .menu_item' );
	var i;
	
	for( i = 0; i < AllCats.length; i++ ) {

		if( AllCats[i].hasClass( 'default' ) || AllCats[i].hasClass( 'active' ) )
			AllCats[i].setStyle( 'display', 'block' );

		AllCats[i].addEvents({
						 
			mouseover: function() {
				
				for( var j = 0; j < AllCats.length; j++ )
					AllCats[j].setStyle( 'display', 'block' );
					
			},
			
			click: function( e ) {
								
				for( var j = 0; j < AllCats.length; j++ ) {
					AllCats[j].removeClass( 'active' );
					AllCats[j].removeClass( 'default' );
				}
				
				this.addClass( 'active' );
				
				for( var j = 0; j < AllCats.length; j++ )
					if( !AllCats[j].hasClass( 'active' ) )
						AllCats[j].setStyle( 'display', 'none' );
				
				e.stop();
						
			}
		});
	}
	
	$( 'category_nav' ).addEvents({
							
		mouseout: function() {
			for( var j = 0; j < AllCats.length; j++ ) {
				if( !AllCats[j].hasClass( 'active' ) && !AllCats[j].hasClass( 'default' ) ) {
					AllCats[j].setStyle( 'display', 'none' );
				}
			}
		}
	});

}

 // ///////////////////////////////////////// //
// /////////////// 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();
	
	
	if( $( 'browse_by_category' ) && $( 'category_nav' ) ) {
		CategorySetup();
	}

	if( $( 'browse_by_category_detail' ) && $( 'category_nav' ) ) DetailCategorySetup();
				
});