function validateFileExtension(fld) {
    if(!/(\.png|\.gif|\.jpg|\.jpeg)$/i.test(fld.value)) {
        //alert("Érvénytelen fájltípus.");
        $("#dialog-file-extension").dialog({
            resizable: false,
            modal: true,
            buttons: {
                Ok: function() {
                    $(this).dialog('close');
                }
            }
        });
        fld.form.reset();
        fld.focus();
        return false;
    }
    return true;
}

function user_registration() {
    $("#dialog-message-registration").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                window.location='/belepes';
            }
        }
    });
}

function user_activate() {
    $("#dialog-message-activate").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                window.location='/belepes';
            }
        }
    });
}

function recept_upload() {
    if (document.forms['recipeuploadform'].photo.value=="") {
        $( "#dialog-confirm-receptek-upload" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Igen": function() {
                    $( this ).dialog( "close" );
                    document.forms['recipeuploadform'].submit();
                },
                "Nem": function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    } else {
        document.forms['recipeuploadform'].submit();
    }
}

function recept_presave() {
    //    if (document.forms['recipeuploadform'].photo.value=="") {
    //        $( "#dialog-confirm-receptek-upload" ).dialog({
    //            resizable: false,
    //            height:140,
    //            modal: true,
    //            buttons: {
    //                "Igen": function() {
    //                    $( this ).dialog( "close" );
    //                    document.forms['recipeuploadform'].submit();
    //                },
    //                "Nem": function() {
    //                    $( this ).dialog( "close" );
    //                }
    //            }
    //        });
    //    } else {
    document.forms['recipeuploadform'].submit();
//    }
}

function validateForm()
{
    var x=document.forms["commentform"]["comment_text"].value
    if (x==null || x=="")
    {
        return false;
    }
}

function addRow(tableID) {    
    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    if(rowCount<"21") {
        var row = table.insertRow(rowCount);
        rowCount = rowCount+1;
        row.setAttribute('id', rowCount);

        var cell3 = row.insertCell(0);

        var element2 = document.createElement("input");
        element2.type = "text";
        element2.name = "ingredients[]";
        element2.className = "search_input_ingredients";
        cell3.appendChild(element2);

        var elementDiv = document.createElement("div");
        elementDiv.className = "plus_ingredient_div";
        elementDiv.innerHTML = "<a href=\'#\' onclick=\'deleteRow("+rowCount+");'><img src=\'/view/images/btn_hv_x.png\'></a>";
        elementDiv.style.cursor="pointer";
        cell3.appendChild(elementDiv);
    }


}

function deleteRow(rowID) {
    var browserName=navigator.appName;
    if (browserName=="Microsoft Internet Explorer") {
        var container = document.getElementById('dataTable');
        for(i=0; i<=container.childNodes[0].rows.length; i++) {
            if(rowID==container.childNodes[0].rows[i].id) {
                container.childNodes[0].removeChild(container.childNodes[0].rows[i]);
            }
        }
    } else {
        table = document.getElementById("dataTable");
        tbody = table.childNodes[1];
        removed = document.getElementById(rowID);
        tbody.removeChild(removed);
    }
}

