
function clLogicInit(doc) {

	cl_document=doc;
	clTrackInit(cl_document);
	clLogicGoToNode(cl_currentnode, cl_currentpage, cl_document);
	clRenderRefreshIndex(cl_document, true);

	return;
}


function clLogicMainClick() {

	clRenderCloseAllPopups();

	return true;
}


function clLogicGetParent(n) {

	return(cl_struct[n][0]);
}


function clLogicGetPrevious(n) {

	return(cl_struct[n][2]);
}


function clLogicGetNext(n) {

	return(cl_struct[n][3]);
}


function clLogicGetNodeName(n) {

	return(cl_struct[n][1]);
}


function clLogicGetNodeTutor(n) {

	return(cl_struct[n][5]);
}


function clLogicGetNodeIndex(n) {

	return(cl_menudfn[cl_struct[n][6]]);
}


function clLogicGetTutorData(n, e) {

	var out="";

	switch (e) {

		case 'address':

			out = eval ('CL_TUTORADDR_' + cl_tutorprofile[clLogicGetNodeTutor(n)][0]);

			break;

		case 'subject':

			out = eval ('CL_TUTORSUBJ_' + cl_tutorprofile[clLogicGetNodeTutor(n)][1]);

			break;


		case 'body':

			out = eval ('CL_TUTORBODY_' + cl_tutorprofile[clLogicGetNodeTutor(n)][2]);

			break;

	}

	return(out);
}


function clLogicHasAttachment(n, p, t) {

	out=false;

	if (cl_struct[n][7][p-1][t].length > 0) out=true;

	return(out);
}



function clLogicGetAttachmentWidth(n, p, t) {

	return(cl_struct[n][7][p-1][t][0]);
}



function clLogicGetAttachmentHeight(n, p, t) {

	return(cl_struct[n][7][p-1][t][1]);
}



function clLogicGetPages(n) {

	return(cl_struct[n][7].length);
}


function clLogicIsLastPage() {

	return(cl_currentpage==clLogicGetPages(cl_currentnode));
}


function clLogicIsFirstPage() {

	return(cl_currentpage==1);
}


function clLogicGetNodePath(n, l) {

	var r="";

	if (n) {

		if (clLogicGetParent(n)) {

			r= clLogicGetNodePath(clLogicGetParent(n), l) + ' > ';
			if (l) r += '<a href="javascript:top.clLogicGoToNode(\'' + n + '\', 1, document)" class="livetxt">';
			r += clLogicGetNodeName(n);
			if (l) r+='</a>';

		} else {

		if (l) r='<a href="javascript:top.clLogicGoToNode(\'' + n + '\', 1, document)" class="livetxt">';
		r += clLogicGetNodeName(n);
		if (l) r+='</a>';

		}
	}

	return (r);
}


function clLogicGoToNode(n, p) {

	cl_referer=cl_currentnode;
	cl_currentnode=n;
	cl_currentpage=p;

	clLogicToggleMenu(cl_activemenu, cl_document);
	hwtPushButtonDisable('up');
	hwtPushButtonDisable('down');
	hwtPushButtonDisable('left');
	hwtPushButtonDisable('right');

	if (CL_LASTPAGELCK) {

		if ((clLogicGetNext(n)!="") && clLogicIsLastPage()) hwtPushButtonEnable('down');

	} else {

		if (clLogicGetNext(n)!="") hwtPushButtonEnable('down');
	}

	if (clLogicGetPrevious(n)!="") hwtPushButtonEnable('up');

	switch (CL_LRBEHAVIOR) {

		case 0:
		
			if (p>1) hwtPushButtonEnable('left');
			if (p<clLogicGetPages(n)) hwtPushButtonEnable('right');

			break;

		case 1:

			if (clLogicGetPages(n) > 1) {

				hwtPushButtonEnable('left');
				hwtPushButtonEnable('right');
			}

			break;

		case 2:

			if(!((clLogicGetPrevious(n)=="") && clLogicIsFirstPage())) hwtPushButtonEnable('left');
			if(!((clLogicGetNext(n)=="") && clLogicIsLastPage())) hwtPushButtonEnable('right');

			break;
	}

	if (clLogicHasAttachment(n, p, 0)) {

		hwtPushButtonEnable('video');

	} else {

		hwtPushButtonDisable('video');

	}
	
	
	if (clLogicHasAttachment(n, p, 1)) {

		hwtPushButtonEnable('audio');

	} else {

		hwtPushButtonDisable('audio');

	}


	if (clLogicHasAttachment(n, p, 2)) {

		hwtPushButtonEnable('teacher');

	} else {

		hwtPushButtonDisable('teacher');

	}


	clTrackSet(n, p);
	cl_coursepr=clTrackGetProgress(CL_P1TRACKGRP, true);

	cl_lessonpr=0;

	if (clTrackGetGroupLength(n) > 0) {

		cl_lessonpr=clTrackGetProgress(clTrackGetGroup(n, 0), false);

	}

	clTrackCookiePut(cl_document);
	clRenderRefresh(cl_document);

	return;
}


