jQuery(document).ready(function() {
	// start the header background changer
    if ( jQuery(".moduletable-headerbg:first").length && jQuery(".moduletable-headerbg:first ul:first li").length > 1 )
		window.setTimeout("headerBgSwitcher()", headerBgAnimationTimeoutMS);

	// start the gallery image changer/fader
    if ( jQuery("div.gallery_list ul.gallery_list").length && jQuery("div.gallery_list ul.gallery_list li").length > 3 )
		window.setTimeout("galleryImageSwitcher()", galleryImageAnimationTimeoutMS);

	if ( jQuery("ul.entry_scroller:first").length ) {
		jQuery("ul.entry_scroller:first").simplyScroll({
			autoMode: 'loop',
			frameRate: 48,
			speed: 2
		});
	}

	if ( jQuery("div.magazines ul.entries_sample:first").length ) {
		jQuery("div.magazines ul.entries_sample:first").simplyScroll({
			autoMode: 'loop',
			frameRate: 48,
			speed: 1
		});
	}

	if ( jQuery("div.partners ul.entries_sample:first").length ) {
		jQuery("div.partners ul.entries_sample:first").simplyScroll({
			autoMode: 'loop',
			frameRate: 24,
			speed: 1
		});
	}

	jQuery('#left ul.menu li a:has(span.harta)').fancybox({
		'hideOnContentClick': false,
		'width': 1060,
		'height': 690,
		'type': 'iframe',
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.7,
		'margin': 0,
		'padding': 2,
		'onStart': function(){ jQuery("body").css({'overflow':'hidden'}); },
		'onClosed': function(){ jQuery("body").css({'overflow':'visible'}); }
    });


	//events regulament button
	jQuery("#events a.regulation-button").fancybox({
		'width': 960,
		'height': 600,
		'autoDimensions': false,
		'overlayColor': '#000',
		'overlayOpacity': 0.7
	});

	// blog scroller
	if (jQuery("#lotus-blog .arw").length) {
		addBlogScrollingFunctionality();
	}

});


var headerBgAnimationSpeedMS	= 800;
var headerBgAnimationTimeoutMS	= 3000;

function headerBgSwitcher() {
	var $active = jQuery(".moduletable-headerbg:first ul:first li.sel");

	if ($active.length == 0) $active = jQuery(".moduletable-headerbg:first ul:first li:last");

	var $next = $active.next().length ? $active.next() : jQuery(".moduletable-headerbg:first ul:first li:first");

	$active.fadeOut(headerBgAnimationSpeedMS, function() {
		$active.removeClass("sel");
	});
	$next.fadeIn(headerBgAnimationSpeedMS, function() {
		$next.addClass("sel");
		setTimeout("headerBgSwitcher()", headerBgAnimationTimeoutMS);
	});
}


var galleryImageAnimationSpeedMS			= 2500;
var galleryImageAnimationSpeedVariationMS	= 100;
var galleryImageAnimationTimeoutMS			= 2000;//5000;

function galleryImageSwitcher() {
	jQuery("ul.gallery_list").each(function() {
		var variationRand = Math.floor(Math.random() * galleryImageAnimationSpeedVariationMS + 1);
		var $ul = jQuery(this);
		if ( $ul.children("li").length < 4 ) return;

		var r = 3 - $ul.children("li").length % 3;
		var step = (r == 0) ? 3 : ( (r == 1) ? 2 : 1 );

		if ( jQuery.browser.msie && parseInt(jQuery.browser.version) == 8 ) {
			var nrItem = $ul.find("div, span").length;
			$ul.find("div, span").each(function(i) {
				var $item = jQuery(this);
				var itemIndex = i;
				$item.fadeOut((galleryImageAnimationSpeedMS + variationRand) / 2, function() {
					if (itemIndex == nrItem-1) {
						for (var i=0; i<step; i++) {
							$ul.children("li:last").insertBefore($ul.children("li:first"));
						}
						$ul.find("div, span").each(function() {
							jQuery(this).fadeIn((galleryImageAnimationSpeedMS) / 2);
						});
					}
				});
			});
		}
		else {
			$ul.fadeOut((galleryImageAnimationSpeedMS + variationRand) / 2, function() {
				for (var i=0; i<step; i++) {
					$ul.children("li:last").insertBefore($ul.children("li:first"));
				}
				$ul.fadeIn((galleryImageAnimationSpeedMS) / 2);
			});
		}
	});

	setTimeout("galleryImageSwitcher()", galleryImageAnimationSpeedMS + galleryImageAnimationTimeoutMS);
}


function openMap(href)
{
	window.open(href, 'hartaLotus', 'height=650, width=1020, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, toolbar=no');
}


function addBlogScrollingFunctionality() {
	jQuery("#lotus-blog .category-column").each(function(i, item) {
		if ( ! jQuery(item).children(".arw").length ) return;

		var $ul		= jQuery(item).find("ul:first");
		var $arwUp	= jQuery(item).find(".arw.up");
		var $arwDn	= jQuery(item).find(".arw.dn");

		var lastChildIndex = jQuery(item).children("li").length;
		var end = jQuery(item).find("li:last").position().top/* + jQuery(item).find("li:last").height() - 300*/;
		if (end < 0) end = 0;

		$arwUp.hover(
			function() {
				$ul.animate({ top: 0 }, -$ul.position().top / 100 * 1000);
			},
			function() {
				$ul.stop();
			}
		);
		$arwDn.hover(
			function() {
				$ul.animate({ top: -end }, (end - -$ul.position().top) / 100 * 1000);
			},
			function() {
				$ul.stop();
			}
		);
	});
}
