// wsv-api-web /includes/popup.js

// -----------------------------------------------
// - This file contains popup window functions.
// -----------------------------------------------

// -----------------------------------------------
// This script closes ALL popups when called
// Only works when called from the creating page
// -----------------------------------------------
function closePopups()
{
 if (typeof win != 'undefined' && win != window && win && !win.closed) win.close();
}

// -----------------------------------------------
// This script closes ALL popups when called
// Only works when called from the parent
// -----------------------------------------------
function closeFullscreen()
{
  window.fullscreen.close();
}

//----------------------------------------------------
// Sample sites popup
//----------------------------------------------------
function popupSample(filename) {
	// Show Sample Web Site popup window based on features provided

	sample_window = window.open( filename, "sample_window", "left=120,top=100,width=750,height=560,resizable=1,scrollbars=1,toolbar=0,status=0,location=0,directories=0,menubar=0");
	sample_window.focus();
}

//----------------------------------------------------
// Create a new pop up window for a given URL and type
//----------------------------------------------------
function popUp(URL, type)
{
   if (arguments.length == 2)
   {
      switch(type)
      {
	 case "fullscreen":
	    width = screen.availWidth - 40;
	    height = screen.availHeight - 40;
	    break;

	 case "dwizard":
	    width = 770;
	    height = 580;
	    break;

	 case "imageeditor":
	    width = 800;
	    height = 605;
	    break;
   
	 case "700x500":
	    width = 700;
	    height = 500;
	    break;
	    
	case "delete":
	    width = 400;
	    height = 230;
	    break;

	 case "save":
	    width = 450;
	    height = 350; // 300;
	    break;

	 case "item":
	    width = 640;
	    height = 530; //570;
	    break;

	 case "thumbnail":
	    width = 640;
	    height = 530;
	    break;
	   
	 case "publishpopup":
	    width = 590;
	    height = 340;
	    break;

	 case "learnmore":
	    width = 325;
	    height = 340;
	    break;

	 case "supportcenter":
	    width = 672;
	    height = 586;
	    break;

	case "superstats":
	    width = 672;
	    height = 515; //586;
	    break;

	 case "block":
	    width = 700;
	    height = 520; // orig 620;
	    break;

	 case "template":
	    width = 750;
	    height = 575;
	    break;

	 case "banner":
	    width = 780;
	    height = 320;
	    break;

	 case "headline":
	    width = 765;
	    height = 430;
	    break;

	 case "previewPopup":
            width = 780;
            height = 580;
            break;

	 case "preview":
	    width = 780;
	    height = 580;
	    break;

	 case "hyperlink":
	    width = 500;
	    height = 330; //300
	    break;

	 case "image_preview":
	    width = 500;
	    height = 300;
	    break;

	 case "guestbook":
	    width = 675;
	    height = 500;
	    break;

	 case "forum":
	    width = 675;
	    height = 500;
            break;

	 case "tempsite_learnmore":
	    width = 700;
	    height = 460;
            break;
            
     case "save_tempsite":
        width = 800;
        height = 680;
            break;

	 case "chat":
	    width = 675;
	    height = 500;
            break;

	 case "sf_refresh":
	    width = 75;
	    height = 75;
            break;
	 
	 case "effects":
	    width = 550;
	    height = 475;
	    break;    

	 case "crop":
	    width = 550;
	    height = 575;
	    break; 
	    
	 case "seotips":
	    width = 470;
	    height = 340;
	    break; 

	 default:
	    width = 550;
	    height = 400;
	    break; 
      }
   }
   else
   {
      width = 550;
      height = 580;
      type = "generic";
   }

   var token = URL.indexOf('?') > -1 ? '&' : '?';
   uniqueId = genUniqueId();
   URL=URL+genUniqueParam(token);

   var leftPosition  = (screen.width - width - 20) / 2;
   var topPosition = (screen.height - height) / 4;

	if ((type == "supportcenter") || (type == "intuithome") || (type == "learnmore")||(type == "seotips")) {
   	eval("win = window.open(URL, '"+type+"', 'toolbar=0,scrollbars=1,location=0,status=0,resizable=1,menubar=0,width="+width+",height="+height+"');");
	} 
	else if(type=="previewPopup")
	{
		 var popupurl = "/Preview.jsp?"+URL;
		//open centered
   		eval("win = window.open(popupurl, '"+type+"', 'toolbar=0,scrollbars=1,location=0,status=0,resizable=1,menubar=0,width="+width+",height="+height+",left="+leftPosition+",top="+topPosition+"');");
	}
	else 
	{
		//open centered
   	eval("win = window.open(URL, '"+type+"', 'toolbar=0,scrollbars=1,location=0,status=0,resizable=1,menubar=0,width="+width+",height="+height+",left="+leftPosition+",top="+topPosition+"');");
	}

   if (parseInt(navigator.appVersion) >= 4)
   {
      win.window.focus();
   }
}

