// we will add our javascript code here
    $(function() {   
	
		/* chumurka pod książką z opisem */
		$(".coverList div").hover(
                function() { $("div.toolTip", this).show(); },
                function() { $("div.toolTip", this).hide(); }
         );		 
		 
		/* dodawnie trójkąta w głównym menu */
	   $("#menu div ul li.menu-p1").mouseover( function() { 
				$(this).addClass("menu_active_hover");	
				 $('ul', this).show();			
				 }).mouseout(function(){
	      	$(this).removeClass("menu_active_hover");
			 $('ul', this).hide();	
	    });		
		
		/* dodawnie trójkąta w głównym menu 
		wersjia z poprawką na A 
	   $("#menu div ul li.menu-p1 a").mouseover( function() { 
				$(this).parent().addClass("menu_active_hover");	
				 $(this).next('ul').show().mouseover( function() {$(this).parent().addClass("menu_active_hover");$(this).show();});			
				 }).mouseout(function(){
	      	$(this).parent().removeClass("menu_active_hover");
			 $(this).next('ul').hide().mouseout( function() {$(this).parent().removeClass("menu_active_hover");$(this).hide();});	
	    });
	    
		*/
		/* zmina fontu w wyszukiwarce */
		$("#top_searcher input").focus(function () { 
	    	$(this).removeClass("inactive");
			
			if (this.value == this.defaultValue){  
	            this.value = '';  
	         }  
	         if(this.value != this.defaultValue){  
	             this.select();  
	         }		 
	    }); 
		
		$('#top_searcher input[type="text"]').blur(function() {
			if ($.trim(this.value) == ''){  
				$(this).addClass("inactive"); 
				this.value = (this.defaultValue ? this.defaultValue : '');  
			}  		
		});
		             
      
 });
 
 /*
 pokazuje liste z półkami
*/
 function showSelectShelfList(){
 	$('div.dropDownListLibrary').hover(			
			function() { $('ul', this).show(); },
			function() { $('ul', this).hide(); }				

	)
 }
 
