$(document).ready(function() {
	
	var site_url = $('#site_url').html();
	
	$(function() {
			$( "#tabs" ).tabs();
	});
	
	$('#menu li').live('click', function(event) {
		// natiahneme texturu az naspodok stranky
		$('#textura').height($(document).height());
		//$('#textura').width($(document).width());
		// zabranime default eventu pre klik
		event.preventDefault();
		// zistime ktory controller budeme volat
		var page = $(this).attr('rel');
		var clicked = $(this);

		// dame texturu nad stranku
		$('#textura').css('z-index', 5000);
		// a zobrazime ju v animacii
		$('#textura').animate({opacity: 1},500, function() {
					$('#menu li.active').removeClass('active');
					clicked.addClass('active');
						$.ajax({
							async: false,
							url: site_url+'/'+page,
							success: function(data) {
								$('#content').html(data);
								if (page == 'titulka/again') {
									$('#menu').hide();
									$('#wrapper').addClass('titulka');
									slider();
								}
								$('#textura').animate({opacity: 0},500, function() {
									$('#textura').css('z-index', -5000);
								});
							}
						});
		});
	});

	$('#enter a').live('click', function(event) {
		// natiahneme texturu az naspodok stranky
		$('#textura').height($(document).height());
		//$('#textura').width($(document).width());
		// zabranime default eventu pre klik
		event.preventDefault();
		// zistime ktory controller budeme volat
		var page = $(this).attr('rel');
		// dame texturu nad stranku
		$('#textura').css('z-index', 5000);
		// a zobrazime ju v animacii
		$('#textura').animate({opacity: 1},500, function() {
					$('#wrapper').removeClass('titulka');
					$.ajax({
						async: false,
						url: site_url+'/gallery/first',
						success: function(data) {
							clearInterval(myInterval);
							$('#wrapper').html(data);
							$('#textura').animate({opacity: 0},500, function() {
								$('#textura').css('z-index', -5000);
							});
						}
					});
					$('#menu').css('display', 'block');
		});
	});

	$('#video-slider .thumbs img').live('click', function(event){
		$('#video-slider .thumbs img').removeClass('active');
		$(this).addClass('active');
		var kod = $(this).attr('rel');
		$.ajax({
			url: site_url+'/gallery/get/'+kod,
			success: function(data) {
				$('#video').html(data);
			}
		});
	});

	$('#up').live('click', function(event) {
		
		if ($('#video-slider .thumbs:animated').length) {
			return;
		}
				
		var move = 120;
		var thumbs = $('#video-slider .thumbs');
		var tpos = thumbs.position();
		var slider = $('#video-slider');
		var spos = slider.position();
		
		var rest = thumbs.height() - slider.height() - ((tpos.top + thumbs.height()) - (spos.top + slider.height())) + 5;
		//alert (rest);
		if (rest < move) {
			//move = rest;
			$('.thumbs').css('top', 0 - $('.thumbs img:last').height() - 5);
			$('.thumbs img:first').before($('.thumbs img:last'));
		}
		
		$('#video-slider .thumbs').animate({
			top: '+='+move
			}, 300, 'swing');
	});
	
	$('#down').live('click', function(event) {
		
		if ($('#video-slider .thumbs:animated').length) {
			return;
		}
		
		var move = 120;
		var thumbs = $('#video-slider .thumbs');
		var tpos = thumbs.position();
		var slider = $('#video-slider');
		var spos = slider.position();
		
		var rest = thumbs.height() - slider.height() - (spos.top - tpos.top) - 10;
		//alert (rest);
		if (rest < move) {
			//move = rest;
		}
		
		// alert(spos.top+':'+tpos.top);
		
		if ((spos.top - tpos.top) > $('.thumbs img:first').height()) {
			$('.thumbs').css('top', 0 - $('.thumbs img:first').height() - 5);
			$('.thumbs img:last').after($('.thumbs img:first'));

		}
		
		$('#video-slider .thumbs').animate({
			top: '-='+move
			}, 300, 'swing');
	});
	
	$('#obrazky').sortable({
		handle : '.handle',
	 	axis: 'y'
	});

	$('#videa').sortable({
		handle : '.handle',
	 	axis: 'y'
	});

	
       $(".pulse").effect( "pulsate", 
        {times:100}, 6000 );
});

