// JavaScript Document
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position
var insidetarget = false;



 var profiles = (function() {
						
				api=[]; // make object to attach methods to
				profileObjs = []; // re-set
				var profilesloaded = false;
				api.load = function() {
				var ajaxRequest = new XMLHttpRequest();
				var url =  "php/get_profiles.php";
				var bustcacheparameter = "?"+new Date().getTime();
				ajaxRequest.open("GET",url+bustcacheparameter, true);
				ajaxRequest.onreadystatechange = function(){
					if(ajaxRequest.readyState == 4){
						if (ajaxRequest.status == 200){
							var returned_data = ajaxRequest.responseText;
							var this_data = returned_data.split('%%');// split each presenter data set and put into array
							 for(var i = 0; i < this_data.length-1; i++) {
							 var this_line = this_data[i].split('||'); // split data set into fields
							 var id = this_line[0];
							 var imageURL = this_line[1];
							 var name = this_line[2];
							 var info = this_line[3];
							 var mail = this_line[4];
							 profileObjs.push(new makeProfileObj(id, imageURL, name, info, mail));// make objects and put into array
						}
						profilesloaded = true;
						}else{
						alert("Error: "+ajaxRequest.status);
						
						}
					 }
				   }
				   	ajaxRequest.send(null); 
				 }
				 
				 
				function makeProfileObj(id, imageURL, name, info, mail){
				this.imageobj = new Image(70,93); // create the image object
				this.imageobj.src = "epg/"+imageURL;
				this.imageobj.itsid = id;
				this.imageobj.presenter = name;
				this.imageobj.info = info;
				this.imageobj.mail = mail;
				this.imageobj.style.cssText = "margin: 3px; border: 2px solid #fff; cursor: pointer;";
				this.imageobj.onmouseover = function(){popup(name); activate_popup();};
				this.imageobj.onmouseout = function(){supress_popup()};
				this.imageobj.onclick = function(){showProfile(this);};
				}	 
						 
			    api.display = function (){
				if(!profilesloaded) window.location.hash = "home.html"; // data not loaded so reload home page
				var profilesDiv =  document.getElementById("thumbnailsInner");
				for(var i = 0; i < profileObjs.length-1; i++) {
				profilesDiv.appendChild(profileObjs[i].imageobj);	
				}
				 
			   }
				 
				function showProfile(obj){
				document.getElementById("content_wrapper").style.position = "static"; // important to change from 'relative' for proper centring
				var hiddenDiv = document.getElementById("hiddenDiv");
				var centrewidth = window.center({width:605,height:530});
				hiddenDiv.style.left = centrewidth + "px";
				var mugDiv=document.getElementById("mugshotDiv");
				var closeDiv=document.getElementById("closeDiv");
				deleteprior(mugDiv);
				var mug = document.createElement("img");
				mug.src = obj.src;
				mugDiv.appendChild(mug);
				var headingDiv = document.getElementById('profile_headingDiv');
				headingDiv.style.cssText = "position: relative; top: 0px; left: 20px; margin-top: 23px; margin-bottom: 6px;";
				var headingHTML ='<span id="heading">Presenter Profiles: </span><span id="presenter">'+obj.presenter+'</span>';
				headingDiv.innerHTML = headingHTML;
				var profileDiv = document.getElementById('profileDiv');
				profileDiv.innerHTML = '<p>'+obj.info+'</p>';
				var emailDiv = document.getElementById('emailDiv');
				emailDiv.innerHTML = '<a href="mailto:'+obj.mail+'">'+obj.mail+'<a/>';
				var mybutton = document.createElement("input");// create button
				mybutton.setAttribute("type","submit");
				mybutton.value="Close";
				$(mybutton).bind('click', function() {
				$("#hiddenDiv").slideUp(200);
				});
				deleteprior(closeDiv);
				closeDiv.appendChild(mybutton);
				$("#hiddenDiv").slideDown(200);
				}


				
				return api;
						
				}());
 
 
 
 // this is used by both Profiles page and popups over montage at top of site
function popup(theName){
if ((theName == "Ronan O\'Rahilly") || (theName == "Ross Revenge")) insidetarget = true;

var popup=document.getElementById("popupDiv");
if(popup.firstChild)
    {
    popup.removeChild(popup.firstChild);
    }
mytextnode = document.createTextNode(theName);
popup.appendChild(mytextnode);
}

function supress_popup(){
insidetarget = false;
}

function activate_popup(){
insidetarget = true;
}



// Follow the mousey
	
function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}
function follow(evt) {
if (insidetarget) {
var obj = document.getElementById("popupDiv").style; 
obj.visibility = 'visible';
obj.left = (parseInt(mouseX(evt))+offX) + 'px';
obj.top = (parseInt(mouseY(evt))+offY) + 'px';
}
else if(!insidetarget){
var obj = document.getElementById("popupDiv").style; 
obj.visibility = 'hidden';
}
}


window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return _x;
}