//
// main.js -- Copyright 2009 One More Story, Inc.
//
// newImage -- Creates and returns an image object with the source set to the incoming
// changeImages -- sets the source for one or more incoming image/source pairs.
// preloadImages -- sets arrow_btn_over and submit_btn_over to images.  For hovers?
// sendtrackingrequest -- uses the XMLHttpRequest object to send a tracking request to
// showLink -- Shows the iconspn
// hideLink -- hides the iconspn
// preventdoubleClick -- disables the submitIMGBTN control
// launch_tour -- Opens the Tour window
// showimageloader -- Shows the Image Loader in a popup window, or a message asking
// sendPOSTRequest -- uses the XMLHttpRequest object to send a POST request to
//

var debug	= false;

if ( debug ) {
	alert( 'In main.js' );
}

var preloadFlag	= false;

this.window.name	= 'OneMoreStory';
var tourWindow 		= null;				// Handle to tour window, prevents multiple opens



//
// newImage -- Creates and returns an image object with the source set to the incoming
// argument
//
function newImage( arg ) {
	if ( document.images ) {
		rslt		= new Image();
		rslt.src	= arg;

		return rslt;
	}
}


//
// changeImages -- sets the source for one or more incoming image/source pairs.
//
function changeImages() {
	if ( document.images && ( preloadFlag == true )) {
		for ( var i = 0; i < changeImages.arguments.length; i += 2 ) {
			document[ changeImages.arguments[ i ]].src = changeImages.arguments[ i + 1 ];
		}
	}
}


//
// preloadImages -- sets arrow_btn_over and submit_btn_over to images.  For hovers?
//
function preloadImages() {
	if ( document.images ) {
		arrow_btn_over	= newImage( "images/arrow_btn-over.gif" );
		submit_btn_over = newImage( "images/submit_btn-over.gif" );
		preloadFlag 	= true;
	}
}


//
// sendtrackingrequest -- uses the XMLHttpRequest object to send a tracking request to
// the server.  Any response is ignored.
//
function sendtrackingrequest( message, insubdir ) {
	var debug	= false;

	if ( debug ) {
		alert( "In sendtrackingrequest:\n" +
				"message = [" + message + "]\n" +
				"insubdir = [" + insubdir + "]" );
	}

	if ( message == undefined ) {
		message = "";
	}

	if ( insubdir == undefined ) {
		insubdir = false;
	}

		// Create the request object

    if ( window.XMLHttpRequest ) {

    	// Use native XMLHttpRequest object

		req = new XMLHttpRequest();
    } else if ( window.ActiveXObject ) {

    	// Use IE/Windows ActiveX version

        req = new ActiveXObject( "Microsoft.XMLHTTP" );
	}

		// Build the POST fields

	if ( debug ) {

		alert( "document.referrer = [" + document.referrer + "]" );
		alert( "location.host = [" + location.host + "]" );
	}
	var url	= "";
	if ( insubdir ) {
		url	+= "../";
	}
	url	+= "Logic/TrackHTMLpage.aspx";
	if ( debug ) {
		alert( "url = [" + url + "]" );
	}
	var fieldstr	= "url=" + location.pathname +
						"&message=" + message +
						"&referrer=" + document.referrer;

	if ( debug ) {
		alert( "fieldstr = [" + fieldstr + "]" );
	}

		// Send the request

	req.open( "POST", url, true );
	req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	req.setRequestHeader( "Content-length", fieldstr.length );
	req.setRequestHeader( "Connection", "close" );
	req.send( fieldstr );

	if ( debug ) {
		alert( "Done sendtrackingrequest" );
	}

}


//
// showLink -- Shows the iconspn
//
function showLink() {
	if ( document.all || document.getElementById ) {
		( document.getElementById ) ?
			nct = document.getElementById( "iconspn" ) :
			nct = document.all.iconspn;
		nct.style.display = "block";
	}
}


//
// hideLink -- hides the iconspn
//
function hideLink() {
	if ( document.all || document.getElementById ) {
		( document.getElementById ) ?
			nct = document.getElementById( "iconspn" ) :
			nct = document.all.iconspn;
		nct.style.display = "none";
	}
}


