function openWindow(url,xs,ys) {
	var mWidth = xs;
	var mHeight = ys;
	var leftPos = (screen.width - mWidth) / 2;
	var topPos = (screen.height - mHeight) / 2;
	w = window.open(url, '_', 'width='+mWidth+',height='+mHeight+',top='+topPos+',left='+leftPos+',location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes')
	w.focus();
}


function loadCategory(c,p) {
		
	var sethash = true;

	if(gSettings[1] != "" && gSettings[0] == c) {

		closeProject(gSettings[1]);

	} else if(gSettings[2] != "" && gSettings[0] == c) {

		closeHistory(gSettings[2]);

	} else {
	
		if(gSettings[1] != "") {
			closeProject(gSettings[1]);
		}
		if(gSettings[2] != "") {
			closeHistory(gSettings[2]);
		} 
		if(gSettings[0] == "") {
			openCategory(c,p)
		} else if(gSettings[0] == c) {
			closeCategory(c);
			sethash = false;
		} else {
			closeCategory(gSettings[0])
			openCategory(c,p)
		} 
		
	}
	if(sethash==true)
		setHash();
}


function loadHistory(y) {
	if(gSettings[2] == "") {
		openHistory(y)
	} else if(gSettings[2] == y) {
		closeHistory(y);
	} else {
		closeHistory(gSettings[2])
		//setTimeout("openHistory("+y+")",400);
		openHistory(y);
	}

	setHash();
}

function closeHistory(y) {

	datadiv = "p_data_year_"+y;
	new Effect.BlindUp(datadiv, { duration: 0.4, queue: 'end' });

	pdiv = "p_year_"+y;
	if(y==9999)
		new Effect.Morph(pdiv,{ style:'width: 440px; background-color: #000',duration: 0.3, queue: 'end' });
	else
		new Effect.Morph(pdiv,{ style:'background-color: #000', duration: 0.3 , queue: 'end' });

	gSettings[2] = "";

}

function openHistory(y) {

	datadiv = "p_data_year_"+y;

	if(testForObject(datadiv)) {
		pdiv = "p_year_"+y;
		if(y==9999)
			new Effect.Morph(pdiv,{ style:'width: 880px; background-color: #44abce',duration: 0.3, queue: 'end' });
		else
			new Effect.Morph(pdiv,{ style:'background-color: #44abce',duration: 0.3, queue: 'end' });
		myDatadiv = document.getElementById(datadiv);
		if(myDatadiv.innerHTML == "") {
			loadHistoryContent(y);
		} else { 
			blindDownHistory(datadiv,y);
		}
		gSettings[2] = y;
	} else {
		gSettings[2] = "";
	}

}



function loadProject(p) {

	if(gSettings[1] == "") {
		openProject(p)
	} else if(gSettings[1] == p) {
		closeProject(p);
	} else {
		closeProject(gSettings[1])
		openProject(p)
	}

	setHash();
}


function closeCategory(c) {

	c_datadiv = "c_data_"+c;
	new Effect.BlindUp(c_datadiv, { duration: 0.4, queue: 'end' });

	h2div = "c_h2_"+c;
	new Effect.Morph(h2div,{ style:'width: 220px', duration: 0.2 , queue: 'end' });
	
	gSettings[0] = "";

}

function openCategory(c,p) {

	if(c=="contact") {
		reloadForm(); 
	}

	c_datadiv = "c_data_"+c;
	
	if(testForObject(c_datadiv)) {
		h2div = "c_h2_"+c;
		if(c != "contact" && c != "hist")
			new Effect.Morph(h2div,{ style:'width: 440px',duration: 0.2, queue: 'end' });
		if(p != "") {
			new Effect.BlindDown(c_datadiv, { duration: 0.4, queue: 'end', afterFinish: function() {openProject(p);} });
			gSettings[1] = p;
		} else {
			new Effect.BlindDown(c_datadiv, { duration: 0.4, queue: 'end' });
		}
		gSettings[0] = c;
	} else {
		gSettings[0] = "";
	}
}

