// SpryPopupDialog.js - version 0.1 - Spry Pre-Release 1.6.1
var Spry;
if (!Spry) Spry = {};
if (!Spry.Widget) Spry.Widget = {};

Spry.Widget.BrowserSniff = function(){
	var b = navigator.appName.toString();
	var up = navigator.platform.toString();
	var ua = navigator.userAgent.toString();
	this.mozilla = this.ie = this.opera = this.safari = false;
	var re_opera = /Opera.([0-9\.]*)/i;
	var re_msie = /MSIE.([0-9\.]*)/i;
	var re_gecko = /gecko/i;
	var re_safari = /(applewebkit|safari)\/([\d\.]*)/i;
	var r = false;
	if ( (r = ua.match(re_opera))) {
		this.opera = true;
		this.version = parseFloat(r[1]);
	} else if ( (r = ua.match(re_msie))) {
		this.ie = true;
		this.version = parseFloat(r[1]);
	} else if ( (r = ua.match(re_safari))) {
		this.safari = true;
		if(parseFloat(r[2]) < 420)
			this.version = 2;
		else
			this.version = 3;		
	} else if (ua.match(re_gecko)) {
		var re_gecko_version = /rv:\s*([0-9\.]+)/i;
		r = ua.match(re_gecko_version);
		this.mozilla = true;
		this.version = parseFloat(r[1]);
	}
	this.windows = this.mac = this.linux = false;

	this.Platform = ua.match(/windows/i) ? "windows" :
					(ua.match(/linux/i) ? "linux" :
					(ua.match(/mac/i) ? "mac" :
					ua.match(/unix/i)? "unix" : "unknown"));
	this[this.Platform] = true;
	this.v = this.version;

	if (this.safari && this.mac && this.mozilla) {
		this.mozilla = false;
	}
};
Spry.is = new Spry.Widget.BrowserSniff();
Spry.Widget.PopupDialog = function(popupdialog_element, rel, options){
	options = Spry.Widget.Utils.firstValid(options, {});
	this.init(popupdialog_element, rel, options);
};
Spry.Widget.PopupDialog.prototype.init = function(popupdialog_element, rel, options){
	var Utils = Spry.Widget.Utils;
	this.popupdialogElement = Utils.getElement(popupdialog_element);

	if (typeof this.popupdialogElement == 'undefined' || !this.popupdialogElement){
		this.showError('The element "' + popupdialog_element + '" do not exists in the page');
		return false;
	}
	this.useEffect = false;
	this.shieldListenersAttached = false;
	this.bodySizeListenersAttached = false;
	Utils.setOptions(this, options);
	if(typeof this.focusElement != 'undefined' && this.focusElement.length > 0){
		if(this.focusElement.indexOf("#")==0){
			this.focusElement = this.focusElement.substring(1);
		}
	}
	this.animator = null;
	
	if (this.useEffect){
			switch (this.useEffect.toString().toLowerCase()){
				case 'fade': this.useEffect = 'Fade'; break;
				default:
					this.useEffect = false;
			}
	}
	this.visiblePopupDialog = false;
	this.popupdialogElement.style.position = 'absolute';
	this.popupdialogElement.style.top = '0px';
	this.popupdialogElement.style.left = '-10000px';
	this.popupdialogElement.style.zindex = "500";
	this.popupdialogElement.style.height = "100%";
	this.popupdialogElement.style.width = "100%";
	//add table for consistent vertical alignment
	this.popupBox=this.popupdialogElement.childNodes[0];
	this.lTable = document.createElement("table");
	this.lTable.border="0";
	this.lTable.align="center";
	this.lTable.cellPadding="0";
	this.lTable.cellSpacing="0";
	this.lTable.width="100%";
	var lRow = this.lTable.insertRow(0);
	var lCell =lRow.insertCell(0);
	lCell.align="center";
	lCell.appendChild(this.popupdialogElement.childNodes[0]);
	this.popupdialogElement.appendChild(this.lTable);
	var tempDiv=document.createElement("div");
	lCell.appendChild(tempDiv);
	lCell.replaceChild(this.popupdialogElement.childNodes[0],tempDiv);
	this.lTable.style.height="100%";
	//recalculate visible area to re-center box
	if(Spry.is.ie && (Spry.is.version == '6')){
		this.lTable.style.height = screen.availHeight-180;
	}
	if(this.allowDrag){
		this.initDragPopUp();
		var box ="";
		var handle ="";
		this.popupbox_elements=this.lTable.rows[0].cells[0].getElementsByTagName('*');
		for (var i=0; i < this.popupbox_elements.length; i++){
			 if(this.popupbox_elements[i].className=="popupBox"){
				box = this.popupbox_elements[i];
			 }
			if(this.popupbox_elements[i].className=="popupBar"){
				handle = this.popupbox_elements[i];
				handle.style.cursor="move";
			 }
		}
		if(box!="" && handle!=""){
			MoveEngine.init(handle, box);
		}
	}
};
Spry.Widget.PopupDialog.addLoadListener = function(handler){
	if (typeof window.addEventListener != 'undefined')
		window.addEventListener('load', handler, false);
	else if (typeof document.addEventListener != 'undefined')
		document.addEventListener('load', handler, false);
	else if (typeof window.attachEvent != 'undefined')
		window.attachEvent('onload', handler);
};
Spry.Widget.PopupDialog.prototype.showPopupDialog = function(){
	if (!this.visiblePopupDialog){
		var bodyElement = (Spry.is.safari) ? document.body : document.documentElement;
		this.popupdialogElement.style.visibility = 'hidden';
		this.popupdialogElement.style.left = '0';
		this.popupdialogElement.style.zIndex = '9999';
		this.popupdialogElement.style.display = 'block';
		this.popupdialogElement.style.top = bodyElement.scrollTop+"px";
		
		var ev = Spry.Widget.Utils.addEventListener;
		var self = this;
		if(!this.modal){
			if(!this.shieldListenersAttached){
				this.popupbox_elements=this.lTable.rows[0].cells[0].getElementsByTagName('*');
				for (var l=0; l < this.popupbox_elements.length; l++){
					ev(this.popupbox_elements[l], 'click', function(e) {
						 if(Spry.is.ie){
							 var e = window.event;
							 e.cancelBubble = true;
							 e.returnValue = false;
						 }
						 if (e.stopPropagation) {
							 e.stopPropagation();
							 e.preventDefault();
						}
					}, false);
				}
			ev(this.popupdialogElement, 'click', function(e) {self.displayPopupDialog(false);return true;}, false);
			this.shieldListenersAttached=true;
			}
		}
	
		if(!this.bodySizeListenersAttached){
			ev(window, 'resize', function(e) {self.sizeShieldLayer();return true;}, false);
			this.bodySizeListenersAttached=true;
		}
		if(!this.allowScroll){
			document.body.parentNode.style.overflow="hidden";
			self.sizeShieldLayer();
		}else{
			document.body.parentNode.style.overflow="";
			ev(window, 'scroll', function(e) {self.sizeShieldLayer();if(bodyElement.scrollHeight<=self.shieldLayer.offsetHeight+10){self.popupdialogElement.style.top = bodyElement.scrollTop +"px"; return true;}}, false);
		}
		if(Spry.is.ie && Spry.is.version == '6')
			this.createIframeLayer(this.popupdialogElement);
			
		if (!this.visiblePopupDialog){
			if (this.useEffect){
				if (typeof this.showEffect == 'undefined')
					this.showEffect = new Spry.Widget.PopupDialog[this.useEffect](this.popupdialogElement, {from: 0, to: 1});
	
				this.showEffect.start();
			}
			else
			this.useEffect
				this.popupdialogElement.style.visibility = 'visible';
				if(typeof(this.focusElement)!='undefined' && this.focusElement!="" && document.getElementById(this.focusElement)!=null)
					document.getElementById(this.focusElement).focus();
	
		}
		this.visiblePopupDialog = true;
	}
};
Spry.Widget.PopupDialog.prototype.hidePopupDialog = function(quick){
	if (this.useEffect && !quick){
			if (typeof this.hideEffect == 'undefined')
				this.hideEffect = new Spry.Widget.PopupDialog[this.useEffect](this.popupdialogElement, {from: 1, to: 0});

			this.hideEffect.start();
	}else{
		if (typeof this.showEffect != 'undefined')
			this.showEffect.stop();
		this.popupdialogElement.style.visibility = 'hidden';
		this.popupdialogElement.style.left="-10000px";
	}
	if(Spry.is.ie && Spry.is.version == '6')
		this.removeIframeLayer(this.popupdialogElement);
	if(this.shieldExists){	
		this.removeShieldLayer(this.popupdialogElement);
	}
	this.visiblePopupDialog = false;
	document.body.parentNode.style.overflow="";
	if(Spry.is.safari){scrollBy(0, 1);scrollBy(0,-1);}
};
Spry.Widget.PopupDialog.prototype.displayPopupDialog = function(show) {
	if (this.popupdialogElement){
		if (show){
			this.createShieldLayer(this.popupdialogElement);
			this.shieldLayer.style.display = 'inline';
		
			if (this.hideTimer){
				clearInterval(this.hideTimer);
				delete(this.hideTimer);

			}
			var self = this;
			this.showTimer = setTimeout(function(){self.showPopupDialog()}, 0);
		}else{
			if (this.showTimer){
				clearInterval(this.showTimer);
				delete(this.showTimer);
			}
			var self = this;
			this.hideTimer = setTimeout(function(){self.hidePopupDialog();}, 0);
		}
	}
};
Spry.Widget.PopupDialog.prototype.destroy = function(){
	for (var k in this)
	{
		try{
				if (typeof this.k == 'object' && typeof this.k.destroy == 'function') this.k.destroy();
				delete this.k;
			}catch(err){}
	}
};
Spry.Widget.PopupDialog.prototype.checkDestroyed = function(){
	if (!this.popupdialogElement || this.popupdialogElement.parentNode == null)
		return true;

	return false;
};
Spry.Widget.PopupDialog.prototype.createIframeLayer = function(popupdialog){
	if (typeof this.iframeLayer == 'undefined'){
		var layer = document.createElement('iframe');
		layer.tabIndex = '-1';
		layer.src = 'javascript:"";';
		layer.scrolling = 'no';
		layer.frameBorder = '0';
		layer.className = 'iframePopupDialog';
		popupdialog.parentNode.appendChild(layer);
		this.iframeLayer = layer;
	}
	this.iframeLayer.style.left = popupdialog.offsetLeft + 'px';
	this.iframeLayer.style.top = popupdialog.offsetTop + 'px';
	this.iframeLayer.style.width = popupdialog.offsetWidth + 'px';
	this.iframeLayer.style.height = popupdialog.offsetHeight + 'px';
	this.iframeLayer.style.display = 'block';
};
Spry.Widget.PopupDialog.prototype.removeIframeLayer =  function(popupdialog){
	if (this.iframeLayer)
		this.iframeLayer.style.display = 'none';
};
Spry.Widget.PopupDialog.prototype.createShieldLayer = function(popupdialog){
	this.shieldLayer = document.createElement('div');
	this.shieldLayer.tabIndex = '-1';
	this.shieldLayer.className = 'shieldPopupDialog';
	this.shieldLayer.innerHTML="&nbsp;";
	this.shieldLayer.style.left = '0px';
	this.shieldLayer.style.top = '0px';
	this.shieldLayer.style.position = 'absolute';
	this.shieldLayer.style.zindex = '499';
	this.shieldExists=true;
	popupdialog.parentNode.appendChild(this.shieldLayer);
	this.shieldLayer.style.display = 'inline';
	this.sizeShieldLayer();
	this.shieldLayer.style.display = 'none';
	shieldLayer = this.shieldLayer;
};
Spry.Widget.PopupDialog.prototype.removeShieldLayer =  function(popupdialog){
	if (this.shieldLayer){
	  popupdialog.parentNode.removeChild(this.shieldLayer);
	  this.shieldExists=false;
	}
};
Spry.Widget.PopupDialog.prototype.sizeShieldLayer = function(){
	var bodyElement = (Spry.is.safari) ? document.body : document.documentElement;
	if(Spry.is.ie && Spry.is.version == '6'){
		this.shieldLayer.style.width = Number(bodyElement.scrollWidth)+"px";
		this.shieldLayer.style.height = Number(bodyElement.offsetHeight)+"px";
	}else{
		this.shieldLayer.style.width = '100%';
		this.shieldLayer.style.height = '100%';

	}
	if(bodyElement.scrollHeight>this.shieldLayer.offsetHeight){
		this.shieldLayer.style.height = Number(bodyElement.scrollHeight)+"px";
	}
}
Spry.Widget.PopupDialog.prototype.initDragPopUp = function(){
	var popupdialogElement=this.popupdialogElement;
	MoveEngine = {
	obj : null,
	init : function(o, oBox, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper){
		o.onmousedown	= MoveEngine.start;
		o.hmode	= bSwapHorzRef ? false : true ;
		o.vmode	= bSwapVertRef ? false : true ;
		o.box = oBox && oBox != null ? oBox : o ;
		if (o.hmode  && isNaN(parseInt(o.box.style.left  ))) o.box.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.box.style.top   ))) o.box.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.box.style.right ))) o.box.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.box.style.bottom))) o.box.style.bottom = "0px";
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
		o.box.onDragStart	= new Function();
		o.box.onDragEnd	= new Function();
		o.box.onDrag		= new Function();
	this.initTop=null;
	this.initLeft=null;
	},
	start : function(e){
		var o = MoveEngine.obj = this;
		if(this.initLeft == null){
			this.initLeft=o.box.offsetLeft;
		}
		if(this.initTop == null){
			this.initTop=o.box.offsetTop;
			if(Spry.is.ie && (Spry.is.version == '6' || Spry.is.version == '7')){
				this.initTop=this.initTop/2;
			}
		}	
		o.minX	= this.initLeft*-1;
		o.maxX	= screen.availWidth-o.box.offsetWidth-34-this.initLeft;
		o.minY=(this.initTop)*-1;
		o.maxY	= popupdialogElement.offsetHeight-o.box.offsetHeight-this.initTop-20;
		e = MoveEngine.fixE(e);
		var y = parseInt(o.vmode ? o.box.style.top  : o.box.style.bottom);
		var x = parseInt(o.hmode ? o.box.style.left : o.box.style.right );
		o.box.onDragStart(x, y);
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}
		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		document.onmousemove = MoveEngine.drag;
		document.onmouseup = MoveEngine.end;
		return false;
	},
	drag : function(e){
		e = MoveEngine.fixE(e);
		var o = MoveEngine.obj;
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.box.style.top  : o.box.style.bottom);
		var x = parseInt(o.hmode ? o.box.style.left : o.box.style.right );
		var nx, ny;
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)
		
		o.box.style[o.hmode ? "left" : "right"] = nx + "px";
		o.box.style[o.vmode ? "top" : "bottom"] = ny + "px";
		o.lastMouseX	= ex;
		o.lastMouseY	= ey;
		o.box.onDrag(nx, ny);
		return false;
	},
	end : function(){
		document.onmousemove = null;
		document.onmouseup   = null;
		MoveEngine.obj.box.onDragEnd(	parseInt(MoveEngine.obj.box.style[MoveEngine.obj.hmode ? "left" : "right"]), 
									parseInt(MoveEngine.obj.box.style[MoveEngine.obj.vmode ? "top" : "bottom"]));
		MoveEngine.obj = null;
	},
	fixE : function(e){
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

	
}