function clLogicGoToNextNode() {

	clLogicGoToNode(clLogicGetNext(cl_currentnode), 1, cl_document);
	
	return;
}


function clLogicGoToPreviousNode() {

	clLogicGoToNode(clLogicGetPrevious(cl_currentnode), 1, cl_document);
	
	return;
}

function clLogicGoToNextPage() {

	clLogicGoToNode(cl_currentnode, cl_currentpage+1, cl_document);

	return;
}

function clLogicGoToPreviousPage(){

	clLogicGoToNode(cl_currentnode, cl_currentpage-1, cl_document);

	return;
}




function clLogicGetButtonLink(l) {

	var out="";

	switch (l) {

		case 'tutor':

			out='mailto:' + escape(clLogicGetTutorData(cl_currentnode, 'address')) + '?';
			out += 'Subject=' + escape(clLogicGetTutorData(cl_currentnode, 'subject')) + '&' + 'Body=' + escape(clLogicGetTutorData(cl_currentnode, 'body'));
	
			out = out.replace(/__NODE/g, escape(clLogicGetNodeName(cl_currentnode)));
			out = out.replace(/__PAGE/g, escape(cl_currentpage));
			out = out.replace(/__PATH/g, escape(clLogicGetNodePath(clLogicGetParent(cl_currentnode), false)));

			break;

		case 'up':

			out="javascript:top.clLogicGoToPreviousNode()";

			break;

		case 'down':

			out="javascript:top.clLogicGoToNextNode()";

			break;

		case 'left':

			if ((CL_LRBEHAVIOR==1) && clLogicIsFirstPage()) {

				out="javascript:top.clLogicGoToNode('" + cl_currentnode + "', " + clLogicGetPages(cl_currentnode) +");";
			
			} else if ((CL_LRBEHAVIOR==2) && clLogicIsFirstPage()) {

				out="javascript:top.clLogicGoToNode('" + clLogicGetPrevious(cl_currentnode) + "', " + clLogicGetPages(clLogicGetPrevious(cl_currentnode)) + ");";

			} else {

				out="javascript:top.clLogicGoToPreviousPage();";
			}

			break;

		case 'right':

			if ((CL_LRBEHAVIOR==1) && clLogicIsLastPage()) {

				out="javascript:top.clLogicGoToNode('" + cl_currentnode + "', 1);";

			} else if ((CL_LRBEHAVIOR==2) && clLogicIsLastPage()) {

				out="javascript:top.clLogicGoToNode('" + clLogicGetNext(cl_currentnode) + "', 1);";

			} else {

				out="javascript:top.clLogicGoToNextPage();";
			}

			break;

		case 'back':

			out="javascript:top.clLogicToggleMenu(top.cl_activemenu)";

			break;

		case 'video':

			out="javascript:top.clRenderToggleAttachment('video')";

			break;

		case 'audio':

			out="javascript:top.clRenderToggleAttachment('audio')";

			break;

		case 'teacher':

			out="javascript:top.clRenderToggleAttachment('teacher')";

			break;

		case 'index':

			out="javascript:top.clLogicToggleMenu('index')";

			break;	
	}

	return(out);
}



function clLogicToggleMenu(id) {


	clRenderCloseAllPopups();

	if(id != cl_activemenu) {

		if (cl_activemenu) {
			
			hwtPushButtonDisable('back');
			clRenderRefreshDynamic(cl_document, 6);
			clRenderHideTab(cl_activemenu, cl_document, 'clLogicToggleMenu("' + id + '", cl_document)');
			cl_activemenu="";

		} else {

			cl_activemenu = id;
			hwtPushButtonEnable('back');
			clRenderRefreshDynamic(cl_document, 6);
			clRenderShowTab(id, cl_document, '');
		}

	} else {

		if (id) {

			cl_activemenu="";
			hwtPushButtonDisable('back');
			clRenderRefreshDynamic(cl_document, 6);
			clRenderHideTab(id, cl_document, '');
		}
	}

	return;
}


function clLogicToggleExternal(id) {

	if (hwtPopupIsActive('external')) {

		hwtPopupClose('external');

	} else {

		clRenderCloseAllPopups();
		hwtPopupSetSize('external', cl_external[id][1], cl_external[id][2]);
		hwtPopupSetTitle('external', cl_external[id][3])
		hwtPopupOpen('external');
		hwtPopupGetWindow('external').location=cl_external[id][0];
		hwtPopupGetWindow('external').title=cl_external[id][3];
	}

	return;
}


function clLogicToggleTracking() {

	if (hwtPopupIsActive('track')) {

		hwtPopupClose('track');

	} else {

		clRenderCloseAllPopups();
		hwtPopupOpen('track');
	}

	return;
}




	

cl_coursepr = 0;
cl_lessonpr = 0;
cl_currentnode=CL_FIRSTNODE;
cl_referer="";
cl_currentpage=CL_FIRSTPAGE;
cl_activemenu="";
cl_document="";