var direction = 'vlavo';

// set image height
var height = 490;

// the speed while hovering the controls
var fast = 1;

// the normal speed
var normal = 10;

// the speed multiplicator
// as this script is quite unefficient, for faster movement we need to do more than 1px move
var multiplicator = 2;

var pic, numImgs, arrLeft, i, totalWidth, n, myInterval;
var width;

//$(document).ready(function() {
$(window).load(function(){
	slider();
});

function slider() {
	
	width = $('#slider').width();
	
	// we need to calculate the appropriate width of images here. if we leave it on css and/or html
	// then it may happen that when we set height for all images in css/html the width calculated
	// by firefox may be different then width calculated by other browsers

	/*
		$('#slider img').each(function(){
			var picwidth = $(this).width()/($(this).height()/height);
			$(this).attr('height', height);
			$(this).attr('width', picwidth);
		});		
	*/
	
	$('.pauza').bind("mouseenter", function(){
		clearInterval(myInterval);
	}).mouseout(function(){
		multiplicator = 2;
		if (direction == 'vlavo') {
			myInterval = setInterval("flexiScrollLeft()",normal);
		}
		if (direction == 'vpravo') {
			myInterval = setInterval("flexiScrollRight()",normal);
		}	
	})
	
	$('.vpravo').bind("mouseenter",function(){
		clearInterval(myInterval);
		multiplicator = 10;
		myInterval = setInterval("flexiScrollRight()",fast);
		direction = 'vpravo';
		
	}).mouseout(function(){
		clearInterval(myInterval);
		multiplicator = 2;
		myInterval = setInterval("flexiScrollRight()",normal);
	});

	$('.vlavo').bind("mouseenter",function(){
		clearInterval(myInterval);
		multiplicator = 10;
		myInterval = setInterval("flexiScrollLeft()",fast);
		direction = 'vlavo';
		
	}).mouseout(function(){
		clearInterval(myInterval);
		multiplicator = 2;
		myInterval = setInterval("flexiScrollLeft()",normal);
	});

	
	pic = $("#slider").children("img");
	numImgs = pic.length;
	arrLeft = new Array(numImgs);

	for (i=0;i<numImgs;i++){

		totalWidth=0;
		for(n=0;n<i;n++){
			totalWidth += $(pic[n]).width();
		}

		arrLeft[i] = totalWidth;
		$(pic[i]).css("left",totalWidth);
	}

	myInterval = setInterval("flexiScrollLeft()",normal);
	$('#imageloader').hide();
	$(pic).show();
}
//});

function flexiScrollLeft(){
	// $('#debug').html(arrLeft[2]);
	for (i=0;i<numImgs;i++){
		arrLeft[i] -= multiplicator;
		// $('#debug').html($(pic[0]).width()+','+$(pic[1]).width()+','+$(pic[2]).width()+','+$(pic[3]).width());		

		if (arrLeft[i] <= -($(pic[i]).width())){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
				if (n!=i){
					totalWidth += $(pic[n]).width();
				}
			}
			// as the images might be far left, we need to substract also the part between left border of the slide
			// and the right border fo the image from the total width
			arrLeft[i] =  totalWidth + (arrLeft[i] + $(pic[i]).width());
		}
		$(pic[i]).css("left",arrLeft[i]);
	}
}

function flexiScrollRight(){
	// $('#debug').html(arrLeft[2]);
	for (i=numImgs-1;i>-1;i--){
		arrLeft[i] += multiplicator;		

		if (arrLeft[i] >= width){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
					totalWidth += $(pic[n]).width();
			}
			arrLeft[i] =  arrLeft[i] - totalWidth;
		}
		$(pic[i]).css("left",arrLeft[i]);
	}
}
