;(function($) {




function tabs(menu, nav) {
	var currentView = 1;
	var tabCount = 0;
	var viewCount = 0;
	
	var tabArray = [];
	var viewArray = [];


	$(menu).each(function() {
		tabArray.push($(this));
		$(this).attr('rel',tabCount);
		tabCount++;
		$(this).click( function(e) {
			e.preventDefault();
			//alert($(this).attr('rel'));
			switchtabs($(this).attr('rel'));
		});
	});
	
	$(nav).each(function() {
		$(this).fadeOut(0);
		$(this).addClass('down');
		viewArray.push($(this));
		$(this).attr('rel',viewCount);
		viewCount++;
	});

	function switchtabs(where) { 
		if(where != currentView){ 
	
			if(tabArray[currentView].hasClass('open')){
				tabArray[currentView].removeClass('open');
			}
			if(viewArray[currentView].hasClass('up')){
				viewArray[currentView].fadeOut(0);
				viewArray[currentView].removeClass('up');
			}
			tabArray[where].addClass('open');
			viewArray[where].fadeIn();
			viewArray[where].addClass('up');
			currentView = where;

		}
	}
	switchtabs(0);
	

}



function loadHomeGallery(postnum){

	var slideCount = 0;
	var buttonCount = 0;
	
	var slidesArray = [];
	var buttonsArray = [];
	
	var playing = true;
	var current = slidesArray.length - 1;

	$('#gal-'+postnum+' a').each(function(){
		slidesArray.push($(this));
		$(this).attr('rel',slideCount);
		slideCount++;
		//$(this).css({opacity: 0.2 });
		$(this).click( function(e){
			e.preventDefault();
			loadPic($(this).attr('href'));
			//$(this).animate({opacity: 1 }, 1000);
			current = $(this).attr('rel');
		});
		$(this).mouseover( function(){
			if(current != $(this).attr('rel')){
				//$(this).animate({opacity: 1 }, 100);
			}
		});
		$(this).mouseout( function(){
			if(current != $(this).attr('rel')){
				//$(this).animate({opacity: 0.2 }, 100);
			}
		});
	});
	

	function finalDim(){
		var photoHeight = $('#image-'+postnum+'').height() + 1;
		var photoWidth = $('#image-'+postnum+'').width() + 20;
		$('#image-cover-'+postnum+'').animate({height:photoHeight }, 200);
		$('#image-cover-'+postnum+'').animate({width:photoWidth }, 200);
	}
	
	
	var current = slidesArray.length - 1;
	
	function loadPic(which){
		//$('#video-post-'+postnum+'').remove();
		//slidesArray[current].animate({opacity: 0.2 }, 200);
		$('#image-'+postnum+'').fadeOut(200, function(){
			$('#image-'+postnum+'').attr('src','');
			$('#image-'+postnum+'').fadeIn(2000, function(){finalDim(); });
			//$("#placeholder").animate({opacity: 0.5 }, 500);
			$('#image-'+postnum+'').attr('src',which);
		});
	}
	function showSliding(){
		if(playing){
			switchSlide(false, true);
		}
	}
	function switchSlide(reversed, repeatIt){
		slidesArray[current].removeClass('active');
		slidesArray[current].animate({opacity: 0.2 }, 200);
		if(reversed){
			current--;
		}else{
			current++;
		}
		if(current >= slidesArray.length){
			current = 0;
		}

		if(current < 0){
			current = slidesArray.length;
		}
		loadPic(slidesArray[current].attr('href'), slidesArray[current].attr('title'), slidesArray[current].attr('desc'));
		slidesArray[current].addClass('active');
		slidesArray[current].animate({opacity: 1 }, 1000);
		if(slidesArray.length > 1){
			if(repeatIt){
				setTimeout(function(){showSliding(); }, 8000);
			}
		}else{
			$(".playPause").css("display", "none");
		}
	}
	showSliding();
	
};

function my_links(){
	$('.linking').each(function(){
		$(this).css('cursor','pointer')
		$(this).click(function(){
			document.location.href = '/?cat='+$(this).attr('rel')+'';
		});
	});
}
function init(){
//	$('#menu-main a').corners("5px");
	
	if($("body").hasClass("home")){
		loadHomeGallery(12);
		tabs('#sekce-prehled-menu a', '#sekce-prehled .item');
		my_links();
	}
	if($("body").hasClass("archive")){
		portfolioPage();
	}
}



$(document).ready(function () {



init();

});



})(jQuery);

