// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
// modified by MJ
// 15 December 2009
// :    $ -> $a (avoid conflicts when used together with Prototype (Lightbox))
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
//		jAlert(width, height, message, [title, callback] )
//		jConfirm(width, height, message, [title, callback] )
//		jPrompt(width, height, message, [value, title, callback] )
// 
// History:
//
//		1.00 - Released (29 December 2008)
//
//		1.01 - Fixed bug where unbinding would destroy all resize events
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 
//


(function($a) {
	
	$a.alerts = {
		
		// These properties can be read/written by accessing $a.alerts.propertyName from your scripts at any time
		
		//verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
		//horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
        overlayZIndex: 99990,                 //
        offSetX: 0,                          //
        offSetY: 0,                          //
        widthX: 0,                           //
        heightY: 0,                          //
		repositionOnResize: true,           // re-centers the dialog on window resize
		overlayOpacity: 0.5,                // transparency level of overlay 0.01
		overlayColor: '#000000',               // base color of overlay     '#FFF',
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
		okButton: '&nbsp;OK&nbsp;',         // text for the OK button
		cancelButton: '&nbsp;Cancel&nbsp;', // text for the Cancel button
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
		
		// Public methods
		
		alert: function(widthx, heighty, message, title, callback) {
			if( title == null ) title = 'Alert';
            $a.alerts.widthX = widthx;
            $a.alerts.heightY = heighty;
            
			$a.alerts._show(title, message, null, 'alert', function(result) {
				if( callback ) callback(result);
			});
		},
		
		confirm: function(widthx, heighty, message, title, callback) {
			$a.alerts.widthX = widthx;
            $a.alerts.heightY = heighty;
            if( title == null ) title = 'Confirm';
			$a.alerts._show(title, message, null, 'confirm', function(result) {
				if( callback ) callback(result);
			});
		},
			
		prompt: function(widthx, heighty, message, value, title, callback) {
			$a.alerts.widthX = widthx;
            $a.alerts.heightY = heighty;
            if( title == null ) title = 'Prompt';
			$a.alerts._show(title, message, value, 'prompt', function(result) {
				if( callback ) callback(result);
			});
		},
		
		// Private methods
		
		_show: function(title, msg, value, type, callback) {
			
            
			$a.alerts._hide();
			$a.alerts._overlay('show');
			
			$a("BODY").append(
			  '<div id="popup_container">' +
			    '<h1 id="popup_title"></h1>' +
			    '<div id="popup_content">' +
			      '<div id="popup_message"></div>' +
				'</div>' +
			  '</div>');
			
			if( $a.alerts.dialogClass ) $a("#popup_container").addClass($a.alerts.dialogClass);
			
			// IE6 Fix
			var pos = ($a.browser.msie && parseInt($a.browser.version) <= 6 ) ? 'fixed' : 'absolute'; 
			//alert(pos);
			$a("#popup_container").css({
				//position: pos,     //nereikia sito, nes IE 6 grazina fixed, o tada viskas sugriuna.. darau absolute
                position: 'absolute',
				zIndex: 99999,
                //z-index: 99999,
				padding: 0,
				margin: 0
			});
			
			$a("#popup_title").text(title);
			$a("#popup_content").addClass(type);
			$a("#popup_message").text(msg);
			$a("#popup_message").html( $a("#popup_message").text().replace(/\n/g, '<br />') );
			
            $a("#popup_container").css({
                width: $a.alerts.widthX, 
                height: $a.alerts.heightY
                
            });
            
            
            /*
			$a("#popup_container").css({
				minWidth: $a("#popup_container").outerWidth(),
				maxWidth: $a("#popup_container").outerWidth()
			});
			  */
			$a.alerts._reposition();
			$a.alerts._maintainPosition(true);
			
			switch( type ) {
				case 'alert':
					$a("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $a.alerts.okButton + '" id="popup_ok" /></div>');
					$a("#popup_ok").click( function() {
						$a.alerts._hide();
						callback(true);
					});
					$a("#popup_ok").focus().keypress( function(e) {
						if( e.keyCode == 13 || e.keyCode == 27 ) $a("#popup_ok").trigger('click');
					});
				break;
				case 'confirm':
					$a("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $a.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $a.alerts.cancelButton + '" id="popup_cancel" /></div>');
					$a("#popup_ok").click( function() {
						$a.alerts._hide();
						if( callback ) callback(true);
					});
					$a("#popup_cancel").click( function() {
						$a.alerts._hide();
						if( callback ) callback(false);
					});
					$a("#popup_ok").focus();
					$a("#popup_ok, #popup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $a("#popup_ok").trigger('click');
						if( e.keyCode == 27 ) $a("#popup_cancel").trigger('click');
					});
				break;
				case 'prompt':
					$a("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $a.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $a.alerts.cancelButton + '" id="popup_cancel" /></div>');
					$a("#popup_prompt").width( $a("#popup_message").width() );
					$a("#popup_ok").click( function() {
						var val = $a("#popup_prompt").val();
						$a.alerts._hide();
						if( callback ) callback( val );
					});
					$a("#popup_cancel").click( function() {
						$a.alerts._hide();
						if( callback ) callback( null );
					});
					$a("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
						if( e.keyCode == 13 ) $a("#popup_ok").trigger('click');
						if( e.keyCode == 27 ) $a("#popup_cancel").trigger('click');
					});
					if( value ) $a("#popup_prompt").val(value);
					$a("#popup_prompt").focus().select();
				break;
			}
			
			// Make draggable
			if( $a.alerts.draggable ) {
				try {
					$a("#popup_container").draggable({ handle: $a("#popup_title") });
					$a("#popup_title").css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		},
		
		_hide: function() {
			$a("#popup_container").remove();
			$a.alerts._overlay('hide');
			$a.alerts._maintainPosition(false);
		},
		
		_overlay: function(status) {
			switch( status ) {
				case 'show':
					var kitas = "alpha(opacity=" + ($a.alerts.overlayOpacity * 100) + ")";
                    $a.alerts._overlay('hide');
                    
                    for (var n=1; n<20; n++)
                    {
                        if (document.getElementById('flashcontent_' + n))
                        {
                            document.getElementById('flashcontent_' + n).style.display = "none";                    
                        }
                        else
                        {
                            break;
                        }
                    }
                    
                    
                    //end
					$a("BODY").append('<div id="popup_overlay"></div>');
					$a("#popup_overlay").css({
						position: 'absolute',
						//zIndex: 99990,
                        zIndex: $a.alerts.overlayZIndex,
                        //z-index: $a.alerts.overlayZIndex,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $a(document).height(),
						background: $a.alerts.overlayColor,
						opacity: $a.alerts.overlayOpacity,
                        "-moz-opacity": $a.alerts.overlayOpacity,
                        "-khtml-opacity": $a.alerts.overlayOpacity,
                        "filter": kitas
                        
                        
					   
                    });
				break;
				case 'hide':
					$a("#popup_overlay").remove();


                    for (var n=1; n<20; n++)
                    {
                        if (document.getElementById('flashcontent_' + n))
                        {
                            document.getElementById('flashcontent_' + n).style.display = "block";                    
                        }
                        else
                        {
                            break;
                        }
                    }
				break;
			}
		},
		
		_reposition: function() {
		
            var myWidth = 0;
            var myHeight = 0;
            
        
            if( typeof( window.innerWidth ) == 'number' ) 
            {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } 
            else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
            {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } 
            else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
            {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
            }     
            
            
            var top = ((myHeight / 2) - ($a.alerts.heightY / 2)) + $a.alerts.offSetY;
            var left = ((myWidth / 2) - ($a.alerts.widthX / 2)) + $a.alerts.offSetX;
            
        
        
            /*
            var top = ((myHeight / 2) - (heightx / 2)) + $a.alerts.offSetY;
            var left = ((myWidth / 2) - (widthx / 2)) + $a.alerts.offSetX;
            */  
            
            
        	
            //var top = (($a(window).height() / 2) - ($a("#popup_container").outerHeight() / 2)) + $a.alerts.verticalOffset;
			//var left = (($a(window).width() / 2) - ($a("#popup_container").outerWidth() / 2)) + $a.alerts.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			if( $a.browser.msie && parseInt($a.browser.version) <= 6 ) top = top + $a(window).scrollTop();
			
			$a("#popup_container").css({
				top: top + 'px',
				left: left + 'px'
			});
			$a("#popup_overlay").height( $a(document).height() );
		},
        
  
		
		_maintainPosition: function(status) {
			if( $a.alerts.repositionOnResize ) {
				switch(status) {
					case true:
                        
						$a(window).bind('resize', $a.alerts._reposition);
					break;
					case false:
						$a(window).unbind('resize', $a.alerts._reposition);
					break;
				}
			}
		}
		
	}
	
	// Shortuct functions
	jAlert = function(widthx, heighty, message, title, callback) {
		$a.alerts.alert(widthx, heighty, message, title, callback);
	}
	
	jConfirm = function(widthx, heighty, message, title, callback) {
		$a.alerts.confirm(widthx, heighty, message, title, callback);
	};
		
	jPrompt = function(widthx, heighty, message, value, title, callback) {
		$a.alerts.prompt(widthx, heighty, message, value, title, callback);
	};
	
})(jQuery);


