/* SCRIPTS.JS -- JavaScript utilities for travelogues.net */

/* ------------------------------------------ URL redirection routines --------------------------------------------- */

/* redirect_index(): If the browser has JavaScript, open JS-enabled index_js.htm. If a sub-page was opened and the user was
      redirected to index.htm, then pass on the name of the entry page to index_js.htm so that we can open that page inside the frame.
*/
function redirect_index()
{
	var query = location.search.substring(1);
	var full_url = "index_js.htm";
	if( query.length > 0 )
		full_url += ("?" + query);
	this.location.replace( full_url );
}

/* set_frame(): Called by index_js.htm: uses internal HTML to modify it. Sets up the frame, and if the entry point was a sub-page,
      sets that page as the default page in the content frame so that the user goes straight there.
*/
function set_frame()
{
	var return_value;
	var args = new Object;
	var content_page, requested_page;

	args = getArgs();

	requested_page = args["page"];
	/* Still dangerous to call contents_js.htm, but who would do that, and why?? */
	if( (args["page"] == null) || (requested_page == "contents.htm") || (requested_page == "contents_js.htm") )
		content_page = "intro.htm"
	else
		content_page = requested_page;

	/* For some reason multiple write statements don't work. So, compile a string and write once */
	return_value="<frameset cols=\"16%,*\">";
	if( tree_supported() )
		return_value+="<frame name=\"contents\" target=\"main\" src=\"contents_js.htm\" scrolling=\"auto\">";
	else
		return_value+="<frame name=\"contents\" target=\"main\" src=\"contents.htm\" scrolling=\"auto\">";
  	return_value+="<frame name=\"main\" ";
	return_value+="src=\"" + content_page +"\" scrolling=\"auto\" target=\"_self\">";

	return_value+="<noframes>";
  	return_value+="<body>";
  	return_value+="<p>This page uses frames, but your browser doesn't support them.</p>";
    	return_value+="</body>";
    	return_value+="</noframes>";
    	return_value+="</frameset>";

	document.write( return_value );
}


/* get_frame(): If someone tuned in on an individual page, redirect them to the main frame */
function get_frame()
{
	var parent_href, this_page, index_page, pos;

	this_page = this.location.href;
	parent_href = parent.location.href.toUpperCase();

	if( parent_href.indexOf( "INDEX.HTM" ) < 0 && parent_href.indexOf( "INDEX_JS.HTM" ) < 0 )
	{
		/* If we got here by directory, not specifying a file */
		if( parent_href.charAt( parent_href.length-1 ) != '/' )
		{
			this_page = strip_dir( this_page );
			index_page = "index.htm" + "?page=" + this_page;
			this.location.href = index_page;
		}
	}
}



/* Parses a query string. Taken from JavaScript, The Definitive Guide, O'Reilly */
function getArgs()
{
	var args = new Object;
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for( var i = 0; i < pairs.length; i++ )
	{
		var pos = pairs[i].indexOf('=');
		if( pos == -1 )
			continue;
		var argname = pairs[i].substring( 0, pos );
		var value = pairs[i].substring( pos + 1);
		args[argname] = unescape(value);
	}
	return args;
}


/* strip_dir(): Gets the name of a page, stripping out the directory path */
function strip_dir( full_path )
{
	var pos, return_value;

	pos = full_path.lastIndexOf("/")
	return_value = full_path.substr( pos >=0 ? pos+1 : 0 );

	return (return_value);
}


/* ---------------------------------------------- Navigation routines ---------------------------------------------------- */

