/**
 * @copyright AionNext Kft.
 * @author Balogh Tibor
 */
(function($){
	var
		jsfile = 'jq.js',
		baseurl;

	$.getBaseUrl = function(){
		if (!baseurl)
		{
			baseurl = $('script[src*='+jsfile+']:first').attr('src');
			baseurl = baseurl.substring(0, baseurl.indexOf(jsfile));
			if (baseurl.charAt(0)!="/"){ baseurl = "/"+baseurl; }
		}
		return baseurl;
	};
})(jQuery);

(function($){
	$.addCSS = function(css){
		$('head').append('<link rel="stylesheet" type="text/css" href="'+css+'" media="screen" />');
	};
})(jQuery);

/**
 * Lightbox inicializálás
 */
(function($){
	$.initLightbox = function(attr){
		if (!$.fn.lightbox){ return; }
		$.addCSS($.getBaseUrl()+'css/lightbox.css');

		var
			index = 0,
			groups = {},
			attr = attr || "rel",
			set = {
				overlayOpacity: 0.37,
				fileLoadingImage : $.getBaseUrl()+'images/loading.gif',
				fileBottomNavCloseImage : $.getBaseUrl()+'images/closelabel.gif',
				strings : {
					help: ' \u2190 / P - el\u0151z\u0151 k\u00E9p\u00a0\u00a0\u00a0\u00a0\u2192 / N - k\u00F6vetkez\u0151 k\u00E9p\u00a0\u00a0\u00a0\u00a0ESC / X - ablak bez\u00E1r\u00E1sa',
					prevLinkTitle: 'El\u0151z\u0151 k\u00E9p',
					nextLinkTitle: 'K\u00F6vetkez\u0151 k\u00E9p',
					prevLinkText:  '&laquo;&nbsp;el\u0151z\u0151',
					nextLinkText:  'k\u00F6vetkez\u0151&nbsp;&raquo;',
					closeTitle: 'Ablak&nbsp;bez\u00E1r\u00E1sa',
					image: 'K\u00E9pek:&nbsp;',
					of: '/'
				}
			};

		$.each($("["+attr+"*=lightbox]"), function(i, obj){
			var name = /(lightbox[^\s]*)/.exec($(obj).attr(attr))[0];
			if (name == "lightbox"){ name = "_"+(index++); }
			if (typeof groups[name] == 'undefined'){ groups[name] = []; }

			groups[name].push(obj);
		});
		$.each(groups, function(i, group){
			$(group).lightbox(set);
		});
	}
})(jQuery);

$(document).ready(function(){
	$("p.showup").show(1200);
	$('span.shield').shield();

	initGuestBook();
	initContact();

	initOrderForm();
	$.initLightbox();
});

function initGuestBook(){
	var gbFormOpener = $("#formOpener");
	if (!gbFormOpener.length){ return; }

	gbFormOpener.show();
	var gbForm = $("div#textArea");

	gbFormOpener.click(function(){
		gbForm.show(750, function(){ gbFormOpener.slideUp(500); });
	});
	$("#formCloser").click(function(){
		gbForm.hide(750, function(){ gbFormOpener.fadeIn(500); });
	});

	$("form#guestbook").submit(function(){
		return isAllFilledForm($(this));
	});
}

function initContact(){
	var contactForm = $("form#contactform");
	if (!contactForm.length){ return; }

	contactForm.submit(function(){
		return isAllFilledForm($(this));
	});
}

function initOrderForm(){
	var orderForm = $("form#orderform");
	if (!orderForm.length){ return; }

	orderForm.submit(function(){
		return isAllFilledForm($(this), "#name,#email,#phone,#address,#quantity", "Minden szükséges mezőt ki kell tölteni!");
	});
}

function isAllFilledForm(form, items, message){
	var success = true;
	form.find(items? items : ":text,:password,textarea").each(function(){
		if (isEmpty(this)){ success = false; }
	});

	if (!success)
	{
		alert(message? message : "Az adatok küldéséhez mindegyik mező kitöltése kötelező!");
	}
	return success;
}

function isEmpty(item){
	return $.trim($(item).val()) == "";
}

/**
 * Browser detection.
 * We modify the jQuery browser object to correct browsers detection.
 * It correctly works from IE5.5, Opera 8 and Gecko 1.1.
 * @author Tibor Balogh
 */
(function($){
	var
		//The html element exists yet when the script runs.
		h = document.getElementsByTagName('html')[0],
		b = $.browser;

	b.msie = h.style.filter !== undefined;
	b.mozilla = h.style.MozBoxAlign !== undefined;
	b.opera = h.style.opPhonemes !== undefined;
	//b.safari= ???;
})(jQuery);

/**
 * Shield jQuery plug-in.
 * @author Tibor Balogh
 */
(function($){
	/**
	 * Public methods
	 */
	$.fn.shield = function(){
		return this.each(function(){
			if (h = $(this.firstChild).html()){
				var
					items = h.split(sp);
					title = items[2]? items[2] : items[4]+at+items[5]+'.'+items[6];
				$(this).html(['<a href="',String.fromCharCode(109, 97, 105, 108, 116, 111, 58),items[3],String.fromCharCode(32,60),items[4],at,items[5],'.',items[6],String.fromCharCode(62,34,62),title,'</a>'].join(''));
			}
		});
	}

	/**
	 * Private attributes
	 */
	var
		at = '&#'+'6'+'4;',
		sp = '//', h;
})(jQuery);

$(".showup").hide(0);