//
// openPopUps v1.01 Copyright (c) 2006 openWebWare.com
// This copyright notice MUST stay intact for use.
//
// The perfect pop up window replacement for your advertisements and web 
// applications. Pop up blockers will fail to prevent an openPopUp window 
// from opening. 
//
// This library is free software; you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation; either version 2.1 of the License, or 
// (at your option) any later version.
//
// This library 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. See the GNU Lesser General Public 
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along 
// with this library; if not, write to the Free Software Foundation, Inc., 59 
// Temple Place, Suite 330, Boston, MA 02111-1307 USA 

// CSS Diretory
cssDir = "include/";

// Images Directory
imageDir = "images/popup/";

newswindow = null;

// Include Style Sheet
document.write('<link rel="stylesheet" type="text/css" href="' +cssDir+ 'popupstyles.css">\n');

/* ---------------------------------------------------------------------- *\
  Function    : JSWindow()
  Description : creates a new window
  Usage       : JSWindow("title", oContent, [x], [y], [id], [width])
  Arguments   : title    - The title of the window as displayed in the titlebar
	              oContent - The content of the Div called using getElementbyID
								x        - The number of pixels from the left the window appears when first open
								y        - The number of pixels from the top the window appears when first open
	              id       - The <div> this window will use for its content
                width    - The default width of the window                
\* ---------------------------------------------------------------------- */

