var last_url=null;
var new_mainbox_timer=null;
$(document).ready(function(){
	
	$('.header').delay(500).slideDown('fast',function(){
		$('.Menu').show('clip',{ direction: "horizontal" },'fast',function(){
			$('.MainBox').slideDown('fast',function(){
				$('.footer').fadeIn('fast',function(){
					AfterAjax();
					ProcessHash(true);
				});
			});
		});
	});
	
	$('.Menu a,.ajax').live('click',function(event){
		if (!$(this).attr('href')) return;
		event.preventDefault();
		location.hash=$(this).attr('href');
	});
	$('.Menu').mouseleave(function(){
		var pos=$('.Menu a.active').position();
		$('.MenuFlow').fadeOut('slow').animate({
			top: pos.top,
			left: pos.left
		},10);
	});
	$('.Menu a[href]').mouseenter(function(){
		$('.MenuFlow').show();
		var pos=$(this).position();
		$('.MenuFlow').clearQueue().animate({
			top: pos.top,
			left: pos.left
		},300);
	});
	
	$('.Galerie img').live('mouseenter',function(){
		$(this).animate({
			opacity:1/*,
			moje:1*/
		}/*,{
			step: function(now, fx) {
		    var data = fx.elem.id + ' ' + fx.prop + ': ' + now;
		    if (fx.prop=="moje") $(fx.elem).css('box-shadow', '0 0 '+(now*10)+'px rgba(255,255,255,'+now+')');
		    //echo('0 0 3px rgba(255,255,0,'+now+')');
		  }
		}*/,'slow');
	});
	
	$('.Galerie img').live('mouseleave',function(){
		$(this).animate({
			opacity:0.6/*,
			moje:0.3*/
		}/*,{
			step: function(now, fx) {
			    var data = fx.elem.id + ' ' + fx.prop + ': ' + now;
			    if (fx.prop=="moje") $(fx.elem).css('box-shadow', '0 0 '+(now*10)+'px rgba(255,255,255,'+now+')');
			    echo(data);
			  }
		}*/,'slow');
	});
	
	
	$(window).bind('hashchange', function(){
	 	ProcessHash(false);
	});
	
	$(document).ajaxStop(function() { 
		AfterAjax();
	});
});

function ProcessHash(first){
	var url="";
	if (location.hash.length<=1) {
		if (first) return;
		url=location.href.replace(/^[^\/]*\/\/[^\/]*/,'');
	} else {
		url=location.hash.substr(1);
	}
	if (last_url==url) return;
	last_url=url;
	echo(url);
 	$('.Menu a').removeClass('active');
 	$.ajax({
		dataType: 'html',
		type: "GET",
		url: url,
		success: function(data){
			$('.Menu a').each(function(){
				if (url.indexOf($(this).attr('href'))!=-1) $(this).addClass('active');
			});
			var prevMainBox=$('.MainBox');
			var newMainBox=$(data);
			$(newMainBox).css('display','none');
			$(newMainBox).prependTo('.Page');
			$(prevMainBox).fadeOut(500);
			clearTimeout(new_mainbox_timer);
			$(newMainBox).fadeIn(500,function(){
				clearTimeout(new_mainbox_timer);
				new_mainbox_timer=setTimeout(function(){
					echo(newMainBox);
					$('.MainBox').not(newMainBox).remove();
				},500);
			});
		}
	});
}


