var paddingX = 30;
var scaleWidth = 0.303; 

$(document).ready( function() {
	var width = scaleWidth * $("#inner-wrap").width();
	$(".news .post").each( function( index ) {
		$(this).css("width", width );
	});
	$("#slider-container").css("width", (3 * ( width + paddingX ) ) );
	$("#slider-control").css("width", $("#number-of-posts").val() * (width + paddingX) + 100 );
	if ( $("#number-of-posts").val() <= 3 ) {
		$("#slider-next").hide();
	}
});

$(window).load(function() {	
	$("div.read-more").hide();
	$('div.news').find('div.post').click(function () {
		if (!$(this).hasClass('active')) {
			var offset = $(this).offset();
			var parentHeight = $(".box.news").height( );
			var width = $(this).width();
			var text = $(this).find('div.read-more').html();
			var currentElement = $(this);
			$('#read-more').slideUp('slow', function( ) {
				$('div.post').removeClass('active');
				currentElement.addClass('active');
				if ( text != "" && text != null ) {
					$("#read-more").css("left", offset.left );
					$("#read-more").css("top", offset.top + parentHeight - 4);
					$("#read-more").css("width", width);
					$("#read-more").html(text);
					$('div.read-more').slideUp('slow');
					$("#read-more").slideDown("slow");
					$("#read-more div.close").click(function() {
						$("#read-more").slideUp('slow', function() {
							currentElement.removeClass('active');
						});
					});
				}			
			});
		}
	});
		
	$('.flexslider').flexslider({
		animation: "slide",
		slideshow: true,                //Boolean: Animate slider automatically
		slideshowSpeed: 4000,           //Integer: Set the speed of the slideshow cycling, in milliseconds
		animationDuration: 600,         //Integer: Set the speed of animations, in milliseconds
		controlNav: true,
		pauseOnHover: true
	});

	$("#slider-next").click( function() {
		$("#slider-prev").fadeIn(1000);
		$("#current-position").val( parseInt( $("#current-position").val() ) + 1 );
		//console.log($("#current-position").val());
		if ( parseInt( $("#current-position").val() ) + 3 >= $("#number-of-posts").val() ) {
			$("#slider-next").fadeOut(1000);
		}
		else {
			$("#slider-next").fadeIn(1000);
		}

		if ( parseInt( $("#current-position").val() ) + 3 <= $("#number-of-posts").val() ) {
			$("#read-more").slideUp("slow", function() {
				$('div.post').removeClass('active');
				var width = $("#slider-container").width() / 3;
				$("#slider-control").animate({
			    	left: '-=' + width
			  	}, 1000, function() {
			  	});
			});
		}
		else {
			$("#current-position").val( parseInt( $("#number-of-posts").val() ) - 3 );	
		}
	});

	$("#slider-prev").click( function() {
		$("#slider-next").fadeIn(1000);
		$("#current-position").val( parseInt( $("#current-position").val() ) -1 );
		//console.log($("#current-position").val());
		if ( $("#current-position").val() <= 0 ) {
			$("#slider-prev").fadeOut(1000);
		}
		else {
			$("#slider-prev").fadeIn(1000);
		}

		if ( $("#current-position").val() >= 0 ) {
			$("#read-more").slideUp("slow", function() {
				$('div.post').removeClass('active');
				var width = $("#slider-container").width() / 3;
				$("#slider-control").animate({
				    left: '+=' + width
				  }, 1000, function() {
				    
				  });
			});
		}
		else {
			$("#current-position").val(0);
		}
	});

	$(window).resize(function() {
		var width = scaleWidth * $("#inner-wrap").width();
		$(".news .post").each( function( index ) {
			$(this).css("width", width );
		});

		if ( $(".active") && $("#read-more").is(":visible") ) {
			var offset = $(".active").offset();
			var width = $(".active").width();
			var parentHeight = $(".box.news").height( );
			$("#read-more").css("width", width);
			$("#read-more").css("left", offset.left );
			$("#read-more").css("top", offset.top + parentHeight - 4);
			$("#read-more").css("width", width);
		}

		$("#slider-container").css("width", (3 * ( width + paddingX ) ) );
		$("#slider-control").css("width", $("#number-of-posts").val() * (width + paddingX) + 100 );
		$("#slider-control").css("left", $("#current-position").val() * ( width + paddingX ) * -1 );
	});
	
	/*
	$("div.box").each(function(displayBox) {
		$(this).delay(displayBox * 1000).fadeIn("slow");
	});
	*/									
});

