var graanul = {
	links: null,
	blogload: false,
	init: function(){
		$("#menu").graanulMenu(); 
		$.ajaxSetup ({  
    		cache: false // Disable caching of AJAX responses
		});
		
		/* remove loading and use showloading/hideloading */
		/*$("#content").after("<div id=\"loading\" style=\"display: none; width: 60px; text-align: center; position: absolute; top: 300px; left: 50%;margin-left:-25px;\"><img src=\"img/ajax-loader.gif\" width=\"50\" height=\"50\" alt=\"Laadimine\" /><br/>Laen...</div>");*/
	},
	loadPage: function(address,$links){
		//jooninfo.links = $links;
		
		$('#header-images').cycle('stop');
		
		$("#loading").css('left','50%');
		$("#loading").fadeIn(200);
		
		var height = $("#content").height();
		$("#content").css({position: 'absolute'});
		$("#content").animate({
			'marginTop': (-1*(height+100))+"px",
			'opacity': 0
		}, 1000 );
		
		$("#content").queue(function () {
			$("#content").css({'marginTop': '-9000px'});
		
			$("#content").load(address + " #content > *",{},function (responseText, textStatus, XMLHttpRequest) { 
				graanul.afterLoad(); 
			});
		});
	},
	afterLoad: function(){
		 
		$('#header-images').cycle({ 
			delay:  2000, 
			speed:  300, 
			before: onBefore 
		}); 
	 
		function onBefore() { 
			$('#header-images-title') 
				.html(this.title); 
		}; 
		
		$("#loading").fadeOut(200);
		var height = $("#content").height();
		$("#content").css({'marginTop': (-1*(height+100))+"px"});
		
		//$("#content").css({opacity: 1,top: 0+"px"});

		$("#content").stop().animate({
			'marginTop': "0px",
			'opacity': 1.0
		}, 1000 );
		
		//$("#content").css({position: 'inherit'});
	}
};

(function($) {
$.fn.graanulMenu = function() {

	return this.each(function() {
		var $links = $('a', this);
		if($links.size()<=0)return false; //kui pole linke siis edasi ei tee midagi
		
		$links.each(function(){ var req = $(this).attr("href"); this.aadress=req; $(this).attr("href", "#"); });
			
		var curr = $('a.active', this)[0] || $($links[0]).addClass('active'); //currenti sättimine -> kui puudub siis 1. ja pilt tuleb ka õigeks panna!!
		
		$links.each(function(){
			this.onmouseover = function(){ if($(this).not(curr)){ move(this); } }
			this.onmouseout = function(){ if(this != curr){ moveback(this); } }	
		});
		
		$links.each(function(){
			this.onclick = function(){ 
				//if($links.act==true)return false; 
				//$links.act = true; ;
				setCurr(this);
				graanul.loadPage(this.aadress,$links);
			}	
		});

        setCurr(curr);
		
        function setCurr(el) {
			$(curr).removeClass('active');
			moveback(curr);
            curr = el;
			move(el);
			$(curr).addClass('active');
		};

		function move(el) {
			$(el).stop()
			.animate({
					backgroundPosition: "0px 0px"
			}, 300);
		};
		
		function moveback(el) {
			$(el).stop()
			.animate({
					backgroundPosition: "-100px 0px"
			}, 300);
		}
	});
};
})(jQuery);

(function($) {
$.fn.langMenu = function() {

	return this.each(function() {
		var $links = $('a', this);
		if($links.size()<=0)return false; //kui pole linke siis edasi ei tee midagi
			
		var curr = $('a.active', this)[0] || $($links[0]).addClass('active'); //currenti sättimine -> kui puudub siis 1. ja pilt tuleb ka õigeks panna!!
		
		$links.each(function(){
			this.onmouseover = function(){ if(this != curr){ move(this); } }
			this.onmouseout = function(){ if(this != curr){ moveback(this); } }	
		});

        setCurr(curr);
		
        function setCurr(el) {
            curr = el;
		};

		function move(el) {
			$(el).stop()
			.animate({
					backgroundPosition: "0px 0px"
			}, 300);
		};
		
		function moveback(el) {
			$(el).stop()
			.animate({
					backgroundPosition: "-30px 0px"
			}, 300);
		}
	});
};
})(jQuery);

function hidePopup(id){
	jQuery('#'+id).hide();
}

function showPopup(id){
	jQuery('#'+id).show();
}

function hideMap(id){
	jQuery('#'+id).hide();
	jQuery('#'+id+'-a').removeClass('active');
}

function showMap(id){
	jQuery('#'+id).show();
	jQuery('#'+id+'-a').addClass('active');
}

//onload init
jQuery(function() {
				
	$('#header-images').cycle({ 
		delay:  2000, 
		speed:  300, 
		before: onBefore 
	}); 
 
	function onBefore() { 
		$('#header-images-title') 
			.html(this.title); 
	}; 

	/*jQuery('#header-images').cycle({ 
		fx:     'fade', 
		speed:   300, 
		timeout: 4000, 
		next:   '#header-images', 
		pause:   1 
	});*/
	
	graanul.init();
	
	$("#lang").langMenu(); 
});
