var buBookWindowHandle = -1;

function launchBook(bi, rw, rh) 
{
    launchBookInternal(bi, rw, rh, 'n', 'dm');
}

function launchBookMode(bi, rw, rh, fa) 
{
    launchBookInternal(bi, rw, rh, ad, 'dm');
}

function launchBookAd(bi, rw, rh, ad) 
{
    launchBookInternal(bi, rw, rh, ad, 'dm');
}

function launchUserBook(bi, rw, rh) 
{
	rh += 30;
	w = rw;
    h = rh;
	left_screen = (screen.width - w) / 2;
    top_screen = (screen.height - h) / 2;
	var url = '/createkidsbooks/read/' + bi;
	
	window.location = url
//	buBookWindowHandle = window.open(url, '_blank', 'fullscreen=no,status=0,top='+top_screen+',left='+left_screen+',width='+rw+',height='+rh+',toolbar=0,resizable=0,scrollbars=0');
//    buBookWindowHandle.focus();
}


function launchBookInternal(bi, rw, rh, ad, fa) 
{
    var location = '/readkidsbooks/read'
    location = location 
             + "/" 
             + bi
             + "/" 
             + fa;
	window.location = location
}

function launchBookMaxView(bi, rw, rh, ad, fa) 
{
    var location = '/readkidsbooks/maxview'
    location = location 
             + "/" 
             + bi
             + "/" 
             + fa;
	if(ad == 'other')
		location = fa
			+ "/"
			+ bi;
		
    var agent = navigator.userAgent.toLowerCase();
    var isMac = (agent.indexOf('mac os x') >= 0);
    var isFirefox = (agent.indexOf('firefox') >= 0);
    var isSafari = (isMac && agent.indexOf('safari') >= 0);

    var openwin_props;
    var openwin_left = 0;
    var openwin_top = 0;
    var s_comma = ',';
    var s_fullscreen = "fullscreen=yes";
    var s_status = 'status=0';
    var s_resizable = 'resizable=0';
    var s_toolbar = 'toolbar=0';
    var s_scrollbars = 'scrollbars=0';
    var s_height;
    var s_width;
    var s_top;
    var s_left;

    var fullscreenmode = document.bookreadmodeform && 
                         document.bookreadmodeform.fullscreen.checked;
    var w = screen.width;
    var h = screen.height;
    
    if (!document.bookreadmodeform || !fullscreenmode)
    {
        rw = rw; // extra for window borders
        rh = rh + 30; // extra for window borders
        while (rw  >= w ||
               rh >= h)
        {
            rw = rw * 0.9;
            rh = rh * 0.9;
        }

        w = rw;
        h = rh;
            
        openwin_left = (screen.width - w) / 2;
        openwin_top = (screen.height - h) / 2;
    
        s_height = 'height=' +  h;
        s_width = 'width=' +  w;
        if (!isMac)
    	    s_top = 'top=' +  openwin_top;
        else
	        s_top = 'top=' +  (openwin_top-5);
        s_left = 'left=' +  openwin_left
		s_fullscreen = "fullscreen=no";
    }
	else
	if (isMac)
	{
        openwin_left = (screen.width - w) / 2;
        openwin_top = (screen.height - h) / 2;
    
        s_height = 'height=' +  h;
        s_width = 'width=' +  w;
        s_top = 'top=' +  openwin_top;
        s_left = 'left=' +  openwin_left
		s_fullscreen = "fullscreen=no";
	}
	else
	if (isFirefox)
	{
        s_height = 'height=' +  screen.height;
        s_width = 'width=' +  screen.width;
        s_top = 'top=0';
        s_left = 'left=0';
	}
    
    openwin_props = s_fullscreen;
    if (s_status)
        openwin_props = openwin_props
                      + s_comma
                      + s_status;
    if (s_top)
        openwin_props = openwin_props
                      + s_comma
                      + s_top;
    if (s_left)
        openwin_props = openwin_props
                      + s_comma
                      + s_left;
    if (s_width)
        openwin_props = openwin_props
                      + s_comma
                      + s_width;
    if (s_height)
        openwin_props = openwin_props
                      + s_comma
                      + s_height;
    if (s_toolbar)
        openwin_props = openwin_props
                      + s_comma
                      + s_toolbar;
    if (s_resizable)
        openwin_props = openwin_props
                      + s_comma
                      + s_resizable;
    if (s_scrollbars)
        openwin_props = openwin_props
                      + s_comma
                      + s_scrollbars;
    
    var openwin_target = "_blank"; // "com_biguniverse_bookreader_window";
    if (!isSafari && buBookWindowHandle != -1)
        buBookWindowHandle.close();     
    buBookWindowHandle = window.open(location, openwin_target, openwin_props);
    buBookWindowHandle.focus();
	if (buBookWindowHandle == null ) {
		alert('Unable to open a new browser window; are you using a pop-up blocker?');
	}
//	else
//		window.location="javascript:history.back()"
} 