function JSWindow(title, oContent, x, y, id, width, popupColor, minimizeIcon)
{
	
	// define variables
	this.title = title;
	
	//i don't know why but somehow a nbsp is added to the beginning.
	if( this.title.substr( 0, 6 ) == "&nbsp;" ) this.title = this.title.substr(6);
	
	this.oContent = oContent;
	this.width = width;
	this.x = x;
	this.y = y;
	this.id = id;
	this.bgcolor = popupColor;
	
	
	// create the table window and define CSS properties
	
	this.oTable = document.createElement("table");
	this.oTable.id = "Window " + this.id;
	this.oTable.style.width = this.width + "px";
//	this.oTable.style.border = "1px solid #000000";
	this.oTable.style.border = "0";
	this.oTable.cellSpacing = 0;
	this.oTable.cellPadding = 2;
	this.oTable.border = 0;
	this.oTable.style.backgroundColor = "#FFFFFF";

	// determine the windows position when first open
	this.oTable.style.position = "absolute";
	this.oTable.style.left = this.x + "px";
	this.oTable.style.top = this.y + "px";

	// link from the table to the JSWindow object
	this.oTable.jsWindow = this;

	// if the table is clicked anywhere, show the table in front of other open windows
	this.oTable.onmousedown = JSWindow.prototype.onBringToFront;

	// append to document body
	document.body.appendChild(this.oTable);

	// add a row for the titlebar
	var oTR = this.oTable.insertRow(0);
	oTR.className = "JSWindowTitleStyle";
	
	
	
/*	
	// Title: add the title to the titlebar	
	oTD = oTR.insertCell(0);
	//oTD.innerHTML = '<img src="images/home/wango-news-title.gif"/>';//title;
	oTD.jsWindow = this;
	oTD.onmousedown = JSWindow.prototype.tdOnMouseDown;
	//oTD.style.borderBottom = "1px solid #2586BA";
	//oTD.style.backgroundImage = "url(" + imageDir + "title_bg.jpg)";
	oTD.height = "20";
	oTD.width = "98%";
	
	// Minimize: add the minimize button to the titlebar
	if (minimizeIcon == 1) {
    this.oMinTD = oTR.insertCell(1);
	 // this.oMinTD.innerHTML = "<img src='" + imageDir + "minimize.jpg'>";
	  //this.oMinTD.onmousedown = JSWindow.prototype.onMinimize;
	  this.oMinTD.jsWindow = this;
   // this.oMinTD.style.borderBottom = "1px solid #2586BA";
	  //this.oMinTD.style.backgroundImage = "url(" + imageDir + "title_bg.jpg)";
	  this.oMinTD.width = "1%";
	  this.oMinTD.style.paddingRight = "1px";	
  }
	
	// Close: add the close button to the titlebar
	if (minimizeIcon == 1) {
	  oTD = oTR.insertCell(2);
	}
	else {
	  oTD = oTR.insertCell(1); 
	}
	//oTD.innerHTML = "<img src='" + imageDir + "close.jpg'>";
	oTD.jsWindow = this;
	oTD.onmousedown = JSWindow.prototype.onClose;
	//oTD.style.borderBottom = "1px solid #2586BA";
	//oTD.style.backgroundImage = "url(" + imageDir + "title_bg.jpg)";
  oTD.width = "1%";
	oTD.style.paddingLeft = "1px";
	*/
	// add a row for the window's content
	oTR = this.oTable.insertRow(1);
	
	this.oContentTD = oTR.insertCell(0);
	if (minimizeIcon == 1) {
	  this.oContentTD.colSpan = 3;
	}
	else {
	  this.oContentTD.colSpan = 2;
	}  
	this.oContentTD.style.backgroundColor = this.bgcolor;
	this.oContentTD.className = "JSWindowContentStyle";
 
 	
	//this.oContentTD.innerHTML = '<table class="shadowx"  cellspacing="0" cellpadding="0" style="border: 2px solid #666666;padding: 8px;"><tr><td class="shadowx-topLeft"><img src="images/0.gif" width="1" height="1"/></td><td class="shadowx-top" colspan="2"><img src="images/0.gif" width="1" height="1"/></td><td class="shadowx-topRight"><img src="images/0.gif" width="1" height="1"/></td></tr><tr><td class="shadowx-left"></td><td class="shadowx-center" style="border-bottom: 1px solid #000000"><img src="'+(this.title)+'"/><td class="shadowx-center" align="right" style="border-bottom: 1px solid #000000"><a href="javascript:newswindow.close()"><img border="0" src="images/popup/close.jpg"/></a></td><td class="shadowx-right"></td></tr><tr><td class="shadowx-left"></td><td class="shadowx-center" colspan="2" style="padding-top: 8px;">' + document.getElementById("Div" + this.id).innerHTML + '</td><td class="shadowx-right"></td></tr><tr><td class="shadowx-bottomLeft"><img src="images/0.gif" width="1" height="1"/></td><td class="shadowx-bottom" colspan="2"><img src="images/0.gif" width="1" height="1"/></td><td class="shadowx-bottomRight"><img src="images/0.gif" width="1" height="1"/></td></tr></table>';
	
	
	this.oContentTD.innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td style="border: 2px solid #666666;padding: 8px;"><div ><div  style="border-bottom: 1px solid #000000"><a href="javascript:newswindow.close()"><img border="0" src="images/popup/close.jpg" style="float: right"/></a><img src="'+(this.title)+'"/><img src="images/0.gif" width="1" height="20"/></div>' + document.getElementById("Div" + this.id).innerHTML + '</div></td></tr></table>';
	//*/
	
	newswindow = this;
	
}

JSWindow.prototype.onBringToFront = function()
{
	this.jsWindow.bringToFront();
}

JSWindow.prototype.bringToFront = function()
{
	// if not already the last child of the document.body, make it so
	if ( document.body.childNodes[document.body.childNodes.length-1] !== this.oTable )
	{
		// move to bottom of document
		document.body.appendChild(this.oTable);
	}
}

JSWindow.prototype.tdOnMouseDown = function()
{
	this.jsWindow.onMouseDown();
}

JSWindow.prototype.onMouseDown = function()
{
	// record that an onmousedown has just occurred
	this.bDown = true;
	
	// link from body to this JSWindow object
	document.body.jsWindow = this;

	// save body mouse handlers
	this.saveMouseMove = document.body.onmousemove;
	this.saveMouseUp = document.body.onmouseup;

	// set new handlers.
	document.body.onmousemove = JSWindow.prototype.bodyOnMouseMove;
	document.body.onmouseup = JSWindow.prototype.bodyOnMouseUp;
}

JSWindow.prototype.bodyOnMouseMove = function(evt)
{
	var e = window.event ? window.event : evt;
	this.jsWindow.onMouseMove(e);
}

