/**
 * @author Yakubov Andrey
 */
$(function() {
	    
    $('#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 (Закрыть)' });
    
    $('#gomovie').attr({
        'class': 'active'
    });
});

function onTitleSelect(item)
{
    document.location = "/movie/" + item.id;
}

function getMoviesNames(query)
{
    $('#test').html(query);
}

function sendMovieForm(movie_form)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });
             
    JsHttpRequest.query(
      	  "/movie?request_mode=ajax",
      	  {'movie_form':movie_form, 'links' : curlinks, 'ajax_action' : 'add_movie'},
      	  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_movie").each( function() {
                                                    this.reset();
    		     	                                });
                            
                     document.location = '/movie/' + result;
                }
             }
      	  },
      	  true
      );
}

function addComment(id, date, text, is_rec)
{
    JsHttpRequest.query(
      	  "/movie?request_mode=ajax",
      	  {'movie_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 = "/movie/"+id;
             }
      	  },
      	  true
      );
}

function addWithoutComment(movie_form)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });
     JsHttpRequest.query(
      	  "/movie?request_mode=ajax",
      	  {'movie_form':movie_form, 'links' : curlinks, 'ajax_action' : 'add_movie_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_movie").each( function() {
                                                    this.reset();
    		     	                                });
                                                    
                     document.location = '/movie/' + result;
                }
             }
      	  },
      	  true
      );   
}


function movieSortComment()
{
    JsHttpRequest.query(
    	  "/movie?request_mode=ajax",
    	  {'ajax_action' : 'sort_comm'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result) {
                $('#items_on_main').html(result);
             }
    	  },
    	  true
      );     
}

function movieSortRec(user_id)
{
     JsHttpRequest.query(
    	  "/movie?request_mode=ajax",
    	  {'ajax_action' : 'sort_rec'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result) {
                $('#items_on_main').html(result);
             }
    	  },
    	  true
      );    
}


function movieSortAll(user_id)
{
     JsHttpRequest.query(
    	  "/movie?request_mode=ajax",
    	  {'ajax_action' : 'sort_all'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
             
             if (result) {
                $('#items_on_main').html(result);
             }
    	  },
    	  true
      );    
}

function deleteMovie(id)
{
     JsHttpRequest.query(
    	  "/movie?request_mode=ajax",
    	  {'id' : id, 'ajax_action' : 'delete_movie'},
    	  function(result, errors) {

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

/**
 * @author kkopachev
 */

function editMovie()
{
	 
	 $('#view_locale_name').hide();
		$('#view_original_name').hide();
		$('#view_pub_date').hide();
		$('#view_director').hide();
		$('#view_params').hide();
		$('#view_edit').hide();
		
		
	$('#edit_movie_name').show();
	$('#edit_original_title').show();
	$('#edit_pub_date').show();
	$('#edit_director').show();
	$('#edit_edit').show();
	$('.delete_link').show();
	$('#edit_add_link').show();
	$('#edit_params').show();
	
	
}

function cancelEditMovie()
{
	$('#edit_movie_name').hide();
	$('#edit_original_title').hide();
	$('#edit_pub_date').hide();
	$('#edit_director').hide();
	$('#edit_edit').hide();
	$('.delete_link').hide();
	$('#edit_add_link').hide();
	$('#edit_params').hide();
	
	$('#add_link').hide();
	
	$('#view_locale_name').show();
	$('#view_original_name').show();
	$('#view_pub_date').show();
	$('#view_director').show();
	$('#view_params').show();
	$('#view_edit').show();
}

function saveMovie(movie_form, id)
{
     var curlinks = new Array();
    
     $('#links_form > li > input').each( function() {
                             curlinks[this.id] = this.value;
                          });
     
     JsHttpRequest.query(
      	  "/movie?request_mode=ajax",
      	  {'movie_form':movie_form, 'links' : curlinks, 'id' : id, 'ajax_action' : 'edit_movie'},
      	  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_movie').css('height','0px').hide();
                	 $('#error_div').hide();
                	 $('#item_inner').hide().html(result.content).show();
                	 $('#add_movie').css('height','').show();*/
                	 
                	 /*$("#add_movie").each( function() {
                                                    this.reset();
    		     	                                });
                                                    
                     document.location = '/movie/' + id;*/
                }
             }
      	  },
      	  true
      );   
}
