$.fn.defaultText = function(value)
{
    var element = this.eq(0);
    element.data('defaultText',value);

    element.focus(function(){
        if(element.val() == value){
            element.val('').removeClass('defaultText');
        }
    }).blur(function(){
        if(element.val() == '' || element.val() == value){
            element.addClass('defaultText').val(value);
        }
    });
    return element.blur();
}

function setMenu()
{
	$('#Menu ul').hide();
	$('#Menu li a').click(function(){
		var checkElement = $(this).next();		
		if((checkElement.is('ul')) && (checkElement.is(':visible')))
		{
			$('#Menu ul:visible').slideUp('normal');
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible')))
		{
			$('#Menu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
		if($(this).hasClass('uniq'))
		{
			$('#Menu ul:visible').slideUp('normal');
		}
	});
}

function setNavigation()
{
	//-URL
	var hash = window.location.hash.substr(1);
	var href = $('#Menu li a').each(function(){		
		var href = $(this).attr('href');
		var ext = href.split('.');		
		if(hash==ext[0])
		{
			var toLoad = hash+'.'+ext[1]; 
			$.get(toLoad, function(data){
				$('#Content').html($('#Content', data).html());
			});
		}  	
	}); 
	//----

	//-IMPORTS (for refresh)
	if(window.location.hash=="#index")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subI.css'});
		$.getScript('Scripts/JS/subMedia.js');
	};
	if(window.location.hash=="#ginasio")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subG.css'});
		$.getScript('Scripts/JS/subMedia.js');
		setActive('#btg', false);
		$('#ulC').show();
	};
	if(window.location.hash=="#curso_info")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subInfo.css'});
		setActive('#btcu', false);	
		$('#ulC').show();
	};
	if(window.location.hash=="#conselhos")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subClh.css'});
		setActive('#btco', false);	
		$('#ulC').show();
	};
	if(window.location.hash=="#imprensa")
	{
		//alert('appending by hash');		
		$('#theme').attr({'href':'Scripts/CSS/subImp.css'});
		$.getScript('Scripts/JS/subMedia.js');
		setActive('#bti', false);
		$('#ulC').show();
	};
	if(window.location.hash=="#fotos_curso")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subF.css'});
		$.getScript('Scripts/JS/subMedia.js');		
		setActive('#btf', false);
		$('#ulC').show();	
	};
	if(window.location.hash=="#videos")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subV.css'});
		$.getScript('Scripts/JS/subMedia.js');
		setActive('#btv', false);
		$('#ulC').show();		
	};	
	if(window.location.hash=="#autor")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subA.css'});
		setActive('#btau', false);
		$('#ulL').show();
	};
	if(window.location.hash=="#livro_d" || window.location.hash=="#livro_k")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subL.css'});
		if(window.location.hash=="#livro_d")
		{
			setActive('#btld', false);
		}
		else
		{
			setActive('#btlk', false);
		}
		$('#ulL').show();
	};
	if(window.location.hash=="#encomendas")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subE.css'});
		$.getScript('Scripts/JS/subEvalidate.js');
	};
	if(window.location.hash=="#contactos")
	{
		//alert('appending by hash');
		$('#theme').attr({'href':'Scripts/CSS/subC.css'});
		$.getScript('Scripts/JS/subCvalidate.js');
	};
	//----

	
	
	//-NAVIGATION						
	$('#Menu li a').click(function(e) 
	{
		if($(this).attr('href') != '#')
		{
			setActive($(this), false);
					
			var toLoad = $(this).attr('href'); 
			$('#Bottom').fadeOut('fast');
			$('#Content').fadeOut('normal', function callback(){
				$('#loading').fadeIn('normal', loadContent);
			});
			
			//-URL
			var ext=0; 
			var href = $(this).attr('href').split('.');
			if(href[1]=="html")
			{
				ext = 5;
			}
			else if(href[1]=="php")
			{
				ext = 4;
			}			
			window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-ext);
			//----								
			
			function loadContent()
			{  
				//alert('loading: '+toLoad);
				$.get(toLoad, function(data){
					$('#Content').html($('#Content', data).html());
				});
				$('#loading').fadeOut('normal', function showNewContent(){  						
					
					if(href[0]=="index")
					{
						$('#theme').attr({'href':'Scripts/CSS/subI.css'});
						$.getScript('Scripts/JS/subMedia.js');
					}
					if(href[0]=="ginasio")
					{
						$('#theme').attr({'href':'Scripts/CSS/subG.css'});
						$.getScript('Scripts/JS/subMedia.js');		
					}
					if(href[0]=="curso_info")
					{
						$('#theme').attr({'href':'Scripts/CSS/subInfo.css'});
					}
					if(href[0]=="conselhos")
					{
						$('#theme').attr({'href':'Scripts/CSS/subClh.css'});
					}
					if(href[0]=="imprensa")
					{
						$('#theme').attr({'href':'Scripts/CSS/subImp.css'});
						$.getScript('Scripts/JS/subMedia.js');
					}
					if(href[0]=="videos")
					{
						$('#theme').attr({'href':'Scripts/CSS/subV.css'});
						$.getScript('Scripts/JS/subMedia.js');	
					}
					if(href[0]=="fotos_curso")
					{
						$('#theme').attr({'href':'Scripts/CSS/subF.css'});
						$.getScript('Scripts/JS/subMedia.js');						
					}
					if(href[0]=="autor")
					{
						$('#theme').attr({'href':'Scripts/CSS/subA.css'});
					}
					if(href[0]=="livro_d" || href[0]=="livro_k")
					{
						$('#theme').attr({'href':'Scripts/CSS/subL.css'});
					}
					if(href[0]=="encomendas")
					{
						$('#theme').attr({'href':'Scripts/CSS/subE.css'});
						$.getScript('Scripts/JS/subEvalidate.js');	
					}
					if(href[0]=="contactos")
					{
						$('#theme').attr({'href':'Scripts/CSS/subC.css'});						
						$.getScript('Scripts/JS/subCvalidate.js');
						//window.location.href = 'contactos.php';
					}
					//----
					$('#Content').fadeIn('normal', function showBottom(){$('#Bottom').fadeIn('slow');});
				});
			}
		}
		e.preventDefault();	
	});
	//----
	
	//-CLICKME
	 if ($.browser.msie)
	 {
		 $('#clickMe').fancybox({
			'type':'inline',
			'modal': false,
			'autoScale': false,
			'width': '670',
			'height': '375',
			'overlayOpacity': 0.5,
			'overlayShow': true,
			'autoDimensions': false,
			'enableEscapeButton': true
		});
	 }
	 else
	 {
		 $('#clickMe').fancybox({
			'type':'inline',
			'modal': false,
			'autoScale': false,
			'width': '653',
			'height': '357',
			'overlayOpacity': 0.5,
			'overlayShow': true,
			'autoDimensions': false,
			'enableEscapeButton': true
		});
	 }
	//----
}

function setActive(elem, setNow)
{
//	alert($(elem).attr('href')+' '+setNow);
	if(!setNow)
	{
		$('#Menu li a').each(function(){		
			if($(this).hasClass('active'))
			{
				$(this).css('border-right','0px');
				$(this).removeClass('active');
			}
		}); 
		
		if(!$(elem).hasClass('uniq'))
		{
			$(elem).css('border-right','5px solid #F60');
			$(elem).addClass('active');
		}
	}
	else
	{
		$(elem).css('border-right','5px solid #F60');
		$(elem).addClass('active');
	}
}

$(document).ready(function()
{
	setMenu();
	setNavigation();	
});

