/**
 * Regeneris Functions
**/
$(document).ready(function(){
	
	
	/***** Custom styled select *****/
	$(".custom_select").each(function() {
		$(this).customSelect();
	});
	
	
	/***** Hover effects on headings in latest page *****/
	var aniSpeed = 400;
	
	$('.latest h2 a').hover( function () {
		$(this).parent().find('span').stop().animate({
			opacity: 1,
			marginLeft: '20px'
		});
	},
	function() {
		$(this).parent().find('span').stop().animate({
			opacity: 0,
			marginLeft: '4px'
		});
	});
	
});



/***** Slider *****/
function slider(){
	$('.slides').slides({
		preload: true,
		generatePagination: true,
		generateNextPrev: true,
		play: 5000,
		pause: 2500
	});
	return;
}



/***** Custom styled select *****/
$.fn.customSelect = function() {
	var container = $(this);
	var list = $(this).find('.custom_select_list');
	
	//console.log(me);
	container.bind("click", function(){
	
		//	Toggle list
		list.toggle();
		list.css( { 'z-index':2 } );
		
		//	Create hider (only if list is visible - funky double div thing happening otherwise)
		if ( list.css( 'display' ) == 'block' ) {
			var doc_h = $(document).height()
			var doc_w = $(document).width()
			var hider = $( '<div id="hide_custom_select">' );
			hider.css( { 'position':'absolute', 'left':0, 'top':0, 'width':doc_w, 'height':doc_h, 'z-index':1 } );
			//hider.css( { 'position':'absolute', 'left':0, 'top':0, 'width':doc_w, 'height':doc_h, 'z-index':1, 'background':'yellow','opacity':0.2 } );
			hider.click( function( e ) { $(this).remove(); list.toggle(); } );
			$( 'body' ).append(hider);
		}
		
	});
	container.find('.custom_select_list li').bind("click", function() {
		//console.log(me.parent().children('.custom_select_name'));
		container.children('.custom_select_name').text($(this).text());
		
		//	Remove hider
		$( '#hide_custom_select' ).remove();
	});
};



