var menu= new Array('homes','projects','contacts');


function activate( id )
{

	for( i = 0; i < menu.length; i++ )
	{
		if( menu[i] == id )
			document.getElementById( id ).style.borderBottomColor = '#406ab0';
		else
			document.getElementById( menu[i] ).style.borderBottomColor = '#fff';
	}
	
}



//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
var section = 'home';
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		
		
		//if there is not anchor, the loads the default section
		if(!currentAnchor){
			query = "section=home";
			
		}
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			section = splits[0];
			delete splits[0];
			//Create the params string
			var params = splits.join('&');
			var query = "section=" + section + params;
		}
				
		
		
		
		//Send the petition
		$("#loading").show();
		$("#content").html('');
	
		
		$.get("ajax.php",query, function(data){			
			$("#content").html(data);
			$("#loading").hide();
			
			if(section = 'projects')
				$('#demos pre code').each(function() {eval($(this).text());});
			}
		);

		
		
		activate(section);
		
	}
	
	
}

function submit_contacts_form()
{
	var name 		= document.getElementById('name');
	var email		= document.getElementById('email');
	var phone		= document.getElementById('phone');
	var message 	= document.getElementById('message');
	var code	 	= document.getElementById('code');
	
	if( name.value == '' || email.value == '' || phone.value == '' || message.value == '' || code.value == '')
	{
		alert('Моля въведете всички полета.')
		return;
	}
	
	var query='section=save_form&name=' + encodeURIComponent(name.value) + '&email=' + encodeURIComponent(email.value) + '&phone=' + encodeURIComponent(phone.value) + '&message=' + encodeURIComponent( message.value ) + '&code=' + code.value;
	
	
	$("#loading").show();
	$("#contactsForm").html('');
	
	$.get("ajax.php",query, function(data){			
		$("#contactsForm").html(data);
		$("#loading").hide();
	});
	
//	loadXMLDoc('action.php?action=save_form&name=' + encodeURIComponent(name.value) + '&contacts=' + encodeURIComponent(contacts.value) + '&message=' + encodeURIComponent( message.value ) + '&code=' + code.value, 'contactsForm');	
//
}