
$(document).ready(function()
{
	menu_init();
	map_init();
	// pokud existuje slideshow
	if($('#slides').length != 0) cycle_init();
	// pokud existují tabs
	if($("#specification-tabs").length != 0) $("#specification-tabs").tabs({effect: 'fade', fadeOutSpeed: 400});
	if($("#nastavby-tabs").length != 0) $("#nastavby-tabs").tabs({effect: 'fade', fadeOutSpeed: 400});
	if($("#soupravy-tabs").length != 0) $("#soupravy-tabs").tabs({effect: 'fade', fadeOutSpeed: 400});
	$("a.fancybox").fancybox();
	
	//$('#lead-box').before("<p>test js</p>");
	
	// cookie na omezení přehrávání intra pouze jednou denně
	if($('#pageid-1').length != 0)
	{
		var flashHtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="960" height="446" id="hesti_intro_v03" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="false" /> <param name="movie" value="/templates/flash/hesti_intro_v03.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="wmode" value="transparent" /> <embed src="/templates/flash/hesti_intro_v03.swf" quality="high" bgcolor="#ffffff" width="960" height="446" name="hesti_intro_v03" align="middle" wmode="transparent" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>';
		if($.cookie("introPlayed") == null)
		{
			$('#lead-box').before(flashHtml);
			//alert('setting new cookie introPlayed: true');
			$.cookie("introPlayed", true, { expires: 1, path: '/', domain: 'hesti.cz' });
			
		}
		else if($.cookie("introPlayed") == true)
		{
			//alert('intro has been played');
			//$('#hesti_intro_v03').hide();
		}
		else if($.cookie("introPlayed") == false)
		{
			$('#lead-box').before(flashHtml);
			//alert('playing intro');
			$.cookie("introPlayed") = true;
		}
		/*
		else {
			alert("Cookie - introPlayed: " + $.cookie("introPlayed"));
		}*/
	}
});

function menu_init()
{
	$('#mainmenu li.headlink ul').hide();
	$('#mainmenu li.headlink').mouseover(
		function()
		{
			$('ul', this).show(); //.slideDown('fast');
			$('a', this).addClass('sel');
		}	
	); 
	$('#mainmenu li.headlink').mouseleave(
		function()
		{
			$('ul', this).hide(); //*.slideUp('fast');
			$('a', this).removeClass('sel');
		}
	);
}

