/*!
 * Tiny Carousel 1.8
 * http://www.baijs.nl/tinycarousel
 *
 * Copyright 2010, Maarten Baijs
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-2.0.php
 *
 * Date: 10 / 11 / 2010
 * Depends on library: jQuery
 */
(function($){
	$.fn.tinycarousel = function(options){
		var defaults = { 
			start: 1, // where should the carousel start?
			display: 1, // how many blocks do you want to move at 1 time?
			axis: 'x', // vertical or horizontal scroller? ( x || y ).
			controls: true, // show left and right navigation buttons.
			pager: true, // is there a page number navigation present?
			interval: false, // move to another block on intervals.
			intervaltime: 5000, // interval time in milliseconds.
			rewind: false, // If interval is true and rewind is true it will play in reverse if the last slide is reached.
			infinite: true, // If interval is true and infite is true it will restart at first slide like in a carousel if the last slide is reached.
			animation: true, // false is instant, true is animate.
			duration: 500, // how fast must the animation move in ms?
			callback: null, // function that executes after every move
			easing: null,
			overview: true 
		};
		var options = $.extend(defaults, options);  

		var oSlider = $(this);
		var oViewport = $('.viewport:first', oSlider);
		var oContent = $('.overview:first', oSlider);
		
		$('a.video_stop').click(function () {  
			ids = $("div#panel object");    
			if(1 >= oldAId){
				idObj = ids.eq(oldAId-1).attr("id");
				var player = document.getElementById(idObj);
				player.sendEvent("PLAY","false");
				$('.slideshow').fadeIn(500);
			}
			oldAId = $(this).attr('id').charAt(1);	
			return false;  
	    }); 
		
		// clone last and first element to simulate infinity
		if (options.infinite) {
			var lastClone = oContent.children().last().clone().addClass('cloned');
			var firstClone = oContent.children().first().clone().addClass('cloned');
			oContent.prepend(lastClone);
			oContent.append(firstClone);
		}		
		var oPages = oContent.children();
		var oBtnNext = $('.next:first', oSlider);
		var oBtnPrev = $('.prev:first', oSlider);
		var oPager = $('.pager:first', oSlider);
		var iPageSize, iSteps, iCurrent, oTimer, bPause, bForward = true, bAxis = options.axis == 'x';
		var oldAId = 1;
		
		return this.each(function(){
			initialize();
		});
		function initialize(){
			iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			var iLeftover = Math.ceil(((bAxis ? oViewport.outerWidth() : oViewport.outerHeight()) / (iPageSize * options.display)) -1);
			if (options.infinite) {
				iSteps = Math.max(1, Math.ceil((oPages.length - 2) / options.display) - iLeftover);
				iCurrent = Math.min(iSteps, Math.max(1, options.start)) - 1;	
				oContent.css(bAxis ? 'width' : 'height', (iPageSize * (oPages.length + 2)));
				var oPosition = {};
				oPosition[bAxis ? 'left' : 'top'] = -iPageSize * options.display;
				oContent.css(oPosition);		
			} else {
				iSteps = Math.max(1, Math.ceil(oPages.length / options.display) - iLeftover);
				iCurrent = Math.min(iSteps, Math.max(1, options.start)) -2;
				oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));	
			}
			move(1);
			setEvents();
			
			
			
			
			
		}
		function setEvents(){
			if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
				//oBtnPrev.removeAttr('disabled');
				oBtnPrev.click(function(oEvent){
					oEvent.preventDefault();
					if (options.infinite) {
						// stop animation
						//oContent.stop();
						setInfiniteContent();						
					}
					move(-1); 
					return false;
				});
				//oBtnNext.removeAttr('disabled');
				oBtnNext.click(function(oEvent){
					oEvent.preventDefault();
					if (options.infinite) {
						// stop animation
						//oContent.stop();
						setInfiniteContent();						
					}
					move(1); 
					return false;
				});
			}
			if(options.interval){
				oSlider.hover(function(){clearTimeout(oTimer); bPause = true},function(){bPause = false; setTimer();});
			}
			if(options.pager && oPager.length > 0){
				$('a',oPager).click(setPager);
			}
		}
		
		function setButtons(newCurrent){
			var current = (typeof newCurrent == 'undefined') ? iCurrent : newCurrent;
			/*if(options.controls && !options.infinite){
				oBtnPrev.toggleClass('disabled', !(current > 0));
				oBtnNext.toggleClass('disabled', !(current +1 < iSteps));
			}*/
			if(options.pager){
				$.each(oPager, function(i, oPager) {
					var oNumbers = $('.pagenum', $(oPager));
					oNumbers.removeClass('activey');
					if (options.infinite) {
						$(oNumbers[current-1]).addClass('activey');					
					}
					else {
						$(oNumbers[current]).addClass('activey');					
					}
				});
			}	
			if(options.overview){
				var oOverview = $('.homepage_banner', oContent);
				$.each(oPager, function(i, oPager) {
					var oOverview = $('.homepage_banner', oContent);
					
					oOverview.children(".identifikace").addClass('schovat');
					setTimeout(function(){
						$(oOverview[current]).children(".identifikace").removeClass('schovat');
					},1170);
					
					
					/*if (iCurrent == 1 || iCurrent == 7)
						$('.div1b').show("slide", { direction: "right" }, 1170);*/
					if (iCurrent == 2)
						$('.div2b').show("slide", { direction: "right" }, 1170);
					if (iCurrent == 3)
						$('.div3b').show("slide", { direction: "right" }, 1170);
					if (iCurrent == 4)
						$('.div4b').show("slide", { direction: "right" }, 1170);
					if (iCurrent == 5) {
						$('.div5b').show("slide", { direction: "right" }, 1470);
						$('.div5c').show("slide", { direction: "right" }, 1570);
						$('.div5d').show("slide", { direction: "right" }, 1670);
						$('.div5e').show("slide", { direction: "right" }, 1770);
						$('.div5f').show("slide", { direction: "right" }, 1870);
						$('.div5g').show("slide", { direction: "right" }, 1970);
					}
					if (iCurrent == 6 || iCurrent == 0)
						$('.div6b').show("slide", { direction: "down" }, 1170);
					
					
					
				});
			}			
		}	
		
		function setPager(oEvent){
			if($(this).hasClass('pagenum')){
				iCurrent = (options.infinite) ? parseInt(this.rel) : parseInt(this.rel) -1;
				move(1);
			}
			return false;
		}
		function setTimer(){
			if(options.interval && !bPause){
				clearTimeout(oTimer);
				oTimer = setTimeout(function(){
					if (options.infinite) {
						move(1);						
					} else {
						iCurrent = !options.rewind && (iCurrent +1 == iSteps) ? -1 : iCurrent;
						bForward = iCurrent +1 == iSteps ? false : iCurrent == 0 ? true : bForward;
						move((options.rewind ? (bForward ? 1 : -1) : 1));						
					}
				}, options.intervaltime);
			}
		}
		function move(iDirection){
			if((options.infinite) || (!options.infinite && iCurrent + iDirection > -1 && iCurrent + iDirection < iSteps)) {				
				iCurrent += iDirection;

				// last page
				if (options.infinite && iCurrent >= oPages.length - 1) {
					setButtons(1);
				}
				// first page
				else if (options.infinite && iCurrent == 0) {
					setButtons(oPages.length - 2);
				}
				else {
					setButtons();
				}

				var oPosition = {};
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));						
				
				oContent.animate(oPosition,{
					queue: false,
					easing: options.easing ? options.easing : false,
					duration: options.animation ? options.duration : 0,
					complete: function(){
						
						if (options.infinite) {
							setInfiniteContent();	
						}
						if(typeof options.callback == 'function')
						options.callback.call(this, oPages[iCurrent], iCurrent);						
					}					
				});
				setTimer();				
			}
		}
		function setInfiniteContent() {
			/**/
			/*var current;
			
			if (options.infinite && iCurrent >= oPages.length - 1) {
				current = 1;
			}
			else if (options.infinite && iCurrent == 0) {
				current = oPages.length - 2;
			}
			else {
				current = iCurrent;
			}*/
			
			/*if(options.overview){
				var oOverview = $('.homepage_banner', oContent);
				$.each(oPager, function(i, oPager) {
					var oOverview = $('.homepage_banner', oContent);
					oOverview.children().removeClass('active');
					$(oOverview[current]).children().addClass('active');
				});
			}*/
			
			
			
			var oPosition = {};
			if (iCurrent >= iSteps + 1) {
				iCurrent = 1;
				oPosition[bAxis ? 'left' : 'top'] = -iPageSize;	
				oContent.css(oPosition);
			} else if (iCurrent == 0) {
				iCurrent = iSteps;
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize));	
				oContent.css(oPosition);
			}
			
		}
	};
})(jQuery);
