// JavaScript for all ALyouth pages


//-----------------------------------------------
// Standard Functions

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_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_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];}
}

//-----------------------------------------------
// AL Global Functions

function AL_preload() {
}

//-----------------------------------------------
// Function to bring focus to a form text entry - only brings focus if no input has been received yet
//eg. AL_formfocus('name','email')

function AL_formfocus() {
  var isfocused = false;
  var fieldnames = AL_formfocus.arguments;
  //var fieldnames = ['name','email'];
  for(var i=0;i<fieldnames.length;i++){
    var elm = document.getElementById(fieldnames[i]);
    if(elm !== undefined){
      if(elm.value != elm.defaultValue){
        isfocused = true;
      }
    }
  }
  if(isfocused != true){
    document.getElementById(fieldnames[0]).focus();
	document.getElementById(fieldnames[0]).select();
  }
}

//------------------------------------------------
// FeatureBox script v1.0
// (c) Erik Gorton 2007


function featurebox_setup(path, names, links) {
	if(names.length > 4 || links.length > 4){alert("JAVA ERROR: You must not have more than 4 slides in the Feature Box - check the HTML");}
	featurebox_delay = 8;
	featurebox_path = path;
	featurebox_names = names;
	featurebox_links = links;
	featurebox_frame = 0;
	featurebox_linknumber = 0;
	featurebox_images = [];
	featurebox_redraw = "TRUE";
	featurebox_timer_id = null;
	featurebox_nav_setup();
	featurebox();
}


function featurebox() {
	if (featurebox_images[featurebox_frame]) {
		if (featurebox_redraw == "FALSE") {
			document["featureboximage"].width = 228;
			featurebox_redraw = "TRUE";
			featurebox_timer_id = setTimeout("featurebox()",100);
		}
		else {
			document["featureboximage"].src = featurebox_images[featurebox_frame].src;
			featurebox_linknumber = featurebox_frame;
			//alert("exisiting image: "+featurebox_frame);
			document["featureboximage"].width = 457;
			if(featurebox_frame++ >= (featurebox_links.length-1)) {featurebox_frame = 0;}
			featurebox_redraw = "FALSE";
			featurebox_nav_setup()
			featurebox_timer_id = setTimeout("featurebox()",featurebox_delay * 1000);
		}
	}
	else {
		for (i=0;i<featurebox_names.length;i++) {
			featurebox_images[i] = new Image;
			featurebox_images[i].src = featurebox_path+'main_frame_'+(i+1)+'.jpg';
		}
		featurebox_timer_id = setTimeout("featurebox()",1);
	}
}

function featurebox_link() {
	location.href = featurebox_links[featurebox_linknumber];
}

function featurebox_nav_setup() {
	var thecell, thepercent, theclass;
	thepercent = featurebox_names.length *25;
	featurebox_nav_text = '<table width="'+thepercent+'%" height="30" border="0" align="center" cellpadding="0"cellspacing="0"><tr>'
	for(i=0;i<featurebox_names.length;i++) {
		theclass='class="MAINfeatureboxNAVnormal"';
		if (i==featurebox_linknumber) {theclass='class="MAINfeatureNAVhilight"';}
		thecell = '<td '+theclass+' id="MAINnav'+(i+1)+'" width="25%" align="center" valign="middle">';
		featurebox_nav_text = featurebox_nav_text + thecell + '<a href="#" onmouseup="featurebox_nav_click('+i+')">' +featurebox_names[i] + '</a></td>';
	}
	featurebox_nav_text = featurebox_nav_text + '</tr></table>';
	//alert('"'+featurebox_nav_text+'"');
	document.getElementById("MAINfeatureboxNAV").innerHTML = featurebox_nav_text;
}

function featurebox_nav_click(thelink) {
	clearTimeout(featurebox_timer_id);
	featurebox_linknumber = thelink;
	featurebox_frame = thelink;
	featurebox_redraw = "FALSE";
	featurebox_nav_setup();
	featurebox();
}
//------------------------------------------------

// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com
// Created by: Mark O'Sullivan :: http://lussumo.com/
// Jeff Larson :: http://www.jeffothy.com/
// Mark Percival :: http://webchicanery.com/
// Modified Erik Gorton 2008
 
function clipboardcopy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="../_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
  alert("The code has been copied to the clipboard - paste it into your profile or website");
}

//----------------------------------------------------
// Media Lab PopUp Script v1.0
// (c) Erik Gorton 2008

function medialab_popup_setup() {
	document.write("<div id='medialabPREVIEWblackout' style='visibility:hidden;top:-2000px;left:-2000px;'></div><div id='medialabPREVIEW' style='visibility:hidden;top:-2000px;left:-2000px;'></div>");
}