function AfterAjax(){
	setTimeout(function(){
		if ($('.ContentWindow').height()<$('.Content').height()) {
			echo($('.ContentWindow').height()+','+$('.Content').height());
			if (!$('.ScrollVertical:visible').length) {
				$('.ScrollVertical').slider({
					animate: true,
					orientation: 'vertical',
					range: false,
					value: $('.Content').height()-$('.ContentWindow').height(),
					max: $('.Content').height()-$('.ContentWindow').height(),
					slide: function(event, ui) {
						ScrollVertiChange(event.target);
					}
				});
				$('.ScrollVertical,.ScrollVerticalDown,.ScrollVerticalUp').fadeIn('slow');
			}
		} else {
			if ($('.ScrollVertical:visible').length) {
				$('.ScrollVertical,.ScrollVerticalDown,.ScrollVerticalUp').fadeOut('slow');
			}
		}
		
		if ($('.GalerieWindow').width()<$('.Galerie').width()) {
			echo($('.GalerieWindow').width()+','+$('.Galerie').width());
			if (!$('.ScrollGalerie:visible').length) {
				$('.ScrollGalerie').slider({
					animate: true,
					range: false,
					value: 0,
					max: $('.Galerie').width()-$('.GalerieWindow').width(),
					slide: function(event, ui) {
						ScrollGalerieChange(event.target);
					}
				});
				$('.ScrollGalerie,.ScrollGalerieLeft,.ScrollGalerieRight').fadeIn('slow');
			}
		} else {
			if ($('.ScrollGalerie:visible').length) {
				$('.ScrollGalerie,.ScrollGalerieLeft,.ScrollGalerieRight').fadeOut('slow');
			}
		}
		
	},1200);
	
	$('.Content').not('.alreadyProcessed').addClass('alreadyProcessed').mousewheel(function(event, delta) {
		event.preventDefault();
		ScrollVertiMove(delta);
	});
	
	$('.ScrollVerticalDown').not('.alreadyProcessed').addClass('alreadyProcessed').click(function(){
		ScrollVertiMove(-1);
	});
	$('.ScrollVerticalUp').not('.alreadyProcessed').addClass('alreadyProcessed').click(function(){
		ScrollVertiMove(1);
	});
	
	
	$('.Galerie').not('.alreadyProcessed').addClass('alreadyProcessed').mousewheel(function(event, delta) {
		event.preventDefault();
		ScrollGalerieMove(-delta);
	});
	
	$('.ScrollGalerieLeft').not('.alreadyProcessed').addClass('alreadyProcessed').click(function(){
		ScrollGalerieMove(-1);
	});
	$('.ScrollGalerieRight').not('.alreadyProcessed').addClass('alreadyProcessed').click(function(){
		ScrollGalerieMove(1);
	});
	
	$('a[rel=colorbox]').colorbox({
		current: 'obrázek {current} z {total}',
		previous: "předchozí",
		next: "další",
		close: "zavřít",
		maxWidth: $(window).width()-20,
		maxHeight: $(window).height()-20
	});
	
	$('a.vysvetlivka-odkaz').colorbox({
		current: 'obrázek {current} z {total}',
		previous: "předchozí",
		next: "další",
		close: "zavřít",
		maxWidth: $(window).width()-20,
		maxHeight: $(window).height()-20,
		html:function(){
			var vysvetlivky=$('.vysvetlivka-odkaz').nextAll('.vysvetlivka-text');
			var text="Vysvětlivka není dopsána. Dejte mi vědět a já to napravím.<br>Mardou";
			if (vysvetlivky.length>0) {
				text='<h2>'+$(vysvetlivky[0]).attr('title')+'</h2>'+$(vysvetlivky[0]).html();
			} 
			return '<div class="vysvetlivka-okno">'+text+'</div>';
		},
		onComplete:function(){ //aby to bylo lepe videt
			$('#cboxLoadedContent').css('border','1px solid #666');
			$('#cboxClose').css('top','10px').css('right','10px');
		}
	});
	
	
 
}

function ScrollVertiMove(delta){
	var scrollbar=$('.ScrollVertical');
	var value=$(scrollbar).slider( "option", "value" );
	value=value+20*delta;
	if (value<0) value=0;
	if (value>$(scrollbar).slider( "option", "max" )) value=$(scrollbar).slider( "option", "max" );
	$(scrollbar).slider( "option", "value", value);
	ScrollVertiChange(scrollbar);
}

function ScrollVertiChange(scrollbar){
	var value=$(scrollbar).slider( "option", "value" );
	$('.Content').css('margin-top','-'+($(scrollbar).slider( "option", "max" )-value)+'px');
}

function ScrollGalerieMove(delta){
	var scrollbar=$('.ScrollGalerie');
	var value=$(scrollbar).slider( "option", "value" );
	value=value+50*delta;
	if (value<0) value=0;
	if (value>$(scrollbar).slider( "option", "max" )) value=$(scrollbar).slider( "option", "max" );
	$(scrollbar).slider( "option", "value", value);
	ScrollGalerieChange(scrollbar);
}

function ScrollGalerieChange(scrollbar){
	var value=$(scrollbar).slider( "option", "value" );
	$('.Galerie').css('margin-left','-'+(value)+'px');
}

function echo(data){
	if (typeof(console)=='undefined') return;
	if (typeof(console.log)=='undefined') return;
	console.log(data);
}


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26256160-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


