$(function() {
	/**
	 * termék menü
	 */
	$('ul#product_list li:has(ul)')
		.click(function(event) {
			var speed = 250;
			var par = $(event.target).parent().get(0);
			if (this == par)
			{
				if ($(this).children('ul').is(':hidden'))
				{
					$(this)
						.children('div')
							.css('background-image', 'url(' + BASE_URL + 'images/folder_minus.gif)')
						.end()
						.children('ul').slideDown(speed);
				}
				else
				{
					$(this)
						.children('div')
							.css('background-image', 'url(' + BASE_URL + 'images/folder_plus.gif)')
						.end()
						.children('ul').slideUp(speed);
				}
			}
		})
		.children('ul').hide()
		.end()
		.children('div')
			.css('background-image', 'url(' + BASE_URL + 'images/folder_plus.gif)')
			.css('cursor', 'pointer');

	$('ul#product_list li.selected')
		.children('ul').show()
		.end()
		.children('div.folder')
			.css('background-image', 'url(' + BASE_URL + 'images/folder_minus.gif)')

	/**
	 * Listaelemek min-height-jének beállítása explorer 6-on
	 */
	if (jQuery.browser.msie)
	{
		var version = parseInt(jQuery.browser.version);
		if (version < 7)
		{
			$('#content .product')
				.each(function() {
					if ($(this).attr('id') == 'used_vehicles')
					{
						alert($(this).height());
						if ($(this).height() < 155)
						{
							$(this).height(155);
							$(this).children('div').height(155);
						}
					}
					else if ($(this).height() < 80)
					{
						$(this).height(80);
						$(this).children('div').height(80);
					}

				})
		}
	}

	/**
	 * Termék fotók
	 */
	var panel_latency = 0;
	var panel_id = 0;
	$('#content a.preview_image')
		.click(function() {
			return false;
		})
		.each(function() {
			var href = $(this).attr('href');
			var size = '';
			if (href.indexOf('?') >= 0)
			{
				var dim = href.split('?').pop().split('x');
				$(this).attr('panel', 'panel' + panel_id)
				$('body')
					.append(
						'<div id="panel' + panel_id + '" class="preview_panel" style="width:' + dim[0] + 'px; height:' + dim[1] + 'px;">'
							+ '<img src="' + href + '" width="' + dim[0] + '" height="' +  dim[1] + '" />'
						+ '</div>'
					)
				++ panel_id;
			}
		})
		.hover(function() {
			var speed = 'fast';
			var latency = 250;
			var this_a = this;
			panel_latency = setTimeout(function() {
				$('div.preview_panel:visible').hide();

				var panel = $('#' + $(this_a).attr('panel')).get(0);

				var item_top = $(this_a).offset().top;
				var panel_height = $(panel).height();
				var item_bottom = item_top + panel_height;
				var win_top = $(document).scrollTop();
				var win_bottom = win_top + $(window).height();

				if (win_bottom < item_bottom)
					$(panel).css('top', (win_bottom - panel_height) + 'px');
				else if (item_top < win_top)
					$(panel).css('top', win_top + 'px');
				else
					$(panel).css('top', item_top + 'px');

				$(panel).css('left', $(this_a).offset().left + 'px');


				$(panel)
					.hover(function() {}, function() {
							$(this)
								.hide(speed)
						})
					.fadeIn(speed);
			}, latency);

		}, function() {
			if (panel_latency) clearTimeout(panel_latency);
		});

	$('div#back')
		.each(function() {
			if (history.length > 0)
				$(this).html(
					'<p><a href="javascript:history.back()">'
						+ '&laquo;&laquo; Vissza</a></p>');
		});
});