// JavaScript Document

function updatePortfolio(id) {
	window.location.hash = id;
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	var strURL = "js/portfolio.php?port=" + id;
	
	if (id == "contacted") {
		var name = document.getElementById("form_name").value;	
		var email = document.getElementById("form_email").value;	
		var message = document.getElementById("form_message").value;	
		strURL = strURL + "&name=" + name + "&email=" + email + "&msg=" + message;
	}
	
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            update = self.xmlHttpReq.responseText.split('</div><div id="right_bot">');
			updatepage(update[0],'right_top');
			updatepage(update[1],'right_bot');
        }
    }
    self.xmlHttpReq.send(null);
}

function updatepage(str,id){
	document.getElementById(id).innerHTML = str;
}

window.onload = function() {
	initialiseStateFromURL();
    setInterval(initialiseStateFromURL, 1000);
}

function initialiseStateFromURL() {
	var initialTab = window.location.hash.substr(1);
	var recentHash = "";
   	if (window.location.hash == recentHash) {
		updatePortfolio('sally_rose_2');
		return;
    }
    updatePortfolio(initialTab);
}
