/****************************************************************/
/***  SUPPORTING FUNCTIONS									  ***/
/****************************************************************/


// ---------------------------------------------------------------------
// string.trim (our own language addition)
// ---------------------------------------------------------------------
if (String.prototype.trim == null) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/, '');
	};
}

$.fn.menuTopDropDown = function () {
	$(this).hover(function(){
		heightDropDown = 0;
		$("ul li",this).each(function(){
			heightDropDown +=   $(this).height();
		});
		$("ul",this).animate({height:heightDropDown},{queue:false,duration:500});
	},function(){
		$("ul",this).animate({height:'0px'},{queue:false,duration:500});
	});
}
//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';
}

// Print current page
function PrintPage()
{
	window.print();
}

// Highlight field background (for forms)
function PlaceFocus(sItemID, bHighlight)
{
	var oItem = document.getElementById(sItemID);
	var sHighlightClass = "highlightItem";

	if (bHighlight)
	{
		//alert("before className - " + oItem.className);
		oItem.className += " " + sHighlightClass;
		//alert("after className - " + oItem.className);
	}
	else
	{
		var iHighlighClassPos = oItem.className.indexOf(sHighlightClass);
		//oItem.className = oItem.className.substring(0, iHighlighClassPos - 2) + oItem.className.substr(iHighlighClassPos + iHighlighClassPos.length + 1);
		oItem.className = oItem.className.substring(0, iHighlighClassPos - 1);
		//alert("className - " + oItem.className);
	}
}


// ---------------------------------------------------------------------
// 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});
 }




