/****************************************************************/
/***  SUPPORTING FUNCTIONS									  ***/
/****************************************************************/


// ---------------------------------------------------------------------
// string.trim (our own language addition)
// ---------------------------------------------------------------------
if (String.prototype.trim == null) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/, '');
	};
}

//TODO activate that after mail print and font size done!!
/* Display Page Functions */
function PageInit()
{
    //document.getElementById('mail_print').className = "show";
    //document.getElementById('font_size_button').className = "show";
}

function AddClass (theNode, theClass) {
	if (theNode.className == '') {
		theNode.className = theClass;
	} else {
		theNode.className += ' ' + theClass;
	}
}

function RemoveClass (theNode, theClass) {
	var oldClass = theNode.className;
	var regExp = new RegExp('\\s?\\b'+theClass+'\\b');
	if (oldClass.match(regExp) != null) {
		theNode.className = oldClass.replace(regExp,'');
	}
}

// Show/Hide a DIV using the classes ".show" & ".hide"
function ShowHide(sDIVID)
{
	var sClass = "";
	
	switch (document.getElementById(sDIVID).className)
	{
		case "hide":
			sClass = "show";
			break;
		default:
			sClass = "hide";
			break;
	}
	document.getElementById(sDIVID).className = sClass;
}



/****************************************************************/
/***  LIBRARY FUNCTIONS										  ***/
/****************************************************************/

/*///////////////////////////////////////////////////////////////////
Function to open a movie in a popup
Param:	movieName - Relative url to the movie
///////////////////////////////////////////////////////////////////*/
function fnOpenMovie (movieUrl) {
		var win=window.open('','mywindow','height=256, width=320');
		win.document.write('\x3Chtml\x3E\x3Chead\x3E\x3Ctitle\x3EVideo Window\x3C/title\x3E\x3Cstyle\x3Ehtml,body {margin:0;padding:0;}\x3C/style\x3E\x3C/head\x3E\x3Cbody\x3E\x3Cembed src=\''+movieUrl+'\' width=\'320\' height=\'256\' autoplay=\'true\' controller=\'true\' type=\'video/quicktime\' scale=\'tofit\' pluginspage=\'http://www.apple.com/quicktime/download/\'\x3E \x3C/embed\x3E\x3C/body\x3E\x3C/html\x3E');
		return false;
}

/*///////////////////////////////////////////////////////////////////
Function to send a mail with the link to the current page
///////////////////////////////////////////////////////////////////*/
function fnMailAFriend (){
	var SubjectLine = 'I saw this on the Assessment for Learning website and thought you might be interested';
	var BodyText= top.document.title + '\n\n' + top.document.location.href;
	
	var Message='<a class="mail" href="mailto:?SUBJECT='+escape(SubjectLine)+'&BODY='+escape(BodyText)+'" title="email this page">Email<\/a>';
	
	document.write(Message); 
}


function newWindow(popup) {
		bookWindow = window.open(popup, 'bookwin', 'width=450,height=200')
		bookWindow.focus()
	}	
function newWindow3(popup) {
		snapshot = window.open(popup, 'snapshot', 'width=500,height=500')
		snapshot.focus()
	}
function newWindowMovie(popup,title) {
		movieWindow = window.open(popup+'&title='+title, 'movieWindow', 'height=320, width=400')
		movieWindow.focus()
	}
	
// Open new window in full-screen mode
function OpenFullScreenWindow(url, options) {
  var winOptions = '';
  switch (options)
  {
    case 'scroll':
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes';
      break;
      
    default:
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no';
  }
  
  var newWin = window.open(url, 'newWindow', winOptions + ", height=" + screen.height + ", width=" + screen.width);
  newWin.moveTo(0,0);
  newWin.focus();
  return false;
}

// Open new window in full-screen mode
function OpenFullScreenWindow(url, options) {
  var winOptions = '';
  switch (options)
  {
    case 'scroll':
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes';
      break;
      
    default:
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no';
  }
  
  var newWin = window.open(url, 'newWindow', winOptions + ", height=" + screen.height + ", width=" + screen.width);
  newWin.moveTo(0,0);
  newWin.focus();
  return false;
}

function OpenPopupWindow(sURL, sWidth, sHeight)
{
    // Open Popup window
    var objNewWin = window.open(sURL, "_blank", "directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes" + ", height=" + sHeight + ", width=" + sWidth);
    // Centre Popup window - watch the security issue if sURL is on a seperate domain!
    try
    {
        objNewWin.moveTo(Math.round((screen.width - parseInt(sWidth)) / 2), Math.round((screen.height - parseInt(sHeight)) / 2));
    }
    catch(e){}
    
    objNewWin.focus();
    return false;
}


// HELP hide/show code
function ShowHelp (sHelpID, iXPos, iYPos)
{
	document.getElementById(sHelpID).style.marginTop = iXPos + 'px';
	document.getElementById(sHelpID).style.marginLeft = iYPos + 'px';
	document.getElementById(sHelpID).style.display = 'block';
}

function HideHelp (sHelpID)
{
	document.getElementById(sHelpID).style.display = 'none';
}

