$(document).ready(function(){

  //pagina corrente
  var current_page = "home";

  //effetto del menù generale
  $("ul.thumb li").hover(function() {
    $(this).css({'z-index' : '10'});
    $(this).find('img').addClass("hover").stop().animate({
      marginTop: '-110px', 
      marginLeft: '-110px', 
      top: '50%', 
      left: '50%', 
      width: '174px', 
      height: '174px',
      padding: '20px' 
    }, 200);
    } , function() {
    $(this).css({'z-index' : '0'});
	$(this).find('img').removeClass("hover").stop().animate({
      marginTop: '0', 
      marginLeft: '0',
      top: '0', 
      left: '0', 
      width: '100px', 
      height: '100px', 
      padding: '5px'
    }, 400);
    });

  //effetto zoom delle icone nelle varie pagine
  $(".zoom").hover(
    function () {
	  $(this).css({'z-index' : '10'});
	  $(this).find('img').addClass("hover").stop().animate({
	    marginTop: "3px",
		marginLeft: "-3px",
		width: "150px",
		height: "79px"
	  }, 100, "linear");
	  $("#title").find('img').addClass("hover").stop().animate({
	    marginTop: "3px",
		marginLeft: "5px",
		width: "102%",
		height: "102%"
	  }, 100, "linear");
    }, 
    function () {
	  $(this).css({'z-index' : '0'});
      $(this).find('img').removeClass("hover").stop().animate({
	    marginTop: "0",
		marginLeft: "0",
		width: "145px",
		height: "74px"
	  }, 100, "linear");
	  $("#title").find('img').removeClass("hover").stop().animate({
	    marginTop: "0",
		marginLeft: "0",
		width: "100%",
		height: "100%"
	  }, 100, "linear");
    }
  );
  
  //caricamento della homepage all'avvio del sito
  $('#scrollpane').load('home.php');
  $('#scrollpane').jScrollPane({scrollbarWidth: 1, topCapHeight: 90, bottomCapHeight: 10, dragMaxHeight: 30});
  $("#comments_box_inner").load("comments.php", { page: "home" } );

  //wrapping della horizontal rule, con la greca
  $("hr").wrap("<div class='hr'></div>");
  
  //aggiungo la classe per gli angoletti rotondi
  $('.rounded').corners("left");
  
  //il pulsante "lascia un commento" sposta il form dei commenti verso destra
  $("#comments_link").click(function(){
    $("#comments_form_box").animate({ 
      left: "-10px",
    }, 1500 );
  });
  
  //la X in alto a destra del form dei commenti
  $("#comments_form_close").click(function(){
    $("#comments_form_box").animate({ 
      left: "-328px",
    }, 1500 );
  });
  
  //configuro il form ajax
  var options = {
    success:    function() { 
      $("#comments_form_box").animate({ 
        left: "-328px",
      }, 1500 );
	  $("#comments_box_inner").load("comments.php", { page: current_page });
    },
	beforeSerialize: function($form, options) { 
      $('#comments_page').attr('value', current_page);
    }
  }; 
  
  //il pulsante "invia" del form dei commenti
  $('#comments_form').ajaxForm(options);
  
  //il campo dei caratteri del commento, con conteggio a 160 caratteri
  $('#textarea').keyup(function(){
    var max = 160;
    if ($(this).val().length > max){
      $(this).val($(this).val().substr(0, 160));
    }
    $('#rimanenti').html('Testo (caratteri rimanenti: ' + (max - $(this).val().length) + ')');
  });
  
  //la funzione che lega la rotella del mouse al box dei commenti
  $('#comments_box').mousewheel(function(event, delta) {
    var y = $('#comments_box').scrollTop() + (delta * (-20));
	$('#comments_box').scrollTop(y);
  });
  
  //la freccia verso l'altro del box dei commenti
  $("#arrow_up").mousehold(function(){
    var y = $('#comments_box').scrollTop() + (-20);
	$('#comments_box').scrollTop(y);
  });
  
  //la freccia verso il basso del box dei commenti
  $("#arrow_down").mousehold(function(){
    var y = $('#comments_box').scrollTop() + (20);
	$('#comments_box').scrollTop(y);
  });
  
  //la funzione che gestisce i pulsanti del menù generale
  $("ul.thumb li img").click(function(){
    var click_page = $(this).attr('src');
	var click_page = /img\/(.*)\.jpg/i.exec(click_page);
	click_page = click_page[1];
	$('#scrollpane').empty();
	$('#image').attr('src', 'img/books_'+click_page+'.jpg');
	$('#title').attr('src', 'img/books_'+click_page+'_title.png');
    $.get(click_page+'.php', function(data) {
      $('#scrollpane').html(data);
	  $('#scrollpane').jScrollPane({scrollbarWidth: 1, topCapHeight: 90, bottomCapHeight: 10, dragMaxHeight: 30});
	});
    $("#comments_box_inner").load("comments.php", { page: click_page } );
	current_page = click_page;
  });
  
});


