﻿jQuery(function($){
	var gallery = $('.gallery');
	var prev = $('.gallery .nav .button.prev');
	var next = $('.gallery .nav .button.next');
	var thumbsBox = $('.gallery .thumbnails > ul');
	var thumbsBoxWidthOrig = thumbsBox.width();
	var thumbs = thumbsBox.children();
	var thumbsPos = 0;
	if(thumbsBox.children().length == 0)
	{
		setupThumbs = function(){};
		startAnimation = function(){};
		return;
	}
	
	function canScroll() {
		var thumbsWidth = getThumbsWidth();
		var maxThumbWidth = getMaxThumbWidth();
		if(thumbsWidth <= thumbsBoxWidthOrig + 1)
			return false;
		else
			return true;
	}
	
	setupThumbs = function()
	{
		thumbs = thumbsBox.children();
		var thumbsBoxWidth = thumbsBox.width();
		var thumbsWidth = getThumbsWidth();
		var maxThumbWidth = getMaxThumbWidth();
		var index = 0;
		var thumbsBoxPos = 0;
		
		if(canScroll()) while(index < 100) // for safety
		{
			if(thumbsWidth > thumbsBoxWidth + maxThumbWidth)
				break;
			for(var i = 0; i < thumbs.length; i++)
			{
				var thumb = $(thumbs.get(i));
				var newThumbsWidth = thumbsWidth + thumb.outerWidth();
				var newThumb = thumb.clone();
				thumbsBox.append(newThumb);
				thumbsWidth = newThumbsWidth;
				newThumb.click(function(){ thumb.find('a').click(); return false; });
			}
			index++;
		}
		thumbs = thumbsBox.children();

		var xpos = 0;
		thumbs.each(function(i,d){d = $(d); d.css('left', xpos+'px'); xpos += d.outerWidth();});
		thumbsBox.css('width', xpos);

		if(!canScroll())
			return false;

		return true;
	}

	function getThumbsWidth(){
		var width = 0;
		thumbs.each(function(i,d){width += $(d).outerWidth();});
		width -= thumbs.last().outerWidth() - thumbs.last().width();
		return width;
	}

	function getMaxThumbWidth(){
		var width = 0;
		thumbs.each(function(i,d){width = Math.max(width, $(d).outerWidth());});
		return width;
	}

	function moveThumbs(offset){
		thumbsPos += offset;
		while(true) {
			var lastThumb = thumbs.last();
			var firstThumb = thumbs.first();
			if(thumbsPos > 0) {
				lastThumb.prependTo(thumbsBox);
				thumbsPos -= lastThumb.outerWidth();
				thumbs = thumbsBox.children();
				var xpos = 0;
				thumbs.each(function(i,d){d = $(d); d.css('left', xpos+'px'); xpos += d.outerWidth();});
			}
			else if(thumbsPos < -firstThumb.outerWidth()) {
				firstThumb.appendTo(thumbsBox);
				thumbsPos += firstThumb.outerWidth();
				thumbs = thumbsBox.children();
				var xpos = 0;
				thumbs.each(function(i,d){d = $(d); d.css('left', xpos+'px'); xpos += d.outerWidth();});
			}
			else
				break;
		}
		thumbsBox.css('left', thumbsPos);
	}
	
	var autoscroll = [];
	var leafs = [];
	
	function removeInactive(effects) {
		var distance = 0;
		for(i in effects) {
			if(!effects[i].running()) {
				distance += effects[i].currentState().distance;
				effects.splice(i, 1);
				i--;
			}
		}
		return distance;
	}
	
	function stopEffects(effects, direction) {
		for(i in effects) {
			if(!(effects[i].direction * direction < 0))
				effects[i].stop();
		}
	}
	
	function scrollEffectAdd(effects, effect) {
		effects.push(effect);
		effect.start();
	}

	function autoscrollAdd() {
		if(!paused && (!autoscroll.length || autoscroll[0].stopTime))
			scrollEffectAdd(autoscroll, new scrollEffect('auto', 2, 0.5, -60));
	}

	function leafAdd(dir) {
		stopEffects(leafs, -dir);
		scrollEffectAdd(leafs, new scrollEffect('auto', 0.1, 0.08, 4000 * dir, 735));
	}
	
	function effectsDistance(effects) {
		var distance = 0;
		for(i in effects) {
			distance += effects[i].currentState().distance;
		}
		return distance;
	}

	startAnimation = function()
	{
		var distancePrev = 0; // px
		currentTime = +new Date();
		autoscrollAdd();

		function animate()
		{
			currentTime = +new Date();
			requestAnimationFrame(animate);

			distancePrev -= removeInactive(autoscroll);
			distancePrev -= removeInactive(leafs);
			var distance = effectsDistance(autoscroll) + effectsDistance(leafs);
			var offset = distance - distancePrev;

			moveThumbs(offset);

			distancePrev = distance;
		}
		animate();
	}
	
	var paused = false;
	pauseEffects = function(){
		paused = true;
		stopEffects(autoscroll);
		stopEffects(leafs);
	}
	
	resumeEffects = function(){
		paused = false;
	}
	
	if(!canScroll())
		return false;

	prev.click(function(){
		leafAdd(1);
	});
	next.click(function(){
		leafAdd(-1);
	});
	
	gallery.mouseenter(function() {
		for(i in autoscroll) {
			autoscroll[i].stop();
		}
	});
	gallery.mouseleave(function() {
		autoscrollAdd();
	});
});