function librarySearch(){
	var def = 'wpisz tytuł książki...';
 	$("#contentTop .searchLib input").focus(function () { 
  	$(this).removeClass("inactive");
		
	if (this.value == def){  
          this.value = '';  
       }  
       if(this.value != def){  
           this.select();  
       }		 
  });
	$('#contentTop .searchLib input[type="text"]').blur(function() {
			if ($.trim(this.value) == ''){  
				$(this).addClass("inactive"); 
				this.value = (def ? def : '');  
			}  		
		});
}
 
 /*
Dodaj do obserwowanych
*/
 function addToFollow(){
 	$(".follow_add").click(function(){
		var element = $(this);
		var I = element.attr("id").substring(7);
		var params = I;
				
		$.ajax({
				type: "GET",
				url: "follow/addCommit/" + params,
				data: null,
				complete: function(){
					$("#loading_"+I).hide();	
				 },
				success: function(){					
					$('#follow_'+I).fadeOut(200).hide();
					$('#unfollow_'+I).fadeIn(200).show();
			}
		});
		
		$("#loading_"+I).ajaxStart(function(){}).show();
		return false;
	});
 }
 
 /*
	Usuń z obserwowanych
*/
 function removeFromFollow(){
 	$(".follow_remove").click(function(){
		var element = $(this);
		var I = element.attr("id").substring(9);
		var params = I;
		
		$.ajax({
			type: "GET",
			url: "follow/removeFollowingCommit/" + params,
			data: null,			
			complete: function(){
				$("#loading_"+I).hide();	
			 },
			success: function(){							
				$('#unfollow_'+I).fadeOut(200).hide();			
				$('#follow_'+I).fadeIn(200).show();			
			}			
		});
		
		$("#loading_"+I).ajaxStart(function(){}).show();
		return false;
	});
 }
 
 
 /*
 dodaje skiążke do bibloteczki na półke
*/
function addLibraryBook(){				
	$('.dropDownListLibrary ul li a').click(function(event){
		event.preventDefault();
		
		var element = $(this).parent().parent().parent();
		var itemID = $('span.item-id', element).text();
		var link = $(this).attr("href");
		var shelfName = $(this).text();			
					
		$(element).html('<span><img src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" class="no-border" ></span>');	
	
		$.ajax({
			type: "GET",
			url: link,
			data: null,
			success: function(html){								
				$(element).removeClass("dropDownListLibrary").html('').append(html);			
				//-- pokaż wartswe								
				if (shelfName == 'Przeczytane') {														
					tb_show("Edycja książki w biblioteczce", 'review/editLayer/'+itemID+'?height=450&width=700&modal=true', null);
				}			
			}
					
		});
	});
}

 /*
 przy przenozeni na połke pzecztane
*/
function moveLibraryBook(){				
	$("a.moveR").click(function(event){
		event.preventDefault();
					
		var link = $(this).attr("href");			
		$(this).ajaxStart(function(){
			$(this).append('<img src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" class="no-border ajaxlbr" style="float:right;" alt="loading" />');
		});
		
		$.ajax({
			type: "GET",
			url: link,
			data: null,	
			success: function(result){								
				$("#boxr-book-" + result).fadeOut(200, function(){					
				//tb_init('a.thickbox_' + result + ', area.thickbox, input.thickbox');
				//tb_show('Napisz opinię przeczytanej książki.', 'review/editLayer/'+result+'?keepThis=true&TB_iframe=true&height=400&width=700', '');
				tb_show("Napisz opinię przeczytanej książki", 'review/editLayer/'+result+'?height=450&width=700&modal=true', null);	
				});													
			}						
		});
	});		
}
 
 
 function tabBookListAjax(){
 	//TODO: zrobic funkcje	
	var ld = '<img src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" class="no-border" id="ajax-loading-covers">'		 
			 
			 $("#covers-library").click(function(){
		   	
				$("#covers-library").addClass('active');
				$("#covers-popular").removeClass('active');
				$("#covers-readNow").removeClass('active');
		   	$("#covers-recent").removeClass('active');
		   	$("#covers-toPublish").removeClass('active');
      	
				   	
				$("#container-cover-list .clBottom").fadeOut(200);
		     	$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/library",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});			
				return false;
		   });
			 
		   $("#covers-popular").click(function(){
		   	
				$("#covers-library").removeClass('active');
				$("#covers-popular").addClass('active');
				$("#covers-readNow").removeClass('active');
		   	$("#covers-recent").removeClass('active');
		   	$("#covers-toPublish").removeClass('active');
      	
				   	
				$("#container-cover-list .clBottom").fadeOut(200);
		     	$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/popular",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});			
				return false;
		   });
		   
		   $("#covers-readNow").click(function(){
				
				$("#covers-library").removeClass('active');
				$("#covers-popular").removeClass('active');
		   	$("#covers-readNow").addClass('active');
		   	$("#covers-recent").removeClass('active');
        $("#covers-toPublish").removeClass('active');
        
				
				$("#container-cover-list .clBottom").fadeOut(200);
				$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/readNow",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});
					
				return false;
		   });
		   
		    $("#covers-toPublish").click(function(){
				
				$("#covers-library").removeClass('active');
				$("#covers-popular").removeClass('active');
		   	$("#covers-readNow").removeClass('active');
		   	$("#covers-recent").removeClass('active');
        $("#covers-toPublish").addClass('active');
      	
				
				$("#container-cover-list .clBottom").fadeOut(200);
				$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/toPublish",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});
				return false;
		   });
		   
		    $("#covers-recent").click(function(){
				
				$("#covers-library").removeClass('active');
				$("#covers-popular").removeClass('active');
		   	$("#covers-readNow").removeClass('active');
		   	$("#covers-recent").addClass('active');
        $("#covers-toPublish").removeClass('active');
      	
				
				$("#container-cover-list .clBottom").fadeOut(200);
				$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/recent",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});
				return false;
		   });			 
			 
			  $("#covers-lastAdded").click(function(){
					
				$("#covers-library").removeClass('active');
				$("#covers-popular").removeClass('active');
		   	$("#covers-readNow").removeClass('active');
		   	$("#covers-recent").removeClass('active');
		   	$("#covers-toPublish").removeClass('active');
        $("#covers-lastAdded").addClass('active');
				
				$("#container-cover-list .clBottom").fadeOut(200);
				$("div.coverList")
					.fadeOut(200, function(){					    
						$("#container-cover-list")
						.html(ld)							
						.load("book/ajaxCoverList/lastAdded",{},
						function(){						
   							$("div.coverList").fadeIn(200);
							$("#container-cover-list p.arrowRight").fadeIn(200);
 						});
					});
					
				return false;
		   });

 }
 
