
$(document).bind("ready", function() {
	function gogo()
	{
		$("table#loader").slideDown(100);

		if ($(this).attr("name")=="img")
		{
			$("div[id=viewbigimg]").html('<img src="'+$(this).attr("href")+'" border="0" width="800" height="600" id="closebig">');
			$("table#viewbig").fadeIn(1000, function () {
				$("table#loader").fadeOut(100);
				$("#closebig").bind('click', closebig);
			});
		}
		else if ($(this).attr("target")=="_blank")
		{
			$("table#loader").fadeOut(100);
			return true;
		} else {
			if ($("#submenu a[href='"+$(this).attr("href")+"']").attr("rel")=="page")
			{
				current=$("#submenu a[href='"+$(this).attr("href")+"']").attr("id");
				
				current_page_href2=$("#submenu a[href='"+$(this).attr("href")+"']").attr("href");
				current_page_id2=$("#submenu a[href='"+$(this).attr("href")+"']").attr("id");
				
				page=true;
			} else {
				current_work_href2=$("#submenu a[href='"+$(this).attr("href")+"']").attr("href");
				current_work_id2=$("#submenu a[href='"+$(this).attr("href")+"']").attr("id");
			
				page=false;
			}
		
			if (page)
			{
			
				if ($(this).attr("id")!=current_page_id && current_page_href!='' && current_page_id!='')
				{
					$("strong[id='"+current_page_id+"']").replaceWith('<a href="'+current_page_href+'" rel="page" id="'+current_page_id+'">' + $("strong[id='"+current_page_id+"']").text() + '</a>');
					$("a[id='"+current_page_id+"']").replaceWith('<a href="'+current_page_href+'" rel="page" id="'+current_page_id+'">' + $("a[id='"+current_page_id+"']").text() + '</a>');
					$("#"+current_page_id).bind('click', gogo);
				} 
				if (current_work_href!='' && current_work_id!='') {
					$("strong[id='"+current_work_id+"']").replaceWith('<a href="'+current_work_href+'" id="'+current_work_id+'">' + $("strong[id='"+current_work_id+"']").text() + '</a>');
					$("a[id='"+current_work_id+"']").replaceWith('<a href="'+current_work_href+'" id="'+current_work_id+'">' + $("a[id='"+current_work_id+"']").text() + '</a>');
					$("#"+current_work_id).bind('click', gogo);
				}
				current_work_id='';
				current_work_href='';
	
				current_page_href=current_page_href2;
				current_page_id=current_page_id2;
				
				$("#"+current_page_id).replaceWith('<strong id="'+current_page_id+'">' + $("#"+current_page_id).text() + '</strong>'); //("href");
			} else {
				$("strong[id='"+current_page_id+"']").replaceWith('<a href="'+current_page_href+'" rel="page" id="'+current_page_id+'" class="active">' + $("strong[id='"+current_page_id+"']").text() + '</a>');
				$("#"+current_page_id).bind('click', gogo);
			
				if (current_work_href!='' && current_work_id!='')
				{
					$("strong[id='"+current_work_id+"']").replaceWith('<a href="'+current_work_href+'" id="'+current_work_id+'">' + $("strong[id='"+current_work_id+"']").text() + '</a>');
					$("a[id='"+current_work_id+"']").replaceWith('<a href="'+current_work_href+'" id="'+current_work_id+'">' + $("a[id='"+current_work_id+"']").text() + '</a>');
					$("#"+current_work_id).bind('click', gogo);
				}

				current_work_href=current_work_href2;
				current_work_id=current_work_id2;
	
				$("#"+current_work_id).replaceWith('<strong id="'+current_work_id+'">' + $("#"+current_work_id).text() + '</strong>');
			}		
			
			function onAjaxSuccess(data)
			{
				if (oldcurrent!=current)
				{
					$("#menu-"+oldcurrent).slideUp('fast');
					$("#menu-"+current).slideDown(1000);
					oldcurrent=current;
				}
				$("#tamplate").fadeOut(500, function() {
					$("div[id=tamplate]").html(data); 
				
					$("#tamplate").fadeIn(1000, function() {
						if (current_work_href=='' && current_work_id=='')
						{
							$(".submenu a").bind('mouseover', view_mouseover);
							$(".submenu a").bind('mouseout', view_mouseout);
						}

					});
					$("#preview a, .navigation a").bind('mouseover', preview_mouseover);
					$("#preview a, .navigation a").bind('mouseout', preview_mouseout);
					$("#tamplate a").bind('click', gogo);
					$("form").bind('submit', submitform);
					$("table#loader").slideUp(100);
				});
			}
			
			$.post(
	  			$(this).attr("href"),{type: Math.random()},onAjaxSuccess
			);		
		}
		return false;	
	}
	
	function closebig() {
		$("table#viewbig").fadeOut(1000);
		return false;
	}
	function view_mouseover() {
		$("#preview a[href='"+$(this).attr("href")+"']").css("border", "5px solid #048300");
		return ;
	}
	function view_mouseout() {
		$("#preview a[href='"+$(this).attr("href")+"']").css("border", "5px solid #eeeeee");
		return ;
	}

	function preview_mouseover() {
		if ($(this).find("img").attr("src")!=undefined)
		{
			$(this).css("border", "5px solid #048300");
		}
		$(".submenu a[href='"+$(this).attr("href")+"']").css({ "text-decoration":"none", "font-weight":"bolder" });
	}
	function preview_mouseout() {
		if ($(this).find("img").attr("src")!=undefined)
		{
			$(this).css("border", "5px solid #eeeeee");
		}
		$(".submenu a[href='"+$(this).attr("href")+"']").css({ "text-decoration":"", "font-weight":"normal" });
	}
	
	function submitform() {
		
		function onAjaxSuccess(data)
		{
			$("#tamplate").fadeOut(500, function() {
				$("div[id=tamplate]").html(data); 
				$("#tamplate").fadeIn(1000);
				$("form").bind('submit', submitform);
				$("table#loader").slideUp(100);
			});
		}		
		
		$("table#loader").slideDown(100);
		$.post(
  			current_page_href,$("form").serialize(),onAjaxSuccess
		);
		return false;
	}
	
	$("#content").fadeIn(2000);
	$("#menu-"+current).slideDown(1000);
	
	$("#preview a, .navigation a").hover(preview_mouseover, preview_mouseout);
	$("#submenu a, #tamplate a").bind('click', gogo);
	$(".submenu a").hover(view_mouseover, view_mouseout );
	$("form").bind('submit', submitform);
	
});
