
var regMail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var regPhone = /^([0-9]{8,11})$/;


var newCount = 0;
var picCount = 0;
var textCount = 0;
var galCount = new Array();
var html_blockControl = '<div class="sb ui-icon-arrow-4-diag" title="Áthelyezés"></div><div class="c ui-icon-close" title="Törlés"></div><div class="i_spacer ui-icon-arrow-4" title=Térköz beillesztése"></div><div class="i_pic ui-icon-image" title="Kép beillesztése"></div><div class="i_text ui-icon-document" title="Szöveg beillesztése"></div>';
var html_spacerControl = '<div class="s ui-icon-arrow-4-diag" title="Térköz áthelyezése"></div><div class="c ui-icon-close" title="Térköz eltávolítása"></div>';
var html_picControl = '<div class="s ui-icon-arrow-4-diag" title="Kép áthelyezése"></div><div class="c ui-icon-close" title="Kép törlése"></div><div class="pic ui-icon-image" title="Kép feltöltése"></div><div class="gal ui-icon-video" title="Galéria megtekintése"></div>';
var html_textControl = '<div class="s ui-icon-arrow-4-diag" title="Szöveg áthelyezése"></div><div class="c ui-icon-close" title="Szöveg törlése"></div>';
var html_floatControl = '<div class="fl ui-icon-arrowthickstop-1-w" title="Balra igazít"></div><div class="fr ui-icon-arrowthickstop-1-e" title="Jobbra igazít"></div>';
var html_galControl = '<div class="gc ui-icon-close" title="Galéria elem törlése"></div>';
var id = '';
var isGal = false;

jQuery.fn.extend({
	enableEditor: function() {
		return this.each(function() {
			var thisId = $(this).attr("id");

			$(this).parent().css("background-color", "transparent");
			$("textarea#" + thisId).css("visibility", "visible");
			tinyMCE.execCommand('mceAddControl', false, thisId);
		});
	},
	disableEditor: function() {
		return this.each(function() {
			var thisId = $(this).attr("id");

			tinyMCE.execCommand('mceRemoveControl', false, thisId);
			$("textarea#" + thisId).css("visibility", "hidden");
			$(this).parent().css("background-color", "#ddd");
		});
	}
});

