var BASE_HREF = BASE_HREF || '../';
var KDEVAMMOHOUPOMOCI_URI = KDEVAMMOHOUPOMOCI_URI || 'kde-vam-mohou-pomoci';

$(document).ready(function()
{
	$.fn.kfBox && $('.lightbox').kfBox();
	$.fn.kfBox && $('.related-images a').kfBox();

	$('table tr:nth-child(even)').addClass('even');

	$('#q').inputDefaultText({ text: 'Hledaný výraz'});
	$('#become-member-email').inputDefaultText({ text: 'Váš e-mail'});

	$('a.external').click(function(){return !window.open($(this).attr("href"))});

	$('a.print').bind('click', function(){ window.print(); return false; });


	if($.fn.cycle)
	{
		$('.news-slide').after('<div class="news-slide-paging"></div>');
		$('.news-slide').cycle({
			fx: 'scrollUp',
			speed: 600,
			timeout: 4000,
			pause: true,
			pager: '.news-slide-paging'
		});
	}

});

$(window).bind('load', function()
{
	fontSizeListener('#footer', equalizeHeights);

	// SVG Map init:
	if($('#map').size() && Raphael)
	{
		$('#map ul, #map h3').remove();
		$('#map').css({ width: 280 }).append('<img src="'+ BASE_HREF + 'img/ajax-loader.gif" />');

		$.get(BASE_HREF + "js/map-all.xml", function(svg)
		{
		    var attrOkres = {
		        fill: "#4FCDFF",
		        stroke: "#fff",
		        cursor: 'pointer',
		        "stroke-width": 1,
		        "stroke-linejoin": "round"
		    };

		    var attrKraj = {
		        stroke: "#4FCDFF",
		        cursor: 'pointer',
		        "stroke-width": 5,
		        "stroke-linejoin": "round"
		    };
		    var areaType = $('#areaType').val() || 'cr';
			var areaId = $('#areaId').val() ;
			var paperSize = { width: 280, height: 170 };

			var bbox = { x: 0, y: 0, width: paperSize.width, height: paperSize.height };
			var translate = { x: 0, y: 0 };
			var scale = 1;

			if($('.col-b2 #map').size())
			{
				$('#map').css({ width: 360, height: 220 });
				paperSize = { width: 360, height: 220 };
				translate = { x: 40, y: 30 };
				scale = 1.285;
			}

			var R = Raphael("map", paperSize.width, paperSize.height);

   			var getBBoxFromPathData = function(d)
			{
				var a = Raphael.parsePathString(d);
				var bbox = { x: 100000000, y: 10000000, width: 0, height: 0 };
				for(i = 0, l = a.length - 1; i < l; i++)
				{
					if(a[i][0] != 'z')
					{
						bbox.x = bbox.x < a[i][1] ? bbox.x : a[i][1];
						bbox.y = bbox.y < a[i][2] ? bbox.y : a[i][2];
						bbox.width = bbox.width > a[i][1] ? bbox.width : a[i][1];
						bbox.height = bbox.height > a[i][2] ? bbox.height : a[i][2];
					}
				}
				bbox.width = bbox.width - bbox.x;
				bbox.height = bbox.height - bbox.y;
				return bbox;
			};

			// Add Select:
			$('#map img').remove();
			$('#map').after('<div class="kraj-select-box"><select id="mapform-kraj" name="mapformkraj"><option value="0">Vyberte kraj…</option></select></div>');
			$select = $('#mapform-kraj');

			if(areaType == 'cr')
			{
				$select.append('<option value="cela-cr">Celá ČR</option>');
			}

			var renderArea = function($path, attr, transform)
			{
				transform = transform || false;
		    	var key = $path.attr('id');
		    	var d = $path.attr('d');
				var label = $path.attr('inkscape:label');
				if(areaType != 'cr') label = 'Okres ' + label;

				$select.append('<option value="' + key + '">' + label + '</option>');

				var kraj = R.path(attr, d);

				if(transform)
				{
					var bboxO = getBBoxFromPathData(d);
					var translateO = { x: bbox.x + bbox.width/2 - bboxO.x - bboxO.width/2, y: bbox.y + bbox.height/2 - bboxO.y - bboxO.height/2  };
					kraj.translate(translate.x - translateO.x * (scale - 1), translate.y - translateO.y * (scale - 1));
					kraj.scale(scale, scale);
				}

				$(kraj.node)
					.bind('mouseenter', function(e)
					{
						if($.browser.mozilla || $.browser.msie) kraj.animate({ fill: '#FFF363' }, 200);
						else kraj.attr({ fill: '#FFF363' });
						$('#mapform-kraj').val(key);
					})
					.bind('mouseout', function(e)
					{
						if($.browser.mozilla || $.browser.msie) kraj.animate({ fill: '#4FCDFF' }, 200);
						else kraj.attr({ fill: '#4FCDFF' });
						$('#mapform-kraj').val('0');
					})
					.bind('click', function(e)
					{
						$select.triggerHandler('change');
					});
			};

			if(areaType == 'cr')
			{
				var $cr = $('g#layer1 path', svg);
				var cr = R.path(attrKraj, $cr.attr('d'));
				cr.scale(scale, scale);
				cr.translate(translate.x, translate.y);

			    $('g#layer2 path', svg).each(function()
				{
					renderArea($(this), attrOkres, 1);
				});

				$('#mapform-kraj').bind('change', function()
				{
					var name = $select.val();
					if(name != 'cela-cr')
					{
							name = name.slice(5);
							if(name != 'praha' && name != 'vysocina') name +=  '-kraj';
					}
					document.location = BASE_HREF + KDEVAMMOHOUPOMOCI_URI + '/' + name + '/';
				});
			}
		});
	}
});

$.fn.inputDefaultText = function(options)
{
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);

	return this
		.val(options.text)
		.bind('focus', function(){ if(this.value == options.text) this.value = ''; })
		.bind('blur', function(){ if(this.value == '') this.value = options.text; });
};

$.fn.kfEqualizeColumns = function(options)
{
	options = $.extend({
		column: '>li'
	}, options);

	return this.each(function(i)
	{
		var $columns = $(options.column, this);
		var maxHeight = 0;
		$columns
			.height('auto')
			.each(function(){
				var h = $(this).height();
				if(h > maxHeight) maxHeight = h;
			})
			.height(maxHeight);
	});
}

function fontSizeListener($el, callback)
{
	$el = $($el);
	var h = 0;
	var interval = setInterval(function()
	{
		if($el.height() != h){
			h = parseInt($el.height());
			callback();
		}
	}, 200);
};

var equalizeHeights = function()
{
	$('.row').kfEqualizeColumns({ column: '.eq1' }).kfEqualizeColumns({ column: '.eq2' });
};

