$(function() {
		$("#content").accordion({
			header: "h2",
			alwaysOpen: false
		});
		
		$('a#contact_link').click(function(){
			$('#container_front').css({"display":"block"}).animate({"opacity":"0"},0).animate({"opacity":".5"},"slow");
			$('#contact_form').css({"display":"block"}).animate({"opacity":"0"},0).animate({"opacity":"1"},1000);
		});
		
		
		$('img.project')
		.animate({"opacity":".6"},0)
		.hover(function(){
			$(this).stop().animate({"opacity":"1"},200)
		},function(){
			$(this).stop().animate({"opacity":".6"},200)
		})
		.click(function(){
			var spl = $(this).attr('id').split('img');
			var id = spl[1];
			var content=$('#info'+id).html();
			
			//$('#info').remove(); //премахваме предишни дивове ако има такива
			close_info();
			$('#content').after('<div id="info"><a href="#" id="close_info" onclick="close_info(); return false;"></a></div>');
			$('#info')
		    .css({"display":"block"}).append(content).animate({"left":"+=250px"},1000);
			
			$('a#close_info')
			.animate({"opacity":.5},0)
			.hover(function(){
				$(this).animate({"opacity":1},100);
			},function(){
				$(this).animate({"opacity":.5},100);
			})
			.click(function(){
				$(this).animate({"opacity":1},0);
			});
			
			$('h2').click(function(){close_info();});
		});
		
		
				
});

function close_info(){
	$('#info').animate({"left":"-=250px"},1000, 'linear', function(){$(this).remove()})
}


/*За контактната форма*/
function closeForm(){
	$('#contact_form').animate({"opacity":"0"},"slow").css({"display":"none"});
	$('#container_front').animate({"opacity":"0"},"slow", function(){$('#container_front').css({"display":"none"})});
}

function sendContact(){	
	var name = $('#name').val();
	var email = $('#email').val();
	var mess = $('#mess').val();
	
	var status='';
	if(name==''){status += "Полето \"Име\" не може да бъде празно";}
	else if(email==''){status += "Полето \"Email\" не може да бъде празно";}
	else if(isValidEmail(email)==false){status += "Въведеният email адрес е невалиден";}
	else if(mess==''){status += "Полето \"Съобщение\" не може да бъде празно";}
	else{
		sendMess(name, email, mess);
		showStatus('success','Вашето съобщение е изпратено.<br />Благодаря Ви!');
	}
	if(status!=''){showStatus('error',status);}
}

function showStatus(status, content){
		var bg='';
		if(status=='error'){bg='url(img/error.gif) 0 0 no-repeat'}
		else if(status=='success'){bg='url(img/success.gif) 0 0 no-repeat'}
		
		$('#contact_form').animate({"opacity":"0"},"slow");
		$('#container').after('<div id="status" ></div>');
		
		$('#status').animate({"opacity":"0"},0).css({"background":bg}).html(content).animate({"opacity":"1"},"slow")
		.click(function(){closeStatus(status);});
}

function closeStatus(status){
	$('#status').animate({"opacity":"0"},"slow").remove();
	if(status == 'error'){$('#contact_form').animate({"opacity":"1"},"slow");}
	else{
		$('#contact_form').css({"display":"none"});		
		$('#container_front').animate({"opacity":"0"},"slow", function(){$('#container_front').css({"display":"none"})});
		clearForm();
	}
}

function sendMess(Cname, Cemail, Cmess){
	var url="contacts.php";
	$.post(url, { name: Cname, email: Cemail, mess:Cmess},
		function(data){
	   // alert("Data Loaded: " + data);
	  });	
}


/*Това е за листване на проектите*/
$(function(){
	var num_projects = $('.project').size();
	$.pages = Math.ceil(num_projects/8)*1; //броя на страиците	
	if(!$.count){$.count=1;}//на коя страница сме
	
	$('#arrows img').animate({"opacity":.5},0)
	.hover(function(){$(this).animate({"opacity":1},100)},function(){$(this).animate({"opacity":.5},100)});
	var height = $('#projects_container2').height();
	
	$('#prev_').click(function(){
		if($.count>1){
		$('#projects_container2').animate({"top":"+=180px"},'slow', 'swing');
		close_info();
		$.count--;
		}
	});
	$('#next_').click(function(){
		if($.count<$.pages){
			$('#projects_container2').animate({"top":"-=180px"},'slow');
			close_info();
			$.count++;
		}
	});		
});

function loadContacts(){
	$('#contacts_content').html('<b>e-mail:</b> <a href="mailto:skoychev@gmail.com">skoychev@gmail.com</a><br /><br /><b>skype:</b> <a href="skype:stanimir.atanasov">stanimir.atanasov</a><br />');	
}

function clearForm(){
	$('#name').val('');
	$('#email').val('');
	$('#mess').val('');
}

function isValidEmail(strEmail){
	  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	  strEmail = document.forms[0].email.value;
	    if (strEmail.search(validRegExp) == -1) 
	   {
	      return false;
	    } 
	    return true; 
	}

