$(function() {
	window.setInterval(function() {
		if(document.title == '' || document.title == null || document.title == undefined) {
			document.title = $config.title;
		}
		if(document.title.indexOf("#") > -1){
		   document.title = document.title.substring(0, document.title.indexOf("#"));
		}
	}, 250);
	
	$('.tooltip').each(function(){
		 $(this).after($('<span/>')
		.attr('class', 'show-tooltip-text')
		.html($(this).attr('title')))
		.attr('title', '');
	});


	
	$('#videos').videos({
		'videosPerPage': 8,
		'channelPrefix': $config.prefix+' - '
	});
	
	$('a.comments').live('click', function() {
		$('#videos:first').showComments();
		return false;
	});

	$('a.post-comment').live('click', function() {
		var $form = $(this).parents('form:first');

		var $email = $("input[name='email']", $form);

		var $re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$('#comments-form .erroremail').remove();
		if (!$re.test($email.val())) {
			$email.addClass('error');
			$email.focus();
			$('#comments-form form').before('<p class="erroremail error">A valid email address is required, please correct this and submit again.</p>');
			return false;
		}

		$form.submit();
		return false;
	});
	
	$('a.page, a.tag, a.channel, a.video, #intro a.button, a.home').live('click', function() {
		var $filters = {
			channel: null,
			tag: null,
			page: 1,
			video: null
		};

		var $hash = $(this).attr('href').split('#');
		$hash = $hash[1];

		var $filter = $hash.split('/');
	
		for(var $i=0; $i<$filter.length; $i++) {
			if(($i % 2 == 0) && $filters[$filter[$i]] != 'undefined' && $filters[$filter[$i+1]] != 'undefined') {
				$filters[$filter[$i]] = $filter[$i+1];
			}
		}
		
		$('a.page, a.tag, a.channel').removeClass('selected');
		
		$('#videos:first').getVideos($filters);
		
		if($(this).attr('class') != 'button'){
			$(this).addClass('selected');
		}
		
		if($filters.channel != null) {
			$("a.channel[href$='"+$filters.channel+"']").addClass('selected');
			document.title = $config.pageTitle + $filters.channel + " ";
			$config.title = $config.pageTitle + $filters.channel + " ";
			pageTracker._trackPageview(document.location.href);
		} else {
			$("a.channel[href$='home']").addClass('selected');
		}
		
		if($filters.tag != null) {
			$("a.tag[href$='"+$filters.tag+"']").addClass('selected');
		}
		
		window.location.hash = $hash;
		
		return false;
	});
});