Spry.Widget.PopupDialog.prototype.showError = function(msg){
	alert('Spry.Widget.PopupDialog ERR: ' + msg);
};


Spry.Widget.PopupDialog.Animator = function(element, opts){
	this.timer = null;
	this.fps = 60;
	this.duration = 500;
	this.startTime = 0;
	this.transition = Spry.Widget.PopupDialog.Animator.defaultTransition;
	this.onComplete = null;
	if (typeof element == 'undefined') return;
	this.element = Spry.Widget.Utils.getElement(element);
	Spry.Widget.Utils.setOptions(this, opts, true);
	this.interval = this.duration / this.fps;
};
Spry.Widget.PopupDialog.Animator.defaultTransition = function(time, begin, finish, duration) { time /= duration; return begin + ((2 - time) * time * finish); };

Spry.Widget.PopupDialog.Animator.prototype.start = function(){
	var self = this;
	this.startTime = (new Date).getTime();
	this.beforeStart();
	this.timer = setInterval(function() { self.stepAnimation(); }, this.interval);
};
Spry.Widget.PopupDialog.Animator.prototype.stop = function(){
	if (this.timer)
		clearTimeout(this.timer);

	this.timer = null;
};
Spry.Widget.PopupDialog.Animator.prototype.stepAnimation = function(){};
Spry.Widget.PopupDialog.Animator.prototype.beforeStart = function(){};
Spry.Widget.PopupDialog.Animator.prototype.destroy = function(){
	for (var k in this)
		try
		{
			delete this.k;
		}catch(err){}
};
Spry.Widget.PopupDialog.Fade = function(element, opts){
	Spry.Widget.PopupDialog.Animator.call(this, element, opts);
	if (Spry.is.ie)
		this.origOpacity = this.element.style.filter;
	else
		this.origOpacity = this.element.style.opacity;
};
Spry.Widget.PopupDialog.Fade.prototype = new Spry.Widget.PopupDialog.Animator();
Spry.Widget.PopupDialog.Fade.prototype.constructor = Spry.Widget.PopupDialog.Fade;
Spry.Widget.PopupDialog.Fade.prototype.stepAnimation = function(){
	var curTime = (new Date).getTime();
	var elapsedTime = curTime - this.startTime;
	var i, obj;
	if (elapsedTime >= this.duration)
	{
		this.beforeStop();
		this.stop();
		return;
	}
	var ht = this.transition(elapsedTime, this.from, this.to - this.from, this.duration);
	if (Spry.is.ie)
	{
		var filter = this.element.style.filter.replace(/alpha\s*\(\s*opacity\s*=\s*[0-9\.]{1,3}\)/, '');
		this.element.style.filter = filter + 'alpha(opacity=' + parseInt(ht * 100, 10) + ')';
	}
	else
	{
		this.element.style.opacity = ht;
	}
	this.element.style.visibility = 'visible';
	this.element.style.display = 'block';
};
Spry.Widget.PopupDialog.Fade.prototype.beforeStop = function(){
	if (this.from > this.to)
		this.element.style.display = 'none';

	if (Spry.is.mozilla)
		this.element.style.filter = this.origOpacity;
	else
		this.element.style.opacity = this.origOpacity;
};
if (!Spry.Widget.Utils)	Spry.Widget.Utils = {};
Spry.Widget.Utils.setOptions = function(obj, optionsObj, ignoreUndefinedProps){
	if (!optionsObj)
		return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};
Spry.Widget.Utils.getElement = function(ele){
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
};
Spry.Widget.Utils.firstValid = function(){
	var ret = null;
	var a = Spry.Widget.Utils.firstValid;
	for(var i=0; i< a.arguments.length; i++)
	{
		if (typeof(a.arguments[i]) != 'undefined')
		{
			ret = a.arguments[i];
			break;
		}
	}
	return ret;
};
Spry.Widget.Utils.addEventListener = function(element, eventType, handler, capture){
	try{
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};
