$(document).ready(function() {

    // step 1

    $("#recipes_step1").validate({
        
        rules : {
            title: {
                required: true
            }
        },
        
        messages: {
            title : {
                required:  ""
            }
        }
    });
    
    $("#recipes_step3").validate({
        
        rules : {
            PERSON_AMOUNT: {
                required: true
            }
        },
        
        messages: {
            PERSON_AMOUNT : {
                required:  ""
            }
        }
    });
    
    
    $("#recipes_step6").validate({
        
        rules : {
            name: {
                required: true
            },
            surname: {
                required: true
            },
            phone: {
                required: true
            },
            email: {
                required: true
            }
        },
        
        messages: {
            name : {
                required:  ""
            },
            surname : {
                required:  ""
            },
            phone : {
                required:  ""
            },
            email : {
                required:  ""
            }
        }
    });
    
    
    $("#add_comment").validate({
        
        rules : {
            name: {
                required: true
            },
            comment: {
                required: true
            }
        },
        
        messages: {
            name : {
                required:  ""
            },
            comment : {
                required:  ""
            }
        }
    });
    

    fixBlocks();
    
    $('.voting a').bind("mouseenter mouseleave", function(){
        $(this).next().prevAll().each(function(){
            if($(this).hasClass('gray')) {
                $(this).removeClass('gray').addClass('color').children('img').attr('src', '/public/media/site/icons/ico_7.gif');
            } else {
                $(this).removeClass('color').addClass('gray').children('img').attr('src', '/public/media/site/icons/ico_8.gif')   
            }
        })
    })
    
    $('.voting a').click(function(){
        
        var recipeId = $(this).attr('rec');
        var vote = $(this).attr('rel');
        
        $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/vote",
            data: { vote: vote, recipeId: recipeId},
            success: function(resp) {
                $('.voting').html('<strong>'+ resp + '</strong>');
                fixBlocks();
            }
        });
    })

});

fixBlocks = function() {
    var rez_block = $('.right_bott_bl').height();
    var good_size = $('.main_content').height();
    var right_block = $('.rightbar_2_content').height();
    
    var additionalBlock = $('.block_10');

    if(additionalBlock.height() != null)
    {
        var good_size = $('.block_9').height();
        $('.right_bott_bl').height(((good_size + additionalBlock.height()) - right_block) + rez_block + 17);
    }
    else
    {
        $('.right_bott_bl').height((good_size - right_block) + rez_block + 100);
    }
}

    // step 2
    
addNewDishType = function(string, parent_type_id, target_id) {

    if(string.length > 2) {
    
        $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/addNewDishType",
            data: { str: string, parent_type: parent_type_id},
            success: function(resp) {
                var data_resp = resp.split(';');
                xajax_step2_addDishType(data_resp[1], data_resp[0], target_id);
                fixBlocks();
            }
        });   
        
    }
}

function serializeData() {
    
    var controls = new Array('dish_type_container', 'dish_type_container_veg', 'dish_type_container_sv', 'dish_type_container_food');
    
    var str = '';
    
    $("ul#" + controls[0]).children().each(function(i) {
            var li = $(this);
            str += li.attr('id') + ';';
    });
    
    $("ul#" + controls[1]).children().each(function(i) {
            var li = $(this);
            str += li.attr('id') + ';';
    });
    
    $("ul#" + controls[2]).children().each(function(i) {
            var li = $(this);
            str += li.attr('id') + ';';
    });
    
    $("ul#" + controls[3]).children().each(function(i) {
            var li = $(this);
            str += li.attr('id') + ';';
    });
    
    $('#serialized_data').val(str);
    fixBlocks();
}

addNewIngridient = function(container_id, _container_nr) {
    
    total_elements = $('#'+container_id).children("table").size();
    parent_totals = $('#main_container').children("div").size();
    parent_totals = parseInt(parent_totals) -1;

    $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/addNewIngridient",
            data: {total: total_elements, parent_total: _container_nr },
            success: function(resp) {
                $('#'+container_id).append(resp);
                var element = 'ing_' + _container_nr + '_' + total_elements;
                var url = BASE_URL + 'lt/receptai/liveSearch2/5/' + total_elements + '/' + _container_nr + '/';
                $("#"+element).autocomplete(url, {
                            width: 204,
                            selectFirst: true,
                            scroll: true,
                            scrollHeight: 300
                        });
                fixBlocks();
            }
        });
}


addNewDishPart = function(container_id) {
    
    parent_total = $('#main_container').children("div").size();
    total_elements = 0;
    
    $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/addNewDishPart",
            data: { total: total_elements, parents: parent_total },
            success: function(resp) {
                $('#'+container_id).append(resp);
                var element = 'ing_' + parent_total + '_' + total_elements;
                var url = BASE_URL + 'lt/receptai/liveSearch2/5/0/' + parent_total + '/';
                $("#"+element).autocomplete(url, {
                            width: 204,
                            selectFirst: true,
                            scroll: true,
                            scrollHeight: 300
                        });
                fixBlocks();
            }
        });
}

removeIngridient = function(tblObj, pk, ing_pk) {
    var ing_name = 'ing_'+ pk +'_'+ing_pk;
    $('#'+ing_name).val('');
    $(tblObj).css('border', '1px solid #FB9191').fadeOut(function(){
        $(this).remove();
        fixBlocks();
    });

    if($(tblObj).parent('div').children('table').size() == 1) {
        $('#'+(pk+1)).fadeOut(function(){
            $(this).remove();
            fixBlocks();
        });
    }
}


addNewStep = function(container_id) {
    
    total_elements = $('#'+container_id).children("table").size();
    
    $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/getNewStep",
            data: {total: total_elements},
            success: function(resp) {
                $('#'+container_id).append(resp);
                fixBlocks();
            }
        });
}

removeStep = function(element_id) {
    
    $('#' + element_id).fadeOut(function(){
        $(this).remove();
        fixBlocks();
    });
}

removeImage = function(obj, imageId, recipeId) {
    $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/removeImage",
            data: {imageId: imageId, recipeId: recipeId},
            success: function(resp) {
                $(obj).fadeOut(function(){
                    $(this).remove();
                    fixBlocks();
                });
            }
        });
}

savePhotoDescription = function(value, photo_id) {
    
    $.ajax({
            type: "POST",
            url: BASE_URL + "lt/receptai/savePhotoDescription",
            data: {imageId: photo_id, text: value},
            success: function(resp) {
                fixBlocks();
            }
        });
}


saveStep5 = function() {
    if($('#agree:checked').length == 1) 
    {
        $('#recipes_step5').submit();
    }
    else
    {
        $('.taisykles').css('background', '#FEE2E2');
        $('.taisykles').css('border', '1px solid red');
    } 
    
    fixBlocks();
}


showGallery = function() {
    $('#video').hide();
    $('#gallery').show();
    
    fixBlocks();
}

showVideo = function () {
    $('#gallery').hide();
    $('#video').show();
    
    fixBlocks();
}