//
// preventdoubleClick -- disables the submitIMGBTN control
//
function preventdoubleClick() {
	if ( document.all || document.getElementById ) {
		( document.getElementById ) ?
			nct = document.getElementById( "submitIMGBTN" ) :
			nct = document.all.submitIMGBTN;
		nct.enabled = false;
	}
}


//
// launch_tour -- Opens the Tour window
//
function launch_tour() {
	var debug	= false;

	if ( debug ) {
		alert( "In launch_tour" );
	}

	var Browser = "<%= Request.UserAgent %>";

	if ( Browser.indexOf( 'AOL' ) != -1 ) {
		var scrnHeight	= window.innerHeight;
		var scrnWidth	= window.innerWidth;
	} else if ( window.screen ) {
		var scrnWidth	= screen.availWidth;
		var scrnHeight	= screen.availHeight;
	} else {
		var scrnHeight = 600;
		var scrnWidth = 800;
	}

	if ( debug ) {
		alert( "scrnHeight = [" + scrnHeight + "]\n" +
				"scrnWidth = [" + scrnWidth + "]\n" +
				"" );
	}

	if (( tourWindow != null ) && ( tourWindow.closed == false )) {
		tourWindow.focus();
	} else {

		var wincontrols	= "toolbar=no," +
							"location=no," +
							"status=no," +
							"menubar=no," +
							"scrollbars=no," +
							"resizable=no," +
							"left=0," +
							"top=0," +
							"width=" + scrnWidth + "," +
							"height=" + scrnHeight;

		if ( debug ) {
			alert( "wincontrols = [" + wincontrols + "]" );
		}

		tourWindow = window.open( 'Library/tourplayer.html',
									'newWin',
									wincontrols );

		if ( !debug ) {
			tourWindow.focus();
		}
	}
}



//
// launch_book -- Opens the Try A Book window
//
function launch_book() {

	var Browser = "<%= Request.UserAgent %>";

	if ( Browser.indexOf( 'AOL' ) != -1 ) {
		var scrnHeight	= window.innerHeight;
		var scrnWidth	= window.innerWidth;
	} else if ( window.screen ) {
		var scrnWidth	= screen.availWidth;
		var scrnHeight	= screen.availHeight;
	} else {
		var scrnHeight = 600;
		var scrnWidth = 800;
	}

	if (( tourWindow != null ) && ( tourWindow.closed == false )) {
		tourWindow.focus();
	} else {
		var wincontrols	= "toolbar=no," +
							"location=no," +
							"status=no," +
							"menubar=no," +
							"scrollbars=yes," +
							"resizable=no," +
							"left=0," +
							"top=0," +
							"width=" + scrnWidth + "," +
							"height=" + scrnHeight;

		tourWindow = window.open( 'Library/bookplayer.html',
									'newWin',
									wincontrols );
		tourWindow.focus();
	}
}


