if (typeof dropit=="undefined"){var dropit=new Object();}
if (typeof dropit.popup=="undefined"){dropit.popup=new Object();}

dropit.popup.DEFAULT_FEATURES = "";
dropit.popup.OPENER_CSSCLASS = "";

dropit.popup.currentPopup = null;

/// Initializes all popuplinks
dropit.popup.Init = function() 
{
	var count = Dropit_Popup_Features.length;

	for( var i=0; i<count; i++ ) 
	{
		var o = Dropit_Popup_Features[i];
		var target		= o.target;
		var features	= o.features;

		if( o.inline )
			dropit.popup.util.linksAsInlinePopup(target,features);
		else
			dropit.popup.util.linksAsWindowPopup(target,features);
	}
}


dropit.popup.util =
{
	findParentLink: function(el)
	{
		if (el == null) return null;
		if ( el.tagName == "A" ) return el;
		return dropit.popup.util.findParentLink(el.parentNode);
	},
	
	linksAsWindowPopup: function (target,features) 
	{
		var callback = function(el) { xAddEventListener(el, "click", event_popup_features(features)); };
		xGetElementsByAttribute("a", "target", "^" + target + "$", callback);
	},
	
	linksAsInlinePopup: function (target,features)
	{
		var callback = function(el) { xAddEventListener(el, "click", event_iframe_features(features)); };
		xGetElementsByAttribute("a", "target", "^" + target + "$", callback);
	},
	
	getFeatureArray: function(features)
	{
		var a = new Array();
		var featureArray = features.split(",");
		for(var i=0; i<featureArray.length; i++)
		{
			var featurePair = featureArray[i].split("=");
			if(featurePair.length>1)
			{
				a[featurePair[0]] = featurePair[1];
			}
		}
		return a;
	},
	
	findPosition: function(el) 
	{
		if( el.offsetParent ) {
			for( var posX = 0, posY = 0; el.offsetParent; el = el.offsetParent ) {
				posX += el.offsetLeft;
				posY += el.offsetTop;
			}
			return [ posX, posY ];
		} else {
			return [ el.x, el.y ];
		}
	},
	
	getDocumentHeight: function()
	{
		if (document.body.clientHeight>document.documentElement.clientHeight)
			return document.body.clientHeight;
		else
			return document.documentElement.clientHeight;
	}
	
}

dropit.popup.eventHandlers =
{
	inlinePopupClick: function(opener,features)
	{
		var currentClick = dropit.popup.currentPopup && dropit.popup.currentPopup.opener == opener;
			
		dropit.popup.eventHandlers.closeCurrent();
		if ( !currentClick )
			dropit.popup.currentPopup = new dropit.popup.InlinePopup(opener,features);
	},
	
	windowPopupClick: function()
	{
	},

	closeCurrent: function (e) 
	{
		if ( dropit.popup.currentPopup )
		{
			dropit.popup.currentPopup.close();
			dropit.popup.currentPopup = null;
		}
	},

	windowScroll: function (e) 
	{
	},

	// To be run when popup window loads. Changes target in popup-window	
	popupWindowLoad: function(targetWindow)
	{
		var opener = window.name;
   		if ( opener.length <= 0 )
   		{
	   		window.name = "WindowOpener";
	   		opener = window.name;
	   	}

		if ( xDef(targetWindow, targetWindow.document, targetWindow.document.links) )
		{
			map(targetWindow.document.links, function(el) { if ( el.target.length <= 0 ) el.target = opener; });
		}
	},
	
	getEventTarget:	function(e)
	{
		if (e) { return e.target || window.event.srcElement; }
		return null;
	},

	getPopupObject: function (e) 
	{
		var el = dropit.popup.eventHandlers.getEventTarget(e);
		while (el != null && el.popup == null)
			el = el.parentNode;

		return el ? el.popup : null;
	}

}
InlinePopupCaller = function(href,features) {

    var opener = document.createElement("a");
    opener.href = href;
    opener.id = "poppis";
    
    document.body.appendChild(opener);
    
     dropit.popup.currentPopup = new dropit.popup.InlinePopup(opener, features);
}
dropit.popup.InlinePopup = function (o,f)
{
	this.opener = o;
	this.features = dropit.popup.util.getFeatureArray(f);

	appendClassName(this.opener,"inlinePopupOpener");

	// mask
	if ( this.features["modal"] == "yes" )
		this.displayMask();

	// holder
	this.holder = document.createElement("div");
	this.holder.popup = this;
	this.holder.className = "inlinePopupHolder";
	document.body.appendChild( this.holder );

	// container
	this.container = document.createElement("div");
	this.container.className = "inlinePopupContainer";
	if ( this.features["width"] )
		this.container.style.width = this.features["width"] +"px";
	this.holder.appendChild( this.container );
	
	// toolbar
	if ( this.features["toolbar"] == "yes" )
		this.createToolbar();

	//content & iframe
	var iframeId = this.opener.id +"-IFrame";
	var content = document.createElement("div");
	content.className = "inlinePopupContent";
	content.innerHTML = "<iframe src='"+ this.opener.href 
		+"' id='"+ iframeId
		+"' name='"+ this.opener.target 
		+"' scrolling='"+ this.features["scrollbars"] 
		+"' height='"+ this.features["height"] 
		+"' width='"+ this.features["width"] 
		+"' frameborder='0'></iframe>";

	this.container.appendChild(content);
	
	if ( this.features["left"] || this.features["top"] )
		this.positionAbsolute();
	else
		this.positionRelative();
		
	this.iframe = xGetElementById(iframeId);
	this.resetLinkTargets();
	
	
}

