/*
	Author: Mark Davies
	Desc: The functions control basic operations used within the site
*/

//Store the timer used for fading
var fadeTimer;

//variables used to handle the fading
var startFade = 0;
var endFade = 100;
var fadeState = 0;

//hold the names of the popup box and close tab
var closeTabId = "";
var popUpBoxId = "";

//hold a reference to the popup box and close tab element
var closeTabElement;
var popUpBoxElement;

/*FUNCTIONS*/

//Handle the update of the clock
function updateClock ()
{
	var currentTime = new Date ();

	var currentDay = currentTime.getDay();
	var currentDate = currentTime.getDate();
	var currentMonth = currentTime.getMonth();
	var currentYear = currentTime.getFullYear();

	var currentHours = currentTime.getHours ( );
	var currentMinutes = currentTime.getMinutes ( );
	var currentSeconds = currentTime.getSeconds ( );

	switch(currentDay)
	{
		case 0:
			currentDay = "Sunday";
			break;
		case 1:
			currentDay = "Monday";
			break;
		case 2:
			currentDay = "Tuesday";
			break;
		case 3:
			currentDay = "Wednesday";
			break;
		case 4:
			currentDay = "Thursday";
			break;
		case 5:
			currentDay = "Friday";
			break;
		case 6:
			currentDay = "Saturday";
			break;
	}

	// Pad the minutes and seconds with leading zeros, if required
	currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
	currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

	// Choose either "AM" or "PM" as appropriate
	var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

	// Convert the hours component to 12-hour format if needed
	currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

	// Convert an hours component of "0" to "12"
	currentHours = ( currentHours == 0 ) ? 12 : currentHours;

	// Compose the string for display
	var currentTimeString = currentDay + " " + currentDate + "/" + currentMonth + "/"+ currentYear +" " + currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

	// Update the time display
	document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

//handle the centre position of the popup box
function blanket_size() 
{
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	
	popUpDiv_height=blanket_height/2-186;//150 is half popup's height
	popUpBoxElement.style.top = popUpDiv_height + 'px';
}

function window_pos() 
{
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	
	window_width=window_width/2-370;//379;//150 is half popup's width
	popUpBoxElement.style.left = window_width + 'px';
}

//Handle if the popup box should be showing or not
function toggle(div_id) 
{
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) 
	{	
		el.style.display = 'block';
	}
	else 
	{
		el.style.display = 'none';
	}
}

//Handle the fading of the popup box and close tab 
function FadeIn()
{
	if(fadeState < endFade)
	{
		fadeState = fadeState + 10;
	
		closeTabElement.style.opacity = (fadeState/100);
		closeTabElement.style.filter = 'alpha(opacity=' + fadeState + ')';
		
		popUpBoxElement.style.opacity = (fadeState/100);
		popUpBoxElement.style.filter = 'alpha(opacity=' + fadeState + ')';
	}
}

//Handle the close tabs position
function CloseTabSetup(closeTabColour)
{
	toggle(closeTabId);

	var closeTabDiv = document.getElementById("popupContent");
		
	//get top pos of popup box
		
	//get left point
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	
	window_width=window_width/2-379;//379;//150 is half popup's width
	
	
	//close box top pos
	closeTabElement.style.top = popUpBoxElement.style.top;
	closeTabElement.style.left = (window_width + 758) + 'px';
	
	//colour the close tab and popup box
	closeTabElement.style.backgroundColor = closeTabColour;	
	closeTabDiv.style.backgroundColor = closeTabColour;	
	popUpBoxElement.style.backgroundColor = closeTabColour;	
}

//Handle the popup of the popup box
function popup(windowname, closeTabName, popUpColour) 
{
	//store the names of the elements
	closeTabId = closeTabName;
	popUpBoxId = windowname;

	//store the elements
	closeTabElement = document.getElementById(closeTabId);
	popUpBoxElement = document.getElementById(popUpBoxId);
	
	//clear the fade timer
	fadeTimer = window.clearInterval(fadeTimer);
		
	//clear previous content
	document.getElementById("popupContent").innerHTML ="";
	
	blanket_size();
	window_pos();
	toggle('blanket');
	toggle(popUpBoxId);
	
	//set the opacity of the close tab and popup box
	closeTabElement.style.opacity = 0;
	closeTabElement.style.filter = 'alpha(opacity=' + 0 + ')';
	popUpBoxElement.style.opacity = 0;
	popUpBoxElement.style.filter = 'alpha(opacity=' + 0 + ')';
	
	//set the initial fadeState to the startFade
	fadeState = startFade;
	
	//set the close tab to be visible 
	CloseTabSetup(popUpColour);
	
	//Add the fade timer so the popup box and close tab fade in
	fadeTimer = self.setInterval("FadeIn()",50);
	
	document.onkeydown = function(e)
		{
			if (e == null) 
			{ 
				// ie
				keycode = event.keyCode;
			}
			else 
			{
				// mozilla
				keycode = e.which;
			}
			
			if(keycode == 27)
			{
				// escape, close box, esc
				//what you nedd
				Close();
			}
		};
}

//Handle the closing of the popup box
function Close()
{	
	toggle('blanket');
	toggle(popUpBoxId);
	toggle(closeTabId);
	
	//set the opacity of the close tab and popup box
	closeTabElement.style.opacity = 0;
	closeTabElement.style.filter = 'alpha(opacity=' + 0 + ')';
	popUpBoxElement.style.opacity = 0;
	popUpBoxElement.style.filter = 'alpha(opacity=' + 0 + ')';
	
	//remove listener
	document.onkeydown = null;
}

function ShrinkPortImg()
{
	var browserName=navigator.appName; 
	
	if (browserName != "Microsoft Internet Explorer")
	{
		document.getElementById("PortText").style.width = "284px";
		document.getElementById("PortText").style.height = "90px";
	}	
}

function EnlargePortImg()
{
	var browserName=navigator.appName; 
	
	if (browserName != "Microsoft Internet Explorer")
	{
		document.getElementById("PortText").style.width = "294px";
		document.getElementById("PortText").style.height = "100px";
	}
	
}

function ShiftIco(tile)
{
	var browserName=navigator.appName; 
	
	if (browserName != "Microsoft Internet Explorer")
	{
		if(tile == "twitter")
		{
			document.getElementById("twitterIcon").style.marginLeft = "-0.4em";
			document.getElementById("twitterIcon").style.marginTop = "-0.2em";
		}
		else if(tile == "linkedin")
		{
			document.getElementById("linkedInIcon").style.marginRight = "-0.2em";
			document.getElementById("linkedInIcon").style.marginTop = "-0.3em";
		}
	}
}

function ReposeIcon(tile)
{
	var browserName=navigator.appName; 
	
	if (browserName != "Microsoft Internet Explorer")
	{	 
		if(tile == "twitter")
		{
			document.getElementById("twitterIcon").style.marginLeft = "0.1em";
			document.getElementById("twitterIcon").style.marginTop= "0em";			
		}
		else if(tile == "linkedin")
		{
			document.getElementById("linkedInIcon").style.marginRight = "0em";
			document.getElementById("linkedInIcon").style.marginTop= "0em";
		}
	}
}
