// JavaScript Documentvar 
animating = false;	// Animation playing - boolean

$(document).ready(function(){	// Onload function
	
	var url = document.URL;
	var page = url.substring(url.lastIndexOf('/')+1);
	
	$(".active").click(function(e){  // Disabling active tab function
		e.preventDefault();							
	});
	
	/* Common page tabs */
	$(".tab_content").each(function(){	// Rewrite each tab ID - unset anchor jumping to id
		var id = $(this).attr("id");
		$(this).removeAttr("id").attr("id","t"+id);
	});
	
	$(".tab").click(function(e){	// Click event activation - Tabs
		if (!$(this).hasClass("active") && !animating)
		{
			animating = true;
			UpdateTabNavigation($(this).attr("href"));
			e.preventDefault();
			return false;
		}
		else
		{
			return false;
		}
	});
	
	/* END - Common page tabs */
	document.getElementById('home_content').style.display = 'block';
	document.getElementById('home_loading').style.display = 'none';
	
	UpdateTabNavigation(0);
	
});

function UpdateTabNavigation(tab)	// Controling Tab navigation
{
	switch(tab)
	{
		case 0:	// On page entering or reloading
			var url = document.URL;
			//var parameter = url.substring(url.indexOf("#")+1,url.length);	// Tab prefix extraction
			var parameter = location.hash.substring(1);
			
//			$("#home_loading").fadeOut(1);	// Hides loading div
			$("#home_loading").hide();
			$("#home_content").fadeIn(550);	// Shows foto content

//			if (url.indexOf("#") != -1 && parameter.length > 0) 	// Url has hash value
			if (parameter.length > 0) 	// Url has hash value
			{
				$(".tab_content").each(function(){
					if ($(this).css("opacity") == 1) 
					{
						$(this).hide();
					}
				});
				
				$("#t"+parameter).fadeIn(350,function(){animating = false;});
				//$("#"+parameter).animate({opacity:1},350,function(){animating = false;});
				
				$(".tab").each(function(){
					if($(this).hasClass("active")) $(this).removeClass("active");
					if($(this).attr("href") == "#"+parameter) $(this).addClass("active");
				});
			}
			else	// Url has no hash value
			{
				$("a[href=#tab1]").addClass("active");
				$(".tab_content").each(function(){
					if ($(this).css("opacity") == 1) 
					{
						$(this).hide();
					}
				});
				
				$("#ttab1").fadeIn(350,function(){animating = false;});
				//$("#tab1").animate({opacity:1},350,function(){animating = false;});
			}
			break;
		default:	// Tab navigation button clicked
			var parameter = tab.substring(tab.indexOf("#")+1,tab.length);
			$(".tab_content").each(function(){
				if ($(this).css("opacity") == 1) 
					{
						$(this).hide();
					}
			});

			$("#t"+parameter).fadeIn(350,function(){animating = false;});	
			//$("#"+parameter).animate({opacity:1},350,function(){animating = false;});
				 		
			$(".tab").each(function(){
				if($(this).hasClass("active")) $(this).removeClass("active");
				if($(this).attr("href") == "#"+parameter) $(this).addClass("active");
			});
			
			parent.location.hash = tab;	// Sets the hash value to url without reloading page
	}
}