var ie6 = false;

(function($) {
$.fn.mainMenu = 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(){ 
			if(!$(this).hasClass('active')){
				this.onmouseover = function(){ 
					
					if($(this).find("img").attr("src")) {
						if(!ie6){
							$(this).find("img").attr("src",$(this).find("img").attr("src").split('-sel.png').join('-n.png'));
						} else {
							var rel = $(this).find("img").attr("rel").split('-sel.png').join('-n.png');
							$(this).find("img").attr("src",rel);	
						}
					}
				}//mouseover
				
				this.onmouseout = function(){ 
					if($(this).find("img").attr("src")) {
						if(!ie6){
							$(this).find("img").attr("src",$(this).find("img").attr("src").split('-n.png').join('-sel.png'));
						} else {
							var rel = $(this).find("img").attr("rel").split('-n.png').join('-sel.png');
							$(this).find("img").attr("src",rel);
						}
					}
				}//mouseout
			}//hasclass
		});//links.each

		
	});
};
})(jQuery);

(function($) {
$.fn.footerMenu = 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(){
			if(!$(this).hasClass('active')){
				this.onmouseover = function(){ 
					if($(this).find("img").attr("src")) {
						if(!ie6){
							$(this).find("img").attr("src",$(this).find("img").attr("src").split('-sel.png').join('-n.png'));
						} else {
							var rel = $(this).find("img").attr("alt").split('-sel.png').join('-n.png');
							$(this).find("img").attr("src",rel);
						}
					}
				} //mouseover
				
				this.onmouseout = function(){ 
					
					if($(this).find("img").attr("src")) {
						if(!ie6){
							$(this).find("img").attr("src",$(this).find("img").attr("src").split('-n.png').join('-sel.png'));
						} else {
							var rel = $(this).find("img").attr("alt").split('-n.png').join('-sel.png');
							$(this).find("img").attr("src",rel);	
						}
					}
				} //mouseout
			} //hasclass
		}); //links each

		
	});
};
})(jQuery);

(function($) {
$.fn.toggleTop = function() {
	
	return this.each(function() {
		var $links = $('a', this);
		var timer;
		if($links.size()<=0)return false; //kui pole linke siis edasi ei tee midagi
		
		$links.each(function(){
			
			/* links */
			$(this).click(function(){
				
				var rel = '#' + $(this).attr('rel');
				var self = this;
				
				/* toggle if not animated */
				if( $(rel).is(':animated') ){
					return false;
				} else {
					
					$links.each(function(){
						if(self != this){
							$(this).removeClass('active');
							$('#'+ $(this).attr('rel')).slideUp();
						}
					});
					
					//links
					if($(this).hasClass('active'))
						$(this).removeClass('active');
					else {
						$(this).addClass('active');
						clearTimeout(timer);
						timer = setTimeout(function(){ 
							$(self).removeClass('active');
							$(rel).slideUp("slow"); 
						},3000);
					}
						
					$(rel).slideToggle("slow");
				}
			});
			
			/* timer */
			var rel = '#' + $(this).attr('rel');
			var link = this;
		
			$(rel).mouseleave(
				function(){
					timer = setTimeout(function(){ 
						$(link).removeClass('active');
						$(rel).slideUp("slow"); 
					},3000);
				}
			).mouseenter(
				function(){ clearTimeout(timer); }
			);
			
		}); //links each

		
	});
}
})(jQuery);

(function($) {
$.fn.chapters = function() {
	
	function viewport() {
			
		// the horror case
		if ($.browser.msie) {
			
			// if there are no scrollbars then use window.height
			var d = $(document).height(), w = $(window).height();
			
			return [
				window.innerWidth || 								// ie7+
				document.documentElement.clientWidth || 	// ie6  
				document.body.clientWidth, 						// ie6 quirks mode
				d - w < 20 ? w : d
			];
		} 
		
		// other well behaving browsers
		return [$(window).width(), $(document).height()];
	
	}
	
	function setOverlaySize() {	
			setOverlay();
			var offset = $($('#menu div.wrap').get(0)).offset();
			$('#chapters').css({'left': offset.left+'px'});
	}
	function setOverlay(){
		var menuoffset = $('#menu').offset();
		var menuheight = $('#menu').height();
		
		var docmin = (menuoffset.top+menuheight);
		var viewport_size = viewport();

		$('#overlay').css({'height':(viewport_size[1]-docmin)+'px', 'opacity':'0.5'});
	}
	
	return this.each(function() {
		
		$(this).click(function(){
			
			if( $('#chapters').is(':animated') ){
				
				return false;
				
			} else {
				
				var offset = $($('#menu div.wrap').get(0)).offset();
				$('#chapters').css({'left': offset.left+'px'});
				
				setOverlay();
				
				if($('#overlay:visible').length){
					$('#overlay').fadeOut();
					$(window).unbind('resize', setOverlaySize);	
				} else {
					$('#overlay').fadeIn();
					$(window).resize(setOverlaySize);
				}
				
				$('#chapters').slideToggle("slow",function(){ setOverlay(); });
				
			}	
		
		});
	
	});
}
})(jQuery);


//onload init
$(function() {
	
	//$(".mainmenu").mainMenu(); 
	//$("#footermenu").footerMenu(); 
	$("#topmenu").toggleTop();
	$('.toggle-chapters').chapters();
	
	$("#search-input").click(function(){
		if($(this).val()=="Search..."){
			$(this).val("");
		}
	});
	$("#search-input").blur(function(){
		if($(this).val()==""){
			$(this).val("Search...");
		}
	});
	
	$(".frontsearch input.text").click(function(){
		if($(this).val()==$(this).attr('title')){
			$(this).val("");
		}
	});
	$(".frontsearch input.text").blur(function(){
		if($(this).val()==""){
			$(this).val($(this).attr('title'));
		}
	});
	if($(".frontsearch input.text").val()==""){
		$(".frontsearch input.text").val($(".frontsearch input.text").attr('title'));
	}
	
	var image_preload = [];
	$('#menu div.mainmenu a').each(function(i){
		$(this).data('src', $($('img',this).get(0)).attr('src'));
		
		if (document.images) {
			image_preload[i] = new Image();
			$(image_preload[i]).attr('src',$(this).data('src')+'&active');
		}
		
		if(!$(this).hasClass('active')){
			
			$(this).mouseover(function(){
				$('img',this).attr('src',$(this).data('src')+'&active');		
			}).mouseout(function(){
				$('img',this).attr('src',$(this).data('src'));
			});
			
		}
	});
	
	var image_preload2 = [];
	$('#footermenu a').each(function(i){
		$(this).data('src', $($('img',this).get(0)).attr('src').replace('&active', ''));
		
		if (document.images) {
			image_preload2[i] = new Image();
			$(image_preload2[i]).attr('src',$(this).data('src'));
		}
		
		if(!$(this).hasClass('active')){
			
			$(this).mouseover(function(){
				$('img',this).attr('src',$(this).data('src'));		
			}).mouseout(function(){
				$('img',this).attr('src',$(this).data('src')+'&active');
			});
			
		}
	});
	
});

