var SLIDESHOW_FREQUENCY = 5000;
var SLIDESHOW_FADE_FREQUENCY = 1000;
var TOOLBAR_FADE_FREQUENCY = 500;
var gm_clicks = [];
var shipping_mode_changed = false;

$(document).ready(function() {
	$('a[href=#]').click(function() {
		alert('A kért hivatkozás fejlesztés alatt áll, ezért '
			+ 'jelenleg nem elérhető.');
		return false;
	})

	$('a,button,input[type=button],input[type=submit]').click(function(event) {
		var label = null, target = null, offs = null, source = null;
		var x = 0, y = 0, w = 0, h = 0;
		if (typeof event.pageX == 'undefined') return;

		source = location.href;

		var $el = $(event.currentTarget);
		var n = $el.attr('nodeName').toLowerCase();
		if (n == 'a')
		{
			label = $el.text();
			target = $el.attr('href');
		}
		else
		{
			if (n == 'button')
				label = $el.text();
			else
				label = $el.val();

			if ($el.parents('form'))
			{
				var $form = $($el.parents('form').get(0));
				target = $form.attr('action');
			}
		}

		label = label.replace(/\s/g, ' ');
		offs = $el.offset();
		x = offs.left;
		y = offs.top;
		w = $el.outerWidth();
		h = $el.outerHeight();

		gm_clicks.push(page_title + '|' + source + '|' + target + '|' + label + '|' + x + '|' + y + '|' + w + '|' + h);
		var val = '';
		for (var i = 0; i < gm_clicks.length; ++ i)
			val += escape(gm_clicks[i] + "\n");

		document.cookie = 'gm_clicks=' + val + ';expires=Thu, 25 Feb 2021 23:59:59 UTC;path=/';
	})

	// létrehozunk egy divet, amelyet a különböző eszközök áttűnésekor
	// arra használunk, hogy ne keveredjenek össze az egyes eszközök
	// elemei
	var bg = $('#user_functions').attr('className');
	$('<div></div>')
		.attr('id', 'current_tool_bg')
		.addClass(bg)
		.appendTo('#user_functions');

	if ($('#registration_text').length == 1)
	{
		$('<a href="javascript:void(0)" id="registration_button">Regisztráció</a>')
			.click(function() {
				$('#login_button').click();
				return false;
			})
			.appendTo($('#main_menu > div.menu'));
	}

	$('#user_functions form.toolbar a[id]')
		.click(function() {
			if ($(this).hasClass('inactive'))
				return true;

			// ha aktív, akkor nem kell újra megjeleníteni
			if ($(this).hasClass('active'))
				return false;

			// a toolbar-on szereplő gombok postfixelve vannak (_button).
			// Ezt lecsapva kapjuk meg a megjelenítendő eszköz azonosítóját.
			if (! $(this).attr('id').match(/^(.+?)_button$/))
				return false;

			// következő megjelenítendő eszköz
			var next_tool = $('#' + RegExp.$1);
			if (next_tool.length == 0) return false;

			// jelenleg megjelenített eszköz
			var current_tool = $('#user_functions .current');
			if (current_tool.length == 0) return false;

			// az aktív gomb már ne legyen aktív
			$('#user_functions form.toolbar a.active')
				.removeClass('active');

			// a következő eszköz gombja legyen aktív
			$(this).addClass('active');

			// a jelenlegi eszköz kerüljön egyel hátrébb
			current_tool
				.removeClass('current')
				.addClass('previous');

			// a háttér div tűnjön elő egyidőben az eszközzel
			$('#current_tool_bg')
				.hide()
				.slideDown(TOOLBAR_FADE_FREQUENCY);

			// a következő eszköz tünjön fel
			next_tool
				.removeClass()
				.addClass('current')
				.hide()
				.fadeIn(TOOLBAR_FADE_FREQUENCY, function() {
					// a jelenlegi eszköz váljon láthatatlanná
					$(current_tool)
						.removeClass()
						.hide()
				})

			return false;
		});

	if ($('#slideshow div').length > 1)
		setInterval('slideshow_change_picture()', SLIDESHOW_FREQUENCY);

	setup_product_menu_toggler()

	setTimeout(function() {
		update_product_menu_border();
	}, 0);

	$('#shipping_calc_opener,a.shipping_calc_opener').each(function() {
		$this = $(this);
		var href = $this.attr('href');
		$this
			.removeAttr('target')
			.attr('href', 'javascript:void(0)')
			.click(function() {
				$.modal('<iframe '
							+ 'src="' + href + '"'
							+ 'height="514" '
							+ 'width="794" '
							+ 'frameborder="0" '
							+ 'marginheight="0" '
							+ 'marginwidth="0" '
							+ 'name="shipping_calc_wrapper" '
							+ 'id="shipping_calc_wrapper" '
							+ 'scrolling="no" />', {
					opacity: 80,
					closeHTML: '<a class="shipping_calc_close"></a>',
					onShow: function() {
					},
					onClose: function() {
						$.modal.close();
						if (shipping_mode_changed)
							window.location.href = $('#cart').attr('action');
					}
				});
			})
	})
});

// slideshow init
function slideshow_change_picture() {
	var current = $('#slideshow div.current');
	if (current.length == 0) current = $('#slideshow div:last');

	var next = current.next();
	if (next.length == 0) next = $('#slideshow div:first');

	current.addClass('previous');

	next.addClass('current')
		.hide()
		.fadeIn(SLIDESHOW_FADE_FREQUENCY, function() {
			current.removeClass();
		});
}

function update_product_menu_border()
{
	var $ul = $('#product_menu');
	if ($ul.length == 0) return;

	var $el = $('#product_menu_bottom');
	if ($el.length == 0)
		$el =
			$('<div/>')
				.attr('id', 'product_menu_bottom')
				.appendTo(document.body);

	var offset = $ul.offset();

	if ($ul.outerHeight() < ($el.outerHeight() * 1.5)) return;

	var left = offset.left + $ul.outerWidth() - $el.outerWidth();
	var top = offset.top + $ul.outerHeight() - $el.outerHeight();

	$el.css({
		top: top + 'px',
		left: left + 'px'
	}).show();
}

function setup_product_menu_toggler()
{
	var $el = $('#product_menu div.selected').not('.product');
	if (! $el.length
		|| ! $el.next('ul')
				.find('li:first > div')
				.eq(0)
				.hasClass('product'))
		return;

	var color = $(document.body).attr('id');

	$el.css({
		backgroundImage: 'none',
		paddingLeft: '0px'
	});

	$('<a href="javascript:void(0)"/>')
		.data('expanded', true)
		.data('toggle_target', $el.next('ul'))
		.css({
			display: 'block',
			float: 'left',
			width: '13px',
			height: '13px',
			marginLeft: '2px',
			marginRight: '4px',
			backgroundImage: 'url(' + base_path + 'images/product_menu_expanded_' + color + '.gif)'
		})
		.click(function() {
			var $this = $(this);
			var $target = $this.data('toggle_target');
			var expanded = $this.data('expanded');
			$('#product_menu_bottom').hide();
			if (expanded)
				$target.fadeOut(75, function() {
					$this.css(
						'background-image',
						'url(' + base_path + 'images/product_menu_collapsed_' + color + '.gif)'
					);
					update_product_menu_border()
				})
			else
				$target.fadeIn(100, function() {
					$this.css(
						'background-image',
						'url(' + base_path + 'images/product_menu_expanded_' + color + '.gif)'
					);
					update_product_menu_border()
				})

			$this.data('expanded', expanded ? false : true);
		}).appendTo($el);
}
