jQuery.fx.interval = 35;
var trans = "easeOutQuint";
var day;
var old_day;
var about_height;
var img_margin;
var img_width = 0;
var img_height = 0;

$(function() {
	daze_init();
});

function daze_init() {
	posts();
	about_panel();
	tumblr_controls();
	$(window).scroll(posts);
	$(window).resize(tumblr_controls);
}

function posts() {
	$('.og .post-holder').each(function() {
		var post_height = $(this).height();
		if($(this).parent('div').parent('div').hasClass('photo')) {
			
				$("img",this).load(function() {
						
					post_height = $(this).parent('div').height();
					img_height = $(this).outerHeight();
					img_width = $(this).outerWidth();
					if(img_height == 0) {
						photos();
						posts();
						return false;
					}
					$(this).parent("div").width(img_width);
					$(this).height(img_height);
					img_margin = -((img_height - 80) / 2);
					
					$(this).css('marginTop',img_margin);
					$(this).parent('div').parent('div').animate({
						opacity:1
					},{
						duration:800,
						easing:trans,
						queue:false
					});
				});
			
		}
		$(this).click(function() {
			
			$('.post-holder.on').parent('div').animate({
				height:80
			},{
				duration:800,
				easing:trans,
				queue:false
			});
 			img_margin_reset($('.post-holder.on img')); 
			if(!$(this).hasClass('on')) {
			$('.post-holder.on').removeClass('on');
			}
			
			if(!$(this).hasClass('on')) {
			$('.post-holder.on').removeClass('on');
			}

			//post_height = $(this).height();
			
			$(this).toggleClass('on');
			if($(this).hasClass('on')) {
				img_margin_expand($("img",this)); 
				$(this).parent('div').animate({
					height:post_height
				},{
					duration:800,
					easing:trans,
					queue:false
				});
			}
			else {
			img_margin_reset($("img",this));
				$(this).parent('div').animate({
					height:80
				},{
					duration:800,
					easing:trans,
					queue:false
				});

			}

		});
		$(this).parent('div').removeClass('og');
	});
	audio_player();
	dates();
	$(".og").removeClass('og');
/*
	$('.single .post-holder').each(function() {
		photos();
		if($(this).parent('div').parent('div').hasClass('photo')) {			
			$("img",this).load(function() {
				img_width = $(this).outerWidth();
				$(this).parent("div").width(img_width);
				if(img_width == 0) {
					photos();
					posts();
					return false;
				}
			});
		}
	});
*/
}

function photos() {
	$('.photo img').each(function() {
		var d = new Date();
		var src = $(this).attr('src');
		$(this).attr('src', src+'?d='+d.getTime());
	});
}



function img_margin_expand(img) {
	img.animate({
		marginTop:0
	},{
		duration:800,
		easing:trans,
		queue:false
	});
}

function img_margin_reset(img) {
	var img_height = img.outerHeight();
	img_margin = -((img_height - 80) / 2);
	img.animate({
		marginTop:img_margin
	},{
		duration:800,
		easing:trans,
		queue:false
	});
}

function dates() {
	$('.og .post-meta').hide();
	$('.og .post-meta').css('opacity',1);
	var dates = $('.date_num');
	var days = $('.day');
	$('.post-meta').each(function(index) {
		var date_str = $(dates[index]).html();

		var date_arr = date_str.split(' ');
		var m = date_arr[1];
		var d = date_arr[0];
		var y = date_arr[2];
		
	
		//Set the two dates
		var berlin =new Date(2011, 6, 05) //Month is 0-11 in JavaScript
		today=new Date(y, m, d);
		//Get 1 day in milliseconds
		var one_day=1000*60*60*24
		
		//Calculate difference btw the two dates, and convert to days
		old_day = day;
		day = Math.ceil((today.getTime()-berlin.getTime())/(one_day));
		$(days[index]).html(day);
		$(days[index]).css('display','block');
		$(dates[index]).css('display','none');
		if(old_day == day) {
			$(this).addClass('repeat');
		}
		$(this).fadeIn();


	});
}


function audio_player() {
	$('.og .audio_player embed').attr('wmode','opaque');
	$('.og .audio_player').each(function() {
		var html = $(this).html();
		$(this).html(html);
	});
}

function about_panel() {
	about_height = $('#about_panel').outerHeight();
	$('#about_panel').css('bottom',-about_height);
	
	$('#about span').click(function(el) {
		el.preventDefault();
		$('#about_panel').toggleClass('open');
		if($('#about_panel').hasClass('open')) {
			open_about();
		}
		else {
			close_about();
		}
	});
	$('#about_panel .close').click(function(el) {
		el.preventDefault();
		$('#about_panel').toggleClass('open');
		close_about();
	});
}

function tumblr_controls() {
	var window_w = $(window).width();
	var margin = (window_w - 960) / 2;
	$('#tumblr_controls').css('right',margin);
}


function open_about() {
	$('#about_panel').animate({
	bottom:0
	},{
	duration:800,
	easing:trans,
	queue:false
	});
	$('#footer').animate({
	bottom:about_height
	},{
	duration:800,
	easing:trans,
	queue:false
	});
	$('body').animate({
	paddingBottom:about_height
	},{
	duration:800,
	easing:trans,
	queue:false
	});
}

function close_about() {
	$('#about_panel').animate({
	bottom:-about_height
	},{
	duration:800,
	easing:trans,
	queue:false
	});
	$('#footer').animate({
	bottom:0
	},{
	duration:800,
	easing:trans,
	queue:false
	});
	$('body').animate({
	paddingBottom:0
	},{
	duration:800,
	easing:trans,
	queue:false
	});
}

