jQuery(document).ready(function($){
	
	function setFieldBehaviour(obj, val) {
		if (!val) val = obj.val();
		if (obj.val() == '') obj.val(val);
		obj.focus(function(){
			$(this).removeClass('valid');
			$(this).removeClass('error');
			if ($(this).val() == val) $(this).val('');
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).val(val);
				$(this).removeClass('prefilled');
			}
		});
		obj.parents('form').submit(function(){
			if (obj.val() == val) obj.val('');
		});
	}
	setFieldBehaviour($('input#s'), $('input#searchsubmit').val());
	
	$('#slideshow').cycle({ 
		cleartypeNoBg: true,
		slideExpr: '.slide',
		prev: '#slideshow a.prev',
		next: '#slideshow a.next',
		timeout: '10000',
		pagerAnchorBuilder: function(index, slide){ 
			return '#slideshow .thumbs li.thumb:eq(' + index + ') a'; 
		},
		pauseOnPagerHover: true,
		activePagerClass: 'active',
		fastOnEvent: 500,
		before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			var index = $('#slideshow .slide').index(nextSlideElement);
			$('#slideshow li.thumb').removeClass('active');
			$('#slideshow li.thumb:eq('+index+')').addClass('active');
		}
	});
	
	$('#slideshow a.ctrl, #slideshow li.thumb a').click(function(){
		$('#slideshow').cycle('pause');
	});
	
	var prev = $('#slideshow a.thumb-ctrl.prev');
	var next = $('#slideshow a.thumb-ctrl.next');
	var thumbs = $('#slideshow li.thumb');
	var total = thumbs.length;
	var row = 1;
	var totalrows = Math.ceil(thumbs.length/7);

	prev.addClass('disabled');
	prev.css('opacity', 0.25);
	if (row == totalrows) {
		next.addClass('disabled');
		next.css('opacity', 0.25);
	}
	
	prev.click(function(){
		if(row > 1) {
			var offset = 574;
			if (row == totalrows && total%7 > 0) {
				offset = total%7 * 82;
			}
			thumbs.each(function(i){
				$(this).animate({'left': '+='+offset}, {'duration': 500, 'queue': false});
			});
			row--;
			if (row == 1) prev.addClass('disabled').fadeTo(250, 0.25);
			if (row < totalrows) next.removeClass('disabled').fadeTo(250, 1);
		}
		return false;
	});
	next.click(function(){
		if(row < totalrows) {
			var offset = 574;
			if (row == totalrows-1 && total%7 > 0) {
				offset = total%7 * 82;
			}
			thumbs.each(function(i){
				$(this).animate({'left': '-='+offset}, {'duration': 500, 'queue': false});
			});
			row++;
			if (row == totalrows) next.addClass('disabled').fadeTo(250, 0.25);
			if (row > 1) prev.removeClass('disabled').fadeTo(250, 1);
		}
		return false;
	});
	
	$('.post.video img').after('<span class="overlay"></span>');
	
	
});
