var pg;
var pgi;
var pgc;
var c = 0;
var t;
var pc;
var pcCt = 0;

$(document).ready(function() {	
	
	$('#slider').cycle({ 
	    fx: 'fade', 
        speed:  'slow',
        timeout: 8000,
        pager:  '#slider #nav',
		slideExpr: 'article'
	});
	
	$('#content .posts article:first').css({borderTop: 0});
	$('#content .posts article:last').css({borderBottom: 0});
	
	pg = $('.post-gallery');
	pgi = $('.post-gallery img');
	pgc = $('.post-gallery-caption p');
	pgic = $(pgi).length;
	pgcon = '<div class="post-gallery-controls"><div class="nav previous-image"><img src="'+templateURL+'/images/previous-image.gif" /> Previous Image</div><div class="nav next-image">Next Image <img src="'+templateURL+'/images/next-image.gif" /></div><div class="count"><span>1</span> of '+pgic+'</div></div><div class="clear"></div>';
	$(pgi).css({display: 'none'});
	$(pgi).eq(c).show().addClass('shown');
	$(pgc).eq(c).show().addClass('shown');
	$(pg).after(pgcon);		
	countWidth = $('.count').width();
	$('.count').css({width: (countWidth+10)+'px', float: 'none'});
	$('.next-image').click(function() {
		if (c != (pgic-1)) {
			c++;
		} else {
			c=0
		}
		loadImage(c);
		$('.count span').text(c+1);
	});
	$('.previous-image').click(function() {
		if (c != 0) {
			c--;
		} else {
			c=(pgic-1)
		}
		loadImage(c);
		$('.count span').text(c+1);
	});
	
	t = setTimeout(function () { adjustColumns(); }, 1000);
	
	$('#fotobook-photos-widget .thumbnail').hide();
	$('#fotobook-photos-widget .thumbnail img').each(function(i) {
		$(this).ready(function() {
			imgW = $(this).width();
			imgH = $(this).height();
			pt = setTimeout(function() { adjustPhoto(i, imgW, imgH) }, 1000);
		});
	});
	$('#fotobook-photos-widget .thumbnail').eq(pcCt).show();
	pc = $('#fotobook-photos-widget .thumbnail').length;
	rpt = setTimeout(function () { rotatePhotos(); }, 5000);
	
	  $("label").inFieldLabels();
});

function loadImage(i) {
	$('.shown').fadeOut(function() {
		$(pgi).eq(i).fadeIn().addClass('shown');
		$(pgc).eq(i).fadeIn().addClass('shown');
	}).removeClass('shown');
}
function adjustColumns() {
	columnLeftH = $('.column-left').height();
	categoryPostsH = $('#category-posts').height();
	singlePostH = $('#single-posts').height();
	sidebarH = $('#sidebar').height();
	if (columnLeftH < sidebarH) {
		diff = (sidebarH - columnLeftH) + 'px';
		$('#secondary-wrapper').css({paddingBottom: diff});
	} else if (columnLeftH > sidebarH) {
		$('#sidebar').height(columnLeftH);
	}
	if (categoryPostsH < sidebarH) {
		$('#category-posts').height(sidebarH);
	} else if (categoryPostsH > sidebarH) {
		$('#sidebar').height(categoryPostsH);
	}
	if (singlePostH < sidebarH) {
		$('#single-posts').height(sidebarH);
	} else if (singlePostH > sidebarH) {
		$('#sidebar').height(singlePostH);
	}
}
function adjustPhoto(t, w, h) {
	if (w > h) {
		$('#fotobook-photos-widget .thumbnail img').eq(t).height(125).css({marginLeft: '-'+(h/2)+'px', marginTop: '-'+(w/2)+'px'});
	} else {
		$('#fotobook-photos-widget .thumbnail img').eq(t).width(195).css({marginLeft: '-'+(h/2)+'px', marginTop: '-'+(w/2)+'px'});
	}
}
function rotatePhotos() {
	 $('#fotobook-photos-widget .thumbnail').eq(pcCt).fadeOut();
	 pcCt++;
	 if (pcCt > (pc-1)) {
	 	pcCt = 0;
	 }
	 $('#fotobook-photos-widget .thumbnail').eq(pcCt).fadeIn();
	 rpt = setTimeout(function () { rotatePhotos(); }, 5000);
}