addLightbox = function(){
	jQuery('.gallery a').lightBox({txtImage: 'Obrázek', txtOf: 'ze', onEnter: function(){ pauseEffects(); }, onExit: function(){ resumeEffects(); }}); // Select all links in object with gallery ID
}

if ( !window.requestAnimationFrame ) {

	window.requestAnimationFrame = ( function() {

		return window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
			window.setTimeout( callback, 1000 / 60 );
		};
	}
)();
}

jQuery(function($){
	addGalleryThumbnailOverlays = function() {
	var gallery = $('.gallery');
	var thumbs = gallery.find('.thumbnail');
		thumbs.each(function(i,d) {
			d = $(d);
			var overlay = d.find('.overlay');
			overlay.hide();
			d.mouseenter(function(){
				overlay.stop();
				overlay.fadeTo("fast", 1);
			});
			d.mouseleave(function(){
				overlay.stop();
				overlay.fadeTo("slow", 0);
			});
		});
	}
});

jQuery(function($){
scrollEffect = function(name, rampupTime, rampdownTime, speed, distance) {
	this.name = name;
	this.rampupTime = rampupTime;
	this.rampdownTime = rampdownTime;
	this.speed = Math.abs(speed);
	this.direction = speed < 0 ? -1: 1;
	this.distance = distance;
	this.start = function() {
		if (this.startTime || this.startTime == 0)
			throw this.name + ' already running';
		this.startTime = this.getCurrentTime();
	}
	this.running = function(currentState) {
		if(!this.startTime)
			return false;
		var currentState = currentState || this.currentState();
		if(currentState.time == 0)
			return true;
		if(currentState.speed != 0)
			return true
		
		return false;
	}
	this.stop = function() {
		if(!this.stopTime)
			this.stopTime = this.getCurrentTime();
	}
	this.currentState = function() {
		var currentTime = this.getCurrentTime();
		return this.state((currentTime - this.startTime)/1000, this.stopTime ? (this.stopTime - this.startTime)/1000: undefined);
	}
	this.state = function(timeFromStart, stopTimeFromStart) {
		if(timeFromStart == this.prevTimeFromStart && stopTimeFromStart == this.stopTimeFromStart)
			return this.prevState;
		this.prevTimeFromStart = timeFromStart;
		this.stopTimeFromStart = stopTimeFromStart;
		// rampup
		var rampupTimeMax = Math.min(stopTimeFromStart || timeFromStart, timeFromStart, this.rampupTime);
		var rampup = this.calc(0, rampupTimeMax, this.rampupAccel, this.distance);

		// normal speed
		var normalTimeMax = Math.max(0, Math.min(stopTimeFromStart || timeFromStart, timeFromStart) - rampup.t);
		var normal = this.calc(rampup.v, normalTimeMax, 0, this.distance - this.rampupFull.s - this.rampdownFull.s);
		
		// rampdown
		var rampdownTimeMax = Math.min(this.rampdownFull.t, Math.max(0, timeFromStart - rampup.t - normal.t));
		var rampdown = this.calc(normal.v, rampdownTimeMax, this.rampdownFull.a, this.distance - this.rampupFull.s - normal.s);

		var distance = rampup.s + normal.s + rampdown.s;
		var time = rampup.t + normal.t + rampdown.t;
		return this.prevState = { distance: distance * this.direction, time: time, speed: rampdown.v };
	}
	this.calc = function(u, t, a, smax) {
		var v = u + a*t;
		if(v < 0)
		{
			v = 0;
			t = -u/a;
		}
		var avgv = (u+v)/2;
		var s = avgv*t;
		isNaN(s);
		if(s > smax)
		{
			s = smax;
			if(a == 0)
			{
				t = s/u;
				v = u;
				avgv = v;
			}
			else
			{
				// using quadratic equation to calculate time
				var ea = a/2;
				var eb = u;
				var ec = -s;
				var ed = Math.pow(eb*eb - 4*ea*ec, 0.5);
				x1 = (-eb + ed)/(2*ea);
				x2 = (-eb - ed)/(2*ea);
				t = Math.max(x1, x2);
				v = u + a*t;
				avgv = (u+v)/2;
			}
		}
		return {u: u, t: t, a: a, v: v, s: s, avgv: avgv, smax: smax};
	}
	// full rampup
	this.rampupAccel = this.speed/(this.rampupTime); // px/(s*s)
	this.rampupFull = this.calc(0, this.rampupTime, this.rampupAccel, this.distance);
	// full rampdown
	this.rampdownAccel = this.rampupFull.v/(this.rampdownTime); // px/(s*s)
	this.rampdownFull = this.calc(this.rampupFull.v, this.rampdownTime, -this.rampdownAccel, this.distance - this.rampupFull.s);
}

scrollEffect.prototype.getCurrentTime = function() {
    return currentTime;
}

scrollEffect.prototype.print = function() {
}

});

jQuery(function($){

    $("ul.dropdown li").hover(function(){
    
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    
    }, function(){
    
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    
    });
    
    //$("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

});
