var animation_speed = 1000;
var border_reset = 'dashed 1px #B1B1B1';

function form_error(text,element)
{
	element.css('border','solid 1px #B1B1B1').parent('div').prev('label').css('color','#FF0000');
	return '<div class="form_error">'+text+'</div>';
}

function check_rules($element,regex,msg)
{
	if(!$element.val().length) {
		$element.after(form_error('Must not be left blank',$element))
	}else{
		if(!$element.val().match(regex))
			$element.after(form_error(msg,$element));
	}
}
function hide_menu()
{
	$('#menu').slideUp(1000);
}
function show_menu()
{
	$('#menu').slideDown(1000);
}

jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

$(function(){
	$('#login_event, #register_event').click(function(){
		$('#login_form, #register_form').find('label').css('color','#000');
		$('.form_error').remove();
		hide_menu();
		return false;
	});
	$('.prompt input').bind('click',function(){
		$(this).css('border',border_reset);
		$(this).next('div.form_error').fadeOut(1000,function(){$(this).remove();});
		$(this).parent('div').prev('label').css('color','#000000');
	});
	$('#login_event').click(function(){
		$('#content').fadeOut(animation_speed).slideUp(animation_speed);
		$('#register_form').fadeOut(animation_speed+100).slideUp(animation_speed,function(){
			$('#login_form').fadeIn(animation_speed).slideDown(animation_speed).find('input[type="text"], input[type="password"]').val('').css('border',border_reset);
			$('#l_username').focus();			
		});
		return false;
	});
	$('#register_event').click(function(){
		$('#content').fadeOut(animation_speed).slideUp(animation_speed);
		$('#login_form').fadeOut(animation_speed+100).slideUp(animation_speed,function(){
			$('#register_form').fadeIn(animation_speed).slideDown(animation_speed).find('input[type="text"], input[type="password"]').val('').css('border',border_reset);
			$('#r_username').focus();
		});
		return false;
	});
	$('.close_form a').click(function(){
		$('.form_error').remove();
		$('#'+$(this).attr('rel')).find('input[type="text"], input[type="password"]').val('').css('border',border_reset);
		$('#'+$(this).attr('rel')).fadeOut(animation_speed).slideUp(animation_speed);
		$('#content').slideDown(animation_speed).fadeIn(animation_speed);
		$('#'+$(this).attr('rel'));
		show_menu();
		return false;
	});
	$('#category_hover,#linkscat_hover,#home_hover').hover(function(){
		//over
		$(this).css('z-index','100');
		$(this).find('ul').show().css('z-index','50');
		$(this).find('.hover').addClass('selected');
	}, function(){
		$(this).css('z-index','10');
		$(this).find('ul').hide().css('z-index','1');
		$(this).find('.hover').removeClass('selected');
	});
	$('.backlink').click(function(){
		var url = window.location.href;
		window.location.href = url.replace($(this).attr('rel'),'');
		return false;
	});
	$('.post .pcontent').each(function(){
		$(this).find('p:first').each(function(){
			var first = $(this).text().charAt(0);
			var body = $(this).text().substring(1, $(this).text().length);
			$(this).html('<span class="start">'+first+'</span>'+body);
		});
	});
	
	$('#menu a').live('click',function(){
		$('#category_hover,#linkscat_hover,#home_hover').trigger('mouseout');
		if(!$(this).attr('rel').match('(code|jobs)'))
		{
			var href = $(this).attr('href');
			$('#content')
				.css('visibility','hidden')
				.slideUp('normal', function(){
					$(this)
						.html('')
						.load(href)
						.slideDown('slow',function(){
							$(this).css('visibility','visible');
						});
				});
			return false;
		}else{
			$(this).attr('target','_blank');
		}
	});
	$('.pagination a').live('click',function(){
		$('#content')
			.load($(this).attr('href'),function(){
				$('body').scrollTo(1000);
		})
		return false;
	});
});