var BASE_HREF = BASE_HREF || '../';

Cufon.replace('.cuf, .side-box h2');

Cufon.replace('#main-menu a .in', { hover: true, color: '-linear-gradient(#5e5957, 0.5=#5e5957, 0.51=#362f2d, #362f2d)', textShadow: '0 1px 0px #fff' });

// Cufon.replace('#main-menu a', { hover: true });


$.fn.inputDefaultText = function(options)
{  
	options = $.extend({
		text: 'Hledaný výraz'
	}, 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; });
};

$(document).ready(function()
{
    $('#q').inputDefaultText({ text: 'Hledaný výraz...' });

	$('a.lightbox').kfBox();
	$.fn.confBox && $('#confBox').confBox();
	$('#confBox').click();

	$('table tr:nth-child(odd)').addClass('even');

	$('.flash-player').each(function(){
 		flashsize = $(this).find('a').attr('data-flashsize');
 		flashsize = flashsize.split("x");
 		$(this).flash(
        {
             src: '/inc/flash/player.swf',
             width: flashsize[0],
             height: flashsize[1],
             wmode: 'opaque',
             allowfullscreen: true,
             flashvars: {file: $(this).find('a').attr('href'), searchbar: 'false', autostart: 'false', frontcolor: '0x666666', image: $(this).find('img').attr('src')}
        });
     	// nebudeme zobrazovat alt
     	$(this).find('.alt').css("display","none");
	});

	$('.flash-banner').each(function(){
		flashsize = $(this).find('a').attr('data-flashsize');
		flashsize = flashsize.split("x");
		$(this).flash(
		{
			src: $(this).find('a').attr('href'),
			width: flashsize[0],
			height: flashsize[1],
			wmode: 'opaque',
			flashvars: {}
		});
		$(this).find('.alt').css("display","none");
	});

	/* Initialization for rating */
	$('.rating-box').each(function(){
		var $averageBox = $(this).find('.rating-average');
		var $textBox = $(this).find('.rating-text');
		var id = $(this).attr('id').substr(7);
		var rating = new kff.widgets.StarRating($(this).find('.kfrating'), {
			starWidth: 15,
			average: parseFloat($averageBox.text()) / 5,
			click: function(ratingInstance, star){
				ratingInstance.disable();
				$.ajax({
				   type: "GET",
				   url: "/",
				   data: "ajax&vote=1&pageID=" + $('#pageID').attr('value') + '&value=' + star,
				   dataType: 'json',
				   success: function(data){
					    if(data){
							ratingInstance.setAverage(data.average / 5);
							ratingInstance.drawAverage();
							//$textBox.html(data.text);
							$('.rating-average').text(data);
						}
				   }
				 });
			}
		});
		rating.activate();
		if($(this).hasClass('rating-disabled')) rating.disable();
	});

	if($.fn.cycle)
	{
		$('.bnr-slider')
			.cycle({
				fx: 'fade',
				speed: 2000,
				timeout: 5000,
				pause: true
			});
	}
	
	$('.signpost-sliding li').each(function()
	{
		var $this = $(this);
		var $textBox = $this.find('.text-box'); 
		var $h3a = $this.find('h3 a');
		$textBox.data('initialHeight', $textBox.height());
		$h3a.bind('click', function()
		{
			if($this.hasClass('collapsed'))
			{
				$textBox.stop().animate({ height: $textBox.data('initialHeight') }, 300);
				$this.removeClass('collapsed');				
			}
			else
			{
				$textBox.stop().animate({ height: 0 }, 300);
				$this.addClass('collapsed');	
			}
			return false;
		});
		$textBox.css({ height: 0, overflow: 'hidden' });
		$this.addClass('collapsed');
	});

});

$(window).bind('load', function()
{

	fontSizeListener('#main', function()
	{
		$('.news-home').kfEqualizeColumns({ column: '.news-home-mid .text-box p' });
		$('.page-homepage #main').kfEqualizeColumns({ column: '.content-box-mid' });
		$('.crossroad').kfEqualizeColumns({ column: '.crossroad-mid' });
	});

});

$.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);
};


$(window).bind('load', function()
{
	// SVG Map init:
	if($('#map').size() && Raphael)
	{
		$('#map ul, #map h3').remove();
		$('#map').css({ width: 520, margin: '0 auto' }).append('<img src="'+ BASE_HREF + 'img/ajax-loader.gif" />');

		$.get(BASE_HREF + "js/map-all.xml", function(svg)
		{
		    var attrOkres = {
		        fill: "#fff",
		        stroke: "#5F5047",
		        cursor: 'pointer',
		        "stroke-width": 2,
		        "stroke-linejoin": "round"
		    };

		    var attrKraj = {
		        stroke: "#5F5047",
		        cursor: 'pointer',
		        "stroke-width": 4,
		        "stroke-linejoin": "round"
		    };
		    var areaType = $('#areaType').val() || 'cr';
			var areaId = $('#areaId').val() ;
			var paperSize = { width: 520, height: 320 };

			var bbox = { x: 0, y: 0, width: paperSize.width, height: paperSize.height };
			var translate = { x: 0, y: 0 };
			var scale = 1;

			var R = Raphael("map", paperSize.width, paperSize.height);

			// 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: '#FFC19F' }, 200);
						else kraj.attr({ fill: '#FFC19F' });
						$('#mapform-kraj').val(key);
					})
					.bind('mouseout', function(e)
					{
						if($.browser.mozilla || $.browser.msie) kraj.animate({ fill: '#fff' }, 200);
						else kraj.attr({ fill: '#fff' });
						$('#mapform-kraj').val('0');
					})
					.bind('click', function(e)
					{
						$select.triggerHandler('change');
					});
			};

			if(areaType == 'cr')
			{
				var $cr = $('g#layer1 path', svg);
				R.path(attrKraj, $cr.attr('d'));

			    $('g#layer2 path', svg).each(function()
				{
					renderArea($(this), attrOkres);
				});

				$('#mapform-kraj').bind('change', function()
				{
					var name = $select.val();
					if(name != 'cela-cr')
					{
							name = name.slice(5);
							if(name != 'praha' && name != 'vysocina') name +=  '-kraj';
							if(name == 'praha') name = 'hlavni-mesto-praha';
							if(name == 'vysocina') name = 'kraj-vysocina';
					}
					document.location = name + '/';
				});
			}
		});
	}
});