function map_init()
{
	// detekce IE 6
	IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
	//$('#footer').append("IE6: "+IE6);
	//alert("IE6: "+IE6);
	
	$('#map').children('a.map-point').each(function(){
	  var pointId = $(this).attr('id');
	  // nalezení príslušného labelu k bodu
	  var label = $('#' + pointId + '-box');
	  // promenna urcujici deformaci souradnic
	  var resize = 1;
	  // uložení souradnic bodu
	  var coords = $(this).attr('rel').split('-');
	  

	  // deformace souradnic pro malou mapu
	  if($('#map').attr('class')=='small')
	  {
		// IE6 pozicuje body úplně mimo
		  if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			//alert("mapa se zatím nezobrazuje korektně");
		  }
		resize = 0.86;
	  }
	  // deformace souradnic pro nejmensi mapu v bocnim panelu
	  if($('#map').attr('class')=='smallest')
	  {
		// IE6 pozicuje body úplně mimo
		  if (IE6) {
			coords[0] += 0;
			coords[1] += 300;
		  }
		  
		resize = 0.475;
	  }
	  // posunutí na stejné souradnice
	  coords[0] *= resize;
	  coords[1] *= resize;
	  label.css({left: coords[0] + 'px', top: coords[1] + 'px'});
	  // vyjímka pro cedulky, které jsou moc vpravo nakraji - vysunou se doleva
	  if(label.attr('class')=="map-popup left")
	  {
		var newX = 386 * resize;
		//alert("label.width(): "+label.width());
		label.css({left: newX + 'px'});
	  }
	  
	  label.mouseleave(function(){
			/*if ( $.browser.msie ) {
				label.next().hide();
			 } else {*/
				//label.next().fadeOut(500);
			 //}
		});
	
	/*
	  label.click(function() {
			if ( $.browser.msie ) {
				label.next().show();
			 } else {
				label.next().fadeIn(500);
			 }
		});
		*/
		
	  // posunutí bodu
	  $(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
		
		.fadeIn(1)				
		.mouseover(function() {
			if ( jQuery.support.leadingWhitespace ) {
				$('.map-popup').fadeOut(500);
				label.stop().fadeIn(500);
			} else {
				$('.map-popup').hide();
				label.stop().show();
			}
			return false;
		})
		.click(function() {
			label.next().show();
		})
		//.mouseleave(function(){$('#' + pointId + '-box').fadeOut(500);})
		
		// pokud se nacházíme na podstránce firmy, tak se rovnou zobrazí její popiska
		if(pointId == ("point-" + $('#cfg-hesti').text())) label.show();
	})
	
	
		
	$('#map').mouseleave(function(){
		
		if ( jQuery.support.leadingWhitespace ) {
			$('.map-popup').fadeOut(500);
			$('#point-' + $('#cfg-hesti').text() + '-box').fadeIn(400);
		} else {
			$('.map-popup').hide();
			$('#point-' + $('#cfg-hesti').text() + '-box').show();	
		}
	});
	
	/*
	// popupy s adresou
	$('.address-popup').hide();
	$('.address-popup .close-button').mouseup(
		function() {
			if ( jQuery.support.leadingWhitespace ) {
				$(this).parent().parent().fadeOut(400);
			} else {
				$(this).parent().parent().hide();
			}
		}
	);
	*/
};
		
function cycle_init()
{
	$('#slides').cycle({
		fx: 'scrollUp', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 0
	});

	$('#slides').hover(
		function() { $('#slides').cycle('pause'); },
		function() { $('#slides').cycle('resume'); }
	);
	
	$('#slides').cycle.updateActivePagerLink = function(pager, currSlideIndex) {
		$(pager).find('li .hover').fadeOut(500);
		$(pager).find('li .label p').animate({color: '#555555'}, 'slow');
		$(pager).find('li:eq('+currSlideIndex+') .hover').stop().fadeIn(500);
		$(pager).find('li:eq('+currSlideIndex+') .label p').stop().animate({color: '#cd2c3e'}, 'slow');
	};
	
	$('#slides').cycle({
		//fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		next:   '#nextBtn',
		prev:   '#prevBtn',
		pager:  '.logo-list',
		timeout: 800,
		pagerAnchorBuilder: function(idx, slide) { 
			return ''; 
		} 
	});
	
	$('#nextBtn, #prevBtn').hide();
	
	$('#nextBtn, #prevBtn').click(
		function() { $('#slides').cycle('pause'); }
	);
	
	$('#lead-box.trailers').hover(
		function() { $('#nextBtn, #prevBtn').fadeIn(300); },
		function() { $('#nextBtn, #prevBtn').fadeOut(500); }
	);
	
	// ovladani slideshow pomoci log znacek
	if($('#header .logo-list') != null)
	{	
		var iterator = 0;
		$('.logo-list .logo .hover').hide();
		$('.logo-list li:eq(0) .hover').fadeIn(500);
		
		$('.logo-list .logo').each(function()
		{
		
			var logoId = $(this).attr('id');
			var logoItem = $('#' + logoId);
			logoItem.attr('rel', iterator);
			/*
			logoItem.click(function()
			{ 
				$('#slides').cycle(Number(logoItem.attr('rel'))); 
				return false; 
			});
			*/
			iterator++;
			
		})		
	};
};

function flashEnded()
{
	//alert('JavaScript function flashEnded() called form Flash.');
	//$('#hesti_intro_v03, embed').fadeOut(300); //, function() {$('#hesti_intro_v03').remove();});
	$('#hesti_intro_v03, embed').remove();
	$('#hesti_intro_v03').empty();
	$('body').css('margin-top', '0');
};

