var BOOKSLIDER = {
	left: 0,
	book_width: 147,
	left_slide: 240,
	duration: 500,
	default_duration: 500,
	enabled: true
}


$(function(){
	if($('#bookshelf.big').length){
		if($('#bookshelf.big .col_left > div').length > 1){
			$('#bookshelf .next, #bookshelf .prev').click(slideBook);
			$("#bookshelf .book-ul li").click( function(){
				$('#bookshelf .next').trigger('click');
			});
			$('#bookshelf .col_left .book:not(.active)').hide();
		} else {
			$('#bookshelf.big .book-ul li').hide();
			$('#bookshelf .next, #bookshelf .prev').click(function(){
				return false;
			});
		}
	}
	
});

function slideBook(ev,obj,step){
	var me = obj ? $(obj) : $(this);
	step = step || 1;
	
	if(!BOOKSLIDER.enabled){
		return false;
	}
	
	if(step > 1){
		BOOKSLIDER.duration = 100;
	} else {
		BOOKSLIDER.duration = BOOKSLIDER.default_duration;
	}
	
	var modifier = me.is('.next') ? 1 : -1;
	
	BOOKSLIDER.enabled = false;
	
	var newLeft = BOOKSLIDER.left - ( BOOKSLIDER.book_width * modifier );
	var newPad  = parseInt( $(".book-ul").css('marginLeft'), 10)  + ( BOOKSLIDER.book_width * modifier );
	
	
	$(".book-ul").animate({left: newLeft},BOOKSLIDER.duration, function(){
		
	});
	
	var first, second;
	
	
	if(modifier > 0){ 
		
		// Right Arrow, Move Book Left
		
		$(".book-ul li.active").css('opacity',1);
		
		// Fade/Handle Finish
		$(".book-ul li.active").animate({opacity:0.0},BOOKSLIDER.duration, function(){
			var one = $(".book-ul li.active");
			var two = one.next();
			one.removeClass('active');
			two.addClass('active');

			// Move to end of list
			one.appendTo( $('.book-ul') ).css('opacity',1);
			$(".book-ul").css('marginLeft',newPad);

			BOOKSLIDER.enabled = true;
		});
		
		// Fade/Cycle Left Side
		first = $(".col_left .book.active");
		second = first.next();
		if(second.length < 1){
			second = $(".col_left .book:first");
		}
		
		first.css({'zIndex':1, opacity: 1}).removeClass('active');
		
		first.animate({
			left:-1 * BOOKSLIDER.left_slide,
			opacity:0
			// display: 'block'
		},BOOKSLIDER.duration,function(){});
		
		second.css({
			zIndex: 2,
			left: BOOKSLIDER.left_slide,
			opacity: 0,
			display: 'block'
		}).addClass('active').animate({
			left:0,
			opacity:1.0
		},BOOKSLIDER.duration, function(){
			
		});
		
	} else { 
		// Left Arrow, Move Book Right
		
		var one = $(".book-ul li.active");
		var two = $(".book-ul li:last");
		one.removeClass('active');
		two.addClass('active');

		// Move to end of list
		two.css('opacity',0).prependTo( $('.book-ul') );
		$(".book-ul").css('marginLeft',newPad);
		
		// Fade/Handle Finish
		two.animate({opacity:1.0},BOOKSLIDER.duration, function(){
			BOOKSLIDER.enabled = true;
		});
		
		// Fade/Cycle Left Side
		first = $(".col_left .book.active");
		second = first.prev();
		if(second.length < 1){
			second = $(".col_left .book:last");
		}
		first.css('z-index',1).removeClass('active');
		first.animate({
			left: BOOKSLIDER.left_slide,
			opacity: 0.0
		},BOOKSLIDER.duration,function(){
			first.css('left',0);
		});
		second.css({ 
			zIndex:2,
			left: -1 * BOOKSLIDER.left_slide,
			opacity:0,
			display:'block'
		}).addClass('active');
		second.animate({
			left:0,
			opacity:1.0
		}, BOOKSLIDER.duration);
		
	}
	
	BOOKSLIDER.left = newLeft;
	
	return false;
}

function chooseBlogCategory(oSel){
	var val = $(oSel).val();

	window.location.href = val;
;}

function chooseBlogDate(oSel){
	var val = $(oSel).val();
	
	window.location.href = "/blog/?date=" + val;
;}