dropit.popup.InlinePopup.prototype =
{
	resetLinkTargets: function()
	{
		var win = this.iframe.contentWindow;
		var onload = function() { dropit.popup.eventHandlers.popupWindowLoad(win); };
		var loadEvent = function() { xAddEventListener(win, "load", onload); };

		// Neccesary to get this running		
		setTimeout(loadEvent, 1);
	},
	
	close: function()
	{
		this.holder.parentNode.removeChild(this.holder);
		this.holder = null;

		this.removeMask();

		removeClassName(this.opener,"inlinePopupOpener");
	},
	
	displayMask: function()
	{
		this.mask = document.createElement("div");
		this.mask.className = "inlinePopupMask";
		this.mask.style.height = dropit.popup.util.getDocumentHeight() +"px";
		document.body.appendChild(this.mask);
		
		appendClassName(document.body,"inlinePopupMasked");
	},
	
	removeMask: function()
	{
		if ( this.mask )
		{
			this.mask.parentNode.removeChild(this.mask);
			this.mask = null;
			
			removeClassName(document.body,"inlinePopupMasked");
		}
	},
	
	createToolbar: function()
	{
		var toolbar = document.createElement("div");
		toolbar.className = "inlinePopupToolbar";
	//	toolbar.innerHTML = this.opener.textContent || this.opener.innerText;
		this.container.appendChild( toolbar );

		// closebutton
		var closebutton = document.createElement("span");
		closebutton.popup = this;
		closebutton.className = "Close";
		closebutton.innerHTML = "<span>[x]</span>";
		closebutton.onclick = dropit.popup.eventHandlers.closeCurrent;
		toolbar.appendChild( closebutton );
	},
	
	positionAbsolute: function()
	{
		this.holder.style.position = "fixed";
		this.repositionXY();

		if(xIE4Up)
		{
			this.holder.style.position = "absolute";
			xAddEventListener(window, "scroll", function(e){this.repositionScroll();} );
		}
	},
	
	positionRelative: function()
	{
		var linkXY	= dropit.popup.util.findPosition(this.opener);
		var cw	= xClientWidth();
		var ch	= xClientHeight();
		var h	= xHeight(this.container);
		var w	= xWidth(this.container);

		this.holder.style.position = "absolute";

    this.holder.style.left = ((cw - w) / 2)+"px";
    this.holder.style.top = ((ch - h) / 2)+"px";
    //xTop( this.holder, (ch - h) / 2 );
    //xLeft( this.holder, (cw - w) / 2 );
//		alert( h + ', ' + w );
/*		
		if (linkXY[0]+w<cw)
			this.holder.style.left = linkXY[0]+"px";
		else
			this.holder.style.left = (cw-w-10)+"px";

		if (linkXY[1]-xScrollTop()<h)
			this.holder.style.top = (linkXY[1]+xHeight(this.opener))+"px";
		else
			this.holder.style.top = (linkXY[1]-h)+"px";
*/
	},
	
	repositionXY: function()
	{
		var l = this.features["left"];
		var t = this.features["top"];

		if (l) { this.holder.style.left = l + (isNaN(l) ? "": "px"); }
		if (t) { this.holder.style.top = t + (isNaN(t) ? "": "px"); }
	},
	
	repositionScroll: function()
	{
		this.container.style.top = xScrollTop()+"px";
		this.container.style.left = xScrollLeft()+"px";
	}	
}


// pops up a window containing url optionally named target, optionally having features
function raw_popup(url, target, features) {
   	// Set parent window name - for link target reference from popup
   	if ( window.name.length <= 0 )
	   	window.name = "WindowOpener";

	features = features || dropit.popup.DEFAULT_FEATURES;
	target   = target || "_blank";

	if (currentSite)
		url = addQueryString(url,"site",currentSite);
	url = addQueryString(url,"from", encodeURIComponent(window.top.location));

    var popupWindow = window.open(url, target, features);
    if ( popupWindow != null )
	    popupWindow.focus();
    return popupWindow;
}

function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) 
{
    link_popup( findParentLink(e.target || event.srcElement) );
    xPreventDefault(e);
}

function event_popup_features(features) 
{
    return function(e) { link_popup( dropit.popup.util.findParentLink(e.target || event.srcElement), features); xPreventDefault(e); };
}

function event_iframe_features(features)
{
    return function(e) 
    {
		var opener = dropit.popup.util.findParentLink(e.target || event.srcElement);
		if (opener)
		{
			xPreventDefault(e);
			dropit.popup.eventHandlers.inlinePopupClick(opener,features);
		}
	};
}