$(document).ready(function() {

    var currentPosition = 0;
    var slideWidth = 1020;
    var slides = $('.slide');
    var numberOfSlides = slides.length;

    $('#slidesContainer').css('overflow', 'hidden');

    slides
    .wrapAll('<div id="slideInner"></div>')
    .css({
        'float' : 'left',
        'width' : slideWidth
    });

    $('#slideInner').css('width', slideWidth * numberOfSlides);

    //    // Insert controls in the DOM
    //    $('#slideshow')
    //    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    //    .append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Hide left arrow control on first load
    manageControls(currentPosition);

    // Create event listeners for .controls clicks
    $('.control')
    .bind('click', function(){
        // Determine new position
        currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;

        // Hide / show controls
        manageControls(currentPosition);
        // Move slideInner using margin-left
        $('#slideInner').animate({
            'marginLeft' : slideWidth*(-currentPosition)
        });
    });

    // manageControls: Hides and Shows controls depending on currentPosition
    function manageControls(position){
        // Hide left arrow if position is first slide
        if(position==0){
            $('#leftControl').hide()
        } else{
            $('#leftControl').show()
        }
        // Hide right arrow if position is last slide
        if(position==numberOfSlides-1){
            $('#rightControl').hide()
        } else{
            $('#rightControl').show()
        }
    }


    var currentPositiontwo = 0;
    var slideWidthtwo = 450;
    var slidestwo = $('.slidetwo');
    var numberOfSlidestwo = slidestwo.length;

    // Remove scrollbar in JS
    $('#slidesContainertwo').css('overflow', 'hidden');

    // Wrap all .slides with #slideInner div
    slidestwo
    .wrapAll('<div id="slideInnertwo"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidthtwo
    });

    // Set #slideInner width equal to total width of all slides
    $('#slideInnertwo').css('width', slideWidthtwo * numberOfSlidestwo);

    //    // Insert controls in the DOM
    //    $('#slideshow')
    //    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    //    .append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Hide left arrow control on first load
    manageControlstwo(currentPositiontwo);

    // Create event listeners for .controls clicks
    $('.controltwo')
    .bind('click', function(){
        // Determine new position
        currentPositiontwo = ($(this).attr('id')=='rightControltwo') ? currentPositiontwo+1 : currentPositiontwo-1;

        // Hide / show controls
        manageControlstwo(currentPositiontwo);
        // Move slideInner using margin-left
        $('#slideInnertwo').animate({
            'marginLeft' : slideWidthtwo*(-currentPositiontwo)
        });
    });

    // manageControls: Hides and Shows controls depending on currentPosition
    function manageControlstwo(positiontwo){
        // Hide left arrow if position is first slide
        if(positiontwo==0){
            $('#leftControltwo').hide()
        } else{
            $('#leftControltwo').show()
        }
        // Hide right arrow if position is last slide
        if(positiontwo==numberOfSlidestwo-1){
            $('#rightControltwo').hide()
        } else{
            $('#rightControltwo').show()
        }
    }

    var currentPositionthree = 0;
    var slideWidththree = 450;
    var slidesthree = $('.slidethree');
    var numberOfSlidesthree = slidesthree.length;

    // Remove scrollbar in JS
    $('#slidesContainerthree').css('overflow', 'hidden');

    // Wrap all .slides with #slideInner div
    slidesthree
    .wrapAll('<div id="slideInnerthree"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidththree
    });

    // Set #slideInner width equal to total width of all slides
    $('#slideInnerthree').css('width', slideWidththree * numberOfSlidesthree);

    //    // Insert controls in the DOM
    //    $('#slideshow')
    //    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    //    .append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Hide left arrow control on first load
    manageControlsthree(currentPositionthree);

    // Create event listeners for .controls clicks
    $('.controlthree')
    .bind('click', function(){
        // Determine new position
        currentPositionthree = ($(this).attr('id')=='rightControlthree') ? currentPositionthree+1 : currentPositionthree-1;

        // Hide / show controls
        manageControlsthree(currentPositionthree);
        // Move slideInner using margin-left
        $('#slideInnerthree').animate({
            'marginLeft' : slideWidththree*(-currentPositionthree)
        });
    });

    // manageControls: Hides and Shows controls depending on currentPosition
    function manageControlsthree(positionthree){
        // Hide left arrow if position is first slide
        if(positionthree==0){
            $('#leftControlthree').hide()
        } else{
            $('#leftControlthree').show()
        }
        // Hide right arrow if position is last slide
        if(positionthree==numberOfSlidesthree-1){
            $('#rightControlthree').hide()
        } else{
            $('#rightControlthree').show()
        }
    }


    var currentPositionfour = 0;
    var slideWidthfour = 560;
    var slidesfour = $('.slidefour');
    var numberOfSlidesfour = slidesfour.length;

    // Remove scrollbar in JS
    $('#slidesContainerfour').css('overflow', 'hidden');

    // Wrap all .slides with #slideInner div
    slidesfour
    .wrapAll('<div id="slideInnerfour"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidthfour
    });

    // Set #slideInner width equal to total width of all slides
    $('#slideInnerfour').css('width', slideWidthfour * numberOfSlidesfour);

    //    // Insert controls in the DOM
    //    $('#slideshow')
    //    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    //    .append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Hide left arrow control on first load
    manageControlsfour(currentPositionfour);

    // Create event listeners for .controls clicks
    $('.controlfour')
    .bind('click', function(){
        // Determine new position
        currentPositionfour = ($(this).attr('id')=='rightControlfour') ? currentPositionfour+1 : currentPositionfour-1;

        // Hide / show controls
        manageControlsfour(currentPositionfour);
        // Move slideInner using margin-left
        $('#slideInnerfour').animate({
            'marginLeft' : slideWidthfour*(-currentPositionfour)
        });
    });

    // manageControls: Hides and Shows controls depending on currentPosition
    function manageControlsfour(positionfour){
        // Hide left arrow if position is first slide
        if(positionfour==0){
            $('#leftControlfour').hide()
        } else{
            $('#leftControlfour').show()
        }
        // Hide right arrow if position is last slide
        if(positionfour==numberOfSlidesfour-1){
            $('#rightControlfour').hide()
        } else{
            $('#rightControlfour').show()
        }
    }



    $("#contestbutton").click(function() {
        $("#recipe").toggle("blind", 500);
    });

    $("#prizebutton").click(function() {
        $("#prize").toggle("blind", 500);
    });


    $("#message").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            //                $("#overlay").fadeOut();
            }
        }
    });
    $("#message-recipe-upload").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                window.location='';
            }
        }
    });
    $("#popupWindow1").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                $("#site").hide();
                $("#loading").show();
                window.location='belepes';
            }
        }
    });
    $("#finish_error").dialog({
        resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
                $("#site").hide();
                $("#loading").show();
                window.location='uploader';
            }
        }
    });

    $( "#dialog" ).dialog( "destroy" );
});


