/*
		Navigation Functions
		Nav.js

		Copyright (c) 2005.  Academic ADL Co-Laboratory.
		 
		This file is part of libSCORM 2004.  libSCORM 2004 is proprietary software for use only 
		by the Academic ADL Co-Laboratory and its clients.  Please read the full license agreement
		included with this software to learn whether you may legally use this software.
		
		libSCORM 2004 is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		
		To obtain a copy of the license agreement, please write to:
		
		Academic ADL Co-Laboratory
		222 W. Washington Ave., Suite 470
		Madison, WI 53703
*/

var contentPageIndex = 0;
var contentOldIndex = -1;
var canPrevious = false;
var canContinue = false;

function init_nav() {
	if(init) {
		try {
			var result = LMSGetValue("adl.nav.request_valid.previous");
		
			if(result == "true") {
				canPrevious = true;
			}
		}
		catch(x) {}
	
		try {
			var result = LMSGetValue("adl.nav.request_valid.continue");
		
			if(result == "true") {
				canContinue = true;
			}
		}
		catch(x) {}
	}
}

function get_location() {
	var cur = eval(contentFrame).document.location.href;
	
	if(pages > 1)
	{
		if( contentPageArray == null )
		{
			var cur_sub = cur.substring(cur.lastIndexOf("_")+1, cur.lastIndexOf("."));
			return parseInt(cur_sub);
		}
		else
		{
			for(var k=0; k<pages; ++k)
				if( cur.lastIndexOf( contentPageArray[k] ) != -1 )
				{
					contentPageIndex = k;
					return k + 1; // remember array is 0-based
				}
			if(isFramed) {
				parent.print_error("Invalid Location", "Nav2004.js", 16);
			}
			else {
				print_error("Invalid Location", "Nav2004.js", 16);
			}
		}
	}
	
	return -1;
}



function nav_continue() {
	try {
		LMSSetValue("adl.nav.request","continue");
	}
	catch(x) {}
	unload(false);
}

function nav_previous() {
	try {
		LMSSetValue("adl.nav.request","previous");
	}
	catch(x) {}
	unload(false);
}

function create_url(new_page_num) {
	if( contentPageArray != null )
	{
		return contentPageArray[new_page_num-1];
	}
	var contentFrameRef = eval(contentFrame); // From sco_info.js
   	var current_url = contentFrameRef.document.location.href;

   	var extention = current_url.substring(current_url.lastIndexOf("."), current_url.length);
   	var stripped_filename = 
   		current_url.substring(current_url.lastIndexOf("/") + 1, current_url.lastIndexOf("_")+1);

   	var new_url = stripped_filename + new_page_num + extention; 	
   	return new_url;
}

function goto_page(page) {
   var new_url = create_url(page);
   eval(contentFrame).document.location.href = new_url;
}

function create_nav(navLayer) {
	var pageIndex = get_location();
	
	var navHTML1 = "";

	// Set up nav bar images
	
	var pageIndex = get_location();

	var eachWidth = 200/pages;
	eachWidth = Math.round(eachWidth);

	// Set up nav bar images
	if (pageIndex==1 && init &&  canPrevious) { // Nav request for the previous SCO
		if( isFramed ) {
			navHTML1 += "<a href='javascript:parent.nav_previous();'><img src='../_common/images/arrow_back_nav.gif' border='0' height='30' alt='button: continue'></a>";
		}
		else {
			navHTML1 += "<a href='javascript:nav_previous();'><img src='../_common/images/arrow_back_nav.gif' border='0' height='30' alt='button: continue'></a>";
		}
	} 
	else if (pageIndex==1) { // If its the first, use the disabled button for the left nav button
		navHTML1 += "<img src='../_common/images/arrow_back_disabled.gif' border='0' height='30' alt='button: back, disabled'> ";
	} 
	else {
		navHTML1 += "<a href='" + create_url(pageIndex-1) + "'><img src='../_common/images/arrow_back_enabled.gif' border='0' height='30' alt='button: back'></a> ";
	}

	for (var i=1; i <= pages; i++) {
		if (i<pageIndex) 
			navHTML1 += "<a href='" + create_url(i) + "'><img src='../_common/images/progress_complete.gif' border='0' height='30' style='{ margin-right: 1px; }' alt='Page " + i + "'></a> ";
		else if (i > pageIndex ) 
			navHTML1 += "<a href='" + create_url(i) + "'><img src='../_common/images/progress_current.gif' border='0' height='30' style='{ margin-right: 1px; }' alt='Page " + i + "'></a> ";
		else // current page index
			navHTML1 += "<a href='" + create_url(i) + "'><img src='../_common/images/progress_incomplete.gif' border='0' height='30' style='{ margin-right: 1px; }' alt='Page " + i + "'></a> ";			
	}

	if( pageIndex == pages && init && canContinue) // If we're at the end of the SCO and we can continue
	{
		if( isFramed ) {
			navHTML1 += "<a href='javascript:parent.nav_continue();'><img src='../_common/images/arrow_next_nav.gif' height='30' border='0' alt='button: continue'></a>";
		}
		else {
			navHTML1 += "<a href='javascript:nav_continue();'><img src='../_common/images/arrow_next_nav.gif' height='30' border='0' alt='button: continue'></a>";
		}
	}
	else if (pageIndex == pages) { // if its at the end, use the disabled right nav button
		navHTML1 += "<img src='../_common/images/arrow_next_disabled.gif' height='30' border='0' alt='button: next, disabled'>";
	} 
	else {
		navHTML1 += "<a href='" + create_url(pageIndex+1) + "'><img src='../_common/images/arrow_next_enabled.gif' height='30' border='0' alt='button: next'></a>";
	}	

	var layerRef = eval(navLayer);
	layerRef.innerHTML = navHTML1;
}
