//<![CDATA[
var marquee = new mqObj();
var timer = new Array();

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function mqObj() {
	var r, 	// a randomized
		sz, // the size of g-s
		gg,	// r split into groups
		g, 	// a single group shifted of off gg
		uid = 'empId', // TODO: unique identifier for employee, i.e. empId
		f,
		fid;	// featured employee id

	this.setRandom = function(a) {
		for(var j, x, i = a.length; i; j = parseInt(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
		r = a;
	}
	this.getRandom = function() {
		if (r.length > 0) {
			return r;
		} else {
			alert ("random array not set.");
		}
	}

	this.setGroupSize = function(num) {
		sz = Math.floor(num);
	}
	this.getGroupSize = function() {
		return sz;
	}
	
	this.setGroups = function() {
		
		// no. of sets that can be made from array given in arg
		var cnt = Math.floor(r.length/sz);
		// alert (cnt);

		gg = new Array(cnt);

		// members left over from given array after making into sets
		var modulus = r.length%sz;

		for(var i=0; i < cnt; i++) {
			gg[i] = new Array(sz);

			for(var j=0; j < sz; j++) {
				gg[i][j] = r.shift();
				// alert("i = " + i + "\n" + "j = " + j + "\n" + sets[i][j].empName.toString() + "(" + sets[i][j].empId.toString() + ")");
			}
		}

		if (modulus) {
			var mod = gg.length;
			gg[mod] = new Array(sz);

			for(var j=0; j < modulus; j++) {
				gg[mod][j] = r.shift();
				// alert("mod = " + mod + "\n" + "j = " + j + "\n" + sets[mod][j].empName.toString() + "(" + sets[mod][j].empId.toString() + ")");
			}

			for(var j=modulus; j < sz; j++) {
				var g = Math.floor(Math.random() * cnt)
				gg[mod][j] = gg[g][j];
				// alert("set = " + set + "\n" + "j = " + j + "\n" + sets[mod][j].empName.toString() + "(" + sets[mod][j].empId.toString() + ")");
			}

			// alert ("modulus: " + modulus);
		}		
	}
	this.getGroups = function() {
		if (gg.length > 0) {
			return gg;	
		}
		else {
			return false;
		}
	}

	this.setGroup = function() {
		if (gg.length > 0) {
			g = gg.shift();
			f = g.shift();
		}
		else {
			// alert ("setGroup failed");
			return false;
		}
	}
	this.getGroup = function() {
		
		if(g.length == (sz-1)) {
			return g;	
		}
		else {
			// alert ("getGroup failed");
			return false;
		}
	
	}

	this.setUID = function(identifier) {
		var field = identifier.toString();
		
		for (var i in r[0]) {
			if (i == field) {
				uid = field;
			}
		}
	}

	// occur after setRandom but before setGroups
	this.setFeatured = function(fid) {
		var len = r.length;
		var fe; // limited scope to this function
		
		for(var i=0; i<len; i++) {
			if(r[i].empId == fid) {
				
				// alert(r.length + " :r before splice, removing 1 element at index: " + i );
				fe = r.splice(i,1);
				
				//alert(r.length + " after splice");
				//alert(fe[0].empId + " :fe after splice");
				
				r.unshift(fe[0]);
				//alert(r[0].empId + " :first r - empId after unshift");
				//alert(r.length + " len after unshift");
			}
		}
		fid = null;
	}
	this.getFeatured = function() {
		if (f) {
			return f;	
		}
		else {
			alert ("cannot get featured - not an array or empty.");
		}
	}
	
	this.setFID = function(featured_id) {
		fid = featured_id;
	}
	this.getFID = function() {
		if (fid) {
			return fid;
		}
		else {
			return false;
		}
	}
}
function init() {	
	// source array
	var empArray = employees.slice();
	
	// randomize elements
	marquee.setRandom(empArray);
	
	// check if this is a specific featured call
	var fid = marquee.getFID();
	if(fid) {
		marquee.setFeatured(fid);
		marquee.setFID(null);
	}
	
	// set up params of groups
	marquee.setGroupSize(groupSize);
	marquee.setGroups();
	marquee.setGroup();
	setMarquee();
}
function reset() {
	
	var mq = document.getElementById("Marquee");
	var thumbs = mq.getElementsByTagName("a");

	for(var i=thumbs.length-1; i>=0; i--) {
		mq.removeChild(thumbs[i]);
	}

	var hq = document.getElementById("HeaderQuotation");

	while (hq.hasChildNodes()) {
		hq.removeChild(hq.childNodes[0]);
	}
	
	var fid = marquee.getFID();
	var group = marquee.setGroup();
	
	if (group) {
		setMarquee();
	}
	else { init(); }
}

function setFeatured(eid) {
	marquee.setFID(eid);

	Effect.Fade('Marquee', {duration:1.0});

	var timerLen = timer.length;
	while(timerLen > 0) {
		clearTimeout(timer[timerLen]);
		timerLen--;
	}
	timer[timer.length] = setTimeout("reset()", 1001);
}
function setMarquee() {
	
	marquee.grp = marquee.getGroup();
	var f = marquee.getFeatured();
		
	var mq = document.getElementById("Marquee");
	mq.className = "transparent";
	mq.style.backgroundImage = 'url(' + fullsizePath + f.imgname + ')';

	var hq = document.getElementById("HeaderQuotation");
	var h3txt, h4txt, ptxt, h3, h4, p;
	// create text nodes and append
	h3txt = document.createTextNode(f.empName);
	h3 = document.createElement("h3");
	h3.appendChild(h3txt);

	h4txt = document.createTextNode(f.empTitle);
	h4 = document.createElement("h4");
	h4.appendChild(h4txt);

	ptxt = document.createTextNode(f.empRegion);
	p = document.createElement("p");
	p.appendChild(ptxt);

	hq.appendChild(h3);
	hq.appendChild(h4);
	hq.appendChild(p);

	Effect.Appear("Marquee", {duration:1.0});

	var timerLen = timer.length;
	while(timerLen > 0) {
		clearTimeout(timer[timerLen]);
		timerLen--;
	}
	timer[timer.length] = setTimeout('setThumbnails()', 5000);
}
function setThumbnails() {
	var mq = document.getElementById("Marquee");
	
	var thumbData = marquee.grp.shift();
	var thumbId = "tmb_" + thumbData.empId;
	var thumbImage = thumbnailPath + thumbData.imgname;
	var thumbElement = document.createElement('a');
		thumbElement.className = 'transparent';
		thumbElement.style.backgroundImage = 'url(' + thumbImage + ')';
		thumbElement.setAttribute("href", "#"+thumbId);	
		thumbElement.setAttribute("name", thumbId);
		thumbElement.setAttribute("title", thumbData.empName);
		thumbElement.setAttribute("id", thumbId);
		thumbElement.onclick = function() {
			var timerLen = timer.length;
			while(timerLen > 0) {
				clearTimeout(timer[timerLen]);
				timerLen--;
			}
			setFeatured(thumbData.empId);
			return false;
		}

	mq.appendChild(thumbElement);
	thumbElement.className = 'marqueeTile opaque';
	
	var timerLen = timer.length;
	while(timerLen > 0) {
		clearTimeout(timer[timerLen]);
		timerLen--;
	}
		
	if(marquee.grp.length > 0) {
		timer[timer.length] = setTimeout("setThumbnails()", 500);
	}
	else {
		timer[timer.length] = setTimeout("Effect.Fade(" + "'Marquee'" + ", {duration:1.0})", 3000);
		timer[timer.length] = setTimeout("reset()", 4001);
		
	}
}

// start marquee when page loads
addLoadEvent(init);
//]]>