(function($) {
	$.fn.nmg_popup_ajax = function(){
			return this.unbind('click').click(_init);
		//_init();
	};
	
	function _init(){
		
		
		$('#nmg-popup-ajax').remove();
		
		_interface("<div style='height:100px; width:200px; vertical-align:center;'><br /><br />&nbsp;&nbsp;&nbsp;Loading...&nbsp;&nbsp;&nbsp;</div>");
		
		$('#nmg-popup-ajax').remove();
		hrf = this.getAttribute('href');
rel = this.getAttribute('rel');

if(rel=='image')
{
_interface("<img src='"+hrf+"' />");
}
else
{

		//alert(hrf);
		$.ajax({
		  url: hrf,
		  success: function(data) {
			_interface(data);
		  },
		  error: function(data){
			_interface("<div style='height:100px; width:200px; vertical-align:center;'><br /><br />&nbsp;&nbsp;&nbsp;There&nbsp;was&nbsp;an&nbsp;error...&nbsp;&nbsp;&nbsp;</div>");	
		  }
		});		
}
		
		return false;
	};
	
	function _interface(html){
		overlay = "<div id='nmg-popup-ajax'><div id='nmg-popup-ajax-close' style='display:none; cursor:pointer; position: absolute; top:0; left:0; border:0px solid black; z-index: 110; text-align: center;'>&nbsp;close&nbsp;</div><div id='nmg-popup-ajax-content' style='display:none; position: absolute; top:0; left:0; border:1px solid black; z-index: 100; text-align: center;'></div><div id='nmg-popup-ajax-overlay' style='position: absolute; top: 0; left: 0; z-index: 90; width: 100%; height: 500px; background:black'></div></div>";
		
		
		arrPageSizes = _nmg_getPageSize();
		arrPageScroll = _nmg_getPageScroll();
		
		$('body').append(overlay);
		$('#nmg-popup-ajax-overlay').css({
			backgroundColor:	'black',
			opacity:			'0.8',
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).fadeIn(0);		
		
		
		$('#nmg-popup-ajax-content').html(html);
		t = arrPageScroll[1] + (arrPageSizes[3] / 10);
		l = (arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2);
		w = $("#nmg-popup-ajax-content").width();
		h = $("#nmg-popup-ajax-content").height();
		tclose = t - $('#nmg-popup-ajax-close').height();
		lclose = l + w - $('#nmg-popup-ajax-close').width();
		$('#nmg-popup-ajax-close').css({
			top:	tclose,
			left:	lclose,
			background: 'white'
		});		
		$('#nmg-popup-ajax-close').hide();

		$('#nmg-popup-ajax-content').html("");
		$('#nmg-popup-ajax-content').css({
			width: 0,
			height: 0,
			top:	t,
			left:	(arrPageSizes[0]/2),
			background: 'white'
		});				
		
		
		$('#nmg-popup-ajax-content').animate({width:w, height:h, left:l}, 500, function(){$('#nmg-popup-ajax-content').html(html); $('#nmg-popup-ajax-close').show();});		

		
		$('#nmg-popup-ajax-close').click(function(){
			_finish();
		});
		
		//$('#nmg-popup-ajax-overlay').click(function(){
		//	_finish();
		//});
		
		// If window was resized, calculate the new overlay dimensions
		$(window).resize(function() {
			// Get page sizes
			var arrPageSizes = _nmg_getPageSize();
			// Style overlay and show it
			$('#nmg-popup-ajax-overlay').css({
				width:		arrPageSizes[0],
				height:		arrPageSizes[1]
			});
			// Get page scroll
			var arrPageScroll = _nmg_getPageScroll();

			t = arrPageScroll[1] + (arrPageSizes[3] / 10);
			l = (arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2);
			w = $("#nmg-popup-ajax-content").width();
			h = $("#nmg-popup-ajax-content").height();
			tclose = t - $('#nmg-popup-ajax-close').height() -1;
			lclose = l + w - $('#nmg-popup-ajax-close').width() - 1;
			$('#nmg-popup-ajax-close').css({
				top:	tclose,
				left:	lclose,
				background: 'white'
			});				
			
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#nmg-popup-ajax-content').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	(arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2)
			});
		});
		
		return false;	
	}
	function _finish(){
		var arrPageSizes = _nmg_getPageSize();
		t = arrPageScroll[1] + (arrPageSizes[3] / 10);
		l = (arrPageSizes[0]/2);
		$('#nmg-popup-ajax-close').hide();
		$('#nmg-popup-ajax-content').html("");
		$('#nmg-popup-ajax-content').animate({width:0, height:0, left:l, top:t, opacity:0}, 500, function(){$('#nmg-popup-ajax').remove();});
		$('#nmg-popup-ajax-overlay').hide();
		$('#nmg-popup-ajax-overlay').unbind("click");
	}
	function _nmg_getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};	
	
	function _nmg_getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
	
	
	
})(jQuery); // Call and execute the function immediately passing the jQuery object



////////////////////////////////////////////////////////////////////////////////////////////////////////


//nmg popup class

function nmg_popup(){
}

