//
function send_message(url)
{
    var id = jQuery('#answer_id').val();
    var text = jQuery('#message').val();
    var count_answer = jQuery('#count_answer_'+id).text();
    count_answer++;
    
    jQuery.post(url,{id:id,text:text},function(data){
        if(data == 1)
        {
            jQuery('#count_answer_'+id).text(count_answer);
            jQuery.jGrowl('Сообщение отправленно.');
        
        }else jQuery.jGrowl(data);
        
    });
    tb_remove();
    return false;
}
//
function answer(id)
{
    jQuery('#answer_id').val(id);
    jQuery('#answer_link').click();
    
    return false;
}
//
function check_settings_form()
{
    var where = [];
    var _return = true;
    
    jQuery("select[name*='user_metro']").each(function(){
         var value = jQuery(this).find("option:selected").val();
         if(value > 0)
            if(in_array(value,where))
            {
                _return = false;
            }
         where[where.length] = value;
    });
    if(!_return)
        jQuery.jGrowl('Станции метро не должны быть одинаковыми');
    return _return;
}
//
function in_array(what, where)
{
    for(var i=0; i< where.length; i++)
        if(what == where[i]) 
            return true;
    return false;
}

function text_limit(id,limit,left_id)
{
    var left = limit - jQuery("#"+id).val().length;
    if (left < 0) {
        left = 0;
        jQuery("#"+id).val(jQuery("#"+id).val().substr(0,limit));
        //return false;
    }
    if(left_id)
        jQuery("#"+left_id).html(left);
    else
        jQuery("#description_left").html(left);
}

function init_carusel()
{
    //jQuery('#mygallery').html(jQuery('#mygallery1').html());
}

jQuery(document).ready(function(){
     
    //jQuery('#mygallery1').hide();
     
     jQuery('a.psevdo').click(function(){
         jQuery('#fileToUpload').click();
         return false;
     });
    
     /*jQuery('#fileToUpload').change(function(){
         ajaxFileUpload();
     }); */
    
    jQuery(".top_search_center #inputString").blur(function(){
        if (!jQuery(this).val())
            jQuery(this).val("Введите название интересующего места, может быть кто-то уже оставил отзыв");
    });
    jQuery(".top_search_center #inputString").focus(function(){
        if (jQuery(this).val() == "Введите название интересующего места, может быть кто-то уже оставил отзыв")
            jQuery(this).val('');        
    });
    
    jQuery("input.restaurant_name").blur(function(){
        if (!jQuery(this).val())
            jQuery(this).val("Название места");
    });
    jQuery("input.restaurant_name").focus(function(){
        if (jQuery(this).val() == "Название места")
            jQuery(this).val('');        
    });
    
    jQuery('.auto-submit-star').rating({
        callback: function(value, link){
            var prop = jQuery('#raiting_form').serialize();
            var url = jQuery('#raiting_form').attr('action');
            //var friends_comment_flag = jQuery('#review_only_checkbox').attr('checked');
            /*mark = '';
            for(i = 5; i < 8; ++i){
                if(prop.charAt(i) != '&')
                    mark += prop.charAt(i);
            }

            //mark = prop.charAt(5);
            if(mark == '')
                mark = 0;
            parseFloat(mark);
            url += mark;
            url += '/' + friends_comment_flag;*/
            url += '/1';
            jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
            });
        }
    });

    jQuery('#review_only_checkbox').change(function(){
            var prop = jQuery('#raiting_form').serialize();
            var url = jQuery('#raiting_form').attr('action');
            /*var friends_comment_flag = jQuery('#review_only_checkbox').attr('checked');
            mark = '';
            for(i = 5; i < 8; ++i){
                if(prop.charAt(i) != '&')
                    mark += prop.charAt(i);
            }*/

            //mark = prop.charAt(5);
            /*if(mark == '')
                mark = 0;
            parseFloat(mark);
            url += mark;
            url += '/' + friends_comment_flag;*/
            jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
            });
    });

    /*jQuery('#sort_by').change(function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    });
    
    jQuery('#pagination_number').change(function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    });
    
    jQuery('#want_visit').change(function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    });
    
    jQuery('#best').change(function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    });*/

    jQuery(".my_select .jNiceSelectText").click(function(){
        jQuery(".my_select").parent().parent().find("ul:first").show();
    });
    jQuery(".my_select .jNiceSelectOpen").click(function(){
        jQuery(".my_select").parent().parent().find("ul:first").show();
    });


    /*jQuery("#profile_form").submit(function(){
        var textarea =  jQuery(this).find('textarea');
        if(textarea.val().length > 0){
            return true;
        } else {
            jQuery.jGrowl('Заполните следующие поля "О себе", "Интересы"!');
            return false;
        }
    });*/
});
/**/
    var sort_by_form = function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    }
    
    var pagination_number_form = function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    }
    
    var want_visit_form = function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    }
    
    var best_form = function(){
        var prop = jQuery('#raiting_form').serialize();
        var url = jQuery('#raiting_form').attr('action');
        jQuery.post(url,prop,function(data){
                jQuery('.right_content').html(data);
        });
    }
/**/
function checkForm(form){
    for(i = 0; i < 10; ++i){
        if(form.elements[i].checked)
            return true;
    }
    jQuery.jGrowl('Вы должны поставить оценку бизнесу!');
    return false;
}

function load_more(url)
{
    var id = jQuery('#load_more_id').val();
    var from = jQuery('#load_more_from').val();
    
    jQuery.post(url,{id:id,from:from},function(data){
        if(data.html == '')
            jQuery('#form_download_more').html('');
        jQuery('#load_more').append(data.html);
        jQuery('#load_more_from').val(data.from_page);
    },'json');
}

function load_more_category(url)
{
    var id = jQuery('#load_more_id').val();
    var from = jQuery('#load_more_from').val();

    jQuery.post(url,{id:id,from:from},function(data){
        if(data.html == '')
            jQuery('#form_download_more').html('');
        jQuery('#form_download_more').before(data.html);
        jQuery('#load_more_from').val(data.from_page);
        if(data.count <= 10)
            jQuery('#load_more_img').hide();
    },'json');
}

jQuery(document).ready(function(){
    jQuery('#load_more_img').click(function(){
        var url = jQuery('#load_more').attr('action');
        var prop = jQuery('#load_more').serialize();
        jQuery.post(url, prop, function(data){
            jQuery('#load_more').before(data.html);
            if(data.all_count <= $('#ajax_limit').val())
                $('#load_more').hide();
        }, 'json');
        limit = parseInt($('#ajax_limit').val()) + 10;
        $('#ajax_limit').val(limit);
    });
    
});
    


function clear_merge(url,ind)
{
    jQuery.post(url,{ind:ind},function(data){
        if(data == 1)
            jQuery('#clear_'+ind).hide();
    });
    return false;
}

