$(document).ready(function(){

	// 
	$(window).bind('scroll', function(){
		$(".post").each(function(i) {
			var calculateZone = ( $(this).position().top-($(window).height()/4)) - $(window).scrollTop();
			if(calculateZone <= 1){
				$(this).addClass('hot-seat');
			}else{
				$(this).removeClass('hot-seat');
			}
		});
	});
	
	$('.slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed:  2500,
		pause:  1 
	});
	
	$('#sigurros-heima-trailer-slidewhow img').removeClass('hide');
	$('#sigurros-heima-trailer-slidewhow').cycle({ 
	    fx:    'fade', 
	    speed:  2500 
	 });
	
	$('#londonink-slidewhow img').removeClass('hide');
	$('#londonink-slidewhow').cycle({ 
	    fx:    'fade', 
	    speed:  2500 
	 });
	
	/*
	$("#sidebar ul li ul, #sidebar ul li div").hide();
	$("#sidebar h2").click(function(event){
		event.preventDefault();
		$(this).toggleClass("active");	
		$(this).next('ul, div').slideToggle('normal');
	});
	*/
	
	//
	//if(!$("body").hasClass('page'){
		//alert('h');
		applyExpandAction();	
	//}else{
		//alert('n');
	//}
	
	if( $('body').hasClass('page-id-382') ){
		$('#n_about').addClass('active');
	}
	
	if( $("body").hasClass('page') ){
		//alert($("body").hasClass('page'));
		//alert('not this');
	}
	//
	if( $("body").hasClass('home') ){
		$('#n_journal').addClass('active');
	//if(!$("body").hasClass('single') || !$("body").hasClass('tag') || !$("body").hasClass('archive') || !$("body").hasClass('page-id-382')){
		if($.cookie('view')){
			if($.cookie("view") == 'list'){
				$('.post-content').slideUp();
				$('.post').addClass('closed');
				$("#open-and-close").addClass('close');
			}else{
				$("#open-and-close").removeClass('close');
			}
		}else{
			$("#open-and-close").removeClass('close');
		}
	}else{
		//alert('2');
	}
	
	//
	if($("body").hasClass('home')){
		$("#open-and-close").click(function(event){
			var current = $('.hot-seat:last');
			event.preventDefault();
			if($('.post').hasClass('closed')){
				$('.post-content').slideDown('normal', function(){
					if(!$.cookie('view')){
						$.cookie("view", "details", { expires: 7 });
					}
					$.cookie('view', 'details');
					$('a.closebox').fadeOut(500, function() { $(this).remove(); });
					$('.post').removeClass('closed');
					$("#open-and-close").removeClass('close');
				});	
			}else{
				//$('html,body').animate({scrollTop: '0px'});
				$('.post-content').slideUp('normal', function(){
					if(!$.cookie('view')){
						$.cookie("view", "list", { expires: 7 });
					}
					$.cookie('view', 'list');
					$('.post').addClass('closed');
					$("#open-and-close").addClass('close');
				});
			}
		
			setTimeout(function(){
				$('html,body').animate({scrollTop: (current.position().top - 30) + 'px'}, 800,function(){ });
			}, 500);
		});
	}
	
	//
	$('#main').infinitescroll({
		navSelector  : "div#more",            
	    nextSelector : "div#more a:last",   
		itemSelector : "div.post",
		loadingImg	 : 'http://www.nuzzaci.co.uk/wp/wp-content/themes/nuzzaci_2/images/',
	}, function(){
		if($('.post').hasClass('closed')){
			$('.post-content').hide();
			$('.post').addClass("closed");
		}else{
			//alert('Posts are expanded');
		}
		applyExpandAction();
		if($(this).attr("id") == 'infscr-page-2'){
			$('#main > div.post:last').addClass('last');
		}else{
			$(this).prev().find('.post:last').addClass('last');
		}
		
		scrollValue = $('.infscr-pages:last').position().top - ($(window).height()/4);
		//alert(scrollValue);
		$('html,body').animate({scrollTop: scrollValue+'px'}, 800,function(){ });
	});
	
	$(window).unbind('.infscr');
	$('div#more a:last').click(function(event){
		event.preventDefault();
		$(document).trigger('retrieve.infscr');
	});
	
});

function applyExpandAction(){
	$('.post').click(function(event){
		if($(this).hasClass('closed')){
			event.preventDefault();
			$(this).removeClass("closed");
			$(this).find('.post-content').slideDown('normal');
			var a = $("<a></a>").attr('href','#').text('collapse').click(function(event){
				event.preventDefault();
				$(this).parent().find('.post-content').slideUp('normal', function(){
					$(this).parent().addClass("closed");	
				});
				
				$(this).fadeOut(500, function() { $(this).remove(); });
			});
			a.addClass('closebox');
			$(this).prepend(a);
		}else{
			//alert('Post is expanded');
		}
	});
}