$(document).ready(function() {
	var fn_pic = function() {
		$("#gallery_control").hide();
		$("#uploader").hide();
		$("#uploader").show();

		id = $(this).parent().attr("id");
		isGal = false;

		$("#form_uploader").unbind("submit");
		$("#form_uploader").submit(function() {
			$("#" + id).children("a").attr({
				title: $("#uploader_name").val()
			});

			$("#" + id).children("a").children("img").attr({
				title: $("#uploader_name").val(),
				alt: $("#uploader_name").val()
			});
		});

		$("#upload_helper").unbind("load");
		$("#upload_helper").load(function() {
			var pic_fname = $(this).contents().find("#output").html();

			if (!isGal) {
				if (!$("#uploader_origsize").is(":checked")) {
					if (!$("#" + id).is(".pic")) {
						$("#" + id).addClass("pic");
					}

					$("#" + id).children("a").children("img").attr({
						src: 'content/pictures/tn/tn_' + pic_fname
					}).removeClass("tilt");

					$("#" + id).children("a").attr({
						href: 'content/pictures/' + pic_fname
					}).addClass("lightbox");
				} else {
					$("#" + id).children("a").children("img").attr({
						src: 'content/pictures/' + pic_fname
					});

					$("#" + id).addClass("pic_orig").removeClass("pic");
					$("#" + id).children("a").attr({
						href: '#'
					}).addClass("tilt");
				}

				$("#" + id).removeClass("pic_empty");
			} else {
				var pic_name = $("#uploader_name").val();

				if (!$("#gallery").length) {
					$("ul.srt").after('<ul id="gallery"></ul>');
				}

				if (!galCount[id]) {
					galCount[id] = 0;
				}

				do {
					galCount[id]++;
				} while($("#" + id + "_" + galCount[id]).length > 0);

				$("#" + id).children("a").attr({
					rel: id
				});

				$("#gallery").append('<li id="' + id + '_' + galCount[id] + '"><a href="content/pictures/' + pic_fname + '" title="' + pic_name + '" class="lightbox" rel="' + id + '"><img src="content/pictures/tn/tn_' + pic_fname + '" alt="' + pic_name + '" title="' + pic_name + '"></a></li>');

				$("#gallery_control").hide();
			}

			$("#uploader").hide();
		});
	}

	var fn_gal = function() {
		$(this).parent().children("div.pic").trigger("click");

		$("#gal_items ul").empty();

		$("#gallery a[rel=" + id + "]").each(function(i, el) {
			$("#gal_items ul").append('<li class="pic" linkedto="' + $(this).parent().attr("id") + '">' + html_galControl + $(this).html() + '</li>');
		});

		$("#gallery_control").show();

		isGal = true;
	}

	var fn_resizeTextStart = function(event, ui) {
		var thisId = $(this).children("textarea").attr("id");

		$(this).parent().children("li.text").children("textarea").disableEditor();
	}

	var fn_resizeTextStop = function(event, ui) {
		var thisId = $(this).children("textarea").attr("id");

		$(this).parent().children("li.text").children("textarea").enableEditor();

		$("#" + thisId + "_ifr").css("height", $(this).css("height"));
		$("#" + thisId + "_tbl").css("width", $(this).css("width"));
		$("#" + thisId + "_tbl").css("height", $(this).css("height"));
	}

	// end of var

	$("#form_ce").submit(function(event) {
		$("input.h2").each(function() {
			$(this).attr("realval", $(this).val());
		});

		$("input.h3").each(function() {
			$(this).attr("realval", $(this).val());
		});

		$("#c").val($("#ce_c").html());
	});

	$("#btn_submit_ce").click(function(event) {
		$("#form_ce").submit();

		event.preventDefault();
	});

	$("#btn_submit_uploader").click(function(event) {
		$("#form_uploader").submit();

		event.preventDefault();
	});

	$("#btn_submit_subpage").click(function(event) {
		$("#subpage_action").val('create');
		$("#form_subpage").submit();

		event.preventDefault();
	});

	$("#btn2_submit_subpage").click(function(event) {
		$("#subpage_action").val('delete');
		$("#form_subpage").submit();

		event.preventDefault();
	});

	$("ul.srt div.block").prepend(html_blockControl);
	$("ul.srt li.spacer").prepend(html_spacerControl + html_floatControl);
	$("ul.srt li.pic, ul.srt li.pic_orig").prepend(html_picControl + html_floatControl);
	$("ul.srt li.text").prepend(html_textControl + html_floatControl);

	$("ul.srt").sortable({
		axis: "y",
		handle: "div.sb",
		tolerance: "pointer",
		start: function(event, ui) {
			$(ui.item).find("li.text").children("textarea").disableEditor();
		},
		stop: function(event, ui) {
			$(ui.item).find("li.text").children("textarea").enableEditor();
		}
	});

	$("ul.block_elements").sortable({
		handle: "div.s",
		tolerance: "pointer",
		start: function(event, ui) {
			$(this).children("li.text").children("textarea").disableEditor();
		},
		stop: function(event, ui) {
			$(this).children("li.text").children("textarea").enableEditor();
		}
	});

	$("ul.block_elements li.spacer").resizable({
		containment: "parent",
		minHeight: 40,
		minWidth: 50
	});

	$("ul.block_elements li.text").resizable({
		containment: "parent",
		start: fn_resizeTextStart,
		stop: fn_resizeTextStop
	});

	$("div.c").live("click", function() {
		if (confirm('Biztosan törli?')) {
			$(this).parent().empty().remove();
		}
	});

	$("li.pic div.pic, li.pic_orig div.pic").click(fn_pic);
	$("li.pic div.gal").click(fn_gal);

	$("li.pic a").live("click", function(event) {
		event.preventDefault();
	});

	$("div.i_spacer").live("click", function() {
		$(this).parent().children("ul.block_elements").prepend('<li class="left spacer">' + html_spacerControl + html_floatControl + '</li>');

		$("ul.block_elements li.spacer").resizable({
			containment: "parent",
			minHeight: 40,
			minWidth: 50,
		});
	});

	$("div.i_pic").live("click", function() {
		do {
			picCount++;
		} while($("#pic_" + picCount).length > 0);

		if ($(this).parent().children("ul.block_elements").prepend('<li class="left pic pic_empty" id="pic_' + picCount + '">' + html_picControl + html_floatControl + '<a href=""><img src="" alt=""></a></li>')) {
			$("#pic_" + picCount + " div.pic").click(fn_pic);
			$("#pic_" + picCount + " div.gal").click(fn_gal);
		}
	});

	$("div.i_text").live("click", function() {
		do {
			textCount++;
		} while($("#text_" + textCount).length > 0);

		$(this).parent().children("ul.block_elements").prepend('<li class="left text">' + html_textControl + html_floatControl + '<textarea id="text_' + textCount + '" name="text_' + textCount + '">Text.</textarea></li>');

		$("ul.block_elements li.text").resizable({
			containment: "parent",
			start: fn_resizeTextStart,
			stop: fn_resizeTextStop
		});

		tinyMCE.execCommand('mceAddControl', false, "text_" + textCount);
	});

	$("div.fl").live("click", function() {
		$(this).parent().addClass("left").removeClass("right");
	});

	$("div.fr").live("click", function() {
		$(this).parent().addClass("right").removeClass("left");
	});

	$("div.gc").live("click", function() {
		if (confirm('Are you sure?')) {
			$("#" + $(this).parent().attr("linkedto")).empty().remove();
			$(this).parent().empty().remove();
		}
	});

	$(".new_block").click(function(event) {
		$("ul.srt").append('<li><div class="block">' + html_blockControl + '<input type="text" maxlength="100" value="New block ' + ++newCount + ' name" class="h3"><ul class="block_elements"><li class="clear"><!-- --></li></ul></div></li>');

		$("ul.block_elements").sortable({
			handle: "div.s",
			tolerance: "pointer",
			start: function(event, ui) {
				$(this).children("li.text").children("textarea").disableEditor();
			},
			stop: function(event, ui) {
				$(this).children("li.text").children("textarea").enableEditor();
			}
		});

		event.preventDefault();
	});

	$(".close").click(function() {
		$(this).parent().hide();
	});

	$("#uploader_close").click(function() {
		$("#gallery_control").hide();
	});
});

