$(document).ready(function(){

anchor.init();

$("#twitter").getTwitter({
	userName: "sensitiveRobot",
	numTweets: 1,
	loaderText: "Loading tweets...",
	slideIn: true,
	showHeading: true,
	headingText: "From the bird's mouth",
	showProfileLink: true
});


//navigation

function clearSelected() {
	$("#home").parent().removeClass("selected");
	$("#about").parent().removeClass("selected");
	$("#work").parent().removeClass("selected");
	$("#contact").parent().removeClass("selected");
}


$("#home").click(function() {
	clearSelected();
	$(this).parent().addClass("selected");
})

$("#about").click(function() {
	clearSelected();
	$(this).parent().addClass("selected");
})

$("#work").click(function() {
	clearSelected();
	$(this).parent().addClass("selected");
})

$("#contact").click(function() {
	clearSelected();
	$(this).parent().addClass("selected");
})


//works section overlays

$(".hotspot").hover(function() {
	$(this).siblings(".overlay").stop();
	$(this).siblings(".overlay").animate({opacity: 0.7}, 200, function(){
	$(this).siblings("div").children(".overlay-link").show();
	});
},
function() {
	$(this).siblings(".overlay").stop();
	$(".overlay-link").hide();
	$(this).siblings(".overlay").animate({opacity: 0}, 200);
	
});


//portrait feature
var duration  = 4000;
var portaitNum = 0;
var portraitLength = 0;
var portraitElements = this.getElementById("photo-container");
var timer = setInterval(changePortrait, duration);
var portraits = portraitElements.getElementsByTagName("img");
var overlays = portraitElements.getElementsByTagName("p");
portraitLength = portraits.length;
portraitNum = portraits.length;

resetOverlay();

function resetOverlay() {
	for (i=0;i<portraitLength;i++) {
		$(overlays[i]).parent().css({opacity:0});
	}
	//alert(portraitNum);
	$(overlays[portraitNum-1]).parent().css({opacity:1});
	
}


function changePortrait() {
	if (portraitNum == 0) {
		portraitNum = portraitLength;
		$(portraits[portraitNum]).parent().animate({opacity: 0}, 1000);
		resetOverlay();
		
		for (i=0;i<portraitNum;i++) {
			//$(portraits[i]).parent().css({opacity:1});
			$(portraits[i]).parent().animate({opacity: 1}, 1000);
		}
		
	
	} else {
		$(portraits[portraitNum]).parent().animate({opacity: 0}, 1000, function(){
			for (i=0;i<portraitLength;i++) {
				$(overlays[i]).parent().css({opacity:0});
			}
			$(overlays[portraitNum]).parent().css({opacity: 1});
			
			
		});	
		
		portraitNum--;
	}
	
	
	
	
}




//contact page
$("#submitBtn").click(function(){
	//document.getElementById("contact-form").style.display  = "none";
	//document.getElementById("contact-address").style.display  = "none";
	//document.getElementById("email-sending").style.display  = "block";
	var valid = '';
	var isr = ' is required.';
	var name = $("#name").val();
	var mail = $("#email").val();
	var subject = $("#subject").val();
	var text = $("#message").val();
	if (name.length<1) {
		valid += '<br />Name'+isr;
	}
	if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
		valid += '<br />A valid Email'+isr;
	}
	if (subject.length<1) {
		valid += '<br />Subject'+isr;
	}
	if (text.length<1) {
		valid += '<br />Text'+isr;
	}
	if (valid!='') {
		document.getElementById("contact-error").style.display  = "block";
	}
	else {
		var datastr ='name=' + name + '&mail=' + mail + '&text=' + text;
		document.getElementById("contact-error").style.display  = "none";
		document.getElementById("contact-form-fields").style.display  = "none";
		document.getElementById("sending-message").style.display  = "block";
		setTimeout("send('"+datastr+"')",2000);
	}
	return false;
	})
	
	



});


function send(datastr){
		$.ajax({	
			type: "POST",
			url: "scripts/sendmail.php",
			data: datastr,
			cache: false,
			success: function(html){
			document.getElementById("sending-message").style.display  = "none";
			document.getElementById("contact-form-fields").style.display  = "none";
			document.getElementById("message-sent").style.display  = "block";
						
		}
		});
	}


