$(function() {     	 
   $("#nav").height($(document).height() - parseInt ($("#nav").css("top")));
 
   if ($("#rental").length == 0){
      $('<div id="rental"></div>').insertAfter($('#content'));
   }
   

   
   if ($("#showCollectionNotice").length == 0){
      $.ajax({
         type: "POST",
         url: "/ajaxform.html?n=1",
         success: function(obj){
            $(obj).insertAfter("#mainnav") ;
            if($("#amount").text()=='0' ) {
            $("#showCollectionNotice").hide();
            }
            $("#showCollectionNotice").css('position','absolute');
            $("#showCollectionNotice").css('left','55px');
            
            
            
         }
         });
    }
    else
    {
    $("#showCollectionNotice").css('position','absolute');
            $("#showCollectionNotice").css('left','55px');
    
    }
    
  
    
   $("p.rental a").click( function (event){
      event.preventDefault();
      pos = $(this).parent().position();
      //noticePos = $("#showCollectionNotice").position();
      url = $(this).attr("href") + '&a=1';

      $.get(url, function(data) {
         $("#rental").html(data);
         $('#rental').animate({  top: pos.top + 85})
         
         
              /* if (noticePos.top > $(window).height() )*/
         
         if (pos.top > $("#mainnav").height())
   
         {
         
          
         
         $('#showCollectionNotice').animate({  top: pos.top -20})
          }
         $("#rentalForm").submit( function (){
            $('#rentalForm').ajaxForm();
            dataString = $("#rentalForm").formSerialize();        
            
            $.ajax({
               type: "POST",
               url: "/ajaxform.html",
               data: dataString,
               dataType: 'json',
               success: function(obj){
                  $('#rentalFormSubmit').attr("value",obj['text']) ;
                  $('#rentalAction').attr("name" ,obj['action']);
                  $('#amount').text(obj['amount']);
                  $('#plural').text(obj['plural']);

                     if (obj['amount'] == 0) {
                        $('#showCollectionNotice').hide();
                     }
                     else {
                        $('#showCollectionNotice').show();
                     }
                  }
               });
            return false;
         });   
      });
   });      
});