//
//  genpopup	-- Displays a popup window, returns handle
//
// This routine displays a popup window.  It can be called to create a fixed
// position window, or to automatically position and size the window either
// vertically, horizontally, or both.  It also can create either fixed size
// or scrollable/moveable windows.
//
// Parameters:
//	urlstr		-- URL to show in window
//	name		-- Name for this window, can be used to control it
//	usecenter	-- String, not empty to auto-center/auto-size the window
//	showscroll	-- String, not empty to show scroll bars and allow sizing
//	left		-- Left position of window, zero to use centering
//	top			-- Top position of window.  zero to use centering
//	width		-- Width of window, zero to use near-fullsize
//	height		-- Height of window, zero to use near-fullsize
//	recycle		-- TRUE to use the last opened popup, if any
//
//	If usecenter is FALSE, then the top, left, width & height values are used
// as is.  If usecenter is TRUE, then the top, left, width & height values are
// used or calculated as needed.
//
//	Setting top or left to 0 with usecenter TRUE will let the code decide the
// actual position of top or left.  Setting width or height to 0 with usecenter
// TRUE will let the code decide the actual size of the window.
//
function genpopup( urlstr, name, usecenter, showscroll, left, top, width, height, recycle ) {
	var debug	= false;
	var retval	= -1;

		// Trace parameters

	if ( debug ) {
		alert( "In genpopup:\n" +
				"urlstr = [" + urlstr + "]\n" +
				"name = [" + name + "]\n" +
				"usecenter = [" + usecenter + "]\n" +
				"showscroll = [" + showscroll + "]\n" +
				"top = [" + top + "]\n" +
				"left = [" + left + "]\n" +
				"width = [" + width + "]\n" +
				"height = [" + height + "]\n" +
				"recycle = [" + recycle + "]\n" );
	}


	if ( recycle == undefined ) {
		recycle = true;
	}

		// Calculate size and positions if needed

	if ( usecenter ) {

		var RIGHTOFFSET		= 10;
		var BOTTOMOFFSET	= 25;
		var vertmargin		= 20;
		var vertoffset		= vertmargin / 2;
		var horizmargin		= 20;
		var horizoffset		= horizmargin / 2;

			// Check for auto size & position

		if ( top == 0 ) {
			if ( height == 0 ) {
				top		= vertoffset;
				height	= window.screen.availHeight - vertmargin - BOTTOMOFFSET;
			} else {
				top		= ( window.screen.availHeight - height ) / 2;
			}
		} else if ( height == 0 ) {
			height	= window.screen.availHeight - ( top * 2 ) - BOTTOMOFFSET;
		}

		if ( left == 0 ) {
			if ( width == 0 ) {
				left	= horizoffset;
				width	= window.screen.availWidth - horizmargin - RIGHTOFFSET;
			} else {
				left	= ( window.screen.availWidth - width ) / 2;
			}
		} else if ( width == 0 ) {
			width	= window.screen.availWidth - ( left * 2 ) - RIGHTOFFSET;
		}
	}

		// Set the properties string to open with

	var settingsstr	= "top=" + top + "," +
						"left=" + left + "," +
						"height=" + height + "," +
						"width=" + width + "," +
						"toolbar=no," +
						"location=no," +
						"directories=no," +
						"status=no," +
						"menubar=no,";

	if ( showscroll ) {
		settingsstr += "scrollbars=yes," +
						"resizable=yes";
	} else {
		settingsstr += "scrollbars=no," +
						"resizable=yes";
	}

	if ( debug ) {
		alert( "window.screen.availWidth = [" + window.screen.availWidth + "]\n" +
			"window.screen.availHeight = [" + window.screen.availHeight + "]" );
		alert( "settingsstr = [" + settingsstr + "]" );
	}

	if ( recycle ) {
		var oldwin	= window.open( "", name );
		if ( oldwin ) {
			oldwin.close();
		}
	}
	retval	= window.open( urlstr, name, settingsstr );

	if ( debug ) {
		alert( "retval = [" + retval + "]" );
		alert( "Done genpopup" );
	}

	return retval;
}


//
// sendPOSTRequest -- uses the XMLHttpRequest object to send a POST request to
// the server.  Does not wait for a response.
//
function sendPOSTRequest( url, fieldstr ) {
	var debug	= false;

	if ( debug ) {
		alert( "In sendPOSTRequest:\n" +
				"url = [" + url + "]\n" +
				"fieldstr = [" + fieldstr + "]\n" +
				"" );
	}


    	// branch for native XMLHttpRequest object

	var req;
	var hasAjax	= false;
    if ( window.XMLHttpRequest ) {
		req		= new XMLHttpRequest();
		hasAjax	= true;
    	// branch for IE/Windows ActiveX version

    } else if ( window.ActiveXObject ) {
		try {
			req		= new ActiveXObject("Msxml2.XMLHTTP");
			hasAjax	= true;
		} catch ( e ) {
			try {
				req		= new ActiveXObject("Microsoft.XMLHTTP");
				hasAjax	= true;
			} catch ( e ) {
			}
		}
	}

	if ( debug ) {
		alert( "hasAjax = [" + hasAjax + "]" );
	}

	if ( hasAjax ) {
		req.open( "POST", url, true );
		req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
		req.setRequestHeader( "Content-length", fieldstr.length );
		req.setRequestHeader( "Connection", "close" );
		req.send( fieldstr );
	}

	if ( debug ) {
		alert( "Done sendPOSTRequest" );
	}

}

if ( debug ) {
	alert( "Done main.js" );
}

