/** nav menu hover function */
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

var menuSide=function(){
	var t=15,z=50,s=6,b;
	function ee(n){this.n=n; this.h=[]; this.c=[]}
	ee.prototype.init=function(p,c){
		b=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.sa('+i+',true)');
			h.onmouseout=new Function(this.n+'.sa('+i+')');
		}
	}
	ee.prototype.sa=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+b;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible';c.style.filter='';}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(b,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		// if(console){ console.log(d,o); }
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		/* if(o>0.99){alert('debugging: '+o);} */
		c.style.height=h+(d*f)+'px';
	}
	return{ee:ee}
}();

/* Webnetwork functions */
var timer = 0; //global scope for timeout;
function waterfordSlideshow(n,p,now)
{
	//n: number of sections to slideshow	
	//p: position in show
	//now: change immediately
	t = 10; // number of seconds between slides
	w = 321; //width of the sidebar (px) 
	x = 3; //number of items per slide
	s = 1800; //speed of transition in milliseconds
	f = 600; //speed of fast transition in milliseconds
	
	if(p==0)
	{
		articles = $('#slider').children();
		// san check
		if((articles.length)/x<=1)
		{
			return;
		}
		
		// write in tracker
		html = '<div class="track"><a href="" onclick="return waterfordSlideshow('+n+',1,true)" class="active"></a>';
		for(i=1;i<n;i++)
		{
			html += '<a href="" onclick="return waterfordSlideshow('+n+','+(i+1)+',true)"></a>';
		}
		html += '</div>';
		
		$('#sidebar').append(html);
		
		//create slides	
		i=0;
		slide = '<div class="slide" style="z-index:0">';
		while(i<articles.length)
		{
			slide += '<div>' + $(articles[i]).html() + '</div>';
			
			i++; //real slide number
			
			if(i%x==0 && i<articles.length)
			{
				slide += '</div><div class="slide" style="z-index:' + i +'">';
			}
		}
		slide += '</div>';
		
		$('#slider').prepend(slide); // now all stacked over the sidebar, hidden with css
		$('#slider .slide').first().toggle().addClass('active'); //reveal the first slide, identical to what is already visible
		
		// $('#slider').css('height','357px'); // lock height 
		articles.remove(); // avoid duplication in the DOM, get rid of the underlying elements
	}
	else
	{
		clearTimeout(timer); // if a manual click this will stop the next scheduled transition
		slides = $('#slider .slide');
		dots = $('#sidebar .track').children();
		
		/*
		$(slides[1]).animate(); // move the second slide (bottom of the stack) over the first 
		$(slides[0]).css('left',x+'px'); // withdraw the first slide;
		
		*/
		$(dots).filter('.active').removeClass('active'); //update the tracker
		if(now)
		{
			$(slides).filter('.active').fadeOut(f).removeClass('active'); // withdraw the current slide;
			$(slides[p-1]).delay(f/2).fadeIn(s).addClass('active'); // show the requested slide after the current one has finished fading out
			$(dots[p-1]).addClass('active'); //update the tracker
			p--;
	
		}
		else
		{
			$(slides[p-1]).fadeOut(s).removeClass('active'); // withdraw the current slide;
			
			if(p==n)
			{
				//return to beginning
				p=0;
			}
				
			$(slides[p]).delay(s/2).fadeIn(s).addClass('active'); // show the next slide after the current one has finished fading out	
			$(dots[p]).addClass('active'); //update the tracker
		}
		
		
	}
	
	//trigger the next run
	timer = setTimeout('waterfordSlideshow('+ n + ','+ (p+1) + ')',t*1000);
	
	return false; //stop the click action if manually triggered
}

//static footer
$(document).ready(function(){
	d = $('body').height();
	p = parseInt($('body').css('paddingBottom'));
	w = $(window).height();
	if(d < w)
	{
		$('body').css('height',(w)+'px');
	}
	
});