/*
	Dodaj plus do opini
*/
 function addPluso(){
 	$("a.add-plus").click(function(event){
		event.preventDefault();		
		var element = $(this).parent().parent();
		var link = $(this).attr("href");		
		$(this).fadeOut(250, function(){						
			$(this).parent().addClass('icon-plus sprite').text('zapisuję...').hide(0).fadeIn(250, function(){									
				$.get(link, function(data) {		  
				  if (data == 1) {
						var count = Number($(element).find('strong').text());						
						$(element).find('strong').fadeOut(250, function(){ $(this).text(parseInt(count)+1).fadeIn(250,function(){
							$(element).find('div.opinion-plus-status').fadeOut(250, function(){ $(this).text('dziękuję!').fadeIn(250); });
						});
					 });						
				  }
					else{
						$(element).find('div.opinion-plus-status').fadeOut(250, function(){ $(this).text('upss coś nie działa').fadeIn(250); });
					}
					
				});
		  });		
			
		});		
		
	});
 }
 

	function likeQuote(){
		$(".qlLike a.icon-plus").click(function(event){
			event.preventDefault();
			var element = $(this);			
			var link = $(this).attr("href");			
			var counter = $(this).parent().next().children('a').children('span');
			var amount = Number($(counter).text());
			
			if(!$(this).hasClass('clickTrue'))	{
				$(this).addClass('clickTrue');
		  
			
				$(this).parent().prev('.qlLikeLoading').html('<img class="no-border" src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" height="11px">');
				
				$.get(link, function(data) {	
					if( $(element).hasClass('greenPlus') ) {
						$(element).removeClass('greenPlus');
						$(element).attr('href', $(element).attr("href").replace('usun-z-ulubionych', 'dodaj-do-ulubionych'));
						$(element).attr('title', 'dodaj cytat do ulubionych');
						$(counter).text(parseInt(amount)-1);
					}
					else {
						$(element).addClass('greenPlus');
						$(element).attr('href', $(element).attr("href").replace('dodaj-do-ulubionych', 'usun-z-ulubionych'));
						$(element).attr('title', 'usuń cytat z ulubionych');
						$(counter).text(parseInt(amount)+1);
					}
					var link = $(element).html('<span>lubię to</span>');
					$(element).parent().prev('.qlLikeLoading').html('&nbsp;');
					$(element).removeClass('clickTrue');
				});	
				
			}				
		});
	}
 
 

 function bigTextarea(){
 	$("a.bigTextarea").click(function(event){
		event.preventDefault();	
		var ta = $(this).closest("form").find("textarea");		
		ta.attr('rows',ta.attr('rows')+8);
	});
 }
 
 /* wydania */
 function showEditions(){
 	$(".bottom a.showEditions").click(function(event){
		event.preventDefault();	
		
		var link = $(this).attr("href");
		
		if($(this).hasClass('slideDown'))	{
			if ($(this).parent().next('.book-item-editions').html() != '') {
	  	$(this).removeClass('slideDown').parent().next('.book-item-editions').slideUp('slow');
	  }
			
		}
		else {				
			if ($(this).parent().next('.book-item-editions').html() == '') {
		  	$(this).addClass('slideDown')
				.next().next().html('<img class="no-border" src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" height="11px">')
				.parent().next('.book-item-editions')
				.load(link, function(){
					$(this).prev().children('.loading').fadeOut(200);
		  		$("div.coverList", this).show().parent().slideDown('slow');
		  	});
		  }
		  else {
		  	$(this).addClass('slideDown').parent().next('.book-item-editions').slideDown('slow');
		  }				
		}
			
	});
 }
 
 
  /* slider */
 function slide(){
 	$(".box div.slnavig a").click(function(event){
		event.preventDefault();		
		var ul = $(this).parent().parent().children(".sllist").children("ul");
		var prev = $(this).parent().parent().children(".slprev").children("a");
		var next = $(this).parent().parent().children(".slnext").children("a");
		var left = parseInt(ul.css("left").replace("px", ""));

		//console.log(left);
		
		if ($(this).parent().hasClass("slnext")) {
			left = left-252;}
		else {
			left = left+252;}

		if (left == 0)
		 prev.hide();	
		else
		 prev.show();
		 
		 if (left == -504)
		 next.hide();	
		else
		 next.show();
		
		ul.animate({left: left}, 700);

	});
 }
 
  /* slider tip*/
 function slideTip(){
	$(".slider .sllist li a").hover(
	  function () {
			var box =  $(this).parent().parent().parent().parent().parent();
			var tip = $(".toolTipContener .toolTip",box);
			
			var title = $(this).parent().children(".title").text();
			var link = $(this).parent().children(".link").text();
			var autor = $(this).parent().children(".autor").text();
			var pos = $(this).parent().children(".pos").text();
									
			$(".bookTitle",tip).text(title);
			$(".bookTitle",tip).attr("href", link);
			$(".toolTipAuthor",tip).text(autor);
			
			tip.css("left", pos).show();

			tip.hover(
				function(){$(this).show(); },
				function(){$(this).hide(); }
				);
	  }, 
	  function () {
	    var box =  $(this).parent().parent().parent().parent().parent();
			var tip = $(".toolTipContener .toolTip",box).hide();
	  }
	);
 }
 
 
 function friendsOptions(){
	$("#readerList li").hover(
	  function () {
			$(".options-", this).toggleClass("hide");		
	  }
	);
 }
 
 function groupTabs(){
 	$(".forum .itemSelector a").click(function(event){
 		event.preventDefault();
 		var link = $(this).attr("href");
 		
 		$(".forum .itemSelector li").each(function(i){
 			$(this).removeClass('active').addClass('blue');
 		});
 		$(this).parent("li").removeClass('blue').addClass('active');
 		
 		$("#topicListContainer .gr-more").fadeOut(200);
 		$("#topicListContainer ul").fadeOut(200, function(){
 			$("#topicListContainer").html('<img src="http://s.lubimyczytac.pl/img/shared/loadingCircle.gif" class="no-border ajaxLoadig">').load(link, {}, function(){
 				$("ul", this).fadeIn(200);
 				$(".gr-more", this).fadeIn(200);
 			});
 		});
 		
 	});
 }

