/** 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}
}();

/* if you are having trouble with the submenu not disappearing completely check it doesn't have a top or bottom border */
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 */
$(document).ready(function(){

	//distribute menu items to fit page width
	cont_width = $('#navigation').innerWidth();
	menu_items = $('#menu > li');
	num = menu_items.length;
	extra = cont_width%num;
	each = (cont_width-extra)/num;
	
	$(menu_items).css('width',each+'px').last().css('width',(each+extra)+'px');
	
	/*
	cont_width = $('#navigation').innerWidth();
	var nav_width = 0;
	menu_items = $('#menu > li');
	$(menu_items).each(function(){
		nav_width += $(this).outerWidth();
	});
	space = cont_width - nav_width;
	
	if(space>0){
		link_padding = parseInt($(menu_items).first().children('a').first('a').css('paddingRight')); //relies on default padding being set up correctly in the stylesheet
		num_items = menu_items.length
		widen_each = space/num_items; //float
		// console.log('widen_each',widen_each); //debugging
		while(widen_each>=1)
		{
			
			//expand all items
			each_side = widen_each/2;
			each_side = Math.floor(each_side);
			if(each_side>=1) // ie widen_each >= 2, add to both sides equally
			{
				link_padding += each_side;
				widen_each -= each_side*2;
				space -= each_side*2*num_items;
				$(menu_items).children('a').css({'paddingLeft':link_padding,'paddingRight':link_padding});
			}
			else // ie 1 <= widen_each < 2, add 1 to the right
			{
				widen_each -= 1;
				space -= num_items;
				$(menu_items).children('a').css({'paddingRight':link_padding+1});
			}
		}
		// console.log('widen_each',widen_each); //debugging
		if(widen_each>0) // some amount of space canot be evenly distributed as whole px
		{
			//expand last item to take up any remaining space
			last_item = $(menu_items).last();
			while(space>1) //in the browsers that are causing trouble we need to leave a px free.
			{
				each_side = space/2;
				each_side = Math.floor(each_side);
				if(each_side>=1)
				{
					link_padding += each_side;
					space -= each_side*2;
					$(last_item).children('a').css({'paddingLeft':link_padding,'paddingRight':link_padding});
				}
				else 
				{
					space -= 1;
					$(last_item).children('a').css({'paddingRight':link_padding+1});
				}
			}
		}
	}
	//console.log('space',space); //debugging
	// */
	//match sidebar height to content, or vice versa
	//h = Math.max($('#navigation').height(),$('#shop_display').height() + $('#page_display').height()); //add _display-s because both are present on front page
	//$('#navigation').height(h); //only the sidebar ever needs to get bigger 

	// search field default text and clearing
	/*
	$('#search').val('Type keyword here').focus(function()
	  { if($(this).val()=='Type keyword here'){$(this).val('');} }).blur(function()
	  { if($(this).val()==''){$(this).val('Type keyword here');} })
	*/
		
	//IE rounded corners
	if(!$.support.htmlSerialize && !$.support.opacity) // IE feature detection
	{
	//identify IE8 or earlier
	$('body').addClass('ie8');
	struct = '<span class="ie-tl"></span><span class="ie-tr"></span><span class="ie-br"></span><span class="ie-bl"></span>';
	//could put everything in one selector but list for clarity
	//$('#menu li:first-child, #menu li:last-child').append(struct);
	//$('#footer').append(struct);

	//compensate for irregular first-child/last-child support
	$('li:first-child').addClass('ie-first');$('li:last-child').addClass('ie-last');
	} //end IE detection

	//flag IE9
	if(!$.support.optSelected && $.support.opacity)
	{
		$('body').addClass('ie9');
	}
	
	/*
	for(i in $.support){
		console.log(i+' '+$.support[i]);
	}
	*/
	
}); //end ready