JSWindow.prototype.onMouseMove = function(evt)
{
	// if mouse not down, stop the move (for IE only)
	if ( (document.all) && !(evt.button & 1) )
	{
		this.onMouseUp();
		return;
	}
	if ( this.bDown )
	{
		this.dx = parseInt(this.oTable.style.left, 10) - evt.clientX;
		this.dy = parseInt(this.oTable.style.top, 10) - evt.clientY;
		this.bDown = false;
	}
	else
	{
		this.oTable.style.left = Math.max((this.dx + evt.clientX),0) + "px";
		this.oTable.style.top = Math.max((this.dy + evt.clientY),0) + "px";
	}
}

JSWindow.prototype.bodyOnMouseUp = function()
{
	this.jsWindow.onMouseUp();
}

JSWindow.prototype.onMouseUp = function()
{
	document.body.onmouseup = this.saveMouseUp;
	document.body.onmousemove = this.saveMouseMove;
	document.body.jsWindow = null;
}












JSWindow.prototype.close = function()
{	
	//document.getElementById( "overlay" ).style.display = "none";
	if( hideList )
	{
		var f;
		for( var i in hideList )
		{
			f = document.getElementById(hideList[i]);
			//f.style.width = hdiv[hideList [i]][0];
			//f.style.height = hdiv[hideList[i]][1];
			f.style.visibility = "visible";
		}
	}
	// remove from browser document
	this.oTable.parentNode.removeChild(this.oTable);
}

JSWindow.prototype.onClose = function()
{
	this.jsWindow.close();
}



/* ---------------------------------------------------------------------- *\
  Function    : hideDiv()
  Description : Hides all Divs on the page.
  Usage       : hideDiv([divNumber])
  Arguments   : divNumber - The number of Divs in the page that must be hidden
\* ---------------------------------------------------------------------- */
function hideDiv(divNumber) {
	for (var count = 1; count <= divNumber;) {
    document.getElementById('Div' + count).style.display = 'none';
		count++;
	}
}



/* ---------------------------------------------------------------------- *\
  Function    : createWindows()
  Description : determines if Window already exists, else calls JSWindow
  Usage       : createWindows([windowID], [width], "title")
  Arguments   : windowID - The <div> this window will use for its content
                width    - The default width of the window
                title    - The title of the window as displayed in the titlebar
\* ---------------------------------------------------------------------- */

function createWindow(title, width, popupColor, windowID, minimizeIcon, x, y)
{
if (document.getElementById('Window ' + windowID)) {
newswindow.close();

  }


  new JSWindow("&nbsp;" + title, document.getElementById("Div" + windowID), x, y, windowID, width, popupColor, minimizeIcon); 

}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


var popupTitle;

var hdiv  = new Object();
var div1;

function cpopUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=0,width=700,height=400,left = "+(screen.width/2-350)+",top ="+(screen.height/2-200)+"');");
}

function loadPopup( nurl, title )
{

	if( !title ) popupTitle = "images/home/wango-news-title.gif";
	else popupTitle = title;
	
	if( !document.getElementById  ) cpopUp( nurl );
	else
	{

		var nl = document.getElementById( "popuploader" );
		if( nl.innerHTML == "" || !nl.contentWindow ) cpopUp( nurl );
		else
		{
			if( !document.getElementById("Div1") )
			{
				div1 = document.createElement("div");
				div1.id = "Div1";
				div1.style.display = "none";
				document.body.appendChild( div1 );
			}
			nl.src = nurl+"&frame=1";
		}
	}
}

function onLoadPopup()
{
	var ndiv = document.getElementById( "Div1" );
	var nl = document.getElementById( "popuploader" );
	
	ndiv.innerHTML = nl.contentWindow.document.getElementById( "content" ).innerHTML;
	
	
	//hide flash

	if( hideList )
	{
		var f;
		for( var i in hideList )
		{
			f = document.getElementById(hideList[i]);
			hdiv[hideList[i]] = [f.style.width, f.style.height ];
			//f.style.width = "1px";
			//f.style.height = "1px";
			f.style.visibility = "hidden";
		}
	}
	
	createWindow( popupTitle, 720, '#ffffff', 1, 0, actualWidth/2-360, Math.ceil( f_scrollTop() + getHeight()/2 - 241 )  );
}
