$(function(){
	var siteHeight = $("body").height() - 148;
	$("#site").css("min-height", siteHeight + "px");
	$("ul#menu li:not(.active)")
		.mouseover(function(){
			$("a span", this).stop().animate({
				top: '34'
			}, 250, function(){
			});
		})
		.mouseleave(function(){
			$("a span", this).stop().animate({
				top: '4'
			}, 250, function(){
			});
		});
	$("ul.divisions li")
		.mouseover(function(){
			$("a span", this).stop().animate({
				left: '258'
			}, 350, function(){
			});
		})
		.mouseleave(function(){
			$("a span", this).stop().animate({
				left: '5'
			}, 350, function(){
			});
		});
	$("select, input:checkbox, input:radio, input:file").uniform();
	$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'facebook',slideshow:2000, autoplay_slideshow: false}); 
	$("#message_form").submit(function(){
		if (($("#message_name").val() == '') || ((($("#message_phone").val() == '') || ($("#message_phone").val().length < 7)) && (($("#message_email").val() == '') || (!$("#message_email").val().match(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)))) || ($("#message_text").val() == '')) {
			$("#js_messages").html('<p class="admin_error">Hiba: nem, vagy nem megfelelően töltötte ki a kötelező mezőket (név, telefonszám vagy e-mail, üzenet)!</p>');
			return false;
		} else {
			return true;
		}
	});
	$("#product_color").change(function(){
		var ral = $("#product_color option:selected").val();
		getMotipSize(ral);
	});
	$("#product_qty").change(function(){
		var ral = $("#product_color option:selected").val();
		var size = $("#product_qty option:selected").val();
		getMotipProducts(ral, size);
	});
	$("#filter_text").keyup(function(){
		var search = $("#filter_text").val();
		getChemicals(search);
	});
	var most = new Date();
	var evszam = most.getFullYear();
	$("#confirmation").val(evszam);
});

$(window).resize(function() {
	var siteHeight = $("body").height() - 148;
	$("#site").css("min-height", siteHeight + "px");
});

function getMotipSize(ral) {
	var lang = $("#site_lang").val();
	$.get("/" + lang + "/ajax-motip-size/" + ral + "/", function(theXML){
		$('#product_qty').find('option').remove();
		var option = $('<option disabled="disabled">(kérem, válasszon)</option>');
		$("#product_qty").append(option);
		var j = 0;
		var lastval;
		$('size',theXML).each(function(i){
			lastval = $(this).text();
			option = $('<option value="' + $(this).text() + '">' + $(this).text() + '</option>');
			$("#product_qty").append(option);
			$("#product_qty").removeAttr("disabled");
			j++;
		});
		var img_exists = $(theXML).find('img_exists').text();
		var img = $("#product_img").attr("src");
		if (img_exists == "1") {
			img = img.substring(0, img.lastIndexOf("/")) + "/ral_" + ral + ".jpg";
		} else {
			img = img.substring(0, img.lastIndexOf("/")) + "/no_image.hu.jpg";
		}
		$("#product_img").attr("src", img);
		$("#products_div").empty();
		if (j == 1) {
			$("#product_qty option[selected]").removeAttr("selected");
			$("#product_qty option[value='" + lastval + "']").attr("selected", "selected");
			getMotipProducts(ral, lastval);
		}
		$.uniform.update("#product_qty");
	});
}

function getMotipProducts(ral, size) {
	var lang = $("#site_lang").val();
	$.get("/" + lang + "/ajax-motip-products/" + ral + "/" + size + "/", function(theXML){
		$("#products_div").empty();
		var products_table = '<table class="product_table" cellspacing="4" cellpadding="0"><thead><tr><th>Termék megnevezés</th><th class="right">Termék egységár</th><th></th></tr></thead><tbody>';
		var i = 1;
		$('item',theXML).each(function(i){
			products_table += '<tr' + (i % 2 != 0 ? ' class="even"' : '') + '><td>' + $(this).find("name").text();
			if (($(this).find("light").text() != '') || ($(this).find("colour").text() != '')) {
				if (($(this).find("light").text() != '') && ($(this).find("colour").text() != '')) {
					products_table += ' (' + $(this).find("light").text() + ', ' + $(this).find("colour").text() + ')';
				} else {
					if ($(this).find("light").text() != '') {
						products_table += ' (' + $(this).find("light").text() + ')';
					} else {
						products_table += ' (' + $(this).find("colour").text() + ')';
					}
				}
			}
			if ($(this).find("text").text() != '') {
				products_table += '<div class="motip_text">' + $(this).find("text").text() + '</div>';
			}
			products_table += '</td><td class="right">' + $(this).find("price").text() + ' Ft</td><td class="order"><a class="button_63" href="/' + lang + '/order/motip/' + $(this).find("id").text() + '/?iframe=true&amp;width=968&amp;height=752" rel="prettyPhoto[iframe_' + i + ']">rendelés</a>' + '</td></tr>';
			i++;
		});
		products_table += '</tbody></table>';
		product_table = $(products_table);
		$("#products_div").append(products_table);
		$("a[rel^='prettyPhoto']").prettyPhoto();
	});
}

function getChemicals(search) {
	var lang = $("#site_lang").val();
	$.get("/" + lang + "/ajax-chem-search/" + search + "/", function(theXML){
		$("#products_div").empty();
		var products_table = '<table class="product_table" cellspacing="4" cellpadding="0"><thead><tr><th>Termék megnevezés, leírás</th><th>Kiszerelés</th><th class="th_price right">Ár (EUR/kg)</th><th></th></tr></thead><tbody>';
		var i = 1;
		$('item',theXML).each(function(i){
			products_table += '<tr' + (i % 2 != 0 ? ' class="even"' : '') + '><td>' + $(this).find("name").text() + ($(this).find("text").text() != '' ? '<div class="chem_text">' + $(this).find("text").text() + '</div>' : '') + '</td><td>' + $(this).find("size").text() + '</td><td class="right">' + $(this).find("price").text() + '</td><td class="order pdf_order">' + ($(this).find("file").text() == 'true' ? '<a class="button_37" href="/files/pdf/chemicals/' + $(this).find("name").text() + '.' + lang + '.pdf">.pdf</a>' : '<span class="button_37_disabled">.pdf</span>') + '<a class="button_63" href="/' + lang + '/order/chemicals/' + $(this).find("id").text() + '/?iframe=true&amp;width=968&amp;height=752" rel="prettyPhoto[iframe_' + i + ']">rendelés</a>' + '</td></tr>';
			i++;
		});
		products_table += '</tbody></table>';
		product_table = $(products_table);
		$("#products_div").append(products_table);
		$("a[rel^='prettyPhoto']").prettyPhoto();
	});
}