function open_recipes(id) {
    $("#recipe-"+id).toggle("blind", 500);
}

function display_bg() {
    $("#closing_div").show();
}

function show_overlay() {
    $("#overlay").show();
}



jQuery.preloadImages = function()
{
    for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

$.preloadImages("view/images/elements/gomb3_1.jpg", "view/images/elements/gomb3_2.jpg","view/images/elements/gomb3_3.jpg", "view/images/elements/gomb3_4.jpg", "view/images/elements/gomb3_5.jpg", "view/images/elements/gomb3_6.jpg", "view/images/elements/gomb3_7.jpg", "view/images/elements/gomb3_8.jpg", "view/images/elements/gomb3_11.jpg", "view/images/elements/terulet_asc.png", "view/images/elements/terulet_desc.png", "view/images/elements/terulet_full.png", "view/images/elements/ar_asc.png", "view/images/elements/ar_desc.png", "view/images/elements/ar_full.png");


function showprogress() {

    var loaded = 0;

    for (var i=0; i<document.images.length; i++) {

        if (document.images[i].complete) {

            loaded++;

        }

    }

    var percentage = Math.round(100 * loaded / document.images.length);

    if (percentage == 100) {
        window.clearInterval(ID);
        becsuk();
    }

}



function becsuk() {
    setTimeout('$("#loading").hide();',100);
    setTimeout('$("#site").show();',100);
}


function receptek_delete(idrecipe) {
    $( "#dialog-confirm-receptek-delete" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Igen": function() {
                $( this ).dialog( "close" );
                window.location='/receptek/favouritedelete/'+idrecipe;
            },
            "Nem": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

function receptjeim_delete(idrecipe) {
    $( "#dialog-confirm-receptjeim-delete" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Igen": function() {
                $( this ).dialog( "close" );
                window.location='/receptjeim/delete/'+idrecipe;
            },
            "Nem": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

function delete_picture(file) {
    $.ajax({
        url: '/delete_picture/?recept_picture_delete&file='+file,
        success: function(data) {
            $("#photo").html(data);
        }
    });
}


