//
//
// ***** Macromedia functions *****
//
//
function MM_preloadImages() {
	//v3.0
	var d = document;
	if (d.images) {
		if (!d.MM_p) {
			d.MM_p = new Array();
		}
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image();
				d.MM_p[j++].src = a[i];
			}
		}
	}
}
function MM_swapImgRestore() {
	//v3.0
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
		x.src = x.oSrc;
	}
}
function MM_findObj(n, d) {
	//v4.01
	var p, i, x;
	if (!d) {
		d = document;
	}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {
		x = d.all[n];
	}
	for (i = 0; !x && i < d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {
		x = MM_findObj(n, d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x = d.getElementById(n);
	}
	return x;
}
function MM_swapImage() {
	//v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array();
	for (i = 0; i < (a.length - 2); i += 3) {
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc) {
				x.oSrc = x.src;
			}
			x.src = a[i + 2];
		}
	}
}
function MM_openBrWindow(theURL, winName, features) {
	//v2.0
	window.open(theURL, winName, features);
}
function MM_reloadPage(init) {
	//reloads the window if Nav4 resized
	if (init == true) {
		with (navigator) {
			if ((appName == "Netscape") && (parseInt(appVersion) == 4)) {
				document.MM_pgW = innerWidth;
				document.MM_pgH = innerHeight;
				onresize = MM_reloadPage;
			}
		}
	} else if (innerWidth != document.MM_pgW || innerHeight != document.MM_pgH) {
		location.reload();
	}
}
MM_reloadPage(true);
//
//
// ***** Date functions *****
//
//
function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