/* nav_buttons() -- lays out 3 buttons on the content pages: prior, related photos or narrative, next */
function nav_buttons( position, page_type, date_string, last_link, related_link, next_link )
{
	var ret_val;
        var alignment;
        var table_width;
        var button_percent;
	var button_width=75;
	var button_height=18;
	var buttons = new Array();
	var button_active = new Array();
	var alt_text = new Array();
	var actions = new Array();
	var i;


	/* Set up the table positioning and proportions */
        if( position == "header" )
                alignment = "right";
        else
                alignment = "center";
        ret_val =  '<div align="' + alignment + '">';


	ret_val += '  <table class="nav_table" border="0" cellpadding="0" '
	/* If the table has a date column, force it to the left by making table 100% wide */
        if( position == "header" && date_string.length > 0 )
                ret_val += 'width = "100%"';
	ret_val+='>';

	/* Add one row */
        ret_val += '<tr>';

        /* If there's a date string, we need an extra column */
        if( position == "header" && date_string.length > 0 )
        {
		/* Force maximum width by making col 100% */
                ret_val += '<td class="nav_date" width="100%">';
                ret_val += date_string;
                ret_val += '</td>';

                button_percent = "12%";
        }
        else
                button_percent = "33%";

	/* --------- Choose button graphics and alt text, depending on what type of page ------------- */

	/* Determine which buttons are active, choose the graphic for previous and next */
	button_active[0] = (last_link.length==0) ? false : true;
	button_active[1] = (related_link.length==0) ? false : true;
	button_active[2] = (next_link.length==0) ? false : true;
	buttons[0]= button_active[0] ? 'buttons/previous.gif' : 'buttons/previous_dim.gif' ;
	buttons[2]= button_active[2] ? 'buttons/next.gif' : 'buttons/next_dim.gif';

	/* Set the alt text, choose 'related' graphic according to page type */
        if(page_type == "photos")
        {
                alt_text[0] = "Previous photo sheet";
                alt_text[1] = "Related narrative";
                alt_text[2] = "Next photo sheet";
		buttons[1]= button_active[1] ? 'buttons/narrative.gif' : 'buttons/narrative_dim.gif';
        }
        else
        {
                alt_text[0] = "Previous narrative";
                alt_text[1] = "Related photos";
                alt_text[2] = "Next narrative";
		buttons[1]= button_active[1] ? 'buttons/photos.gif' : 'buttons/photos_dim.gif';
        }


	/* Put links in an array for FORM actions */
	actions[0] = last_link;
	actions[1] = related_link;
	actions[2] = next_link;

	/* Draw the buttons */
	for( i=0; i < 3; i++ )
	{
	        if (button_active[i])
        	{
		        ret_val += '<td width="' + button_width + '">';
        		ret_val +='<FORM METHOD ="link" ACTION = "' + actions[i] + '" >';

	        	ret_val +='<INPUT TYPE = "image" SRC="' + buttons[i]
		        + '" BORDER = "0" TITLE="' + alt_text[i] + '" '
			+ ' width="' + button_width + '" height="' + button_height + '">';

	        	ret_val += '</FORM>';
        		ret_val += '</td>';
        	}
        	else
        	{
			ret_val+= '<td width="' + button_width + '" valign="top">';
			ret_val+= '<img src="' + buttons[i] + '" '
                	+ ' border = "0" alt="' + alt_text[i] + '" '
			+ ' width="' + button_width + '" height="' + button_height + '">';
        	}

	}

        ret_val += '</tr>';
        ret_val += '</table>';
        ret_val += '</div>';

document.write( ret_val );
return true;
}

/* menu_supported() --  browser and version check to see if Tigra Menu will work */
function menu_supported()
{
	/* Opera goes first since it masquerades as other browsers */
	var browsers= new Array( ["Opera", 7 ], ["MSIE", 5], ["Netscape", 6], ["Mozilla", 5], ["Konqueror", 2] );

	return_value=browser_version( browsers );
	return return_value;
}

/* tree_supported() --  browser and version check to see if Tigra Tree Menu will work */
function tree_supported()
{
	var browsers= new Array( ["Opera", 7 ], ["MSIE", 5], ["Netscape", 6], ["Mozilla", 5], ["Konqueror", 2] );

	return_value=browser_version( browsers );
	return return_value;
}


