// Global variables

var post_index;
var show_index;
var widgets_open=false;

// Helper functions


function open_showcase(show_object) {

	show_object.height($('#content-'+post_index).height());
	show_object.slideDown(400,function() {
		$('#pad').slideUp(400);	
		$(this).append(the_content).children().hide().fadeIn(600, function() {
			//j_fade_init();
			//$.scrollTo(this_post, 1000, {easing:'easeInOutQuart', offset:0} );
		});
		show_index=show_object.attr("id").replace("show-","");
		});
}

function close_showcase(show_object, call_object) {
	show_object.children().fadeOut(400, function() {
			$('#pad').height(show_object.height()).slideDown(400,function () {$(this).slideUp(2000)});
			show_object.slideUp(400, function() {
				$(this).css('display','none').empty();
				if (call_object) open_showcase(call_object);
				});						
			});
}

// Beem JS by Johannes Kollender

jQuery(window).load(function(){

  // Lazy Load images below the fold
  $(".content img.thumbnail").lazyload();

  // The magic sliding panels
	$('.entry-content span.slide-title').css({
		opacity : '0.0'
	});
	$('.entry-content').mouseover(function(e){
      $(this).find('div.thumbnail-container').stop().animate({
	  	marginTop : '-24px'
	  }, 100).parent('div').find('span.slide-title').stop().fadeTo("slow",1.0);
	});
	$('.entry-content').mouseout(function(e){
      $(this).find('div.thumbnail-container').stop().animate({
	  	marginTop : '0'
	  }, 100).parent('div').find('span.slide-title').stop().fadeTo("slow",0.0);
	});

	// Preping
	
	$('div.showcase').hide();
	$('#gimikk_msg').hide();
	$('#pad').hide();

	$('#wrapper').hide();
	$('#wrapper').fadeIn(400);
	
	$('#widget-area').width($('#widget-area').children().length*235);
	
	/*$('#widget-area').height($('#widget-area').height());
	$('#widget-area').width($('#widget-area').width());
	$('#widget-wrapper').hide();*/
	
	$('.entry-content').click(function () {
		this_post = $(this);
		old_index=post_index; // Previous index
		post_index = this_post.attr("id").replace("post-",""); // Current index
		//console.log($('#content-'+post_index).outerHeight(false));
		the_content = $('#content-'+post_index).clone(true).children();
		showcase = this_post.nextUntil('div.showcase').next('div.showcase');
		
		if (old_index!=post_index) {
			$('#thumb-'+old_index).animate({opacity: 1},600);						
			$('#thumb-'+post_index).animate({opacity: .5},600);
			
			if (show_index) close_showcase($('#show-'+show_index),showcase);
			else open_showcase(showcase);
		}
		
		else {
			$('#thumb-'+post_index).animate({opacity: 1},600);
			close_showcase(showcase);
			post_index=null;
			show_index=null;
		}
		
	});
	
	
// jFade for the links

$(".jfade a").jFade({
		trigger: "mouseover",
		property: 'background',
		start: '0F0F0F',
		end: 'F9DA3B',
		steps: 25,
		duration: 5
	}).jFade({
		trigger: "mouseout",
		property: 'background',
		start: 'F9DA3B',
		end: '0F0F0F',
		steps: 25,
		duration: 5
	});
	
	
	// Widget Area
	
	/*
$('body').click(function () {
if ($('#widget-wrapper').hasClass('open')) {
		$('#widget-wrapper').removeClass('open');
		$('#widget-area').fadeOut(400, function() {
			$('#widget-wrapper').slideUp(400);
			});
}
});*/
	
$('#widget-button').click(function () {
	if (widgets_open) {
		widgets_open=false;
		$('#widget-area > li').fadeOut(400, function() {
			$('#widget-wrapper').slideUp(400);
			});
		}
	else {
		widgets_open=true;
		$('#widget-area > li').hide();
		$('#widget-wrapper').slideDown(400, function() {
			for(i=1;i<=$("#widget-area > li").length; i++){
				setTimeout("$('#widget-area > li:nth-child("+i+")').fadeIn(400);", 200*i);
				}
			});	
		}
});
	
});
