jQuery.fn.extend({
	open: function() {
		return this.each(function() {
			new jQuery.Popup(this);
		});
	}
});

jQuery.Popup = function(obj) {
	var LinkDefaults = {
		'id':'popwin',
		'mode':'simple',
		'titleBar':'title',
		'close':'<span class="close-pop"></span>',
		'autoCenter':true,
		'height':400,
		'width':400,
		'left':50,
		'top':50,
		'resizable':'yes',
		'scrollbars':'yes'
	};
	
	var ImgDefaults = {
		'id':'popimg',
		'titleBar':'alt',
		'identifier':'_large',
		'close':'<span class="close-pop"><span>x</span></span>',
		'autoCenter':true,
		'left':0,
		'top':0
	};
	
	var DivDefaults = {
		'id':'popdiv',
		'titleBar':'title',
		'close':'<span class="close-pop"><span>x</span></span>',
		'autoCenter':true,
		'left':50,
		'top':50,
		'width':400,
		'height':250
	};
	
	var _openWin = function(url,options) {
		if(options.autoCenter) {
			var dim = _getPageSize();
			options.left = parseInt((dim[0] - options.width)/2);
			options.top = parseInt((dim[1] - options.height)/2);
		}
		var features = _setFeatures(options);		
		switch(options.mode.toLowerCase()) {
			case 'modal':
			case 'modeless':
				if($.browser.msie) {
					features = features.replace(/\,/gi,';');
					features = features.replace(/\=/gi,':');
					features = features.replace(/scrollbars/gi,'scroll');
					features = features.replace(/left/gi,'dialogLeft');
					features = features.replace(/top/gi,'dialogTop');
					features = features.replace(/width/gi,'dialogWidth');
					features = features.replace(/height/gi,'dialogHeight');
					if(features.search('scroll')==-1) {features+=';srcoll:no';}
					if(features.search('status')==-1) {features+=';status:no';}
					if(features.search('help')==-1) {features+=';help:no';}
					w = (options.mode.toLowerCase=='modeless') ? 
						window.showModelessDialog(url,window,features) : 
						window.showModalDialog(url,window,features);
				} else {
					features = 'dependent=yes,'+features;
					w = window.open(url,options.id,features);
				}
				w.focus();
				break;
			case 'inline':
				_openInline(url,'iframe',options);
				break;
			case 'full':
				top.location.relplace(url);
				break;
			default:
				w = window.open(url,options.id,features);
				w.focus();
				break;
		}
	}
	
	_getPageSize = function () {
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
		return new Array(w,h) 
	}
	
	_getPageScrollTop = function(){
		var yScrolltop;
		var xScrollleft;
		if (self.pageYOffset || self.pageXOffset) {
			yScrolltop = self.pageYOffset;
			xScrollleft = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
			yScrolltop = document.documentElement.scrollTop;
			xScrollleft = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScrolltop = document.body.scrollTop;
			xScrollleft = document.body.scrollLeft;
		}
		return new Array(xScrollleft,yScrolltop) 
	}
	
	var _openInline = function(url,type,options) {
		if($('#VPop').size()==0) {
			$('<div id="VPop"></div>').appendTo('body');
		}
		if(options.title) {
			$('#VPop').append('<div class="pop-title">'+options.title+'</div>');
		}
		if(options.close) {
			var c = $(options.close).appendTo('#VPop');
			$(c).click(function(){$('#VPop').remove();});
		}
		$('<div id="TFake">&nbsp;</div>').appendTo('#VPop');
		$('#VPop').css({'left':'-2000px','top':'-2000px'}).show();
		var padW = ($('#VPop')[0].offsetWidth-$('#VPop')[0].clientWidth);
		var padH = ($('#VPop')[0].offsetHeight-$('#VPop')[0].clientHeight);
		padH +=($('#TFake')[0].offsetTop);
		var posW = parseInt(padW)+parseInt(options.width);
		var posH = parseInt(padH)+parseInt(options.height);
		$('#TFake').remove();
		document.onkeydown = function(e) {
			keycode = (e==null) ? keycode = event.keyCode : e.which; 
			if(keycode==27) {$('#VPop').remove();}
		}
		var scroll =_getPageScrollTop();
		if(options.autoCenter) {
			var dim = _getPageSize();
			lt = parseInt(scroll[0] + (dim[0] - posW)/2);
			tp = parseInt(scroll[1] + (dim[1] - posH)/2);
		} else {
			lt = scroll[0] + options.left;
			tp = scroll[1] + options.top;
		}
		$('#VPop').css({'position':'absolute','left':lt,'top':tp,'height':posH,'width':posW});
		if(type=='iframe') {
			$('<iframe src="'+url+'" id="'+options.id+'"></iframe')
				.css({'height':options.height,'width':options.width})
				.appendTo('#VPop');
		} else if(type=='img') {
			$('<img src="'+url+'" />').appendTo('#VPop');
		} else {
			$('<div class="pop-box"></div>').appendTo('#VPop');
			$('div.pop-box','#VPop').css({'height':options.height,'width':options.width,'overflow':'auto'}).html($(url).html());
		}
		$('#VPop').show();
		$(window).scroll(function(){
			var dim = _getPageSize();
			var scroll =_getPageScrollTop();
			if(options.autoCenter) {
				var style = {left: (scroll[0] + (dim[0] - posW)/2), 
				top: (scroll[1] + (dim[1]-posH)/2)};
			} else {
				var style = {left: (scroll[0] + options.left), 
				top: (scroll[1] + options.top)};
			}
			$("#VPop").css(style);
		});
	}
	
	var _setFeatures = function(opts) {
		var tmp = [];
		var featureList = 'dependent,height,hotkeys,innerHeight,innerWidth,left,location,menubar,resizable,sreenX,screenY,scrollbars,status,toolbar,top,width,';
		var pixelVal = 'height,innerWidth,innerHeight,left,screenX,screenY,top,width,';
		for(a in opts) {
			if(featureList.search(a+',')!=-1) {
				if(pixelVal.search(a+',')!=-1) {opts[a]+='px';}
				tmp[tmp.length] = a+"="+opts[a];
			}
		}
		return tmp.join(',');
	}
	
	imgPreloader = new Image();
	imgPreloader.onload = function() {
		imgPreloader.onload = null;
		var imageWidth = imgPreloader.width;
		var imageHeight = imgPreloader.height;
	}
	
	if(obj.nodeName.toLowerCase()=='img') {
		$(obj).css({'cursor':'pointer'});
	}
	
};

$(document).ready(function(){$('a.popup').open();});
