var sitemap = {
	showObjs : new Array(),
	maxHeight : 50,
	overObj : null,

	/* create onlick on all div elements */ 
	init_sub : function (obj,num)
	{
			if (obj.attr('nodeName')=='UL')
			{
				obj.contents().each(function (i)	{
					
				if ($(this).attr('nodeName')=='LI')
				{
					$(this).bind('mouseover',function(){sitemap.over($(this));return false});
					$(this).bind('mouseout',function(){sitemap.out();return false});	
				}
					
				if ($(this).children().eq(0).attr('nodeName')=='DIV')
				{
					
					if (num==1)
					{
						$(this).children().eq(0).bind('click',function(){sitemap.open($(this),1);return false});
						$(this).children().eq(0).children().eq(0).bind('click',function(){sitemap.open($(this).parent(),1);return false});
					}
					
					if (num==2)
					{
						$(this).children().eq(0).bind('click',function(){sitemap.open($(this),2);return false});
						$(this).children().eq(0).children().eq(0).bind('click',function(){sitemap.open($(this).parent(),2);return false});
					}
					
					$(this).contents().each(function (i)	{
							sitemap.init_sub($(this),num+1);
					});
				}
				else
				{
					if ($(this).children().eq(0).attr('nodeName')=='A')
						$(this).bind('click',function(){location.href=$(this).children().eq(0).attr('href')});	
				}
			});
		}
	},
	
	/* create onlick on all div elements */ 
	init : function ()
	{		
		if ($('#sitemap #content').attr('nodeType'))
		{
			$('#sitemap #content').contents().each(function (i)	{
				if($(this).attr('class')=='container')
				{
					$(this).children().eq(0).children().eq(0).bind('click',function(){
						sitemap.hide(1);
						toggle.toggleView($(this).parent(),1);return false
					});
					$(this).children().eq(0).bind('click',function(){
						sitemap.hide(1);return false
					});
					sitemap.init_sub($(this).children().eq(0).next().children().eq(0).children().eq(0),1);
				}
			});
		}
	},
	
	/* click on element */
	open : function (obj,num)
	{
		rnum=0;
		if (obj.next().css('display')=='block')//hide if show
		{
			sitemap.hide(num);
			rnum=-1;
		}
		else //show if hide
		{
			sitemap.hide(num);
			obj.next().css('display','block');
			obj.parent().attr('class','activ');
			sitemap.showObjs[num]=obj;
		}
		
		//set background & height
		maxHeight=sitemap.maxHeight;
		for(i=1;i<=2;i++)
			if (sitemap.showObjs[i])
				if (sitemap.showObjs[i].next().attr('offsetHeight')>maxHeight)
					maxHeight=sitemap.showObjs[i].next().attr('offsetHeight');
		
		if (num==1)//linkdeep 1
		{
			minobj=obj.parent().parent();
			backgroundobj=obj.parent().parent().parent().parent();
		}

		if (num==2)//linkdeep 2
		{
			minobj=obj.parent().parent().parent().parent();
			backgroundobj=obj.parent().parent().parent().parent().parent().parent();
		}
		
		if (num+rnum)
			backgroundobj.attr('class','box box-style'+(num+rnum));
		else
			backgroundobj.attr('class','box');
		
		
		if (minobj.attr('offsetHeight')<maxHeight)
			minobj.css('height',(maxHeight)+'px');		
		else
			minobj.css('height','auto');		
			
	},
	
	hide : function (num)
	{
		if (sitemap.showObjs[1])//reset background & height
		{
			sitemap.showObjs[1].parent().parent().parent().parent().attr('class','box');
			sitemap.showObjs[1].parent().parent().css('height','auto');
		}
		
		for(num;num<=2;num++)//hide all deeper items
		{
			if (sitemap.showObjs[num])
			{
				sitemap.showObjs[num].parent().attr('class','');
				sitemap.showObjs[num].next().css('display','none');
				sitemap.showObjs[num]=null;
			}
		}
	},


	over : function (obj)
	{
		if (sitemap.overObj) sitemap.out();
		sitemap.overObj=obj;
		if (sitemap.overObj.attr('class')!='activ')
			sitemap.overObj.attr('class','over');
		else
			sitemap.overObj=null;
	},
	
	out : function()
	{
	 if (sitemap.overObj)
	 {
	 	if (sitemap.overObj.attr('class')!='activ')
	 		sitemap.overObj.attr('class','');
	 	sitemap.overObj=null;
	 }
	}
}
	


$(document).ready(function(){
	sitemap.init();
});