function closeProject(p) {

	datadiv = "p_data_"+p;
	new Effect.BlindUp(datadiv, { duration: 0.4, queue: 'end' });

	h3div = "p_h3_"+p;
	new Effect.Morph(h3div,{ style:'width: 440px; background-color: #000', duration: 0.3 , queue: 'end' });

	gSettings[1] = "";

}

function openProject(p) {

	datadiv = "p_data_"+p;

	if(testForObject(datadiv)) {
		h3div = "p_h3_"+p;
		new Effect.Morph(h3div,{ style:'width: 880px; background-color: #44abce',duration: 0.3, queue: 'end' });
	
		myDatadiv = document.getElementById(datadiv);
		if(myDatadiv.innerHTML == "") {
			loadProjectContent(p);
		} else { 
			blindDownProject(datadiv,p);
		}
		gSettings[1] = p;
	} else {
		gSettings[1] = "";
	}
	
}

function blindDownProject(datadiv,p) {

	//new Effect.BlindDown(datadiv, { duration: 0.4, queue: 'end', afterFinish: function() {startSB('p_'+p);} });
	new Effect.BlindDown(datadiv, { duration: 0.4, queue: 'end', afterFinish: function() {startSBNG();} });

}
function blindDownHistory(datadiv,p) {

	//new Effect.BlindDown(datadiv, { duration: 0.4, queue: 'end', afterFinish: function() {setTimeout("startSB_Hist_"+p+"();",100);} });
	new Effect.BlindDown(datadiv, { duration: 0.4, queue: 'end', afterFinish: function() {startSBNG();} });

}
function blindUpProject(datadiv) {

	new Effect.BlindUp(datadiv, { duration: 0.3, queue: 'end' });

}


function loadProjectContent(p) {
	
	url="../project.php?Lang="+Lang+"&pid="+p;
	
	new Ajax.Request(url, {
	  method: 'post',
	  onCreate: function() {
		document.getElementById("p_load_"+p).innerHTML = " <img src=\"../pics/loading.gif\" width=\"9\" height=\"9\" border=\"0\" />";
	  } ,
	  onSuccess: function(transport) {
		Effect.Fade("p_load_"+p, { duration: 0.3 });
		document.getElementById("p_data_"+p).innerHTML = transport.responseText;
		blindDownProject("p_data_"+p,p);
	  }
	});
	
}

function startSB(myGallery) {
 Shadowbox.init({
  skipSetup: true // skip the automatic setup
 });
 Shadowbox.setup($$('a.ampimg'), {
  gallery: myGallery
 });
}

function startSBNG() {
 Shadowbox.init({
  skipSetup: true // skip the automatic setup
 });
 Shadowbox.setup($$('a.ampimg'));
}

function loadHistoryContent(y) {
	
	url="../project.php?Lang="+Lang+"&year="+y;
	
	new Ajax.Request(url, {
	  method: 'post',
	  onCreate: function() {
		document.getElementById("p_load_"+y).innerHTML = " <img src=\"../pics/loading.gif\" width=\"9\" height=\"9\" border=\"0\" />";
	  } ,
	  onSuccess: function(transport) {
		Effect.Fade("p_load_"+y, { duration: 0.3 });
	  	document.getElementById("p_data_year_"+y).innerHTML = transport.responseText;
		blindDownHistory("p_data_year_"+y,y);
		//loadHistoryJs(y,d); // loads shadowbox js and blinds div down
	  }
	});
	
}
function loadHistoryJs(y) {
	
	url="../project_history_js.php?Lang="+Lang+"&year="+y;
	
	new Ajax.Request(url, {
	  method: 'post',
	  onSuccess: function(transport) {
		eval(transport.responseText);
		blindDownHistory("p_data_year_"+y,y);
	  }
	});
	
}