nmg_popup.prototype.show = function(data, method, vars){
	if(method=='get'||method=='post'){
		this.nmg_popup_interface("<div style='height:100px; width:200px; vertical-align:center;'><br /><br />&nbsp;&nbsp;&nbsp;Loading...&nbsp;&nbsp;&nbsp;</div>");
		
		$('#nmg-popup-ajax').remove();
		hrf = data;
		//alert(hrf);
		$.ajax({
		  type: method,
		  data: vars,
		  url: hrf,
		  success: function(html) {
			p = new nmg_popup();
			p.nmg_popup_interface(html);
		  },
		  error: function(d){
			p = new nmg_popup();
			p.nmg_popup_interface("<div style='height:100px; width:200px; vertical-align:center;'><br /><br />&nbsp;&nbsp;&nbsp;There&nbsp;was&nbsp;an&nbsp;error...&nbsp;&nbsp;&nbsp;</div>");	
		  }
		});		
		
		return false;
	}
	//innerHTML
	else{
		$('#nmg-popup-ajax').remove();
		this.nmg_popup_interface(data);
	}
}



nmg_popup.prototype.nmg_popup_interface = function(html){
	overlay = "<div id='nmg-popup-ajax'><div id='nmg-popup-ajax-close' style='display:none; cursor:pointer; position: absolute; top:0; left:0; border:0px solid black; z-index: 110; text-align: center;'>&nbsp;close&nbsp;</div><div id='nmg-popup-ajax-content' style='display:none; position: absolute; top:0; left:0; border:1px solid black; z-index: 100; text-align: center;'></div><div id='nmg-popup-ajax-overlay' style='position: absolute; top: 0; left: 0; z-index: 90; width: 100%; height: 500px; background:black'></div></div>";
	
	
	arrPageSizes = this.nmg_getPageSize();

	arrPageScroll = this.nmg_getPageScroll();
	
	$('body').append(overlay);
	$('#nmg-popup-ajax-overlay').css({
		backgroundColor:	'black',
		opacity:			'0.8',
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn(0);		
	
	
	$('#nmg-popup-ajax-content').html(html);
	t = arrPageScroll[1] + (arrPageSizes[3] / 10);
	l = (arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2);
	w = $("#nmg-popup-ajax-content").width();
	h = $("#nmg-popup-ajax-content").height();
	tclose = t - $('#nmg-popup-ajax-close').height();
	lclose = l + w - $('#nmg-popup-ajax-close').width();
	$('#nmg-popup-ajax-close').css({
		top:	tclose,
		left:	lclose,
		background: 'white'
	});		
	$('#nmg-popup-ajax-close').hide();

	$('#nmg-popup-ajax-content').html("");
	$('#nmg-popup-ajax-content').css({
		width: 0,
		height: 0,
		top:	t,
		left:	(arrPageSizes[0]/2),
		background: 'white'
	});				
	
	
	$('#nmg-popup-ajax-content').animate({width:w, height:h, left:l}, 500, function(){$('#nmg-popup-ajax-content').html(html); $('#nmg-popup-ajax-close').show();});		

	
	$('#nmg-popup-ajax-close').click(function(){
		p = new nmg_popup();
		p.nmg_popup_finish();
	});
	
	//$('#nmg-popup-ajax-overlay').click(function(){
	//	nmg_popup_finish();
	//});
	
	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = this.nmg_getPageSize();
		// Style overlay and show it
		$('#nmg-popup-ajax-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
		// Get page scroll
		var arrPageScroll = this.nmg_getPageScroll();

		t = arrPageScroll[1] + (arrPageSizes[3] / 10);
		l = (arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2);
		w = $("#nmg-popup-ajax-content").width();
		h = $("#nmg-popup-ajax-content").height();
		tclose = t - $('#nmg-popup-ajax-close').height() -1;
		lclose = l + w - $('#nmg-popup-ajax-close').width() - 1;
		$('#nmg-popup-ajax-close').css({
			top:	tclose,
			left:	lclose,
			background: 'white'
		});				
		
		// Calculate top and left offset for the jquery-lightbox div object and show it
		$('#nmg-popup-ajax-content').css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
			left:	(arrPageSizes[0]/2)-($("#nmg-popup-ajax-content").width()/2)
		});
	});
	
	return false;	
}


nmg_popup.prototype.nmg_popup_finish = function (){
	var arrPageSizes = this.nmg_getPageSize();
	t = arrPageScroll[1] + (arrPageSizes[3] / 10);
	l = (arrPageSizes[0]/2);
	$('#nmg-popup-ajax-close').hide();
	$('#nmg-popup-ajax-content').html("");
	$('#nmg-popup-ajax-content').animate({width:0, height:0, left:l, top:t, opacity:0}, 500, function(){$('#nmg-popup-ajax').remove();});
	$('#nmg-popup-ajax-overlay').hide();
	$('#nmg-popup-ajax-overlay').unbind("click");
}

nmg_popup.prototype.nmg_getPageScroll = function() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
}


nmg_popup.prototype.nmg_getPageSize = function() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