function medialab_popup(theType, theName, thePreviewimage, theFilename, theLink) {
	var theHTML
	
	if (theType == "WEB BANNER") {
	medialab_popup_theCode = '<br><a href= "'+ theLink +'"><img src= "'+ theFilename +'" border=0 alt= "'+ theName +'" title= "'+ theName +'"></a><br>';
	theHTML ='\
<table width="500" height="300" border="0" cellpadding="0" cellspacing="0">\
<tr><td align="center" valign="middle" bgcolor="#CCCCCC"><img src="'+ thePreviewimage +'" border="0" /></td>\
</tr>\
</table>\
<table width="100%" border="0" cellspacing="0" cellpadding="0">\
<tr>\
<td colspan="2" valign="top"><img src="../img/spacer.gif" alt="" width="1" height="10" /></td>\
</tr>\
<tr>\
<td align="left" valign="middle">'+ theType +' &nbsp; <span class="medialabPREVIEWname">'+ theName +'</span></td>\
<td align="right" valign="middle" class="medialabPREVIEWhelp">Click \'Copy Code\' then paste it into your profile</td>\
</tr>\
</table>\
<table width="100%" border="0" cellspacing="0" cellpadding="0">\
<tr>\
<td colspan="3" valign="top"><img src="../img/spacer.gif" alt="" width="1" height="7" /></td>\
</tr>\
<tr>\
<td width="120" align="left" valign="middle" class="medialabPREVIEWdownload">\
<a href="javascript:clipboardcopy(medialab_popup_theCode);medialab_popup_close();">COPY CODE</a></td>\
<td align="center" valign="middle">\
<textarea class="medialabPREVIEWcode" readonly="readonly" rows="2">'+ medialab_popup_theCode +'</textarea></td>\
<td width="70" align="right" valign="middle" class="medialabPREVIEWclose"><a href="javascript:medialab_popup_close();">CLOSE X </a></td>\
</tr>\
</table>\
	';
}
else {
	theHTML ='\
<table width="500" height="300" border="0" cellpadding="0" cellspacing="0">\
<tr><td align="center" valign="middle" bgcolor="#CCCCCC"><img src="'+ thePreviewimage +'" border="0" /></td>\
</tr>\
</table>\
<table width="100%" border="0" cellspacing="0" cellpadding="0">\
<tr>\
<td colspan="3" valign="top"><img src="../img/spacer.gif" alt="" width="1" height="10" /></td>\
</tr>\
<tr>\
<td width="120" align="left" valign="middle" class="medialabPREVIEWdownload"><a href= "javascript:window.location=\''+ theFilename +'\';medialab_popup_close();">DOWNLOAD</a></td>\
<td align="center" valign="middle">'+ theType +' &nbsp; <span class="medialabPREVIEWname">'+ theName +'</span></td>\
<td width="120" align="right" valign="middle" class="medialabPREVIEWclose"><a href="javascript:medialab_popup_close();">CLOSE X </a></td>\
</tr>\
</table>\
	';
}
	document.getElementById('medialabPREVIEW').innerHTML = theHTML;
	centerDiv('medialabPREVIEW');
	document.getElementById('medialabPREVIEWblackout').style.top = '0px';
	document.getElementById('medialabPREVIEWblackout').style.left = '0px';
	document.getElementById('medialabPREVIEWblackout').style.visibility = 'visible';	
}

function medialab_popup_close() {
	document.getElementById('medialabPREVIEWblackout').style.visibility = 'hidden';
	document.getElementById('medialabPREVIEWblackout').style.top = '-2000px';
	document.getElementById('medialabPREVIEWblackout').style.left = '-2000px';
	document.getElementById('medialabPREVIEW').style.visibility = 'hidden';
	document.getElementById('medialabPREVIEW').style.top = '-2000px';
	document.getElementById('medialabPREVIEW').style.left = '-2000px';
}

//function medialab_popup_download(theFilename) {
//	document.write('Wait until the image has downloaded then right click on the image and select SAVE IMAGE AS...<br><br><img src="'+ theFilename +'" width="50%" height="50%" border="2"/>');
//}

function centerDiv(divobj){ //Centers a div element on the page and makes it visible
	divobj=document.getElementById(divobj);
	var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
	var ie=document.all && !window.opera;
	var dom=document.getElementById;
	var scroll_top=(ie)? standardbody.scrollTop : window.pageYOffset;
	var scroll_left=(ie)? standardbody.scrollLeft : window.pageXOffset;
	var docwidth=(ie)? standardbody.clientWidth : window.innerWidth;
	var docheight=(ie)? standardbody.clientHeight: window.innerHeight;
	var docheightcomplete=(standardbody.offsetHeight>standardbody.scrollHeight)? standardbody.offsetHeight : standardbody.scrollHeight; //Full scroll height of document
	var objwidth=divobj.offsetWidth; //width of div element
	var objheight=divobj.offsetHeight; //height of div element
	var topposition=(docheight>objheight)? (scroll_top+docheight/2-objheight/2)-20+"px" : scroll_top+10+"px"; //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
	divobj.style.left=docwidth/2-objwidth/2+"px"; //Center div element horizontally
	divobj.style.top=Math.floor(parseInt(topposition))+"px";
	divobj.style.visibility="visible";
}