//
// IMAGE LOADING
//

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

//
// MISC
//

function homeRedirect(){
    window.location = "index.php";
}


//
// FLASH EXTERNAL INTERFACE
//

function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";	
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash(){
	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");
	if (document.getElementById) {
		if(opera == -1) return true;
		else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
	}
	return false;
}
 
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
function sendToPage(mc, var1, var2, var3, var4, var5) {
	getFlashMovie(mc).sendTextToFlash(var1, var2, var3, var4, var5);
}
function popMedia(url) {
	GB_show('Cloud9', url, 800, 800);
}



//
// AJAX
//

function sendRequest(file, request, func){
	
	var ajaxRequest;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Please enable Javascript support");
				return false;
			}
		}
	}
	
	ajaxRequest.onreadystatechange = function() {
		handleRequest(ajaxRequest.readyState, ajaxRequest.responseText, func);
	};
	
	ajaxRequest.open("POST", file, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	ajaxRequest.send(request); 
}

function handleRequest(ready, text, func) {
	
	if (ready == 0) {
		// not initialized
	} else if (ready == 1) {
		// connection established
	} else if (ready == 2) {
		// request received
	} else if (ready == 3) {
		// answer in process
	} else if (ready == 4) {
		// finalized
		
		// Array that will contain the arguments passed by the script
		var list = new Array();
		// Array that will contain the actual content for every argument
		var result = new Array();
		
		var varList = text.split("&");
		var k=0;
		for (var i in varList) {
			var charPos = varList[i].indexOf("=");
			var key = varList[i].substr(0, charPos);
			var res = varList[i].substr(charPos+1);
			
			if (key.indexOf("_list") > -1) key = key.split("_list")[0];
			// List of keys
			list[k] = key;
			// List of values
			result[k] = res;
			k++;
		}
		var content = new Array();
		for (var i=0; i < list.length; i++) {
			// If every variable has a list of values then split them.
			if (result[i].indexOf("^") > -1) {
				var newList = result[i].split("^");
				var values = new Array();
				for (var j = 0; j < newList.length-1; j++) {
					values[j] = newList[j];
				}
				content[list[i]] = values;
			} else {
				content[list[i]] = result[i];
			}
		}
		
		func(content);
	}
	
}

//
// COOKIES
//

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}		


// this function gets the cookie, if it exists
// don't use this, it's weak and does not handle some cases
// correctly, this is just to maintain legacy information
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}


// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	

//
// UTILITIES
//

function spellDay(newtime) {
	
	if (!newtime) {
		newtime = _time;
	}
	var year = Number(newtime.substr(0,4));
	var month = Number(newtime.substr(5,2));
	var day = Number(newtime.substr(8,2));
	var hr = Number(newtime.substr(11,2));
	var min0 = Number(newtime.substr(14,2));
	var sec = Number(newtime.substr(17,2));
	
	switch(month) {
		case 1:
			monthtext='January';
		break;
		case 2:
			monthtext='February';
		break;
		case 3:
			monthtext='March';
		break;
		case 4:
			monthtext='April';
		break;
		case 5:
			monthtext='May';
		break;
		case 6:
			monthtext='June';
		break;
		case 7:
			monthtext='July';
		break;
		case 8:
			monthtext='August';
		break;
		case 9:
			monthtext='September';
		break;
		case 10:
			monthtext='October';
		break;
		case 11:
			monthtext='November';
		break;
		case 12:
			monthtext='December';
		break;
	}
	
	switch(day) {
		case 1:
			daytext = day+'st';
		break;
		case 2:
			daytext = day+'nd';
		break;
		case 3:
			daytext = day+'rd';
		break;
		case 11:
			daytext = day+'st';
		break;
		case 12:
			daytext = day+'nd';
		break;
		case 13:
			daytext = day+'rd';
		break;
		case 21:
			daytext = day+'st';
		break;
		case 22:
			daytext = day+'nd';
		break;
		case 23:
			daytext = day+'rd';
		break;
		case 31:
			daytext = day+'st';
		break;
		default:
			daytext = day+'th';
		break;
	}
	
	if (hr > 11) {
		var ampm = 'pm';
		hr-=12;
	} else {
		var ampm = 'am';
	}
	return monthtext+' '+daytext+', '+year+' at '+hr+':'+min0+ampm;
}


function getMonthDays(month) {
	switch(month) {
		case 0: 	return 31; break;
		case 1:		return 31; break;
		case 2:		return 28; break;
		case 3:		return 31; break;
		case 4:		return 30; break;
		case 5:		return 31; break;
		case 6:		return 30; break;
		case 7:		return 31; break;
		case 8:		return 31; break;
		case 9:		return 30; break;
		case 10:	return 31; break;
		case 11:	return 30; break;
		case 12:	return 31; break;
	}
}

function calculateTime(oldtime) {
	
	var year1 = Number(oldtime.substr(0,4));
	var month1 = Number(oldtime.substr(5,2));
	var day1 = Number(oldtime.substr(8,2));
	var hr1 = Number(oldtime.substr(11,2));
	var min1 = Number(oldtime.substr(14,2));
	var sec1 = Number(oldtime.substr(17,2));
	
	var year2 = Number(_time.substr(0,4));
	var month2 = Number(_time.substr(5,2));
	var day2 = Number(_time.substr(8,2));
	var hr2 = Number(_time.substr(11,2));
	var min2 = Number(_time.substr(14,2));
	var sec2 = Number(_time.substr(17,2));
	
	var year = year2-year1;
	var month = month2-month1;
	var day = day2-day1;
	var hr = hr2-hr1;
	var min0 = min2-min1;
	var sec = sec2-sec1;
	
	if (month<0) {
		year--;
		month = 12+month;
	}
	
	if (day<0) {
		month--;
		if (month < 0) month = 12;
		day = getMonthDays(month)+day;
	}
	
	if (hr<0) {
		day--;
		if (day < 0) day = getMonthDays(month);
		hr = 24+hr;
	}
	
	if (min0<0) {
		hr--;
		if (hr < 0) hr = 23;
		min0 = 60+min0;
	}
	if (sec<0) {
		min0--;
		if (min0 < 0) min0 = 59;
		sec = 60+sec;
	}
	
	var newtime = new String();
	
	if (year > 0) {
		newtime = month+"/"+day+"/"+year;
	} else if (month > 0) {
		newtime = (month == 1) ? month + " month ago" : month + " months ago";
	} else if (day > 0) {
		newtime = (day == 1) ? "Yesterday" : day + " days ago";
	} else if (hr > 0) {
		newtime = (hr == 1) ? hr + " hour ago" : hr + " hours ago";
	} else if (min0 > 0) {
		newtime = (min0 == 1) ? min0 + " minute ago" : min0 + " minutes ago";
	} else if (sec > 0) {
		newtime = "Just now!";
	}
	
	return newtime;

}