(function($){
	$.fn.jTruncate = function(options) {
	   
		var defaults = {
			length: 300,
			minTrail: 20,
			moreText: "więcej",
			lessText: "mniej",
			ellipsisText: "...",
			moreAni: "",
			lessAni: ""
		};
		
		var options = $.extend(defaults, options);
	   
		return this.each(function() {
			obj = $(this);
			var body = obj.html();
			
			if(body.length > options.length + options.minTrail) {
				var splitLocation = body.indexOf(' ', options.length);
				if(splitLocation != -1) {
					// truncate tip
					var splitLocation = body.indexOf(' ', options.length);
					var str1 = body.substring(0, splitLocation);
					var str2 = body.substring(splitLocation, body.length - 1);
					obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText + 
						'</span>' + '<span class="truncate_more">' + str2 + '</span>');
					obj.find('.truncate_more').css("display", "none");
					
					// insert more link
					obj.append(
						'<span class="clearboth">' +
							' <a href="#" class="truncate_more_link blue xsmall">' + options.moreText + '</a>' +
						'</span>'
					);

					// set onclick event for more/less link
					var moreLink = $('.truncate_more_link', obj);
					var moreContent = $('.truncate_more', obj);
					var ellipsis = $('.truncate_ellipsis', obj);
					moreLink.click(function() {
						if(moreLink.text() == options.moreText) {
							moreContent.show(options.moreAni);
							moreLink.text(options.lessText);
							ellipsis.css("display", "none");
						} else {
							moreContent.hide(options.lessAni);
							moreLink.text(options.moreText);
							ellipsis.css("display", "inline");
						}
						return false;
				  	});
				}
			} // end if
			
		});
	};
})(jQuery);

	function rateBook(bookID, operation){
	  $(".star-" + bookID).rating({
      required: true,
      callback: function(value, link){
        if (!value) value = '0';        
        $("#loading_" + bookID).ajaxStart(function(){
        }).show();
        
        $.ajax({
            type: "GET",
            url: "ksiazka/glosuj/"+bookID+"/" + value,
            data: " ",
            complete: function(){
              $("#loading_" + bookID)								
							.closest("td.degree")
							.load("library/myLibraryInPublicFragment/" + bookID, function() {							 
								if (operation == "add") {
									tb_show("Edycja książki w biblioteczce", 'review/editLayer/' + bookID + '?height=450&width=700&modal=true', null);
								}
							});								
            }            
        });   
				       
      }	      
	  });
	}
 
 
 
 
 
 
 
 