// Highlight field background (for forms)
function PlaceFocus(sItemID, bHighlight)
{
	var oItem = document.getElementById(sItemID);
	var sHighlightClass = "highlightItem";

	if (bHighlight)
	{
		oItem.className += " " + sHighlightClass;
	}
	else
	{
		var iHighlighClassPos = oItem.className.indexOf(sHighlightClass);
		oItem.className = oItem.className.substring(0, iHighlighClassPos - 1);
	}
}

// JQuery functions
function FieldFocus(sRowID, sHelpID, bHighlight)
{
    var sClassName = "highlightItem";
    
    // Highlight row
    if (bHighlight)
    {
        $("#" + sRowID).addClass(sClassName);
    }
    else
    {
        $("#" + sRowID).removeClass(sClassName);
    }
    
    // Show/Hide help
    if (sHelpID != '')
        $("#" + sHelpID).animate({opacity: 'toggle'}, "normal");
}


// Print current page
function PrintPage()
{
	window.print();
}


// ---------------------------------------------------------------------
// Cookies
// ---------------------------------------------------------------------

 /**
 * @name cc.cookie.set
 * @desc Set a cookie
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Hash options A set of key/value pairs for optional cookie parameters.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 * If set to null or omitted, the cookie will be a session cookie and will not be retained
 * when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 * require a secure protocol (like HTTPS).
 */
 function SetCookie (name, value, options) {
	options = options || {};
	var expires = '';

	options.expires = options.expires || 356;

	var date = new Date();
	date.setTime(date.getTime()+(options.expires*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

	var path = options.path ? '; path=' + options.path : '';
	var domain = options.domain ? '; domain=' + options.domain : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
 }

 /**
 * @name cc.cookie.get
 * @desc Get the value of a cookie
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 */
 function GetCookie (name) {
	var cookieValue = null;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i].trim();
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, name.length + 1) == (name + '=')) {
				cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
				break;
			}
		}
	}
	return cookieValue;
 }

 /**
 * @name cc.cookie.remove
 * @desc Delete a cookie
 * @param String name The name of the cookie.
 */
 function DeleteCookie (name) {
	SetCookie(name, "", {expires: -1});
 }


 /*
 ** popUp function for Flash games/scenarios/explorer
 */
  var newWin = null;
 function popUp(strURL, strType, strHeight, strWidth) {
     if (newWin != null && !newWin.closed)
         newWin.close();
     var strOptions = "";
     if (strType == "console") strOptions = "resizable,height=" + strHeight + ",width=" + strWidth;
     if (strType == "fixed") strOptions = "menubar,height=" + strHeight + ",width=" + strWidth;
     if (strType == "elastic") strOptions = "toolbar,menubar,scrollbars,resizable,location,height=" + strHeight + ",width=" + strWidth;
     newWin = window.open(strURL, 'newWin', strOptions);
     newWin.focus();
 }

 /*
 ** openWindow function for Flash games/scenarios/explorer
 */
 function openWindow(tUrl, tName) {
     var tWindowName = tName;
     var agnt = navigator.userAgent.toLowerCase();
     var theVersion = parseFloat(navigator.appVersion);
     var isOpera = (navigator.appName == "Opera") ? 1 : 0;
     var ie = (document.all && !isOpera) ? 1 : 0;
     var ie4 = (navigator.userAgent.indexOf("MSIE 4") > -1) ? 1 : 0;
     var ns4 = (navigator.appName == "Netscape" && theVersion < 5) ? 1 : 0;
     var ns6 = document.getElementById && !document.all;
     var isWin = ((navigator.appVersion.indexOf('Windows') != -1) && (navigator.appVersion.indexOf('3.1') == -1));
     var isNs = (ns4 || ns6);
     var isMac = (navigator.appVersion.indexOf('Mac') != -1);
     var isW3C = (document.getElementById && !ie) ? 1 : 0;
     var havePluginJsSupport = ((ie && isWin) || ns4);
     var width, height, x, y;
     width = (screen.availWidth ? screen.availWidth : 800) - 10;
     height = (screen.availHeight ? screen.availHeight : 600) - 28;
     //alert('availh:'+screen.availHeight);
     x = 0;
     y = 0;
     if (isMac) {
         if (isNs) {
             height += 40;
             //height=screen.height-45
             width += 10;
         } else {
             width += 10;
             height += 6;
         }
     } else {
         x = -1;
         y = -3;
         if (ns6) {
             width += 4;
             height += 2;
         }
     }
     //
     // if big height, used the centred ones, otherwise the top sticking ones.
     if (height > 610) {
         var tUrlArray = tUrl.split("/");
         tUrlArray[tUrlArray.length - 1] = "centred_" + tUrlArray[tUrlArray.length - 1];
         tUrl = tUrlArray.join("/");
     }
     // 
     if (ie == 0 || height > 610) {
         fullWin = window.open(tUrl, tWindowName, "left=" + x + "0,top=" + y + ",width=" + width + ",height=" + height + ",fullscreen=no, status=no, scrollbars=no,resizable=yes,menubar=no,toolbar=no");
         fullWin.moveTo(x, y);
         if (fullWin && isMac && fullWin.moveTo && fullWin.resizeTo) {
             fullWin.moveTo(x, y);
             fullWin.resizeTo(width, height);
             fullWin.focus();
         }
     } else {
         //alert("going full!");
         fullWin = window.open(tUrl, tWindowName, "fullscreen=yes");
     }
     //self.location = "newlocation"; 
 }
