var base_url   = location.protocol + '//' + location.host + '/';
var url_suffix = '';
var entire_url = base_url + url_suffix;

var menuImagesUrl       = entire_url + 'images/';
var menuMouseOverNames  = new Array('header_menu_over', 'left_categories_over', 'category_add', 'category_minus', 'category_blank');
var menuMouseOverImages = new Array();
var IE7_PNG_SUFFIX = ":";

$(window).load(function(event)
{
	if (($.browser.msie) && ($.browser.version == '6.0'))
	{
		document.execCommand("BackgroundImageCache", false, true);
		purge(document.body);
	}
	
	// Preload images
	$(menuMouseOverNames).each(function(i)
	{
		menuMouseOverImages[i]     = new Image();
		menuMouseOverImages[i].src = menuImagesUrl + this + '.png';
	});
	
	initEvents();
	
	// tooltip for [.tooltip, .errortip] elements
	tooltipFromTitle('tooltip');
	tooltipFromTitle('errortip');
	
	Shadowbox.init({
		overlayOpacity: 0.85
	});
	
	// TODO: teszt ezt, hátha összeállítja az urlt egyszerűbben
	//alert(document.URL);
	//alert(document.URL.replace(/(\\|\/)[^\\\/]*$/, '/'));
});

function initEvents()
{
	// left side category list menu
	initCategoriesMenu();
	
	// index
	var searchValue = $('#s').val();
	
	// search
	$('#search').submit(function(event) { validateSearch(event, 's', 3); });
	$('#s').focus(function() {
		if (this.value == searchValue) this.value = '';
	}).blur(function() {
		if (this.value == '') this.value = searchValue;
	});
	
	// delete buttons confirm
	$('.confirm_delete').click(function(event) { confirmDelete(event); });
	
	// registration
	if ($('#registration').length)
	{
		$("input[name='customer_type']").change(function() {
			var value = $(this).val();
			
			if (value == 1)
			{
				$('#customer_more_fields').removeClass('display_n');
				$('#customer_type_1_more_fields').removeClass('display_n');
				$('#customer_type_1_more_fields').css('display', '');
			}
			else
			{
				$('#customer_more_fields').removeClass('display_n');
				$('#customer_type_1_more_fields').css('display', 'none');
			}
		});
		
		$("input[name='customer_delivery_option']").change(function() {
			var value = $(this).val();
			
			if (value == 1)
			{
				$('#customer_delivery_fields').removeClass('display_n');
				$('#customer_delivery_option_1_more_fields').removeClass('display_n');
				$('#customer_delivery_option_1_more_fields').css('display', '');
			}
			else
			{
				$('#customer_delivery_fields').removeClass('display_n');
				$('#customer_delivery_option_1_more_fields').css('display', 'none');
			}
		});
		
		$("input[name='user_name']").change(function() {
			$("input[name='customer_bill_name']").val($("input[name='user_name']").val());
		});
	}
	
	// toggle reduced price
	$('.toggle_reduced_price').each(function ()
	{
		$(this).click(function() {
			$(this).toggle('fast');
			$(this).next('span').toggle('fast');
			return false;
		});
	});
}

function initCategoriesMenu()
{
	$('#categories .sub_category').each(function ()
	{
		$(this).prev().click(function() {
			$(this).find('a img').attr('src', menuImagesUrl + 'category_' + ($(this).next().children().css('display') == 'none' ? 'minus' : 'add') + '.png');
			$(this).next().children().toggle('fast');
			return false;
		});
	});
}

// üresen küldés megakadályozása és minimum 3 karakter
function validateSearch(event, inputFieldId, lengthFrom)
{
	var value = $('#' + inputFieldId).val();
	
	if (value == '')
	{
		alert("Üresen maradt a keresés mező!");
		event.preventDefault();
		return false;
	}
	
	if (value.length < lengthFrom)
	{
		alert("Túl rövid a keresett kifejezés (minimum " + lengthFrom + " karakter)!");
		event.preventDefault();
		return false;
	}
}

function confirmDelete(event)
{
	if (!confirm("Biztosan törli a bejegyzést?"))
	{
		event.preventDefault();
		return false;
	}
}

function tooltipFromTitle(tipType)
{
	var yOffset = 20;
	var xOffset = 30;
	
	$('.' + tipType).hover(function(tipElement) {
		$(this).data('tooltipTitle', this.title);
		this.title = '';
		
		var label = ($(this).data('tooltipTitle') != '') ? $(this).data('tooltipTitle') : '';
		
		$('body').append('<p id="' + tipType + '_popup">' + label + '</p>');
		$('#' + tipType + '_popup')
		.css('top', (tipElement.pageY - yOffset) + 'px')
		.css('left', (tipElement.pageX + xOffset) + 'px')
		.fadeIn('fast');
	},
	function() {
		this.title = $(this).data('tooltipTitle');
		$('#' + tipType + '_popup').remove();
	});
	
	$('.' + tipType).mousemove(function(tipElement) {
		$('#' + tipType + '_popup')
		.css('top', (tipElement.pageY - yOffset) + 'px')
		.css('left', (tipElement.pageX + xOffset) + 'px');
	});
}

function purge(d)
{
	var a = d.attributes, i, l, n;
	
	if (a)
	{
		l = a.length;
		for (i = 0; i < l; i += 1)
		{
			n = a[i].name;
			if (typeof d[n] === 'function')
			{
				d[n] = null;
			}
		}
	}
	
	a = d.childNodes;
	if (a)
	{
		l = a.length;
		for (i = 0; i < l; i += 1)
		{
			purge(d.childNodes[i]);
		}
	}
}
