$(window).load(function()
{
	//Correcting Menu-Style
	$("#menuFirst td").each(function(i)
	{
		$(this).width($(this).width()+29);
		$(this).attr("valign","top");
	});
	
	//Add Scroll-Pane
	$("#content").jScrollPane({scrollbarMargin:10, animateInterval:20, showArrows:true, animateTo:true, maintainPosition:false});
	
	//Load Submenu
	loadMenu(menuTop);
	
	//Process Forms
	processForms();
	
	loadTestimony();
});

function processForms()
{
	$("#content form").each(function(i)
	{
		if($(this).attr("action")!=undefined&&$(this).attr("action").charAt(0)=="/"&&$(this).attr("action").substring(1).indexOf("/")==-1)
		{
			$(this).ajaxForm({
				beforeSubmit:	function(){$("#content").fadeOut("normal");},
				success:		function(){$("#content").fadeIn("normal",function(){$("#content").jScrollPane({scrollbarMargin:10, animateInterval:20, showArrows:true, animateTo:true, maintainPosition:false});});},
				url:			"/articles/ajaxArticle"+$(this).attr("action"),
				target:			"#content"
			});
		}
	});	
}

function loadMenu(submenu,hasContent)
{	
	if(hasContent==true) loadContent(submenu);

	if($("#menuSecond ul").attr("name")!=submenu)
	{
		$("#menuSecond").fadeOut("normal",function(){
			if($("#submenu_"+submenu).html()!=null)
			{
				$("#menuSecond").html("<ul name='"+submenu+"'>"+$("#submenu_"+submenu).html()+"</ul>");
			
				$("#menuSecond a").each(function(i)
				{
					$(this).removeAttr("href");
					$(this).click(function(){loadContent($(this).attr("id"));});			
				});
				
		    	$("#menuSecond").fadeIn("normal");
		    }
		    else $("#menuSecond").html("<ul></ul>");
		});
	}
}

function loadContent(alias)
{
	//alert(alias);
	if($("#content").attr("name")!=alias)
	{
		$("#content").fadeOut("normal",function(){
		
			$("#content").load("/articles/ajaxArticle/"+alias,null,function(){
				
				$("#content").html($("#content").html()+"<br/><br/>");
				$("#content").attr("name",alias);
				
				var href="";
				//Make internal links Ajax Links
				$("#content a").each(function(i)
				{
					if($(this).attr("href")!=undefined)
					{
						href = $(this).attr("href").replace("http://"+location.hostname,"");
						if(href.charAt(0)=="/"&&href.substring(1).indexOf("/")==-1)
						{						
							$(this).attr("id",href.substring(0));
							$(this).removeAttr("href");
							$(this).click(function(){loadContent($(this).attr("id"));});	
						}
					}
				});
				
				//make Forms to Ajax Forms
				processForms();
			
				$("#content").fadeIn("normal",function(){
					//Add Scroll-Pane
					$("#content").jScrollPane({scrollbarMargin:10, animateInterval:20, showArrows:true, animateTo:true, maintainPosition:false});
				});
			});
		});
		
		loadTestimony();
	}
}

function loadTestimony()
{
	$("#testimony_inner").fadeOut("normal",function(){
	
		$("#testimony_inner").load("/modules/testimonies/index/"+Math.random(), function(){
			$("#testimony_inner").fadeIn("normal");
		});
	});
}