function gotoSelectURL(urlprefix, list)
{
	if (list.selectedIndex == 0) 
	   return;
	   
	window.location = urlprefix + "/" + list.options[list.selectedIndex].value
}

function gotoInputURL(urlprefix, input)
{
	window.location = urlprefix + "/" + input.value
}

function launchEPublisher()
{
	launchEPublisherWithFile("")
}

function launchEPublisherWithFile(filename)
{
	var location = "/buauthor/app";
	if (filename && filename != "")
		location = location + "/bu" + filename
	var rw = 920;
	var rh = 760;

	window.location = location
}

function launchEPublisherFullScreen()
{
	launchEPublisherFullScreen("")
}

function launchEPublisherFullScreen(filename)
{
	if (!confirm("This will launch the Big Universe Author in a separate window.\n\nWARNING: Any unsaved work will be lost. Would you like to continue?"))
	    return;
		
	var openwin_target = "_blank"; // "com_biguniverse_buauthor_window";
	var rw = screen.width; 
	var rh = screen.height;
	var openwin_props;
	var openwin_left = 0;
	var openwin_top = 0;
	var s_comma = ',';
	var s_fullscreen = "fullscreen=yes";
	var s_status = 'status=0';
	var s_resizable = 'resizable=0';
	var s_toolbar = 'toolbar=0';
	var s_scrollbars = 'scrollbars=1';
	var s_height;
	var s_width;
	var s_top;
	var s_left;
	
	var w = screen.width;
	var h = screen.height;
	

	var agent = navigator.userAgent.toLowerCase();
	var isMac = (agent.indexOf('mac os x') >= 0);
	var isFirefox = (agent.indexOf('firefox') >= 0);
	var isSafari = (isMac && agent.indexOf('safari') >= 0);
	
//	var location = 'epublisher.html';
	var location = "/buauthor/fullscreen";
	if (filename && filename != "")
		location = location + "/" + filename
	
	openwin_left = (screen.width - w) / 2;
	openwin_top = (screen.height - h) / 2;
	
	if (!isMac)
		s_top = 'top=' +  openwin_top;
	else
	{
	    temp_top = openwin_top - 30;
	    if (temp_top < 0) temp_top = 0;
		s_top = 'top=' + temp_top;
	}
	s_left = 'left=' +  openwin_left;
	s_height = 'height=' +  h;
	s_width = 'width=' +  w;
	
	if (s_status)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_status;
	if (s_top)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_top;
	if (s_left)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_left;
	if (s_width)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_width;
	if (s_height)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_height;
	if (s_toolbar)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_toolbar;
	if (s_resizable)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_resizable;
	if (s_scrollbars)
	    openwin_props = openwin_props
	                  + s_comma
	                  + s_scrollbars;
    if (s_fullscreen)
        openwin_props = openwin_props
                      + s_comma
                      + s_fullscreen;
	
	buBookWindowHandle = window.open(location, openwin_target, openwin_props);
	buBookWindowHandle.focus();
	if ( buBookWindowHandle == null ) {
		alert('Unable to open a new browser window; are you using a pop-up blocker?');
	}
	else
		window.location="javascript:history.back()"
}		