function browserName()
{
	var useragent;
	var pos;
	var browserbrand;
	var versionnumber;
	var browserversion;
	var semicolon;
	var numberpos;
	var returnvalue;

	/* likely not to be foolproof, but this string seems to have all the info */
	useragent = navigator.userAgent;

	/* Determine the maker. MSIE is often co-branded or impersonated, but AYOR.
	   If not one of these three brands, be conservative and return "unknown"
	*/

	if( (pos=useragent.indexOf("Opera")) >= 0 )
		browserbrand="Opera";
	else if ( (pos=useragent.indexOf("Netscape")) >= 0 )
		browserbrand="Netscape";
	else if ( (pos=useragent.indexOf("MSIE")) >= 0 )
		browserbrand="MSIE";
	else
		browserbrand="unknown";

	return browserbrand;
}


function browser_version( supported )
{
	var i;
	var useragent;
	var index = -1;
	var pos;
	var browserversion;
	var semicolon;
	var numberpos;
	var versionnumber;
	var equation;
	var return_value;

	/* likely not to be foolproof, but this string seems to have all the info */
	useragent = navigator.userAgent;

	for( i=0; i < supported.length; i++ )
	{
	    if( (pos=useragent.indexOf(supported[i][0])) >= 0 )
	    {
	    	index=i;
		/* pull out the string we want, which is delimited by a semicolon */
		browserversion=useragent.substring( pos );
		semicolon= browserversion.indexOf(";");
		/* If no semicolon, it's at the end */
		if( semicolon >= 0 )
			browserversion=browserversion.substring( 0, semicolon );

		/* Get the first browser major version. Search for the instance of an integer,
		to get the position, then use parseInt() to get the entire integer
		*/
		numberpos=browserversion.search("[0-9]");
		browserversion=browserversion.substring( numberpos );
		versionnumber=parseInt(browserversion);
	    	break;
	    }
	}
	if( index==-1 )
	    return false;
	if( versionnumber >= parseInt(supported[index][1]) )
		return_value=true;
	else
		return_value=false;
	return return_value;

}

/* -------------------------------------------- Other utilities ------------------------------------------------------ */
/* show_large_photo(): when user clicks on a photo page, this opens a new window with a larger version */
function show_large_photo(photo_obj)
{
	var raw_name = photo_obj.src;
	var regExpr = /images/;
	var big_image_dir = "images_large"
	var big_photo_name;
	var body_begin = '<body topmargin="0" leftmargin="0" bgcolor="#000000"	><p>';
	var body_end = '</body>';
	var small_photo_height = photo_obj.height;
	var small_photo_width = photo_obj.width;
	var big_photo_width;
	var big_photo_height;
	var window_height;
	var window_width;
	var window_size;
	var window_border = 20;
	var enlargement;
	var big_photo_x;
	var big_photo_y;

	enlargement=1+(2/3);
	/* Replace "images" directory name with the other. NB - not foolproof if a file name has
	   the word "images" in it!
	*/
	big_photo_name=photo_obj.src;
	big_photo_name = big_photo_name.replace( regExpr, big_image_dir );

	big_photo_x = Math.round(small_photo_width*enlargement);
	big_photo_y = Math.round(small_photo_height*enlargement);
	big_photo_height = 'height="' + big_photo_y + '"';
	big_photo_width = 'width="' + big_photo_x + '"';
	window_height = big_photo_y + window_border;
	window_width = big_photo_x + window_border;

	if( window_height >= screen.availHeight )
		window_height = screen.availHeight;
	if( window_width >= screen.availWidth )
		window_width = screen.availWidth;

	window_size = "height=" + window_height + ","
		+ "width=" + window_width;

	var big_photo_format = '<img border="0" src=" '
		+ big_photo_name
		+ '"'
		+ big_photo_height
		+ big_photo_width
		+'>' ;
	photowindow = window.open( "", "thewindow",
			"scrollbars,resizable," + window_size+",screenX=0,screenY=0" );
	/* In case the window was already open, move back to origin and redo, in case we go
		between portrait and landscape. Netscape does OK without these lines, and
		freezes when they're in
	 */
	if( navigator.appName != "Netscape" )
	{
		photowindow.resizeTo( window_width, window_height );
		photowindow.moveTo( 0, 0 );
	}
	photowindow.document.write(body_begin+big_photo_format+body_end);
	photowindow.document.close();
/*        photowindow.style.cursor="default"; */
	photowindow.focus();
}


/* Rollover functions from Dreamweaver: used on the home page  */
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


