/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var subwindow;

/*------------------------------ON LOAD INITIALIZERS---------------------------------*/

document.observe("dom:loaded", function() {
    if ($('SearchString') != 0) {
        new Ajax.Autocompleter('SearchString', 'Search_Suggestions', 'index.cfm?event=searchResultsAjax', {updateElement:'searchvalnul'});
    }
    if ($('username')) {
        $('username').focus();
    }
    if ($('mobileNum')) {
        inputChecker = new inputChecker();
    }
    if ( window.location.href.search(/showSummaryEssayGrade/i) > -1) {
        $('feedback').show();
    }
});
/*
 * 20.12.2010 added
 * second initializer for timer, 
 * this is because on CW as flash would load, it would mess with the timer-
 * randomly and only on ie7-8! 
 */

Event.observe(window,"load",function() {
    if ( window.location.href.search(/showSummary/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
    if ( window.location.href.search(/showEssay/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
    if ( window.location.href.search(/showQuiz/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
    if ( window.location.href.search(/showPeerReview/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
    if ( window.location.href.search(/pgjump/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
    if (document.getElementById('ti_asid')) {
        ActivityTimer = new ActivityTimer();
    }
    if ( window.location.href.search(/showActivity/i) > -1) {
        ActivityTimer = new ActivityTimer();
    }
});


function popupwin(aDocument)
{
	if (!subwindow || subwindow.closed)
	{ 
		subwindow = window.open(aDocument,"subwindow","width=660,height=635,resizable,scrollbars=yes");
	} else {
		subwindow.document.location.href = aDocument;
		subwindow.focus();
	}
}

function showDetail(recID){
	elementName = 'Detail_'+recID; 
	Element.toggle(elementName);
}

function showAssigmentNote(aid){
	if($('Assigarrow_'+aid).src.search(/bullet.gif/) > 1){
		$('Assigarrow_'+aid).src = "img/icons/icon_bullet_open.gif";
	}else{
		$('Assigarrow_'+aid).src = "img/icons/icon_bullet.gif";
	}

	elementName = 'assignmentNote_'+aid; 
	Element.toggle(elementName);
}


function showVideo(recID){
	elementName = 'Detail_'+recID; 
	Element.toggle(elementName);
	Element.toggle('controls');
}

function showSubLevel(SkillID) {
	if($('control_'+SkillID).src.search(/bullet.gif/) > 1){
		$('control_'+SkillID).src = "img/icons/icon_bullet_open.gif";
	} else {
		$('control_'+SkillID).src = "img/icons/icon_bullet.gif";
	}
	$('subLevel_'+SkillID).toggle();
}

function check(activityID) {
	if($('check_'+activityID).src.search(/check.gif/) > 1){
		$('check_'+activityID).src = "img/icons/icon_remove.gif";
	} else {
		$('check_'+activityID).src = "img/icons/icon_check.gif";
	}
	$('assignDetails_'+activityID).toggle();
}

function saveDate(activityID,txt) {
	$('save_'+activityID).update('<span class="alert">'+txt+'</span>');
} 

function classDayToggle(day) {
	$('dayDetail_'+day).toggle();
} 



function toggleAnswer(aid) {
	if($('arrow_'+aid).src.search(/bullet.gif/) > 1){
		$('arrow_'+aid).src = "img/icons/icon_bullet_open.gif";
	}else{
		$('arrow_'+aid).src = "img/icons/icon_bullet.gif";
	}
	$('Detail_'+aid).toggle();

}
function markAsComplete(){
	new Ajax.Request('index.cfm?event=completeActivity&ajax=true', {asynchronous:true, evalScripts:true, parameters:Form.serialize($('markAsComplete')), onComplete: markAsCompleteSuccess}); 
}

function markAsCompleteSuccess(){
	$('markAsCompleteBut').value = "Completed";
	$('markAsCompleteBut').disabled = true;
}


// calendar popup

var curCalTarget = 0;

function showcal(calID){
	curCalTarget = calID;
	$('mycal').toggle();
	$('mycal').absolutize();
	calCoords = $(calID).cumulativeOffset();
	
	$('mycal').style.top = calCoords[1] + 25 + 'px';
	$('mycal').style.left = calCoords[0] - 45 + 'px';
	newCoords = $('mycal').cumulativeOffset();
}

function updatecal(newdate){
	var url = 'index.cfm';
	var pars = 'event=calChooser&date='+newdate;
	var target = 'mycal';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function choosedate(datechoosen){
	$(curCalTarget).value = datechoosen;
	closecal();
}

function closecal(){
	$('mycal').toggle();
}


var inputChecker = Class.create();

inputChecker.prototype = {

	initialize: function(){
	},
	//add to numeric only form fields onkeypress="return inputChecker.requireNum(event);"
	requireNum: function(e){
		var unicode=e.charCode? e.charCode : e.keyCode;
		if (unicode!=8 && unicode!=9 && unicode!=37 && unicode!=39){ //if the key isn't the backspace key (which we should allow) 
			if (unicode < 48 || unicode > 57){ //if not a number
				return false; //disable key press 
			} else {
				return true; //disable key press 
			}
		}
	}
}



var ActivityTimer = Class.create();

ActivityTimer.prototype = {
/*	ok this her function tracks time on task
 * 	if we keep sending call to updated the time, the session never ends
 * 	so we can send the page to logout if the timer hits two hours */

	initialize: function(){
                var dateobj = new Date();
		var timer=setInterval("ActivityTimer.markTime()",30000);
		this.timeon = 30;
		this.dateobj = dateobj;

                ($("ti_aid")  != undefined ) ? this.aid  = $F("ti_aid")  : this.aid=0;
		($("ti_asid") != undefined ) ? this.asid = $F("ti_asid") : this.asid=0;
		($("ti_cuid") != undefined ) ? this.cuid = $F("ti_cuid") : this.cuid=0;
		($("ti_prid") != undefined ) ? this.prid = $F("ti_prid") : this.prid=0;
		this.markTime();
	},
	markTime: function(){
                timestamp = this.dateobj.getTime();
                //($("ti_aid")  != undefined ) ? this.aid  = $F("ti_aid")  : this.aid=0;

		this.timeon = this.timeon + 30;
		var url = 'index.cfm';
		var pars = 'event=markTime&ajax=true&aid=' + this.aid + '&asid=' + this.asid + '&cuid=' + this.cuid + '&prid=' + this.prid + '&timeon=' + this.timeon + '&now=' + timestamp ;
		var myAjax = new Ajax.Request(url, { asynchronous:true, parameters: pars, evalScripts: true, method: 'get'});
		if (this.timeon == 3600){ //if the page has been sitting for 1 hour, log out the user 
                    document.location.href='index.cfm?event=logout';
		}
		//$("ti_aid_alert").update( this.aid + " " + this.timeon );
	}
}


////// stuff for note book /////

function newNoteBookFck() {
	var newFckNote = new FCKeditor("newNotefckEditor");
	//newFckNote.toolbarSet = "MGHajax" ;
	newFckNote.BasePath = "views/fckeditor/"
	newFckNote.CustomConfigurationsPath = "../mghConfig.js" ;
	$('NewNote').show(); 
    newFckNote.ReplaceTextarea() ;
	//newFckNote.value=''; 
}

function editNoteBook(NoteBookID) {
	var url = 'index.cfm';
	var pars = 'event=NoteBookEdit&ajax=true&NoteBookID=' + NoteBookID;
	var myAjax = new Ajax.Request(url, { asynchronous:true, parameters: pars, evalScripts: true, method: 'get'});
}

function updateClientListing(ClientID) {
	var url = 'index.cfm';
	var pars = 'event=AdminClientListEdit&ajax=true&pruClientID=' + ClientID;
	var myAjax = new Ajax.Request(url, { asynchronous:true, parameters:Form.serialize($('Client_Form_'+ClientID)), evalScripts: true, method: 'get'});
}

////// Required for FCK editor and form serialize function/////
function fckUpdateClass()
{
  this.UpdateEditorFormValue = function()
  {
    for ( i = 0; i < parent.frames.length; ++i )
    if ( parent.frames[i].FCK )
    parent.frames[i].FCK.UpdateLinkedField();
  }
}
// instantiate the class
var FckUpdateObject = new fckUpdateClass();


document.observe("dom:loaded", function() {
	if ($('Rubric')) {
		LoStudentAssignment = new LoStudentAssignment();

		if ($('left-blog')) {
			LoStudentAssignment.leftdiv = "left-blog";
		}
	}
});
var LoStudentAssignment = Class.create();

LoStudentAssignment.prototype = {
/* 
 * this will take the rubric div and cover the left2 column.
 */
	yPos : 0,
	xPos : 0,

	initialize: function(){
		this.content = $('Rubric');
		this.leftdiv = $('left2');
	},
	rubric: function(){

		this.content.absolutize(); 
		this.content.clonePosition($(this.leftdiv)); 
		this.content.style.display = 'block';
		this.content.setStyle({ 'background' : '#fff' });
		if($('player')) {
			$('player').toggle();
		}
	},
	assignment: function(){
		this.content.style.display = 'none';
		if($('player')) {
			$('player').toggle();
		}
	}
}



