var aMagicPopups = [];

function magicPopup( elem ) {
	
	this.oElement = elem;
	this.oObject = $(elem);
	this.isShown = false;
	
	this.init();
}

magicPopup.prototype = {
	
	init : function () {
		this.oObject.css('z-index', 1000 + aMagicPopups.length + 1);
		this.Id = this.oElement.id;
		this.oObject.find('.closer:first').bind('click', { oo : this }, function (e) { e.data.oo.close(); })
	},
		
	show : function( p1, p2 ) {
		var newCoords = [0,0];
		this.toMove = false;
		
		if (p1 && p2) {
			//coords
			newCoords = [p1, p2];
		} else if (p1) {
			//object
			var c = $(p1);
			if (c && c.offset()) {
				newCoords = [ c.offset().left, c.offset().top + 15 ];
			} else 
				return false;
			
		} else {
			//to center
			newCoords = [ $(window).width() / 2, $(document).height() / 2 ];
			this.toMove = true;
		}
		
		// get max coords
		var cMargin = 20;
		var docBounds = [ $(document).width(), $(document).height() ];
		var objBounds = [ this.oObject.width(), this.oObject.height() ];
		var finalX = Math.min( Math.max( newCoords[0], cMargin ), docBounds[0] - cMargin - objBounds[0] );
		var finalY = Math.min( Math.max( newCoords[1], cMargin ), docBounds[1] - cMargin - objBounds[1] );
		
//		this.oObject.css('display', 'block');
		this.oObject.css('left', finalX + 'px');
		this.oObject.css('top', finalY + 'px');
//		this.oObject.css('visibility', 'visible');
		var cbo = this.oObject;
//		this.oObject.fadeIn(100, function () { cbo.css('top', parseInt(cbo.css('top')) + 1 + 'px' ); });
		if (this.toMove) {
//			alert(this.oObject.width()/2)
			
		}
		this.oObject.fadeIn(this.toMove ? 500 : 100, this.toMove ? function () {cbo.animate({ 'marginLeft' : '-' + (cbo.width()/2) + 'px', 'marginTop' : '-' + (cbo.height()/2) + 'px' });} : null);
		
		this.isShown = true;
//		this.oObject.css('top', parseInt(this.oObject.css('top')) + 1 + 'px');
//		document.getElementsByTagName('body')[0].className += ' ';
	},
	
	close : function () {
		if (this.isShown) {
			this.oObject.fadeOut(100);
			this.isShown = false;
			this.toMove = false;
		}
//		this.oObject.css('top', parseInt(this.oObject.css('top')) + 1 + 'px');
//		this.oObject.css('display', 'none');
//		this.oObject.css('visibility', 'hidden');
	}
	
}

function getMagicPopup(pname) {
	if (aMagicPopups.length) {
		for (var i = 0; i < aMagicPopups.length; i++) {
			if (aMagicPopups[i].Id == pname)
				return aMagicPopups[i];
		}
	}
	return false;
}

function showMagicPopup(popupName, p1, p2) {
	var cp = getMagicPopup(popupName);
	if (cp) {
		if (cp.isShown)
			cp.close();
		else {
			cp.show(p1,p2);
			onMagicPopupShow(cp);
		}
	} else 
		alert(popupName);
}
function onMagicPopupShow(popup) {}
function closeAllMagic() {
	for (var i = 0; i < aMagicPopups.length; i++) {
		if (aMagicPopups[i].isShown)
			aMagicPopups[i].close();
	}
}

$(document).ready( function () { 
	$('.magicPopup').each( function () { 
		var cp = new magicPopup(this);
		aMagicPopups[aMagicPopups.length] = cp;
	 } );
	$(window).resize( function() {
		closeAllMagic();
	});
 } );