
$(function(){

	// Various form elements
	$('#login_form input, #thread_form input, #thread_form textarea, #post_blog input, #keywords, #member_srch, #vids_srch').each(function(){
		var title = $(this).attr('title');

		if( $(this).val() == '' || $(this).val() == title ){
			$(this).val(title).addClass('dim');
		}
		$(this).focus(function(){
			if( $(this).hasClass('dim') ){
				$(this).val('').removeClass('dim');
			}
		});
		$(this).blur(function(){
			if( $(this).val() == '' || $(this).val() == title ){
				$(this).val(title).addClass('dim');
			}
		});
	});//*/
	/* My version of the above function, superseded by Fred's: 
	$('.autofill').each(function() {
		var d = $(this).attr('title');
		var v = $(this).val();
		if (!v) {
			$(this).val(d);
		}
		$(this).focus(function() {
			var d = $(this).attr('title');
			var v = $(this).val();
			if (v == d) {
				$(this).val('');
			}
		});
		$(this).blur(function() {
			var d = $(this).attr('title');
			var v = $(this).val();
			if (!v) {
				$(this).val(d);
			}
		});
	});
	*/
	
	// scroll panel
	$('.pnl_scroll .prev').click(function(){
		var pnl = $(this).closest('.pnl_scroll').find('.scroll');
		var currentPos = pnl.position().left;
		var newPos = parseInt(currentPos + 508);
		//alert("Current: " + currentPos + "\nNew: " + newPos);
		if( newPos <= 0 ){
			pnl.animate({left: newPos + "px"});
		}
		return false;
	});
	$('.pnl_scroll .next').click(function(){
		var pnl = $(this).closest('.pnl_scroll').find('.scroll');
		var currentPos = pnl.position().left;
		var newPos = parseInt(currentPos - 508);
		var width = pnl.width();
		//alert("Current: " + currentPos + "\nNew: " + newPos + "\nWidth: " + width);
		if( newPos >= (2 - width) ){
			pnl.animate({left: newPos + "px"});
		}
		return false;
	});
	
	var count = 0;
	var numImgs = $('.pnl_scroll img').length;
	$('.pnl_scroll img').load(function(){
		count++;
		//console.log("Loading image " + count + ": " + $(this).attr('src'));
		if( count == numImgs ){
			$('.pnl_scroll').each(function(){
				var height = 0;
				var items = $(this).find('.item');
				items.each(function(){
					if( $(this).height() > height ) height = $(this).height();
				});
				items.height(height);
			});
		}
	
	});
	
	$('.scroll .open_item').PHPLightBox({extraClose:'.vidclose'});
	
	
	// calendar
	
	/* Suspended: I'll need to rewrite this as proper CI code, I think?
	
	function getEvents(currentMonth, direction){
		$.post('get_events.php',{month: currentMonth, direction:direction}, function(html){
			$('#events').html(html);
			var newMonth = {name: $('#thismonth').text(), rel: $('#thismonth').attr('rel') };
			if( newMonth.name ){
				$('.curMonth').attr('rel', newMonth.rel ).html( newMonth.name );
				$('#thismonth').remove();
			}
		});
	}
	$('#cal_next').click(function(){
		var currentMonth = $('.curMonth').attr('rel');
		getEvents(currentMonth, 'next');
		return false;
	});
	$('#cal_prev').click(function(){
		var currentMonth = $('.curMonth').attr('rel');
		getEvents(currentMonth, 'prev');
		return false;
	});
	
	*/
	
	// Events categories
	$('#cal_cats ul').hide();
	$('#cal_cats h3').css({cursor:"pointer"}).click(function(){
		$(this).next('ul').slideToggle();
	});
	
	
});