/**
 * @author Andrey Yakubov
 */
$(function() {
    $('#gobook').attr({
        'class': 'active'
    });
    
    $('#calendar').datepicker({ 
                            dateFormat: 'yy-mm-dd', 
                            firstDay: 1,
                            dayNamesMin: [js_dictionary.sunday_min, 
                                          js_dictionary.monday_min, 
                                          js_dictionary.tusday_min, 
                                          js_dictionary.wensday_min, 
                                          js_dictionary.thursday_min, 
                                          js_dictionary.friday_min,
                                          js_dictionary.saturday_min],
                            monthNames: [js_dictionary.january, 
                                         js_dictionary.february, 
                                         js_dictionary.march, 
                                         js_dictionary.april, 
                                         js_dictionary.may, 
                                         js_dictionary.june, 
                                         js_dictionary.july, 
                                         js_dictionary.august, 
                                         js_dictionary.september, 
                                         js_dictionary.october, 
                                         js_dictionary.november, 
                                         js_dictionary.december],
                            showButtonPanel: true,
                            closeText: 'x (Закрыть)' });
});
function addBookWithoutComment(book_form)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });
     JsHttpRequest.query(
      	  "/book?request_mode=ajax",
      	  {'movie_form':book_form, 'links' : curlinks, 'ajax_action' : 'add_book_nc'},
      	  function(result, errors) {
             if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result)
             {
                 if (result.error_status) {
                     $('#error_div').html('<br><br>'+result.error_msg);
                     $('#error_div').show();
                 }
                 else
                 {
         	  	     $("#add_book").each( function() {
                                                    this.reset();
    		     	                                });
                                                    
                     document.location = '/book/' + result;
                }
             }
      	  },
      	  true
      );   
}

function sendBookForm(book_form)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });

    JsHttpRequest.query(
      	  "/book?request_mode=ajax",
      	  {'book_form':book_form, 'links' : curlinks, 'ajax_action' : 'add_book'},
      	  function(result, errors) {
             if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result)
             {
                 if (result.error_status) {
                     $('#error_div').html('<br><br>'+result.error_msg);
                     $('#error_div').show();
                 }
                 else
                 {
         	  	     $("#add_book").each( function() {
                                                    this.reset();
    		     	                                });
                                                    
                     document.location = '/book/' + result;
                }
             }
      	  },
      	  true
      );
}

function addBookComment(id, date, text, is_rec)
{
    JsHttpRequest.query(
      	  "/book?request_mode=ajax",
      	  {'book_id':id, 'date' : date, 'text' : text, 'is_rec' : is_rec, 'ajax_action' : 'add_comment'},
      	  function(result, errors) {
             if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result)
             {
                document.location = "/book/"+id;
             }
      	  },
      	  true
      );
}

function deleteBook(id)
{
     JsHttpRequest.query(
    	  "/book?request_mode=ajax",
    	  {'id' : id, 'ajax_action' : 'delete_book'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result) {
                document.location = '/book/';
             }
    	  },
    	  true
      );     
}

/**
 * @author kkopachev
 */

function editBook()
{
	
	$('#view_name_date').hide();
	$('#view_author').hide();
	$('#view_params').hide();
	$('#view_edit').hide();
	
	$('#edit_name_date').show();
	$('#edit_author').show();
	$('#edit_params').show();
	$('#edit_edit').show();
	
	$('.delete_link').show();
	$('#edit_add_link').show();
	
}

function saveBook(book_form, id)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });
     JsHttpRequest.query(
      	  "/book?request_mode=ajax",
      	  {'book_form':book_form, 'links' : curlinks, 'id' : id, 'ajax_action' : 'edit_book'},
      	  function(result, errors) {
             if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result)
             {
                 if (result.error_status) {
                     $('#error_div').html('<br><br>'+result.error_msg);
                     $('#error_div').show();
                 }
                 else if (result.content)
                 {
                	 window.location.reload();
                	 
                	 /*$('#add_book').css('height','0px').hide();
                	 $('#error_div').hide();
                	 $('#item_inner').hide().html(result.content).show();
                	 $('#add_book').css('height','').show();*/
                	 
         	  	     /*$("#add_book").each( function() {
                                                    this.reset();
    		     	                                });
                                                    
                     document.location = '/book/' + id;*/
                }
             }
      	  },
      	  true
      );   
}
