$(document).ready(function() {
    initReCaptcha();
    initMitAjanlasz();
    $.datepicker.setDefaults($.datepicker.regional['hu']);
    $(".jqdate").datepicker();
});

function pager(actual) {
    document.getElementById('pageractual').value = actual;
    document.getElementById('pagerform').submit();
}

function pager2(actual) {
    document.getElementById('p').value = actual;
    document.getElementById('pagerform').submit();
}

function initReCaptcha() {
    $('#other_captcha_image').click(function() {
        $('#captcha_img').attr('src', '/captcha.php?sid=' + $("#siid").val() + "&chid=" + (new Date()).getTime());
        return false;
    });
}

function initMitAjanlasz() {
    $("#content_suggest_item").autocomplete("/mit_ajanlasz.php",{
        minLength: 1,        
        formatItem: formatItem2,
        formatResult: formatResult2
    });
    
    $('#content_suggest_item').result(function(event, data, formatted) {
        var a = formatted.split(";");
        $("#content-suggest2").attr("action", "/receptek/" + a[2] + "/" + a[0] + "/");
    });        
    
    function formatItem2(row) {
        var a = row[0].split(";");
        return jQuery.trim(a[1]);
    }
    function formatResult2(row) {
        var a = row[0].split(";");
        return  jQuery.trim(a[1]);
    }    
}

$(function() {



/* legördülő menük */

	$.each($(".form-dropdown-searchbox"), function() {
		var e = $(this);
		e.css("display", "none");
		var html = '<div class=" selected-item ' + e.children().first().attr("value") + '" id="selected-item-gys" style="cursor: pointer;">' + e.children(":selected").html() + '</div>';
		e.parent().append(html);
		var ef = e.parent().find("div").first();
		html = '<div class="form-dropdown-rest-wrapper" style="z-index: 1500000; position: relative;"><div class="form-dropdown-rest" style="margin-left:-6px; padding: 0 0 0 6px; position: absolute; left: 0; display: none; z-index: 999;">';
		$.each(e.children(), function() {
			html = html + '<div class="' + $(this).attr("value") + '" style="padding: 0 0 0 4px; cursor: pointer; border-right: none; background: #fff;">' + $(this).html() + '</div>';
		});
		html = html + '</div></div>';
		e.parent().append(html);
		var er = e.parent().find(".form-dropdown-rest");
		ef.click(function() {
			if(er.css("display") == "none") {
				$(".form-dropdown").parent().find(".form-dropdown-rest").stop().slideUp('fast', function() {});
				er.stop().slideDown('fast', function() {});
			}
			else {
				e.find('option[value=' + $(this).attr("class") + ']').attr('selected', 'selected');
				er.stop().slideUp('fast', function() {});
			}
		});
		$.each(er.children(), function() {
			var ec = $(this);
			ec.click(function() {
				e.find('option[value=' + ec.attr("class") + ']').attr('selected', 'selected');
				ef.text(ec.html().replace(/&nbsp;/ig, ''));
				ef.attr("class", ec.attr("class"));
				er.stop().slideUp('fast', function() {});
			});
			ec.mouseover(function() {
				ec.css({"color" : "#fff", "background" : "#3ca1bd"});
			});
			ec.mouseout(function() {
				ec.css({"color" : "#3ca1bd", "background" : "#fff"});
			});
		});
	});



	$.each($(".form-dropdown"), function() {
		var e = $(this);
		e.css("display", "none");
		var html = '<div class="selected-item ' + e.children().first().attr("value") + '" id="selected-item-gys" style="cursor: pointer;">' + e.children(":selected").html() + '</div>';
		e.parent().append(html);
		var ef = e.parent().find("div").first();
		html = '<div class="form-dropdown-rest-wrapper" style="margin-left:-1px; z-index: 1500000; position: relative;"><div class="form-dropdown-rest" style="padding: 0 0 0 6px; position: absolute; left: 0; display: none; z-index: 999;">';
		$.each(e.children(), function() {
			html = html + '<div class="' + $(this).attr("value") + '" tittle="'+$(this).attr("tittle")+'" style="padding: 0 0 0 4px; cursor: pointer; background: #fff;">' + $(this).html() + '</div>';
		});
		html = html + '</div></div>';
		e.parent().append(html);
		var er = e.parent().find(".form-dropdown-rest");
		ef.click(function() {
			if(er.css("display") == "none") {
				$(".form-dropdown").parent().find(".form-dropdown-rest").stop().slideUp('fast', function() {});
				er.stop().slideDown('fast', function() {});
			}
			else {
				e.find('option[value=' + $(this).attr("class") + ']').attr('selected', 'selected');
				er.stop().slideUp('fast', function() {});
			}
		});
		$.each(er.children(), function() {
			var ec = $(this);
			ec.click(function() {
				e.find('option[value=' + ec.attr("class") + ']').attr('selected', 'selected');
                              e.find('option[value=' + ec.attr("class") + ']').change();
				ef.text(ec.html().replace(/&nbsp;/ig, ''));
				//ef.attr("class", ec.attr("class"));
				er.stop().slideUp('fast', function() {});
			});
			ec.mouseover(function() {
				ec.css({"color" : "#fff", "background" : "#3ca1bd"});
			});
			ec.mouseout(function() {
				ec.css({"color" : "#3ca1bd", "background" : "#fff"});
			});
		});
	});

/* hozzávalók hozzáadása */


	if($(".add-ingredient-and-or")) {
		var i = 2;
		$(".add-ingredient-and-or").click(function() {
			var html = '<div class="ingredients-and-or-list-row">' +
				'<div class="ingredients-and-or-input">' +
				'<input id="form_upload_ingredient_and_or_a' + i + '" name="form_upload_ingredient_a' + i + '" class="input-text" type="text" />' +
				'</div>' +
				'<div class="ingredients-and-or-radio">' +
				'<input class="input-radio" type="radio" name="group_a' + i + '" value="es" checked />és<br />' +
				'<input class="input-radio" type="radio" name="group_a' + i + '" value="vagy" />vagy' +
				'</div>' +
				'<div class="ingredients-and-or-input">' +
				'<input id="form_upload_ingredient_and_or_b' + i + '" name="form_upload_ingredient_b' + i + '" class="input-text" type="text" />' +
				'</div>' +
				'<div class="ingredients-and-or-radio">' +
				'<input class="input-radio" type="radio" name="group_b' + i + '" value="es" checked />és<br />' +
				'<input class="input-radio" type="radio" name="group_b' + i + '" value="vagy" />vagy' +
				'</div>' +
				'<div class="ingredients-and-or-input">' +
				'<input id="form_upload_ingredient_and_or_c' + i + '" name="form_upload_ingredient_c' + i + '" class="input-text" type="text" />' +
				'</div>' +
				'</div>';
			$(".ingredients-and-or-list").append(html);
			i = i + 1;
		});
	}
});