//-------------------------------------------------
// Popup for Printer Friendly page - With menus
//-------------------------------------------------

function printerPopup(link, winName)
{
if (! window.focus)return true;
var href;
if (typeof(link) == 'string')
   href=link;
else
   href=link.href;
window.open(href, winName, 'width=550, height=600, scrollbars=yes, toolbar=yes, menubar=yes, resizeable=yes, status=no');
return false;
}


//-----------------------------------------
// Open Window for Support Center popups
//------------------------------------------

function openWindow(theURL,winName,features)
{
    window.open(theURL,winName,features);
}


// ----------------------
// Generate Unique Id
// ----------------------
function genUniqueId()
{
   day = new Date();
   return day.getTime();
}

// -------------------------------------------------
// Generate Unique parameter to be appended to the URL
// This will allow us to realod the HTML from the server
// -------------------------------------------------
function genUniqueParam(parameter)
{
   return parameter+'unique_id='+genUniqueId();
}


// -------------------------------------------------
// Refresh the window that opened a frame or window with
// the location specified
// -------------------------------------------------
function refreshWindow(url, refresh_top)
{
    var opened_window = window;
    var window_to_refresh;

    // get the parent of this window
   while ( opened_window.parent != 'undefined' )
   {
       if ( opened_window.parent == opened_window )
            break;
        opened_window = opened_window.parent;
   }
    // it is possible for this window not to have an opener.  Ex. Ctrl-N on IE
    window_to_refresh = opened_window.opener;
    if ( ( typeof window_to_refresh == 'undefined' ) || ( window_to_refresh == null ) )
    {
       return;
    }

    // find the top for refreshing
    if (refresh_top != null && refresh_top != 'null' && refresh_top != '')
    {
        window_to_refresh = window_to_refresh.top;
    }
    else
    {
        // We want to keep moving up from the
        // current window until we get to
        // the se_content window that is being used
        // as the main window for the WSV editor
        var digDeeper = true;
        while (digDeeper)
        {

	    if ( ( typeof window_to_refresh.parent == 'undefined' ) || ( window_to_refresh.parent == null ) )
            {
                digDeeper = false;
            }
            else if (window_to_refresh.name == window_to_refresh.top.name)
            {
		// Uh-oh, we went to the top of the page...we want to now find the content frame
                window_to_refresh = window_to_refresh.frames["se_content"];
                digDeeper = false;
	    }
            else if (window_to_refresh.name == "se_content")
            {
                digDeeper = false;
	    }
            else
            {
                window_to_refresh = window_to_refresh.parent;
	    }
        }
    }
    // send the window to the new url
    window_to_refresh.location.href = url;
}

function refreshFrame(url, refresh_frame)
{
    var opened_window = window;
    var window_to_refresh;

    // get the parent of this window
   while ( opened_window.parent != 'undefined' )
   {
       if ( opened_window.parent == opened_window )
            break;
        opened_window = opened_window.parent;
   }
    // it is possible for this window not to have an opener.  Ex. Ctrl-N on IE
    window_to_refresh = opened_window.opener;
    if ( ( typeof window_to_refresh == 'undefined' ) || ( window_to_refresh == null ) )
    {
       return;
    }

    // find the frame for refreshing
    if (refresh_frame != null && refresh_frame != 'null' && refresh_frame != '')
    {
    	window_to_refresh = window_to_refresh.top;
        window_to_refresh = window_to_refresh.frames["se_content"];
        window_to_refresh.document.getElementById(refresh_frame).src=url;
    }   
}

function invalidateSession(url)
{
    var refreshWindow = window.top;
    while (refreshWindow.opener != null)
    {
       refreshWindow = refreshWindow.opener.top;
    }
    refreshWindow.location.href=url;
}

//<!-- END Javascript for Generic Pop-up -->


// ----------------------------------------------
// This function dynamically generates a chat 
// pop-up. (ASV)
// YUK: This method is also defined in
// wsv-api-app/htdocs/includes/js/user.js
// -----------------------------------------------
function viewChat(instance_id, url)
{
   props = 'toolbar=0,scrollbars=0,location=0,status=0,resizable=1,menubar=0,width=584,height=410';
   win = window.open('','Chat',props);
   win.document.writeln('<html>');
   win.document.writeln('<head><title>' + top.se_top.ICtxgo.pe_txt11 +'</title></head>');
   win.document.writeln('<body>');
   win.document.writeln('<applet codebase="'+url+'" archive="multichat.jar" CODE="GUIClient.class" width="562" height="385" align="BOTTOM">');
   win.document.writeln('<param name="CABBASE" value="multichat.cab">');
   win.document.writeln('<param name="roomid" value="'+instance_id+'">');
   win.document.writeln('</applet>');
   win.document.writeln('</body></html>');
}


