var counter = 1;
$(document).ready(function(){
    $('.lenght-68').max_length(68);
    $('.lenght-100').max_length(100);
    $('.lenght-500').max_length(500);
    $('.lenght-1000').max_length(1000);
    $('.lenght-2000').max_length(2000);

    $('.email').click(function(){
        $('#email-confirm').show();    
    });

    var rolled = true;
    $('.more_search').click(function(){
        $('#search_advance').toggle('normal');
        if(rolled){
            rolled = false;
            $(this).html('Mniej&lt;&lt;');
        }else{
            $(this).html('Więcej&gt;&gt;');
            rolled = true;
        }
    });
    $('.td_edit').click(edit_click);
    $('.cancel').click(function(){
      $(this).cancel_edit();
    });
    $('.cancel-cell').click(function(){
      $(this).cancel_cell_edit();
    });
    $('.delete').click(function(){
      $(this).delete_edit();
    });
    $('.delete2').click(function(){
      $(this).delete2_edit();
    });
    $('.edit-input').click(rolldown_list);
    $('.update-location').click(function(){
        var item = $('#user_'+$(this).attr('alt')).val();
        jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
            attr:$(this).attr('alt'),value:item}), dataType:'script', type:'post', url:'/users/edit_address'});
    });
    $('.update-input').click(function(){
        var item = $('#user_'+$(this).attr('alt')).val();
        jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
            attr:$(this).attr('alt'),value:item}), dataType:'script', type:'post', url:'/users/edit_profile'});
    });
    $('.update-cell').click(function(){
        var item = $('#user_'+$(this).attr('alt')).val();
        jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
            attr:$(this).attr('alt'),value:item}), dataType:'script', type:'post', url:'/users/edit_profile'});
    });
    $('.cell_edit').click(edit_click);
    $(".gender-male").click(function(){
        $(this).parent().css({'background-position':'top'});
        $(this).parent().attr('alt','Wybrano Mężczyznę');
        $(this).parent().qtip("destroy");
        $(this).parent().gender_qtip();

        $(this).children().attr("checked", "checked");
    });
    $(".gender-female").click(function(){
        $(this).parent().css({'background-position':'bottom'});
        $(this).parent().attr('alt','Wybrano Kobietę');
        $(this).parent().qtip("destroy");
        $(this).parent().gender_qtip();
        $(this).children().attr("checked", "checked");
    });
    $('.tip').qtip({
        content: $(this).attr("alt"),
        show: 'mouseover',
        hide: 'mouseout',
        style: {
            name: 'light',
            tip: 'bottomMiddle'
        },
        position: {
              corner: {
                 target: 'topMiddle',
                 tooltip: 'bottomMiddle'
              }
           }
    });
});

jQuery.fn.gender_qtip = function(){
    $(this).qtip({
        content: $(this).attr("alt"),
        show: 'mouseover',
        hide: 'mouseout',
        style: {
            name: 'light',
            tip: 'bottomMiddle'
        },
        position: {
              corner: {
                 target: 'topMiddle',
                 tooltip: 'bottomMiddle'
              }
           }

    })
}

function edit_click(){
  $(this).children('DIV').css("display", "block");
  $(this).children('span').css("display", "none");
  $(this).unbind('click');
};

function rolldown_list(){
    $(this).rolldown_list();
};

function rollup_list(){
  $(this).css("background-position","right -14px");
  $(this).siblings(".rolldown-list").css("left","-999em")
  $(this).unbind('click');
  $(this).click(rolldown_list);
};

function updateGallery(){
    if(page == 0){
        $('.prev_btn').children('img').attr('src','/images/my_profile/arrow_left_inactive.png'); 
        $('.next_btn').children('img').attr('src','/images/my_profile/arrow_right_active.png');
    }
    else{
        if(page == page_size){
            $('.next_btn').children('img').attr('src','/images/my_profile/arrow_right_inactive.png');
        }
        else{
            $('.next_btn').children('img').attr('src','/images/my_profile/arrow_right_active.png');
        }
        $('.prev_btn').children('img').attr('src', '/images/my_profile/arrow_left_active.png');
    }
    $('#images_spin').animate({marginLeft: '-' +(page * 105)+'px' }, 1000);
}

jQuery.fn.li_click = function(){
    var obj = $(this).parent().siblings('.edit-input');
    var em = obj.children('em');
    em.html($(this).html());
    obj.css("background-position","right -14px");
    obj.siblings(".rolldown-list").css("left","-999em")
    obj.unbind('click');
    obj.click(rolldown_list);
    jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),attr:em.attr('id'),value:$(this).attr('value')}), dataType:'script', type:'post', url:'/users/edit_profile'});
};

