// only load the global sections via jquery if the current page is not index.html/404.html
// contact.php uses include
var fileName = location.href.substring(location.href.lastIndexOf('/')+1);
if (fileName && fileName != "index.html" && fileName != "contact.php" && fileName != "404.html") {
	window.onload = function() {addthis.init();};
	$(document).ready(function() {
		// find element with ID of "#target" and put file contents into it
		$('#top_header_all').load('../../engine/inc/header.html');
		$('#left_menu').load('../../engine/inc/left_menu.html');
		$('#footer').load('../../engine/inc/footer.html');
	});
}

function randomFromTo(from, to) {
	return Math.floor(Math.random() * (to - from + 1) + from);
}

function generateBackground () {
	var bg = document.getElementById("bg");
	if (bg) {
		bg.innerHTML = "<img src='../images/bg/"+randomFromTo(1,14)+".jpg' width='100%'>";
	}
}

window.onresize = calibrate;

function calibrate() {
	var ch = document.body.clientHeight;

	var scrollable_area = document.getElementById("scrollable_area");
	if (scrollable_area) scrollable_area.style.height = ch - 80 + "px";

	generateBackground();
}

// generate random backgrounds
// generate random post-it thumbnails

shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    a = v.join("");
	return a;
};

function generatePostIts() {
	var space =	document.getElementById("post-it");
	var r = 0;
	var howManyPostIts = 4;
	
	postWhat = new Array();
	postWhat[0] = "../../data/thumbnails/post-it/school_construction.jpg";
	postWhat[1] = "../../data/thumbnails/post-it/day-to-day.jpg";
	postWhat[2] = "../../data/thumbnails/post-it/dinner_party.jpg";
	postWhat[3] = "../../data/thumbnails/post-it/kanubeh_football.jpg";
	postWhat[4] = "../../data/thumbnails/post-it/portraits.jpg";
	postWhat[5] = "../../data/thumbnails/post-it/in_school.jpg";
	postWhat[6] = "../../data/thumbnails/post-it/scenery.jpg";
	postWhat[7] = "../../data/thumbnails/post-it/gud_people.jpg";
	postWhat[8] = "../../data/thumbnails/post-it/join.jpg";
	postWhat[9] = "../../data/thumbnails/post-it/volunteer.jpg";
	
	postWhere = new Array();
	postWhere[0] = "../../data/albums/school_construction.html";
	postWhere[1] = "../../data/albums/day-to-day.html";
	postWhere[2] = "../../data/albums/2010-11_dinner_party.html";
	postWhere[3] = "../../data/albums/kanubeh_football.html";
	postWhere[4] = "../../data/albums/portraits.html";
	postWhere[5] = "../../data/albums/in_school.html";
	postWhere[6] = "../../data/albums/scenery.html";
	postWhere[7] = "../../data/albums/volunteers.html";
	postWhere[8] = "../../data/volunteer/join.html";
	postWhere[9] = "../../data/volunteer/join.html";
	
	var a = [0,1,2,3,4,5,6,7,8,9];
	var s = shuffle(a);
	
	for (r = 0; r < howManyPostIts; r++) {
		var w = s.charAt(2*r);
		space.innerHTML += "<a href="+postWhere[w]+"><img src="+postWhat[w]+" width=160 height=160></a>";
	}
}

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var mxheader = 0;

// open hidden layer
function mopen(id)
{
	// cancel close timer
	mcancelclosetime();
	
	// close old layer
	if(ddmenuitem) {
		ddmenuitem.style.visibility = 'hidden';
	}
	
	// lowlight old menu header and reset variable
	if(mxheader) {
		mxheader.style.color = '#ffffff';
		mxheader = 0;
	}

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	if(ddmenuitem) {
		ddmenuitem.style.visibility = 'visible';
	}
	
	// highlight new menu header
	if(!mxheader) {
		mxheader = document.getElementById(id+"header");
		mxheader.style.color = '#339933';
	}
}

// close showed layer
function mclose() {
	if(ddmenuitem) {
		ddmenuitem.style.visibility = 'hidden';
	}
	// lowlight menu header and reset variable
	if(mxheader) {
		mxheader.style.color = '#ffffff';
		mxheader = 0;
	}
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;