// ----------------------------
// Position a popup window topleft, topright, centre, botleft, or botright
// ----------------------------

	// this function calls a popupWindow where
	// win is the page address eg '../page.htm'
	// winWidth is the required window width in pixels
	// winHeight is the required window height in pixels
	// place is where the window should be placed .. choose from topleft, topright, centre, botleft, botright
	// winName is the name of the new window
	
	function popupWindow(win,winWidth,winHeight,place,winName){
	
	leftPos = 0; topPos = 0; // default variables for old browsers

		if (screen){ 
		
		   // checks specified parameters are not outside screen area
		   
    		if (winWidth>screen.width){
    		   winWidth = screen.width;
    		}
    		
    		if (winHeight>screen.height){
    		   winHeight = screen.height - 55;
    		}
    		
    		if (winWidth <= 0){
    		   winWidth = screen.width;
    		}
    		
    		if (winHeight <= 0){
    		   winHeight = screen.height - 55;
    		}
    		
			// positions window by checking place parameter
					
			if (place == 'topleft'){ // positions window in top left corner
			leftPos = 0;
			topPos = 0;
			}
			
			if (place == 'centre'){ // centers window
			leftPos = screen.width/2 - winWidth/2;
			topPos = screen.height/2 - winHeight/2;
			}
			
			if (place == 'topright'){ // positions window in top right corner
			leftPos = screen.width - 15 - winWidth; // minus 15 to prevent edge of window being off screen
			topPos = 0;
			}
			
			if (place == 'botleft'){ // positions window in bottom left corner
			leftPos = 0;
			topPos = screen.height - (winHeight + 55); // add 55 to prevent bottom of window being obscured by taskbar
			}
			
			if (place == 'botright'){ // positions window in bottom right corner
			leftPos = screen.width - 15 - winWidth; // minus 15 to prevent edge of window being off screen
			topPos = screen.height - (winHeight+55); // add 55 to prevent bottom of window being obscured by taskbar
			}
					
		}
		
		newWindow = window.open(win,winName,'toolbar=no,location=no,scrollbars=yes,resizable=yes,width='+winWidth+',height='+winHeight+',left='+leftPos+',top='+topPos+'');
		newWindow.focus();
	}

   function atsPopup(mypage, myname, w, h, scroll)
   {
      var winl = (screen.width - w) / 2;
      var wint = (screen.height - h) / 2;
      winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',nonresizable'
      win = window.open(mypage, myname, winprops)
      if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   }


function popupIframe(url, isLegacy) {
	
	var iFrameContainer = document.getElementById('iFrameContainer') ? document.getElementById('iFrameContainer') : document.createElement('DIV');
	var iFrameObj = document.getElementById('iframeBox') ? document.getElementById('iframeBox') : document.createElement('IFRAME');
	var screen = document.getElementById('IC_bg_screen') ? document.getElementById('IC_bg_screen') : document.createElement('DIV');
	
	if(isLegacy) {	
		document.body.appendChild(screen);
		document.body.appendChild(iFrameContainer);
	}
	
	screen.id = 'IC_bg_screen';	
	iFrameContainer.id = 'iFrameContainer';
	iFrameObj.id = 'iframeBox';
	iFrameObj.style.backgroundColor = '#FFFFFF';
	
	iFrameContainer.appendChild(iFrameObj);
	iFrameObj.src=url;
	
	
	screen.style.display = 'block';
    iFrameContainer.style.display = 'block';
    iFrameObj.src=url;	
	
	
	screen.style.height = YAHOO.util.Dom.getClientHeight() + 'px';
    iFrameObj.style.width=YAHOO.util.Dom.getClientWidth() - 100 + 'px';
    iFrameObj.style.height=YAHOO.util.Dom.getClientHeight() - 100 + 'px';
    iFrameContainer.style.width = YAHOO.util.Dom.getViewportWidth() - 100 + 'px'
    iFrameContainer.style.height = YAHOO.util.Dom.getViewportHeight() - 100 + 'px'
    iFrameContainer.style.left = '50px';
    iFrameContainer.style.top = '50px';
	 
}

window.onresize = sizeIframe;

function sizeIframe() {	
	if(document.getElementById('IC_bg_screen')) {
		document.getElementById('IC_bg_screen').style.height = YAHOO.util.Dom.getClientHeight() + 'px';
	    document.getElementById('iframeBox').style.width=YAHOO.util.Dom.getClientWidth() - 100 + 'px';
	    document.getElementById('iframeBox').style.height=YAHOO.util.Dom.getClientHeight() - 100 + 'px';
	    document.getElementById('iFrameContainer').style.width = YAHOO.util.Dom.getViewportWidth() - 100 + 'px'
	    document.getElementById('iFrameContainer').style.height = YAHOO.util.Dom.getViewportHeight() - 100 + 'px'
	    document.getElementById('iFrameContainer').style.left = '50px';
	    document.getElementById('iFrameContainer').style.top = '50px';
	}
}