jQuery.fn.li_click2 = function(){
    var obj = $(this).parent().siblings('.edit-input');
    var em = obj.children('em');
    em.html($(this).html());
    em.siblings('input').val($(this).attr('value'));
    obj.css("background-position","right -14px");
    obj.siblings(".rolldown-list").css("left","-999em")
    obj.unbind('click');
    obj.click(rolldown_list);
};

jQuery.fn.range_li_click = function(type){
    var obj = $(this).parent().siblings('.edit-input');
    var em = obj.children('em');
    em.html($(this).html());
    em.siblings('input').val($(this).attr('value'));
    obj.css("background-position","right -14px");
    obj.siblings(".rolldown-list").css("left","-999em")
    if(type == 'min'){
        var second_em = $('#'+em.attr('id').substring(0,em.attr('id').length-3)+'max');
        if(second_em.html() == ''){ 
            var em_val = second_em.parent().siblings('ul').children(":last").html();
            second_em.html(em_val);
            second_em.siblings('input').val(em_val);
        }else{
            var em_val = Number(em.html());
            if(em_val >= Number(second_em.html())){
                second_em.html(em_val+1);
                second_em.siblings('input').val(em_val+1);
            }
        }

    }else{
        var second_em = $('#'+em.attr('id').substring(0,em.attr('id').length-3)+'min');
        if(second_em.html() == ''){
            var em_val = second_em.parent().siblings('ul').children(":first").html();
            second_em.html(em_val);
            second_em.siblings('input').val(em_val);
        }else{
            var em_val = Number(em.html());
            if(em_val <= Number(second_em.html())){
                second_em.html(em_val-1);
                second_em.siblings('input').val(em_val-1);
            }
        }
    }
    obj.unbind('click');
    obj.click(rolldown_list);
};

jQuery.fn.max_length = function(max_length){

    $(this).keyup(function(){
        var length = this.value.length;
        if(length > max_length){
            var text = $(this).val().substring(0, max_length);
            $(this).val(text);
        }
    });
    
    $(this).keypress(function(event){
        var key = event.which;
        if(key >= 33 || key == 13) {
            var length = this.value.length;
            if(length == max_length) {
                event.preventDefault();
            }
        }
    });
};

jQuery.fn.rolldown_list = function(){
      $(this).css("background-position","right 2px");
      $(this).siblings(".rolldown-list").css("left","auto")
      $(this).unbind('click');
      $(this).click(rollup_list);
};

jQuery.fn.cancel_edit = function(){
    $(this).parent().css("display", "none");
    $(this).parent().siblings('span').css("display", "block");
    $(this).parent().parent().mousemove(function(){
      $(this).unbind('mousemove');
      $(this).click(edit_click);
    });
};

jQuery.fn.cancel_cell_edit = function(){
    $(this).parent().siblings('.edit').css("display", "none");
    $(this).parent().css("display", "none");
    $(this).parent().siblings('span').css("display", "block");
    $(this).parent().parent().mousemove(function(){
      $(this).unbind('mousemove');
      $(this).click(edit_click);
    });
};

jQuery.fn.delete_edit = function(){
    var obj = $(this).parent();
    var em = obj.children("div").children("div").children("em");
    em.html("Brak");
    obj.css("background-position","right -14px");
    obj.siblings(".rolldown-list").css("display","none")
    obj.unbind('click');
    obj.click(rolldown_list);
    jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),attr:em.attr('id'),value:null}), dataType:'script', type:'post', url:'/users/edit_profile'});
};

jQuery.fn.delete2_edit = function(){
    var obj = $(this).siblings('div');
    var em = obj.children('em');
    jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),attr:em.attr('id'),value:null}), dataType:'script', type:'post', url:'/users/edit_profile'});
};

jQuery.fn.custom_spinner = function(min, max,val){
    $(this).slider({
        range: "min",
        value: val,
        min: min,
        max: max,
        width: 150,
        slide: function(event, ui) {
            $(this).siblings('em').html(ui.value);
        },
        change: function(event, ui) {
            var em = $(this).siblings('em');
            jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),attr:em.attr('id'),value:ui.value}), dataType:'script', type:'post', url:'/users/edit_profile'});
        }
    });
  };

jQuery.fn.custom_range_spinner = function(min, max, min_val, max_val){
    $(this).slider({
        range: true,
        values: [min_val, max_val],
        min: min,
        max: max,
        width: 130,
        slide: function(event, ui) {
            $(this).children('a:first').html(ui.values[0]);
            $(this).children('a:last').html(ui.values[1]);
            $(this).siblings('.spin-range').val(ui.values[0]+':'+ui.values[1]);
        },
        change: function(event, ui) {
            $(this).children('a:first').html(ui.values[0]);
            $(this).children('a:last').html(ui.values[1]);
            $(this).siblings('.spin-range').val(ui.values[0]+':'+ui.values[1]);
        }
    });
    $(this).children('a:first').html(min_val);
    $(this).children('a:last').html(max_val);
  };