var bookshelf = {
	shelf_width: 0,
	opacity_show: { opacity: 1 },
	opacity_hide: { opacity: 0 },
	opacity_time: 400,
	init: function()
	{

        if($.browser.msie)
    	{
    		bookshelf.opacity_show = {};
    		bookshelf.opacity_hide = {};
    		bookshelf.opacity_time = 0;
    	}
        /*$('.book').each(function()
        {
            bookshelf.shelf_width += $(this).outerWidth();
        });*/
        $('#bookshelf_books').width(2000);
        
        $('.close_book').click(function(e){
			e.preventDefault();
			bookshelf.closeBooks($('.open'));
        });    
        bookshelf.logoBubble();
        bookshelf.bubbleEvents();
        bookshelf.doBookshelf();    
        	
	},
	bubbleEvents: function()
	{
        $('.book').mouseenter(function(e){
            // $('#bookshelf_logo_comment').animate(bookshelf.opacity_hide,bookshelf.opacity_time, function(){$('#bookshelf_logo_comment').hide()});
            if($('.open').length == 0)
            {   
                if($(this).position().left > 660)
                {
                    
                    $(this).next().addClass('book_bubble_left');
                  	$(this).next().css('left',($(this).position().left+(parseInt($(this).width()/2)-190))+'px').css('top',($(this).position().top+30)+'px');
                  //  $(this).find('.book_bubble').css('left',(parseInt($(this).width()/2)-190)+'px');
                
                }
                else
                {
                    $(this).next().removeClass('book_bubble_left');
                    $(this).next().css('left',($(this).position().left+(parseInt($(this).width()/2)-36))+'px').css('top',($(this).position().top+30)+'px');
                   // $(this).find('.book_bubble').css('left',(parseInt($(this).width()/2)-36)+'px');
                }
                $(this).next().show();
            }
        });

        $('.book').mouseleave(function(e){
            $(this).next().hide();
        });
        
        $('.book_bubble').mouseenter(function(e){
            $(this).show();
        });

        $('.book_bubble').mouseleave(function(e){
            $(this).hide();
        });
	},
	logoBubble: function()
	{
		$('#bookshelf_logo_link').click(function(e){
        	e.preventDefault();
        	if($('#bookshelf_logo_comment:visible').length == 0)
        	{
				if($('.open').length > 0)
				{
					var new_book = $('.open');
		        	$('.open').find('.book_content').animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
		                    $(this).hide();
		                    new_book.animate({width: parseInt(new_book.attr('rel'))},1000,function(){
		                        new_book.removeClass('open');
		                        $(this).find('.book_spine_text').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
		                        $('#bookshelf_books').animate({left: '20px'},1000,function(){
		                        	$('#bookshelf_logo_comment').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
		                        });  
		                    });
		             });
	             }
	             else
	             {
	             	$('#bookshelf_logo_comment').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
	             }	        		
        			
        	}
        	
        	
        });  	
	},
	doBookshelf: function()
	{
        $('.book_activate').click(function(e){
        	e.preventDefault();
        	if($(this).hasClass('book'))
        	{
        		$(this).unbind('mouseenter').unbind('mouseleave');
        		$(this).next().unbind();
        		$(this).next().hide();
        		var new_book = $(this);
        		
        	}
        	else
        	{
        		$(this).unbind();
        		$(this).prev().unbind('mouseenter').unbind('mouseleave');
        		$(this).hide();
        		
        		var new_book = $(this).prev();
        	}
        
            
            $('#bookshelf_logo_comment').animate(bookshelf.opacity_hide,bookshelf.opacity_time, function(){$('#bookshelf_logo_comment').hide()});
                        
            
            
            if(new_book.hasClass('open'))
            {
                /*$('.open').find('.book_content').animate({opacity: 0},400,function(){
                    $(this).hide();
                    new_book.animate({width: parseInt(new_book.attr('rel'))},1000,function(){
                        new_book.removeClass('open');
                        $(this).find('.book_spine_text').show().animate({opacity: 1},400);
                        $('#bookshelf_books').animate({left: '20px'},1000);  
                    });          
                }); */
            }
            else if($('.open').length > 0)
            {
                $('.open').next().next().animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
                    $(this).hide();
                    $('.open').animate({width: parseInt($('.open').attr('rel'))},1000,function(){
                        $('.open').removeClass('open');
                        $(this).find('.book_spine_text').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
                            $('#bookshelf_books').animate({left: ((20 - new_book.position().left) +122)+'px'}, 1000,function(){
                            new_book.find('.book_spine_text').animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
                                new_book.addClass('open');
                                $(this).hide();
                                new_book.animate({width: 680},1000,function()
                                {
                                    new_book.next().next().css('left',(new_book.position().left +10)+'px').css('top',(new_book.position().top+(530-new_book.find('.book_spine').height()))+'px');
                                    new_book.next().next().show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
                                    //new_book.find('.book_content').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
        	                      /*  new_book.next().next().css('left',(new_book.position().left+20)+'px').css('bottom',(new_book.position().bottom+10)+'px');                                    
                                    new_book.next().next().animate(bookshelf.opacity_show,bookshelf.opacity_time);
                                    if(new_book.next().next().next().hasClass('book_content_bubble'))
        	                        {
            	                        new_book.next().next().next().css('left',(new_book.position().left+(parseInt(new_book.width()/2)+120))+'px').css('top',(new_book.position().top+160)+'px');
            	                        new_book.next().next().next().show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
        	                        }  */                          
        	                   });
                            })
                            
                        })
                    });
                
                });
               
            }
            else{
          	    $('#bookshelf_books').animate({left: ((20 - new_book.position().left )+122)+'px'}, 1000,function(){
	                new_book.find('.book_spine_text').animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
	                    new_book.addClass('open');
	                    $(this).hide();
	                    new_book.animate({width: 680},1000,function()
	                    {
	                        new_book.next().next().css('left',(new_book.position().left +10)+'px').css('top',(new_book.position().top+(530-new_book.find('.book_spine').height()))+'px');
	                        new_book.next().next().show().animate(bookshelf.opacity_show,bookshelf.opacity_time);

//	                        new_book.find('.book_content').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
	                 /*       new_book.next().next().css('left',(new_book.position().left+20)+'px').css('bottom',(new_book.position().bottom+10)+'px');
	                        new_book.next().next().animate(bookshelf.opacity_show,bookshelf.opacity_time);
	                        if(new_book.next().next().next().hasClass('book_content_bubble'))
	                        {
            	                new_book.next().next().next().css('left',(new_book.position().left+(parseInt(new_book.width()/2)+120))+'px').css('top',(new_book.position().top+160)+'px');
    	                        new_book.next().next().next().show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
	                        } */
	                    });
	                });      
	            });
            
            }
        });
	
	},
	closeBooks: function(book)
	{
	
			$('#bookshelf_logo_comment').delay(2500).show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
					
        	var new_book = book;
        	/*	 if($('.open').next().next().next().hasClass('book_content_bubble'))
                {
                    $('.open').next().next().next().animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
                        $(this).hide();
                    });
                } */
              //  $('.open').next().next().animate(bookshelf.opacity_hide,bookshelf.opacity_time);
        	//	$('.open').find('.book_content').animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
                  $('.open').next().next().animate(bookshelf.opacity_hide,bookshelf.opacity_time,function(){
                        $(this).hide();
                        new_book.animate({width: parseInt(new_book.attr('rel'))},1000,function(){
                            new_book.removeClass('open');
                            $(this).find('.book_spine_text').show().animate(bookshelf.opacity_show,bookshelf.opacity_time);
                            $('#bookshelf_books').animate({left: '20px'},1000,function(){
                            	bookshelf.bubbleEvents();
                            });  
                        });
                 });	
     	}


}
    
$(function(){
	bookshelf.init();

});
