

	/**

	 * VARIABLES

	 *

	 * Global variables used within functions and event handlers.

	 */

	var animate_speed=800,easing_method='easeOutExpo';

	var contact_height=null,login_height=null;

	var debug=false;

	

	/**

	 * GRID SETUP

	 *

	 * Setup initial grid display and page actions.

	 */

	$(function() {

		//debug = 1;

		setup_grid();

		setup_logo_block();

		//setup_dropdowns();

		//setup_contact();

	});

	

		//setup clickable more DJB

	 $(document).ready(function() {

		$("#show-more").click(function () { 

			load_more_items();

		});

	});



	/**

	 * ISSET

	 *

	 * PHP style isset() method for variables and objects.

	 *

	 * @param mixed variables.

	 */

	function isset(variable)  {

		try {

			if(typeof(eval(variable))!='undefined')

			if(eval(variable)!=null) return true;

		} catch(e) {}

		return false;

	}

	

	/**

	 * TRACE

	 *

	 * Browser safe console logging method.

	 *

	 * @param string msg

	 */

	function trace(msg) {

		try {window.console.log(msg)} catch (err) {}

	}

	

	/**

	 * SETUP_LOGO_BLOCK

	 *

	 * Prepare logo block and mouse actions.

	 */

	function setup_logo_block() {

	

		if(filter) $('#grid .logo .slider').slider({min:0,max:100,value:50,step:1,slide: slider_update,stop: update_grid});

		if(filter) $('#grid .logo').hover(function() {

			$(this).find('.sliders').stop().animate({left:'36px'},Math.floor(animate_speed/2),easing_method);

			$(this).stop().animate({height:'330px'},animate_speed,easing_method);

		},

		function() {

			$(this).find('.sliders').stop().animate({left:'-172px'},Math.floor(animate_speed/2),easing_method);

			$(this).stop().animate({height:'154px'},animate_speed,easing_method);

		});

		else $('#grid .logo span.gridhoverarea').hide();

	

	}

	

	/**

	 * SLIDER_UPDATE

	 *

	 * Logo block slider event handler.

	 */

	function slider_update(e) {

	

		var values=get_slider_values();

		

		//

		// Calculate Difference

		var diff=Math.floor((150-(values.photography+values.illustration))/2);

		if(debug) trace('slider adjustment: '+diff);

		

		//

		// Adjust Sliders

		switch($(e.target).parents('.group').find('span:first').text()) {

		

			case 'illustration':

				$('#grid .logo .group:eq(1) .slider').slider('value',(values.photography+diff));

				break;

			

			case 'photography':

				$('#grid .logo .group:eq(0) .slider').slider('value',(values.illustration+diff));

				break;		

		}

		

	}

	

	/**

	 * GET_SLIDER_VALUES

	 *

	 * Retrieve values from logo block sliders.

	 */

	function get_slider_values() {

		var values={

			photography: Math.floor($('#grid .logo .group:eq(0) .slider').slider('value')),

			illustration: Math.floor($('#grid .logo .group:eq(1) .slider').slider('value'))

		};

		if(isNaN(values.photography)) values.photography = 50;

		if(isNaN(values.illustration)) values.illustration = 50;

		return values;

	}

	

	/**

	 * SETUP_DROPDOWNS

	 *

	 * Setup contact and login dropdown elements.

	 */

	function setup_dropdowns() {

		

		contact_height=$('#grid div.contact').height();

		login_height=$('#grid div.login').height();

		

		$('#grid nav li.contact a').click(function() {

			if($('#grid div.contact').is(':hidden')) {

				$(this).addClass('active');

				$('#grid .logo').css({zIndex:10});

				$('#grid div.contact').css({display:'block',height:0}).stop().animate({height:contact_height+'px'},animate_speed,easing_method);

			} else {

				$(this).removeClass('active');

				$('#grid div.contact').stop().animate({height:0},animate_speed,easing_method,function() { $(this).hide(); $('#grid .logo').css({zIndex:55}); });

			}

			return false;

		});

		

		$('#grid nav li.login a').click(function() {

			if($('#grid div.login').is(':hidden')) {

				$(this).addClass('active');

				$('#grid div.login').css({display:'block',height:0}).stop().animate({height:login_height+'px'},animate_speed,easing_method);

			} else {

				$(this).removeClass('active');

				$('#grid div.login').stop().animate({height:0},animate_speed,easing_method,function() { $(this).hide(); });

			}

			return false;

		});

		

		$('#grid div.login form a.submit').click(function() {

			$(this).parents('form').submit();

			return false;

		});

		

	}

	

	// --------------------------------------------- GRID METHODS ------------------------------------------------- //

	

	var grid=null,rows=200,columns=4,max_rows=200,max_items=200;

	var row_height=160,column_width=256;

	var strict=false,overlay=false,building=false,scroll=false,filter=false;

	var grid_full=false,loading_items=false;

	

	/**

	 * SETUP_GRID

	 *

	 * Setup grid and apply event handlers.

	 */

	function setup_grid() {

		

		// grid setup

		set_columns();

		$(window).resize(function() { if(columns!=set_columns()) place_grid_items(true); });

		$('#grid .item').hide();

		

		//djb no scroll

		scroll = 0;

		// scroll loader

		if(scroll) $(window).bind('scroll',scroll_handler);

		

		// item actions

		item_actions();

		

		// place items

		place_grid_items(false);

		

		// check height

		if(scroll) scroll_handler(null);

	

	}

	

	/**

	 * ITEM_ACTIONS

	 *

	 * Apply event handlers to individual grid elements.

	 */

	function item_actions() {

		

		//

		// Caption Element Popup

		$('#grid div.caption:hidden').hover(function() {},function() {

			if(!building) {

				fadein_grid(this);

				$(this).find('.popup').stop().animate({bottom:'-88px'},Math.floor(animate_speed/1),easing_method);

				$(this).find('span.icon').css({zIndex:60});

			}

		});

		$('#grid .caption:hidden span.icon').hover(function() {

			if(!building) {

				fadeout_grid($(this).parents('.item'));

				$(this).parents('.item').find('.popup').stop().animate({bottom:0},animate_speed,easing_method);

				$(this).css({zIndex:40});

			}

		},function() {});

		

		//



		

		

		//

		// Slider Element

		$('#grid .slider:hidden').each(function() {

		

			$(this).data('current',1);

			$(this).data('total',$(this).find('.slide').length);

			

			$(this).find('.slide').css({left:'-2000px',zIndex:1});

			$(this).find('.slide:first').css({left:0,zIndex:10});

			

			$(this).hover(function() {

				$(this).find('.hovercaption').each(function() {

					if($(this).is(":hidden")) {

						$(this).css({display:'block',bottom:'-120px'}).stop().animate({bottom:0},animate_speed,easing_method);

					} else {

						$(this).stop().animate({bottom:0},animate_speed,easing_method);

					}

				});

			},function() {

				$(this).find('.hovercaption').stop().animate({bottom:'-120px'},animate_speed,easing_method, function() { $(this).hide(); });

			});

			

			$(this).find('a.previous').click(function() {

			

				var parent = $(this).parents('.slider');

				var current = $(parent).data('current');

				var total = $(parent).data('total');

				var slides = $(parent).find('.slide');

				var previous = current;

				current--;

				if(current<=0) current = total;

				

				$(slides).css({left:'-2000px',zIndex:1});

				$(slides[previous-1]).css({left:0});

				

				$(slides[previous-1]).stop().animate({left:$(slides[0]).width()+'px'},animate_speed,easing_method);

				$(slides[current-1]).stop().css({left:-$(slides[0]).width()+'px',zIndex:10}).animate({left:0},animate_speed,easing_method);

				

				$(parent).data('current',current);

				

				return false;

			

			});

			

			$(this).find('a.next').click(function() {

			

				var parent = $(this).parents('.slider');

				var current = $(parent).data('current');

				var total = $(parent).data('total');

				var slides = $(parent).find('.slide');

				var previous = current;

				current++;

				if(current>total) current = 1;

				

				$(slides).css({left:'-2000px',zIndex:1});

				$(slides[previous-1]).css({left:0});

				

				$(slides[previous-1]).stop().animate({left:-$(slides[0]).width()+'px'},animate_speed,easing_method);

				$(slides[current-1]).stop().css({left:$(slides[0]).width()+'px',zIndex:10}).animate({left:0},animate_speed,easing_method);

				

				$(parent).data('current',current);

				

				return false;

			

			});

		

		});

	

	}

	

	/**

	 * UPDATE_GRID

	 *

	 * Update grid after logo block slider events.

	 */

	function update_grid(e) {

		

		var values=get_slider_values();

		$.ajax({

			type: "POST",

			url: "/load_more.php",

			data: "photography="+values.photography+"&illustration="+values.illustration+"&user_id="+$('#user_id_text').val(),

			success: function(items) {

				//$('#grid').html('<div class="logo"><img id="fflogo" src="http://89.255.133.179/images/ff-logo-small-trans.png" alt="FolioFox" /><h3>Folio<span class="orange">Fox</span></h3><br /><p>Hover for Options</p><span class="icon"></span><div class="sliders"><div class="group"><span>Photography</span><div class="slider-bar"><div class="slider"></div></div><ul><li>less</li><li class="more">more</li></ul>       </div>		<div class="group"><span>Illustration</span><div class="slider-bar"><div class="slider"></div></div><ul><li>less</li><li class="more">more</li></ul></div></div></div>');

				$('#grid').html('<div class="logo" style="overflow: hidden; height: 330px;">'+$('.logo').html()+'</div>');

				setup_logo_block();

				$('#grid .logo .group:eq(0) .slider').slider('value',values.photography);

				$('#grid .logo .group:eq(1) .slider').slider('value',values.illustration);

				grid_full=false;

				$('footer.bottom').html('');

				$('#grid').append(items);

				$('#grid .item').hide();

				item_actions();

				place_grid_items(false);

				scroll_handler(null);
				
				//do stuff for lightbox
				if(typeof $(".popup-element").colorbox == 'function') 
				{ 
					$(".popup-element").colorbox({transition:"fade", height:"80%"}); 
				}

			}

		 });

		

	}

	

	/**

	 * SCROLL_HANDLER

	 *

	 * Handler browser scroll events, adjust grid accordingly.

	 */

	function scroll_handler(e) {

		if(!grid_full&&!building) {

			var bottom_offset = ($('#grid').height()+$('#grid').height()+$('footer.bottom').height()+25)-($(window).height()+$(window).scrollTop());

			if(debug) trace('bottom offset: '+bottom_offset);

			//if(bottom_offset<=10) {

			if(false) {

				if(debug) trace('bottom offset load triggered');

				//load_more_items();

				if(get_total_items()>=max_items) {

					grid_full=true;
					
					$('#show-more h4').html(($('#show-more h4').html().replace("SHOW MORE", "SHOWING ALL")));
					$('#show-more h4').css("cursor","default");
					//$('footer.bottom').html('<a href="#top" class="button">Back to Top</a>');

				}

			}

		}

	}

	

	/**

	 * LOAD_MORE_ITEMS

	 *

	 * Triggered by scroll handler to load more grid items.

	 */

	function load_more_items() {

	

		if(!grid_full) {

	

			if(get_total_items()>=max_items) {

				grid_full=true;
				$('#show-more h4').html(($('#show-more h4').html().replace("SHOW MORE", "SHOWING ALL")));				
				$('#show-more h4').css("cursor","default");
				$('footer.bottom').html('<a href="#top" class="button">Back to Top</a>');

				return false;

			}

		

			if(!loading_items) {

		

				loading_items=true;

			

				$('footer.bottom').html('<div class="loading">loading items...</div>');

			

				var values=get_slider_values();

				var existing_ids;
				//get id of all images
				$('#grid .photography').each(function(index) {
					existing_ids += $(this).data('id')+"_";
				});
								
				$.ajax({

					type: "POST",

					url: "/load_more.php",

					data: "photography_date="+$('#grid .photography:last').data('date')+"&photography="+values.photography+"&illustration_date="+$('#grid .illustration:last').data('date')+"&illustration="+values.illustration+"&user_id="+$('#user_id_text').val()+"&ids="+existing_ids,

					success: function(items) {

						if(items!=0) {

							$('#grid').find('.item').addClass('existing');

							$('#grid').append(items);

							$('#grid .item').not('.existing').hide();

							$('#grid .existing').removeClass('existing');

							item_actions();

							$('footer.bottom').html('');

							place_grid_items(false);

							scroll_handler(null);
							
							//do stuff for lightbox
							if(typeof $(".popup-element").colorbox == 'function') 
							{ 
								$(".popup-element").colorbox({transition:"fade", height:"80%"}); 
							}

						} else {

							grid_full=true;
							$('#show-more h4').html(($('#show-more h4').html().replace("SHOW MORE", "SHOWING ALL")));
							$('#show-more h4').css("cursor","default");
					
							$('footer.bottom').html('<a href="#top" class="button">Back to Top</a>');

						}

					}

				});

			

			}

		

		}

		

		$(".follow").jScroll({

		speed : "slow",

		top : 100

		});

		

	}

	

	/**

	 * CREATE_GRID_ARRAY

	 *

	 * Two dimensional array grid model creation.

	 */

	function create_grid_array() {

		var array = new Array(rows);

		for(var i=0; i<rows; i++) {

			array[i] = new Array(columns);

			for(var j=0; j<columns; j++) {

				array[i][j]=false;

			}

		}

		return array;

	}

	

	/**

	 * GET_ITEM_SIZE

	 *

	 * Get item size based on CSS styling.

	 */

	function get_item_size(item) {

		if($(item).hasClass('a')) return 'a';

		if($(item).hasClass('b')) return 'b';

		if($(item).hasClass('c')) return 'c';

		if($(item).hasClass('d')) return 'd';

		if($(item).hasClass('e')) return 'e';

		if($(item).hasClass('f')) return 'f';

		return '';

	}

	

	/**

	 * GET_INSERT_LOCATION

	 *

	 * Get next location to insert element. Offset allows to skip openings if desired.

	 */

	function get_insert_location(offset) {

		for(var i=0; i<rows; i++) {

			for(var j=0; j<columns; j++) {

				if(!grid[i][j]) {

					if(offset==0) {

						if(debug) trace('found location ['+i+','+j+']');

						return {row: i,column: j};

					} else {

						if(debug) trace('ignoring location ['+i+','+j+'] decrementing offset');

						offset--;

					}

				}

			}

		}

	}

	

	/**

	 * GET_TOTAL_ROWS

	 *

	 * Get total number of rows in current grid.

	 */

	function get_total_rows() {

		for(var i=0; i<rows; i++) {

			var empty=true;

			for(var j=0; j<columns; j++) {

				if(grid[i][j]) empty=false;

			}

			if(empty) return i;

		}

		return i;

	}

	

	/**

	 * GET_TOTAL_ITEMS

	 *

	 * Get total number of items in current grid.

	 */

	function get_total_items() {

		return $('#grid .item').length;

	}

	

	/**

	 * GET_TOP_POSITION

	 *

	 * Get value for CSS 'top' style for input row.

	 *

	 * @param int $row

	 * @return px

	 */

	function get_top_position(row) {

		return (row*row_height)+'px';

	}

	

	/**

	 * GET_LEFT_POSITION

	 *

	 * Get value for CSS 'left' style for input columns.

	 *

	 * @param int $column

	 * @return px

	 */

	function get_left_position(column) {

		return (column*column_width)+'px';

	}

	

	/**

	 * SET_COLUMNS

	 *

	 * Sets and adjusts the number of columns in the grid for the current

	 * window width. Called from browser resize event handler.

	 */

	function set_columns() {

		var new_columns=Math.floor(($(window).width()-30)/column_width);

		if(debug) trace('calculated columns: '+new_columns);

		if(new_columns>=4&&new_columns!=columns) {

			if(debug) trace('changing columns: '+new_columns);

			columns=new_columns;

			var width=columns*column_width;

			$('#grid').css({width:width+'px'}); // set new grid size

			$('#grid .container').css({width:width+'px'}); // update header position

		}

		return columns;

	}

	

	/**

	 * PLACE_ITEM

	 *

	 * Place an item in the current grid at the input position.

	 *

	 * @param obj item

	 * @param int row

	 * @param int column

	 * @param boolean animate

	 */

	function place_item(item,row,column,animate) {

		var size=get_item_size(item);

		var location={top:get_top_position(row),left:get_left_position(column)};

		switch(size)

		{

		

			case 'a': // 1x1

				if( grid[row][column] ) return false; // space clear?

				grid[row][column] = 'a1';

				break;

			

			case 'b': // 1x2

				if( grid[row][column] || grid[row+1][column] ) return false; // space clear?

				if( strict && (

				

					( (column-1)>=0 && grid[row][column-1]=='b1' && grid[row+1][column-1]=='b2' ) // size b to left

					||

					( (column+1)<columns && grid[row][column+1]=='b1' && grid[row+1][column+1]=='b2' ) // size b to right

					||

					( (column-1)>=0 && grid[row][column-1]=='d1' && grid[row+1][column-1]=='d3' ) // size d to left

					||

					( (column+1)<columns && grid[row][column+1]=='d1' && grid[row+1][column+1]=='d3' ) // size d to right

					

				)) return false; // strict neighbor rules

				grid[row][column]	='b1';

				grid[row+1][column]	='b2';

				break;

			

			case 'c': // 2x1

				if( (column+1)>=columns || grid[row][column] || grid[row][column+1] ) return false; // space clear?

				if( strict && (

					

					( (row-1)>=0 && grid[row-1][column]=='c1' && grid[row-1][column+1]=='c2' ) // size c above

					||

					( (row+1)<max_rows && grid[row+1][column]=='c1' && grid[row+1][column+1]=='c2' ) // size c below

					||

					( (row-1)>=0 && grid[row-1][column]=='d3' && grid[row-1][column+1]=='d4' ) // size d above

					||

					( (row+1)<max_rows && grid[row+1][column]=='d1' && grid[row+1][column+1]=='d2' ) // size d below

					

				)) return false; // strict neighbor rules

				grid[row][column]	='c1';

				grid[row][column+1]	='c2';

				break;

				

			case 'd': // 2x2

				if( (column+1)>=columns || grid[row][column] || grid[row][column+1] || grid[row+1][column] || grid[row+1][column+1] ) return false; // space clear?

				if( strict && (

					

					( (row-1)>=0 && grid[row-1][column]=='c1' && grid[row-1][column+1]=='c2' ) // size c above

					||

					( (row+1)<max_rows && grid[row+1][column]=='c1' && grid[row+1][column+1]=='c2' ) // size c below

					||

					( (row-1)>=0 && grid[row-1][column]=='d3' && grid[row-1][column+1]=='d4' ) // size d above

					||

					( (row+1)<max_rows && grid[row+1][column]=='d1' && grid[row+1][column+1]=='d2' ) // size d below

					||

					( (column-1)>=0 && grid[row][column-1]=='b1' && grid[row+1][column-1]=='b2' ) // size b to left

					||

					( (column+1)<columns && grid[row][column+1]=='b1' && grid[row+1][column+1]=='b2' ) // size b to right

					||

					( (column-1)>=0 && grid[row][column-1]=='d1' && grid[row+1][column-1]=='d3' ) // size d to left

					||

					( (column+1)<columns && grid[row][column+1]=='d1' && grid[row+1][column+1]=='d3' ) // size d to right

					

				)) return false; // strict neighbor rules

				grid[row][column]		='d1';

				grid[row][column+1]		='d2';

				grid[row+1][column]		='d3';

				grid[row+1][column+1]	='d4';

				break;

			

			case 'e': // 2x3

				if( (column+1)>=columns || grid[row][column] || grid[row][column+1] || grid[row+1][column] || grid[row+1][column+1] || grid[row+2][column] || grid[row+2][column+1] ) return false; // space clear?

				grid[row][column]		='e1';

				grid[row][column+1]		='e2';

				grid[row+1][column]		='e3';

				grid[row+1][column+1]	='e4';

				grid[row+2][column]		='e5';

				grid[row+2][column+1]	='e6';

				break;

			

			case 'f': // 3x3

				if( (column+2)>=columns || grid[row][column] || grid[row][column+1]|| grid[row][column+2] || grid[row+1][column] || grid[row+1][column+1] || grid[row+1][column+2] || grid[row+2][column] || grid[row+2][column+1] || grid[row+2][column+2] ) return false; // space clear?

				grid[row][column]		='f1';

				grid[row][column+1]		='f2';

				grid[row][column+2]		='f3';

				grid[row+1][column]		='f4';

				grid[row+1][column+1]	='f5';

				grid[row+1][column+2]	='f6';

				grid[row+2][column]		='f7';

				grid[row+2][column+1]	='f8';

				grid[row+2][column+2]	='f9';

				break;

			

			default:

				return false;

				

		}

		if(animate) {

			$(item).show().stop().animate({top:location.top,left:location.left},animate_speed,easing_method,function() { building=false; });

		} else {

			$(item).show().css({top:location.top,left:location.left}); building=false;

		}

		return true;

	}

	

	/**

	 * PLACE_GRID_ITEMS

	 *

	 * Place all items in current grid array into display grid.

	 *

	 * @param boolean animate

	 */

	function place_grid_items(animate) {

	

		if(debug) trace('placing grid items');

	

		building=true;

	

		// hide items

		$('#grid .item').hide();

		loading_items=false;

	

		// create grid

		grid=create_grid_array();

		

		// fill 0,0 (logo)

		grid[0][0]=1;

		if($('#grid div.logo').hasClass('talllogo')) { grid[1][0]=1; grid[2][0]=1; }

		

		// loop and place items

		var items=$('#grid .item');

		var placed=false;

		var insert=null;

		for(var i=0; i<items.length; i++) {

			placed=false;

			for(var j=0; !placed; j++) {

				insert=get_insert_location(j);

				if(insert.row >= max_rows) break;

				placed=place_item(items[i],insert.row,insert.column,animate);

				if(placed) {

					if(debug) trace('item #'+i+' placed at ['+insert.row+','+insert.column+']');

				} else {

					if(debug) trace('item #'+i+' NOT placed at ['+insert.row+','+insert.column+']');

				}

			}

		}

				

		// update grid height

		$('#grid').css({height:(get_total_rows()*row_height)+'px'});

		$('#grid').css({"max-height":((get_total_rows()*row_height)-166)+'px'});

		if(debug) trace('new grid height: '+$('#grid').height()+'px');

		

		var s = window.location.pathname;

		//console.log(s);

		//profile

		if(s.indexOf("profile/") != -1)

			$('#grid').css({"max-height":((get_total_rows()*row_height)-325)+'px'});

		else if(s=="/") //index

			$('#grid').css({"max-height":((get_total_rows()*row_height)-166)+'px'});

		else

			$('#grid').css({"max-height":((get_total_rows()*row_height)-326)+'px'});

		if(debug) trace('new grid height: '+$('#grid').height()+'px');

				

	}

	

	/**

	 * FADEOUT_GRID

	 *

	 * Fadeout grid elements NOT e.

	 *

	 * @param object e

	 */

	function fadeout_grid(e) {
		

		if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {

		$(e).css({zIndex:2000});

		if(!$('#grid .overlay').is(':visible')) {

			$('#grid').append('<div class="overlay"></div>');

			$('#grid .overlay').css({opacity:0}).stop().animate({opacity:0.9},Math.floor(animate_speed*1),easing_method);

		}
		}

	}

	

	/**

	 * FADEIN_GRID

	 *

	 * Fade in grid elements NOT e.

	 *

	 * @param object e

	 */

	function fadein_grid(e) {
	
		if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			$('#grid .overlay').stop().animate({opacity:0},animate_speed,easing_method, function() { $(e).css({zIndex:'auto'}); $(this).remove(); });
		}

	}

	

	/**

	 * CLEAR_ERRORS

	 *

	 * Clear errors from form.

	 *

	 * @param object form

	 */

	function clear_errors(form) {

		$(form).find('.error').removeClass('error').unwrap().next('span.error').remove();

	}

	

	/**

	 * SET_ERROR

	 *

	 * Set a field as an error field.

	 *

	 * @param object field

	 */

	function set_error(field) {

		if($(field).is(':visible'))

			$(field).addClass('error').wrap('<div class="field" />').after('<span class="error" />').next('span.error').css({right: '50px'}).animate({right: '-32px'},Math.floor(animate_speed/2));

	}

	

	/**

	 * CLEAR_FORM

	 *

	 * Clear form data from input form.

	 *

	 * @param object form

	 */

	function clear_form(form) {

		clear_errors(form);

		$(form).find('input:visible').val('');

		$(form).find('textarea:visible').val('');

	}

	

	/**

	 * CHECK_COMMENTS_HEIGHT

	 *

	 * Set overflow on comment block for restricted height.

	 *

	 * @param object comments

	 */

	function check_comments_height(comments) {

	

		if($(comments).height()>700) {

	

			$(comments).find('ul').css({position:'absolute',top:0,left:0,margin:0}).wrap('<div class="ulscrolling" />');

			$(comments).find('div.ulscrolling').append('<div class="ulscrollbar"></div>');

			

			//

			// Set Up Scrollbar

			$(comments).find('div.ulscrollbar').slider({

				orientation: "vertical",

				min: 0,

				max: 220,

				value: 220,

				change: scroll_handler,

				slide: scroll_handler

			});

			

			//

			// Scroll Handler

			function scroll_handler( event, ui ) {

				var percentage = (220-ui.value)/220;

				var body_height = $(comments).find('ul').height();

				var position = -((body_height-300)*percentage);

				$(comments).find('ul').css({top: position+'px'});

			}

			

			//

			// Capture Mousewheel

			$(comments).find('ul').mousewheel(function(event, delta) {

				var dir = delta > 0 ? 'up' : 'down';

				var scrollbar = $(event.target).parents('div.comments').find('div.ulscrollbar');

				var value = $(scrollbar).slider("value");

				if(dir=='up') {

					value += 15;

					$(scrollbar).slider("value", value);

				} else {

					value -= 15;

					$(scrollbar).slider("value", value);

				}

				return false;

			});

		

		}

		

	}