function setHash() {
	
	//var hashval;
	
	if(gSettings[0]!="") {
		hashval = 's:' + gSettings[0];
		urchinPath = '/'+Lang+'/ext/'+gSettings[0];
		if(gSettings[1]!="" && gSettings[1]!=undefined) {
			hashval += '&p:' + gSettings[1];
			urchinPath += '/'+gSettings[1];
		} else if(gSettings[2]!="") {
			hashval += '&y:' + gSettings[2];
			// urchinPath += '/'+gSettings[2];
		}
	} else {
		hashval="#";
	}
	
	if(hashval!="" && hashval!=undefined) {
		document.location.hash = hashval;
		if(isPreview != true)
			urchinTracker(urchinPath);
	} else {
		document.location.hash = "#";	
	}
	
	dhtmlHistory.add(hashval);
	
}

function get_gup() {
	var section = getHash('s');
	var project = getHash('p');
}

function getHash(name) {

	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\#&]"+name+":([^&]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
	return results[1];

}
 
 /* code for history handling */ 
 var navListener = function(newLocation, historyData) {
	if(newLocation != hashval && hashval != "#") {
		var section = getHash('s');
		var project = getHash('p');
		if(gSettings[0] != section)
    		loadCategory(section,project);
    	else if(gSettings[1] != project)
    		loadProject(project);
	}
 }
 window.dhtmlHistory.create({
	toJSON: function(o) {
		return Object.toJSON(o);
	}
	, fromJSON: function(s) {
		return s.evalJSON();
	}
 });



function testForObject(Id) {
	
	var o = document.getElementById(Id);
	if (o) {
		return true;
	}
	
	return false;
}

function sendForm() {

	var f = document.contact_form;
	var err = false;

	if(f.name.value == "") {
		new Effect.Morph("contact_name",{ style:'color: #f00', duration: 0.2, queue: 'end' });
		f.name.focus();
		err = true;
	} else {
		new Effect.Morph("contact_name",{ style:'color: #000', duration: 0.2, queue: 'end' });
	}
	if(f.email.value == "") {
		new Effect.Morph("contact_email",{ style:'color: #f00', duration: 0.2, queue: 'end' });
		if(!err)
			f.email.focus();
		err = true;
	} else {
		new Effect.Morph("contact_email",{ style:'color: #000', duration: 0.2, queue: 'end' });
	}
	if(f.comment.value == "") {
		new Effect.Morph("contact_comment",{ style:'color: #f00', duration: 0.2, queue: 'end' });
		if(!err)
			f.comment.focus();
		err = true;
	} else {
		new Effect.Morph("contact_comment",{ style:'color: #000', duration: 0.2, queue: 'end' });
	}
	
	if(err) {
		return false;
	}

	url="../email.php?Lang="+Lang;

	new Ajax.Request(url, {
		method: 'post',
		parameters: $('contact_form').serialize(true),
		onSuccess: function(transport) {
			document.getElementById("c_contact_result").innerHTML = transport.responseText;
			new Effect.BlindDown("c_contact_result", { duration: 0.3, queue: 'end', afterFinish: function() { resetForm(f); } });
		}
	});
	
	new Effect.BlindUp("c_contact_form", { duration: 0.5, queue: 'end' });
	
	return false

}

function sendFormAgain() {
	new Effect.BlindUp("c_contact_result", { duration: 0.3, queue: 'end' });
	new Effect.BlindDown("c_contact_form", { duration: 0.5, queue: 'end' });
}

function reloadForm() {
		document.getElementById("c_contact_form").style.display = "";
		document.getElementById("c_contact_result").style.display = "none";
		new Effect.Morph("contact_name",{ style:'color: #000', duration: 0.3 });
		new Effect.Morph("contact_email",{ style:'color: #000', duration: 0.3 });
		new Effect.Morph("contact_comment",{ style:'color: #000', duration: 0.3 });
}

function resetForm(f) {
		f.name.value = "";
		f.email.value = "";
		f.comment.value = "";
}
