﻿jQuery.extWindow={};
$.extend($.extWindow, {
	config:{
		stop:false,
        okbuttonText: 'OK',
        okEvent: function(e) { },
		cancelbuttonText:'Cancel',
		cancelEvent: function(e) {},
        initWidth: 400,
        marginTop: 200,
        title: 'extWindow Dialog',
        content: 'This is a jquery plugin!',
        docWidth: $(this).width(),
        docHeight: $(this).height(),
		overlayCss: {
                'position': 'absolute',
				'width':'100%',
				'height':'100%',
                'left': '0',
                'top': '0',
                'background-color': '#7f7f7f',
                'opacity': '0',
                'z-index': '200'
       },
       extWindowbgCss: {
                'z-index': '201',
                'position': 'fixed',
                'background-color': '#555'
       },
       extWindowCss: {
                'z-index': '202',
                'height': 'auto',
                'position': 'fixed',
                'opacity': '0'
       },
       closeCss: {
       			'position': 'absolute',
                'right': '5px',
                'top': '5px',
                'width': '16px',
                'height': '16px',
                'font-weight': 'bold',
                'color': '#fff',
                'text-align': 'center',
                'line-height': '16px',
                'display': 'block',
                'text-decoration': 'none'
       },
       titleCss: {
                'height': '30px',
                'background-color': '#E9ECEF',
                'font-weight': 'bold',
                'text-indent': '10px',
                'color': '#008080',
                'line-height': '30px',
                'border-bottom': 'groove 2px #4682B4'
       },
       contentCss: {
                'height': 'auto',
                'background-color': '#F5FFFA',
                'padding': '20px 10px',
				'font-size':'11px',
				'font-family': 'Tahoma'
       },
       bottomCss: {
                'background-color': '#eee',
                'border-top': '1px solid #ccc',
                'padding': '5px',
                'text-align': 'center'
       },
       buttonCss: {
                'background-color': '#E9ECEF',
                'border-color': '#B8D4E8 #124680 #124680 #B8D4E8',
                'border-style': 'solid',
                'border-width': '2px',
                'color': '#008080',
                'cursor': 'pointer',
                'font-size': '12px',
                'padding': '2px 15px',
                'text-align': 'center',
                'margin-right': '5px',
                'font-family': 'Tahoma'
       }
	},
	hideExtWindow:function(options){
						$('#extWindow-overlay').animate({ opacity: 0 }, function() { $(this).css('display', 'none'); $('#extWindowfield').remove(); });
            			$('#extWindow-bg').animate({ opacity: 0 }, function() { $(this).css('display', 'none') });
            			$('#extWindow').animate({ opacity: 0 }, function() { $(this).css('display', 'none') });
		},
	closeExtWindow:function(options){
			$('#extWindow-close').css(options.closeCss).hover(function() {
            	$(this).css({ 'background-color': '#F0FFF0', 'color': '#000' });
        	}, function() {
            	$(this).css({ 'background-color': 'transparent', 'color': '#fff' });
        	}).one('click', function(e) {jQuery.extWindow.hideExtWindow(options);});
	},
	alert:function(options){
				var options = $.extend($.extWindow.config,options);
								        
				if ($('#extWindow').length == 0) {
            		$(document.body).append('<div id="extWindowfield"><div id="extWindow-overlay" />' +
                							'<div id="extWindow-bg" />' +
                							'<div id="extWindow">' +
                							'<a href="javascript:void(0);" id="extWindow-close" title="закрыть">X</a>' +
                							'<div id="extWindow-container-title" />' +
                							'<div id="extWindow-container-content" />' +
                							'<div id="extWindow-container-bottom">' +
                							'<button id="extWindow-bottom-okbutton" ></button>' +
                							'</div></div>');
        		} else {
            		$('#extWindow-overlay').css({ opacity: 0, 'display': 'block' });
            		$('#extWindow-bg').css({ opacity: 0, 'display': 'block' });
            		$('#extWindow').css({ opacity: 0, 'display': 'block' });
        		}
				$('#extWindow-container-title').css(options.titleCss).html(options.title);                        
                $('#extWindow-container-content').css(options.contentCss);
                                
                $('#extWindow-container-bottom').css(options.bottomCss)
                                            .children('#extWindow-bottom-okbutton')
                                                .text(options.okbuttonText)
                                                    .css(options.buttonCss)
                                                        .one('click', function() {
                                                            options.okEvent(options);
																jQuery.extWindow.hideExtWindow(options);
                                                        });
        		if (typeof options.content == 'string') {
            		$('#extWindow-container-content').html(options.content);
        		}
        		if (typeof options.content == 'function') {
            		var e = $('#extWindow-container-content');
            		e.holder = $('#extWindow');
            		e.modalBG = $('#extWindow-bg');
            		options.content(e);
        		}
				
        		$('#extWindow-overlay').css(options.overlayCss).css('width',$(document).width()).animate({ opacity: 0.7 });
        		$('#extWindow').css(options.extWindowCss).css({
            		width: options.initWidth,
            		left: (options.docWidth - options.initWidth) / 2,
            		top: (options.marginTop + document.documentElement.scrollTop)
        			}).animate({ opacity: 1 }, function() {
						
					$('#extWindow-bg').css(options.extWindowbgCss).css({
                	width: options.initWidth + 20,
                	left: (options.docWidth - options.initWidth) / 2 - 10,
                	height: $(this).height() + 20,                	
                	top: (options.marginTop - 10 + document.documentElement.scrollTop)
            		}).animate({opacity: 0.7});	
				});
				
				
       		jQuery.extWindow.closeExtWindow(options);
	},
	confirm:function(options){
		var options = $.extend($.extWindow.config,options);
								        
				if ($('#extWindow').length == 0) {
            		$(document.body).append('<div id="extWindowfield"><div id="extWindow-overlay" />' +
                							'<div id="extWindow-bg" />' +
                							'<div id="extWindow">' +
                							'<a href="javascript:void(0);" id="extWindow-close" title="закрыть">X</a>' +
                							'<div id="extWindow-container-title" />' +
                							'<div id="extWindow-container-content" />' +
                							'<div id="extWindow-container-bottom">' +
                							'<button id="extWindow-bottom-okbutton" ></button><button id="extWindow-bottom-cancelbutton" ></button>' +
                							'</div></div>');
        		} else {
            		$('#extWindow-overlay').css({ opacity: 0, 'display': 'block' })
            		$('#extWindow-bg').css({ opacity: 0, 'display': 'block' });
            		$('#extWindow').css({ opacity: 0, 'display': 'block' });
        		}
				$('#extWindow-container-title').css(options.titleCss).html(options.title);                        
                $('#extWindow-container-content').css(options.contentCss);
                $('#extWindow-status').css(options.contentCss);
				
                $('#extWindow-container-bottom').css(options.bottomCss)
                                            .children('#extWindow-bottom-okbutton')
                                                .text(options.okbuttonText)
                                                    .css(options.buttonCss)
                                                        .one('click', function(e) {
                                                            options.okEvent(options);
																	jQuery.extWindow.hideExtWindow(options);
                                                        });
											$('#extWindow-bottom-cancelbutton')
												.text(options.cancelbuttonText)
													.css(options.buttonCss)
														.one('click', function(e) {
															jQuery.extWindow.hideExtWindow(options);
															});
        		if (typeof options.content == 'string') {
            		$('#extWindow-container-content').html(options.content);
        		}
        		if (typeof options.content == 'function') {
            		var e = $('#extWindow-container-content');
            		e.holder = $('#extWindow');
            		e.modalBG = $('#extWindow-bg');
            		options.content(e);
        		}
				
        		$('#extWindow-overlay').css(options.overlayCss).css('height',$(document).height()).animate({ opacity: 0.7 });
        		$('#extWindow').css(options.extWindowCss).css({
            		width: options.initWidth,
            		left: (options.docWidth - options.initWidth) / 2,
            		top: (options.marginTop)
        			}).animate({ opacity: 1 }, function() {
					
					$('#extWindow-bg').css(options.extWindowbgCss).css({
                	width: options.initWidth + 20,
                	left: (options.docWidth - options.initWidth) / 2 - 10,
                	height: $(this).height() + 20,                	
                	top: (options.marginTop - 10)
            		}).animate({opacity: 0.7});	
				});
				
				
       		jQuery.extWindow.closeExtWindow(options);			
	},
	window:function(options){
		var options = $.extend($.extWindow.config,options);
				        
				if ($('#extWindow').length == 0) {
            		$(document.body).append('<div id="extWindowfield"><div id="extWindow-overlay" />' +
                							'<div id="extWindow-bg" />' +
                							'<div id="extWindow">' +
                							'<a href="javascript://" id="extWindow-close" title="закрыть">X</a>' +
                							'<div id="extWindow-container-title" />' +
                							'<div id="extWindow-container-content" />' +
											'<div id="extWindow-status" />' +
											'</div>'
                							);
        		} else {
            		$('#extWindow-overlay').css({ opacity: 0, 'display': 'block' });
            		$('#extWindow-bg').css({ opacity: 0, 'display': 'block' });
            		$('#extWindow').css({ opacity: 0, 'display': 'block' });
        		}
				
				$('#extWindow-container-title').css(options.titleCss).html(options.title);                        
                $('#extWindow-container-content').css(options.contentCss);
							
        		if (typeof options.content == 'string') {
            		$('#extWindow-container-content').html(options.content);
        		}
        		if (typeof options.content == 'function') {
            		var e = $('#extWindow-container-content');
            		e.holder = $('#extWindow');
            		e.modalBG = $('#extWindow-bg');
            		options.content(e);
        		}
				
        		$('#extWindow-overlay').css(options.overlayCss).animate({ opacity: 0.7 });
        		$('#extWindow').css(options.extWindowCss).css({
            		width: options.initWidth,
            		left: (options.docWidth - options.initWidth) / 2,
            		top: (options.marginTop + document.documentElement.scrollTop)
        			}).animate({ opacity: 1 }, function() {
						
					$('#extWindow-bg').css(options.extWindowbgCss).css({
                	width: options.initWidth + 20,
                	left: (options.docWidth - options.initWidth) / 2 - 10,
                	height: $(this).height() + 20,                	
                	top: (options.marginTop - 10 + document.documentElement.scrollTop)
            		}).animate({opacity: 0.7});	
				});				
				
        	jQuery.extWindow.closeExtWindow(options);		
	}
});