/***** Case Study - Show more functionality *****/
$( function(){

	var CaseStudy ={
	
		/* 
		 * Instance variables
		 */
		case_h:				0,
		container_maxh:		0,
		container_curh:		0,
		container:			null,
		num_rows_start:		3,
		rows_per_load:		2,
		loaded_rows:		0,
		visible_rows:		0,
		
		
		/* 
		 * Init()
		 * 
		 * TODO: Bind event to scroll so we're not using a button to increase the viewport
		 * 
		 */
		init : function()
		{
			this.container	= $( '#case_studies_list' );
			
			//	Calculate the relevant height
			this.container_maxh		= this.container.height();
			this.case_h = this.container.find( 'li:first-child' ).outerHeight( true );
			this.loaded_rows		= this.num_rows_start;
			this.visible_rows		= this.num_rows_start;
			
			//	Set some CSS
			this.container.css({ 'overflow' : 'hidden', 'height' : this.container_maxh} );
			
			//	Hide overflow elements
			var start_height = this.case_h * this.num_rows_start
			this.container_curh = start_height;
			this.container.css({'height' : start_height} );
			
			//	Bind events
			$( '#more' ).click( this.doLoad );
			$( '.custom_select li' ).click( function(){ CaseStudy.doFilter( $(this).attr( 'rel' ), $(this) ) } );
			$( '#casestudy_reset' ).click( function() { CaseStudy.reset( true, $(this) ); return false; } );
		},
		
		
		/* 
		 * doLoad()
		 * 
		 * Loads more rows when the 'more' button is clicked
		 * 
		 */
		doLoad: function( load_new )
		{
			//	If results are being filtered then container is at correct size and shouldn't be resized.
			if ( CaseStudy.being_filtered )
				return false;
			
			//	Otherwise load a new row if we're still within the limts of the container
			
			//	New height is (number of visible rows * height of each row) + ( the number of rows to load * the height of each row )
			var new_height = (CaseStudy.visible_rows * CaseStudy.case_h ) + ( CaseStudy.case_h * CaseStudy.rows_per_load );
			
			//	Limit new_height to the maximum size of the container
			new_height = ( new_height > CaseStudy.container_maxh ) ? CaseStudy.container_maxh : new_height;
			
			//	Animate the container
			CaseStudy.container.animate({ 'height' : new_height});
			
			//Update variables
			CaseStudy.visible_rows = CaseStudy.loaded_rows = CaseStudy.loaded_rows + CaseStudy.rows_per_load;
			CaseStudy.container_curh = new_height;
			
			//	Show/hide the 'more' Button
			CaseStudy.showMoreButton();
			
			return false;
		},
		
		
		/* 
		 * doFilter()
		 * 
		 * Filters the content
		 * 
		 */
		doFilter: function( filter_str, elem )
		{
			//	Reset everything regardless
			if ( typeof( elem ) != 'undefined' ) {
				//	I know this is hacky. :( (puke)
				var tmp_name = elem.text();
				CaseStudy.reset( false );
				elem.parents('.custom_select').find( '.custom_select_name' ).html( tmp_name );
			}
			
			
			//	We are filtering
			if ( filter_str ){
			
				CaseStudy.being_filtered = true;
				//console.log( 'searching for: ' + filter_str );
				$( '#casestudy_reset' ).show();
				
				var counter = 0;
				function filter( el, filter_str )
				{
					//	Search the className
					if ( ! el.hasClass( filter_str ) ){
						el.hide();
					} else{
						el.show();
						counter++;
					}
						
				}
				//	Loop over all the Case Studies and filter them
				$.each(
					$( 'li', CaseStudy.container ),
					function(){
						filter ( $( this ), filter_str );
					}
				);
				
				//console.log( 'found: ' + counter );
				
				//	Resize the container
				var num_rows	= Math.ceil( counter / 4 );
				var new_height	= num_rows * CaseStudy.case_h;
				
				//minimum height of 2 row
				new_height = ( new_height < CaseStudy.case_h ) ? CaseStudy.case_h : new_height;
				
				CaseStudy.container.animate({ 'height' : new_height} );
				//CaseStudy.container_maxh = new_height;
				
				//	Update some variables
				CaseStudy.visible_rows = num_rows;
				
				//	Hide 'more' button as it's effectively useless
				CaseStudy.hideMoreButton();
				
			//	No filter string, reset to how we were
			} else {
			
				CaseStudy.being_filtered = false;
				
				//	Show all the case studies again
				$( 'li', CaseStudy.container ).show();
				
				//	Old height is (number of loaded rows * height of each row)
				var old_height = (CaseStudy.loaded_rows * CaseStudy.case_h );
					
				//	Animate the container
				CaseStudy.container.animate({ 'height' : old_height} );
				
				//	[re]set some variables
				CaseStudy.visible_rows = CaseStudy.loaded_rows;
				
				//	Show the more button again
				CaseStudy.showMoreButton();
			}	
		},
		
		
		/* 
		 * showMoreButton()
		 * 
		 * Shows the 'more' button if there's content which can still be loaded
		 * 
		 */
		showMoreButton: function()
		{
			var max_rows = Math.ceil( $( 'li', CaseStudy.container ).length / 4 );
			if ( CaseStudy.loaded_rows < max_rows )
			{
				$( '#more' ).fadeIn( 500 );
			} else {
				CaseStudy.hideMoreButton();
			}
		},
		
		
		/* 
		 * hideMoreButton()
		 * 
		 * Hides the 'more' button
		 * 
		 */
		hideMoreButton: function()
		{
			$( '#more' ).fadeOut( 500 );
		},
		
		
		/* 
		 * reset()
		 * 
		 * Resets form names, resets entire form if sctrict == true
		 * 
		 */
		reset: function( strict, elem )
		{
			if (strict === true){
			
				CaseStudy.doFilter();
				elem.fadeOut( 100, function() {
					elem.hide();
				} );
			
			}
			
			//	Reset dropdown values
			$.each( $( '.custom_select_name' ), function() {
			
				var reset_text = $(this).attr( 'rel' );
				$(this).html( reset_text );
			
			});
			
			return false;
		}
	
	}
	
	CaseStudy.init();
});
