// +-----------------------------------------+
// | @author: Juarez Gonçalves Nery Junior   |
// | @email: juareznjunior@gmail.com         |
// +-----------------------------------------+
// | cria um box modal                       |
// | testado no ie7, ff e opera              |
// | ex:                                     |
// | $(document).ready(function(){           |
// |  $('button').bind('click',function(){   |
// |   $.dialog({options});                  |
// |  })                                     |
// | });                                     |
// | depende de jquery.window.css            |
// +-----------------------------------------+
;(function($){
	var IE = function() {
		var userAgent = navigator.userAgent.toLowerCase()
		,version = function() {
			return ($.browser.msie ? (userAgent.match(/msie.[\d]+/))[0].replace(/[^\d]/g,'') : 0);
		};
		return {
			is8: (8 == version())
			,is7: (7 == version())
			,is6: (6 == version())
		}
	}();
	var htmlTemplate = function() {
		$('<div id="jq-dialog">'+
			'<div>'+
				'<div id="jq-dialog-header" />'+
				'<div id="jq-dialog-content" />'+
				'<div id="jq-dialog-button" />'+
			'</div>'+
		'</div>').appendTo(document.body);
		$('<div id="jq-dialog-overlay" />').appendTo(document.body);
	}
	,cssDivs = {
		jq_dialog : {
			position: (IE.is6) ? 'absolute' : 'fixed'
			,zIndex: 10000
			,backgroundColor:'#F3BD00'
			,padding: '0 5px 5px'
			,marginTop:-1000
			,border:'1px solid #cdcdcd'
			,'-webkit-box-shadow': '2px 4px 12px #2f2f2f'
			,'-moz-box-shadow': '2px 4px 64px #2f2f2f'
			,'-moz-border-radius-bottomleft':2
			,'-moz-border-radius-bottomright':2
			,'-moz-border-radius-topleft':2
			,'-moz-border-radius-topright':2
			,'-webkit-border-bottom-left-radius':2
			,'-webkit-border-bottom-right-radius':2
			,'-webkit-border-top-left-radius':2
			,'-webkit-border-top-right-radius':2
		}
		,jq_dialog_header : {
			font: 'bold 11px verdana,arial,sans,sans-serif'
			,color:'#fff'
			,padding: '4px 0'
			,'text-shadow':'1px 1px 0 #000'
			,display: ($.browser.msie && !IE.is8) ? 'block' : 'table'
		}
		,jq_dialog_content : {
			border:'1px solid #8f8f8f'
			,backgroundColor:'#fff'
			,overflow:'auto'
			,clear:'both'
			,zoom:1
			,'-moz-border-radius-bottomleft':2
			,'-moz-border-radius-bottomright':2
			,'-moz-border-radius-topleft':2
			,'-moz-border-radius-topright':2
			,'-webkit-border-bottom-left-radius':2
			,'-webkit-border-bottom-right-radius':2
			,'-webkit-border-top-left-radius':2
			,'-webkit-border-top-right-radius':2
		}
		,jq_dialog_button : {
			textAlign:'right'
			,padding: '4px 4px 4px 0'
		}
		,jq_dialog_overlay : {
			position:'absolute'
			,top:0,left:0
			,margin:0,padding:0
			,borderWidth:0
			,height:500
			,width:'100%'
			,height:'100%'
			,backgroundColor:'#000000'
			,zIndex: 9999
			,opacity:.70
			,zoom:1
		}
	};

	$.dialog = function(opt) {
		
		var options = $.extend({},$.dialog.defaults,opt)
		,$window = $(window)
		,$windowHeight = $window.height()
		,$windowWidth = $window.width();

		var dimensions = function() {
			if ('document' === (options.width)) {
					$(this).width(parseInt($windowWidth)-30);
			} else if (!isNaN(options.width)) {
				$(this).width(options.width);
			}
			
			if ('document' === (options.height)) {
				$(this).height(parseInt($windowHeight)/1.1);
			} else if (!isNaN(options.height)) {
				$(this).height(options.height);
			}
			return this;
		};
		
		if (IE.is6) $('select').css({visibility:'hidden'});

		htmlTemplate();

		var $dialog = $('#jq-dialog').css(cssDivs.jq_dialog)
		,$dialogHeader = $('#jq-dialog-header')
		,htmlHeader = '<div style="float:left">'+options.title+'</div>'
			+'<div style="float:right;cursor:pointer;color:#fff;font-size:120%;text-shadow:none;font-weight:bold">X</div>';

		if (typeof options.buttons == 'object' && options.buttons !== null) {
			var $dialogButton = $('#jq-dialog-button').css(cssDivs.jq_dialog_button);
			$.each(options.buttons,function(name,fn){
				$('<button type="button"></button>')
					.text(name)
					.click(function(){
						fn()
					}).appendTo($dialogButton[0])
			})
		}

		var $dialogOverlay = $('#jq-dialog-overlay').css(cssDivs.jq_dialog_overlay).each(function(){
			if ($.browser.msie && !IE.is8)
				$(this).width($windowWidth);
			if (IE.is8)
				$(this).height(parseInt($(document).height() - 4));
			else
				$(this).height($(document).height())
		})
		,$dialogContent  = $('#jq-dialog-content').css(cssDivs.jq_dialog_content).map(dimensions).append(options.content);
		
		($.isFunction(options.onBeforeShow) && options.onBeforeShow.apply($dialogContent[0]));
		
		$dialogHeader
			.css(cssDivs.jq_dialog_header)
			.width(($.browser.msie && !IE.is8) ? $dialogContent.width() : '100%')
			.append(htmlHeader)
			.children(':last')
				.bind('click',function() {
					$dialog.remove();
					$dialogOverlay.remove();
					if (IE.is6) $('select').css({visibility:'visible'});
					$(window).unbind('resize.dialog');
					($.isFunction(options.onHide) && options.onHide.apply($dialogContent[0]));
					$(this).unbind()
				})
			.end();

		$(window).bind('resize.dialog',function(){
			$dialogOverlay.height(500).height($(document).height());
		});

		if (options.closeOnESC) {
			$(document).one('keydown.dialog',function(event){
				(27 == event.keyCode && $.dialog.close());
			});
		}

		setTimeout(function(){
			$dialog.center().each(function(){
				($.isFunction(options.onShow) && options.onShow.apply($dialogContent[0]));
			});
		},250);
	};

	$.dialog.close = function() {
		$('#jq-dialog-header').children(':last').trigger('click')
	};

	$.dialog.reposition = function() {
		$('#jq-dialog').center()
	};

	// center plugin
	$.fn.center = function() {
		var $self = $(this),outerW = $self.outerWidth(),outerH = $self.outerHeight();
		
		$self.css({
			top:'50%',left:'50%',
			marginLeft: (outerW/2)*(-1),
			marginTop: (outerH/2)*(-1)
		});
		
		// ie6
		if (IE.is6) {
			$self.css('top',parseInt( ($(window).height() / 2 + (document.documentElement.scrollTop || document.body.scrollTop || 0)) ));
		}
		
		return this;
	};

	$.dialog.defaults = {
		width:'auto'
		,height:'auto'
		,content:''
		,title:''
		,closeOnESC: true
		,onBeforeShow: null
		,onShow: null
		,onHide: null
		,buttons:null
	};
})(jQuery);