function check_date(myDate) {
	// Checks if date passed is in valid dd/mm/yyyy format
	if (myDate.length == 10) {
		if (myDate.substring(2, 3) == "/" && myDate.substring(5, 6) == "/") {
			var date = myDate.substring(0, 2);
			var month = myDate.substring(3, 5);
			var year = myDate.substring(6, 10);
			var test = new Date(year, month - 1, date);
			if (year == y2k(test.getYear()) && (month - 1 == test.getMonth()) && (date == test.getDate())) {
				// It's a valid date y'all
				return true;
			} else {
				// Right format, wrong date order
				return false;
			}
		} else {
			// Separators are wrong
			return false;
		}
	} else {
		// Wrong length
		return false;
	}
}
//
//
// ***** DIV functions *****
//
//
function toggleLayer(whichLayer){
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	} else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	} else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}
function hideLayer(whichLayer){
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"none";
	} else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"none";
	} else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"none";
	}
}
//
//
// ***** Full window function *****
//
//
var htmlpage
function fullWindow(htmlpage){
	// Execute code only if there's a valid URL
	if(htmlpage != "-1" && htmlpage != -1){
		// Open page in fullscreen popup window if the screen is 1024x768 or smaller
		// Otherwise open in the same window
		if (Number(screen.width) <= 1024) {
			//window.open(htmlpage,'lme','width='+(screen.width)+',height='+(screen.height)+',scrollbars=1,location=yes,resizable=0,top=0,left=0');
		} else {
			//location.href = htmlpage;	
		}
		// Sack that idea off
		window.open(htmlpage,'lme','width='+(screen.width)+',height='+(screen.height)+',scrollbars=1,location=yes,resizable=0,top=0,left=0');
	}
}
//
//
// ***** Function to call Skype and check that you've actually got Skype - won't work on OSX!!! *****
//
//
function callSkype(){
	self.location = "skype:+447709343202?call";
	return skypeCheck();
}
//
//
// ***** Javascript to Flash functions *****
//
//
function latestProject() {
	// Access a callback function inside the SWF movie with the ID 'base' - opens the latest project
    thisMovie("base").projectCallBack();
}
function thisMovie(movieName) {
	// Return a reference to the requested Flash movie
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
//
//
// ***** Enable/disable the Latest Project link in the HTML footer *****
//
//
function latestProjectLink(visibility){
	// Set a blank link or an active link depending on whether 'hide ' or 'show' has been asked for
	if (visibility == "hide") {
		var linkHTML = "<span class=\"footerblue\">Latest Project</span>";	
	} else if (visibility == "show") {
		var linkHTML = "<a href=\"javascript:action('latestProject');\">Latest Project</a>";
	} else if (visibility == "disable") {
		var linkHTML = "<span class=\"footergrey\">---------------</span>";	
	}
	if (document.getElementById){
		// this is the way the standards work
		document.getElementById("latestProject").innerHTML = linkHTML;
	} else if (document.all){
		// this is the way old msie versions work
		document.all["latestProject"].innerHTML = linkHTML;
	} else if (document.layers){
		// this is the way nn4 works
		document.layers["latestProject"].innerHTML = linkHTML;
	}
}
//
//
// ***** Actions for the main site footer
//
//
function action(section, id){
	if (section == "home") {
		// Reload page
		self.location = "/";
	} else if (section == "latestProject") {
		// Launch latest project
		latestProject();
	} else if (section == "phone") {
		// Ring my mobile from Skype
		callSkype();
	} else if (section == "email") {
		// Email me
		self.location = "mailto:gareth@4foot30.com";
	} else if (section == "msn") {
		// MSN chat
		self.location = "msnim:chat?contact=4foot30@live.co.uk";
	} else if (section == "iChat") {
		// iChat window
		self.location = "aim:GoIM?ScreenName=gareth4foot30";
	} else if (section == "skype") {
		// Skype chat
		self.location = "skype:gareth4foot30?call";
	} else if (section == "aim") {
		// AIM chat
		self.location = "aim:GoIM?ScreenName=gareth4foot30";
	} else if (section == "project") {
		// Open a specific project inside Flash
    	thisMovie("base").projectCallBack(id);	
	}
}
//
//
// ***** Software icon rollover functions *****
//
//
function highLightSoftware(id, action){
	if (action == 'rollout') {
		// Rollout was called so reset the classes back to their un-highlighted state
		var iconClass = 'softwareicon';
		var textClass = 'softwaretext';	
	} else {
		// Set rollover class names
		var iconClass = 'softwareiconrollover';
		var textClass = 'softwaretextrollover';
	}
	// Apply the required classes
	if (document.getElementById){
		// this is the way the standards work
		document.getElementById("software_icon_" + id).className = iconClass;
		document.getElementById("software_text_" + id).className = textClass;
	} else if (document.all){
		// this is the way old msie versions work
		document.all["software_icon_" + id].className = iconClass;
		document.all["software_text_" + id].className = textClass;
	} else if (document.layers){
		// this is the way nn4 works
		document.layers["software_icon_" + id].className = iconClass;
		document.layers["software_text_" + id].className = textClass;
	}	
}
//
//
// ***** Scriptaculous function to show the HTML page elements for the first time *****
//
//
function showPage(){
	// First fade in the intro div
	new Effect.Appear('intro', { duration: 1.0 });
	// Then fade in the footer div - make this happen after the intro by putting the effect in a queue
	// After that then hide the spacer div
	new Effect.Appear('footer', 
		{ duration: 1.0,
		  queue: 'end',
		  afterFinish: hideSpacer });
}
function hideSpacer(){
	// Call the standard hideLayer layer function - this is required because it appears that Scriptaculous's afterFinish method 
	// won't accept function calls which include parameters e.g. afterFinish: hideLayer('spacer') just calls hideLayer instantly
	// After addition of SWFObject 2.4 the Flash loaded before the spacer layer appeared, so Firefox had no scrollbar to start with,
	// so content jumped left after a second
	hideLayer('spacer');
	// Site should now be 'ready' so preload the technology images that you'll need
	MM_preloadImages("images/technology/as2.gif","images/technology/as2_nobody.gif","images/technology/as2_they.gif","images/technology/as2_we.gif");
	MM_preloadImages("images/technology/php.gif","images/technology/php_nobody.gif","images/technology/php_they.gif","images/technology/php_we.gif");
	MM_preloadImages("images/technology/design.gif","images/technology/design_nobody.gif","images/technology/design_they.gif","images/technology/design_we.gif");
	MM_preloadImages("images/technology/audio.gif","images/technology/audio_nobody.gif","images/technology/audio_they.gif","images/technology/audio_we.gif");
	MM_preloadImages("images/technology/css.gif","images/technology/css_nobody.gif","images/technology/css_they.gif","images/technology/css_we.gif");
	MM_preloadImages("images/technology/cms.gif","images/technology/cms_nobody.gif","images/technology/cms_they.gif","images/technology/cms_we.gif");
	MM_preloadImages("images/technology/as3.gif","images/technology/as3_nobody.gif","images/technology/as3_they.gif","images/technology/as3_we.gif");
	MM_preloadImages("images/technology/xml.gif","images/technology/xml_nobody.gif","images/technology/xml_they.gif","images/technology/xml_we.gif");
	MM_preloadImages("images/technology/mysql.gif","images/technology/mysql_nobody.gif","images/technology/mysql_they.gif","images/technology/mysql_we.gif");
	MM_preloadImages("images/technology/video.gif","images/technology/video_nobody.gif","images/technology/video_they.gif","images/technology/video_we.gif");
	MM_preloadImages("images/technology/asp.gif","images/technology/asp_nobody.gif","images/technology/asp_they.gif","images/technology/asp_we.gif");
	MM_preloadImages("images/technology/html.gif","images/technology/html_nobody.gif","images/technology/html_they.gif","images/technology/html_we.gif");
	MM_preloadImages("images/technology/ajax.gif","images/technology/ajax_nobody.gif","images/technology/ajax_they.gif","images/technology/ajax_we.gif");
	MM_preloadImages("images/technology/javascript.gif","images/technology/javascript_nobody.gif","images/technology/javascript_they.gif","images/technology/javascript_we.gif");
	MM_preloadImages("images/technology/air.gif","images/technology/air_nobody.gif","images/technology/air_they.gif","images/technology/air_we.gif");
}
//
//
// ***** Scriptaculous function to highlight required software icons *****
//
//
function fadeSoftware(direction, id){
	if (direction == "down") {
		// Fade out all the icons
		new Effect.Opacity('software_icon_flash', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_dreamweaver', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_photoshop', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_fireworks', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_air', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_audition', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_zinc', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
		new Effect.Opacity('software_icon_wordpress', 
			{ duration:0.5, 
			  from:1.0, 
			  to:0.3 });
	} else if (direction == "icon") {
		// Set the required icon back  to full opacity
		var div = "software_icon_" + id;
		var current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
	} else if (direction == "up") {
		// Fade all icons back up to 100%
		div = 'software_icon_flash';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_dreamweaver';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_photoshop';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_fireworks';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_air';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_audition';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_zinc';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
		div = 'software_icon_wordpress';
		current_opacity = Element.getOpacity(div);
		new Effect.Opacity(div, 
			{ duration:1.0, 
			  from:current_opacity, 
			  to:1.0 });
	}
}
//
//
// ***** Award icon rollover functions *****
//
//
function highLightAward(id, action){
	if (action == 'rollout') {
		// Rollout was called so reset the classes back to their un-highlighted state
		var iconClass = 'award';
	} else {
		// Set rollover class names
		var iconClass = 'awardrollover';
	}
	// Apply the required classes
	if (document.getElementById){
		// this is the way the standards work
		document.getElementById("award_" + id).className = iconClass;
	} else if (document.all){
		// this is the way old msie versions work
		document.all["award_" + id].className = iconClass;
	} else if (document.layers){
		// this is the way nn4 works
		document.layers["award_" + id].className = iconClass;
	}	
}
//
//
// ***** Load a project's details by replacing the standard homepage text *****
//
//
function projectDetails(id) {
	// Put the ID into a global variable for use in other functions
	project_id = id;
	// Create xmlHttp object for the relevant browser
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				// Older versions of IE
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return false;
			}
		}
	}
	// Function to execute when the AJAX response is received
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			// Data has finished loading
			if (xmlHttp.status == 200) {
				// Data has successfully loaded so put the response into a global variable
				project_data = xmlHttp.responseText;
				// Fade out the homepage text
				new Effect.Opacity('introtext', 
					{ duration:0.5, 
			 		 from:Element.getOpacity('introtext'), 
			 		 to:0.0,
			 		 queue: 'end',
			 		 afterFinish: showDetails });
			}
		}
	}
	// If the ID is greater than 0 then you're looking at a project, so add a random number onto the end of the querystring to prevent caching
	// If it's 0 or less then you're reloading the homepage text, so it doesn't matter if it caches
	if (id > 0) {
		xmlHttp.open("GET", "../php/project_details.php?project_id=" + id + '&time=' + Math.floor(Math.random() * 100000), true);
	} else {
		xmlHttp.open("GET", "../php/project_details.php?project_id=" + id, true);
	}
	xmlHttp.send(null);
}
function showDetails() {
	// Show the project data in the DIV
	document.getElementById("introtext").innerHTML = project_data;
	// Fade in the DIV
	new Effect.Appear('introtext', { duration: 0.5 });
}
//
//
// ***** Software icon rollover functions *****
//
//
function highLightKey(id, action){
	// Slice the underscore off the front of the ID - might be empty, if so change it into 'me'
	if (id == "") {
		id = "me";	
	} else {
		id = id.slice(1, id.length);
	}
	if (action == 'rollout') {
		// Rollout was called so reset the classes back to their un-highlighted state
		var keyClass = 'key' + id;
	} else {
		// Set rollover class names
		var keyClass = 'key' + id + 'over';
	}
	// Apply the required classes
	if (document.getElementById){
		// this is the way the standards work
		document.getElementById("key_" + id).className = keyClass;
	} else if (document.all){
		// this is the way old msie versions work
		document.all["key_" + id].className = keyClass;
	} else if (document.layers){
		// this is the way nn4 works
		document.layers["key_" + id].className = keyClass;
	}	
}
