// +----------------------------------------------------------------------+
// | KAYWA Maintools                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2006  KAYWA AG                                         |
// +----------------------------------------------------------------------+
// | This program is subject to the KAYWA-Framework license.              |
// | You are not allowed to redistribute or use it without our approval.  |
// | Fore more information, contact us at contact@kaywa.com               |
// +----------------------------------------------------------------------+
// | Author: David Michon <david.mchon@kaywa.com                          |
// +----------------------------------------------------------------------+
//
img_code = '<img src="http://qrcode.kaywa.com/img.php?s=5&d=http://d.kaywa.com/pppp"/>'; 
/**
 * iDialog is a JQuery extension to display User Interface (UI) dialog box
 * in javascript and allow to display HTML content in it.
 *
 * This extension give also some helper tools to allow callback function handler
 * for yes/no dialog
 *
 * Script Dependencies:
 *
 * jquery.js
 * interface.js
 * 
 * A default CSS template for iDialog
 * 
 * <style type="text/css">
 * #DialogBoxOverlay
 * {
 *      background-color: #000;
 * }
 *
 * #DialogBoxContainer
 * {
 *  	background-color: #32353f;
 *  	border: 2px solid black;
 * }
 * 
 * #DialogBoxPicto
 * {
 *      border-right: 2px dotted #6aacc5;
 * }
 *
 * #DialogBoxTitle
 * {
 *      color: white;
 * }
 * 
 * #DialogBoxContent
 * {
 *      color: #6aacc5;
 * }
 * 
 * #DialogBoxTitleText
 * {
 *      color: white;
 *      font-size: 12px;
 *      font-weight: bold;
 * }
 * 
 * #DialogBoxCloseLink
 * {
 *      color: white;
 *      font-size: 11px;
 *      font-weight: bold;
 * }
 *
 * #DialogBoxCloseLeft
 * {
 *      display: block;
 *      background : url('javascript/UI/source/close_left_border.png');
 *      width: 6px;
 *      height: 18px;
 *      float: left;
 * }
 *
 * #DialogBoxCloseCenter
 * {
 *      display: block;
 *      background : url('javascript/UI/source/close_center.png');
 *      height: 16px;
 *      float: left;
 *      padding-top: 2px;
 * }
 * 
 * #DialogBoxCloseRight
 * {
 *      display: block;
 *      background : url('javascript/UI/source/close_right_border.png');
 *      width: 6px;
 *      height: 18px;
 *      float: left;
 * }
 * </style>
 *
 *
 * @package 	jQuery
 * @subpackage  Plugins/Interface
 * @author 		David Michon <david.michon@kaywa.com>
 *
 * @option      Float       overlayOpacity      Change the overlay opacity background. 
 *                                              Value can be between 0 (transparent) and 1 (black)
 * @option      Integer     fadeDuration        Fade duration of the overlay. In miliseconds
 * @option      Integer     width               Width of the Dialog Box
 * @option      Integer     height              Height of the Dialog Box
 * @option      String      pictoSrc            A picture source to place on left of the box
 * @option      String      titleText           Title of the Dialog Box
 * @option      String      closeHTML           Close text/button in HTML of the Dialog Box
 * @option      String      contentHTML         Content to display in the Dialog Box. It can be
 *                                              - text : HTML content
 *                                              - url  : An URL to a script/HTML page
 *                                              - yn   : Yes/no dialog box. Callback function has 
 *                                                       to be defined
 * @option      Function    onShow              Callback function on Dialog Box show
 * @option      Function    onHide              Callback function on Dialog Box hide
 *
 * @version 	1.0
 * @copyright 	KAYWA AG
 * @since 		20.06.2007
 * @see 		JQuery, Interface (http://interface.eyecon.ro)
 *//*
function metatags_qr(){ 
	metatags = document.getElementsByTagName("meta");

// Iterate through the array, listing them all
// Update only the Title meta tag
for (cnt = 0; cnt < metatags.length; cnt++)
{
    var name = metatags[cnt].getAttribute("name");
    var content = metatags[cnt].getAttribute("content");

    // List the original meta tag name=content
    // Update the Title meta tag
    if (metatags[cnt].getAttribute("name") == "qrcode"){
         var meta_qr_code = metatags[cnt].getAttribute("content");
		 meta_qr_code = '<img src="http://qrcode.kaywa.com/img.php?s=5&d=http://d.kaywa.com/'+meta_qr_code+'"/>';
		 return meta_qr_code;
	}
} 
}
var img_code = metatags_qr();*/
jQuery.iDialog = {
   
    /**
     * Internal Property
     */
    animationInProgress : false,
	opened : false,
	
	showCallback : null,
	hideCallback : null,
	
	/**
	 * Default options parameters
	 * Can be change through the constructor class
	 */
	options : {
		overlayOpacity		: 0.3,
		fadeDuration		: 200,
		width               : 300,
		height              : 100,
		pictoSrc            : null,
		titleText           : 'Dialog Title',
		closeHTML           : '<div id="DialogBoxCloseLeft"></div><div id="DialogBoxCloseCenter">Close</div><div id="DialogBoxCloseRight"></div>',
		contentHTML         : 'klop klop' ,
		onShow              : null,
		onHide              : null
	},
	
	/**
	 * Class Constructor
	 * 
	 * @params      Object      options             Options parameters see below:
	 * @option      Float       overlayOpacity      Change the overlay opacity background. 
     *                                              Value can be between 0 (transparent) and 1 (black)
     * @option      Integer     fadeDuration        Fade duration of the overlay. In miliseconds
     * @option      Integer     width               Width of the Dialog Box
     * @option      Integer     height              Height of the Dialog Box
     * @option      String      pictoSrc            A picture source to place on left of the box
     * @option      String      titleText           Title of the Dialog Box
     * @option      String      closeHTML           Close text/button in HTML of the Dialog Box
     * @option      String      contentHTML         Content to display in the Dialog Box. It can be
     *                                              - text : HTML content
     *                                              - url  : An URL to a script/HTML page
     *                                              - yn   : Yes/no dialog box. Callback function has 
     *                                                       to be defined
     * @option      Function    onShow              Callback function on Dialog Box show
     * @option      Function    onHide              Callback function on Dialog Box hide
     * @return  void
     * @access  public
     * @author 	David Michon <david.michon@kaywa.com>
	 */
	init : function (options) {
        
	    // Extend jQuery.iDialog options with passed parameters
        if (options) {
			jQuery.extend(jQuery.iDialog.options, options);
        }
        
        // Register Callback
        if (jQuery.iDialog.options.onShow !== null) {
            jQuery.iDialog.showCallback = jQuery.iDialog.options.onShow;
        }
        
        // Register Callback
        if (jQuery.iDialog.options.onHide !== null) {
            jQuery.iDialog.hideCallback = jQuery.iDialog.options.onHide;
        }
        
        // Only allow one Dialog Box opened at a time
        if (jQuery.iDialog.opened == true) {
            return false;
        }
        
        // check if an URL is passed in the contentHTML
    	contentHTMLLink = jQuery.iDialog.options.contentHTML.match(/(http|https).*\.(php|html|js)(\?(.+)){0,1}/gi);
        
    	
        if (jQuery.browser.msie) {
			iframe = document.createElement('iframe');
			jQuery(iframe)
				.attr(
					{
						id			: 'DialogBoxOverlayIframe',
						src			: 'javascript:false;',
						frameborder	: 'no',
						scrolling	: 'no'
					}
				)
				.css (
					{
						display		: 'none',
						position	: 'absolute',
						top			: '0',
						left		: '0',
						filter		: 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
					}
				);
			jQuery('body').append(iframe);
		}
		
    	
		// Overlay part to display this grey area on top of the page
		overlay	= document.createElement('div');
		
		jQuery(overlay)
			.attr('id', 'DialogBoxOverlay')
			.css(
				{
					position	: 'absolute',
					display		: 'none',
					top			: '0',
					left		: '0',
					opacity		: 0
				}
			)
			.append(document.createTextNode(' '));
		
		
		// Construct the inner container for data content
		
		if (contentHTMLLink == null) {
		    innerContainerHeight = jQuery.iDialog.options.height - 20 + 'px';
		} else {
		    innerContainerHeight = jQuery.iDialog.options.height + 'px'
		}
		
		innerContainer = document.createElement('div');
		jQuery(innerContainer)
			.attr('id', 'DialogBoxInnerContainer')
			.css(
				{
					position	: 'relative',
					overflow	: 'auto',
					textAlign	: 'left',
					margin		: '0',
					padding     : '0 0 0 0px',
					height      : innerContainerHeight
				}
			);
			
		// Construct the content container of the Dialog Box	
		container = document.createElement('div');
		jQuery(container)
			.attr('id', 'DialogBoxContainer')
			.css(
				{
					display		: 'none',
					position	: 'absolute',
					overflow	: 'hidden',
					top			: '0',
					left		: '0',
					textAlign	: 'center',
					lineHeigt	: '0',
					padding     : '10px',
					zIndex		: 3
				}
			);
    	
		// Construct the Close button HTML element of the Dialog Box	
		closeLink = document.createElement('a');
		if (jQuery.iDialog.options.closeHTML !== null) {
    		jQuery(closeLink)
    			.attr(
    				{
    					id			: 'DialogBoxCloseLink',
    					href		: '#'
    				}
    			)
    			
    			.append(jQuery.iDialog.options.closeHTML)
    			.bind('click', jQuery.iDialog.hide);
		}
		
		closeBox = document.createElement('div');
		jQuery(closeBox)
			.attr('id', 'DialogBoxCloseBox')
			.css(
				{
				    float	: 'right'
				}
			)
			.append(closeLink);
		
		closeEl = document.createElement('div');
		jQuery(closeEl)
			.attr('id', 'DialogBoxClose')
			.css(
				{
				    textAlign	: 'right'
				}
			)
			.append(closeBox);
		
    	// Not a link
    	if (contentHTMLLink == null) {
    		if (jQuery.iDialog.options.pictoSrc !== null) {
        		// Construct the picto element of the Dialog Box
        		pictoImg = document.createElement('img');
        		jQuery(pictoImg)
        			.attr('id', 'DialogBoxPictoImg')
        			.attr('src', jQuery.iDialog.options.pictoSrc);
        		
        		// Construct the Title area element of the Dialog Box	
        		picto = document.createElement('div');
        		jQuery(picto)
        			.attr('id', 'DialogBoxPicto')
        			.css(
        				{
        					display     : 'block',
        				    float       : 'left',
        					textAlign	: 'left',
        					padding     : '0',
        					margin		: '0 10px',
        					width       : '50px',
                            height      : '100%'
        				}
        			)
        			.append(pictoImg);
    		}
    		
    		// Construct the Title Text element of the Dialog Box
    		titleText = document.createElement('div');
    		jQuery(titleText)
    			.attr('id', 'DialogBoxTitleText')
    			.append(document.createTextNode(jQuery.iDialog.options.titleText));
    		
    		// Construct the Title area element of the Dialog Box	
    		title = document.createElement('div');
    		jQuery(title)
    			.attr('id', 'DialogBoxTitle')
    			.css(
    				{
    					display     : 'block',
    				    textAlign	: 'left',
    					margin		: '0 auto'
    				}
    			)
    			.append(titleText);
    		
    		contentEl = document.createElement('div');
    		jQuery(contentEl)
    			.attr('id', 'DialogBoxContent')
    			.css(
    				{
    					display     : 'block',
    				    textAlign	: 'left',
    					margin		: 'auto'
    				}
    			)
    			.append(jQuery.iDialog.options.contentHTML);
    		  		
    		jQuery(innerContainer).append([title, contentEl]);
    			
    		if (jQuery.iDialog.options.pictoSrc !== null) {
    			jQuery(container).append([picto, innerContainer, closeEl]);
    		} else {
    		    jQuery(container).append([innerContainer, closeEl]);
    		}
    	} else {
    	    // Content is link, create a div and display the page
 	    
    	    ajaxContent = document.createElement('div');
    		jQuery(ajaxContent)
    			.attr('id', 'DialogBoxIframe')
    			.css(
    				{
    					border      : 0,
    				    height      : jQuery.iDialog.options.height + 'px',
    					width       : '100%'
    				}
    			)
    			.load(contentHTMLLink);
    			
    		jQuery(innerContainer).append(ajaxContent);
    		
    		jQuery(container).append([innerContainer]);
    	}
    	
		// Add elements to the body of the page
		jQuery('body').append(overlay).append(container);
		
		jQuery.iDialog.show();
		
		// Attach event on scroll and resize of windows to
		// reposition the Dialog Box and resize the overlay layer
		jQuery(window).scroll(jQuery.iDialog._positionElements);
		jQuery(window).resize(jQuery.iDialog._positionElements);
    },
    
    /**
	 * Show the Dialog Box
	 * 
	 * @params  void
     * @return  void
     * @access  private
     * @author 	David Michon <david.michon@kaywa.com>
	 */
	show : function () {
        container = jQuery('#DialogBoxContainer');
		overlay = jQuery('#DialogBoxOverlay');
        
		jQuery.iDialog.animationInProgress = true;
		
		containerW = jQuery.iDialog.options.width;
		containerH = jQuery.iDialog.options.height;
		
		container
			.css(
				{
					width	: containerW + 'px',
					height	: containerH + 'px'
				}
			);
			
		clientSize = jQuery.iUtil.getClient();
		pageSize = jQuery.iUtil.getScroll();
		
		
		if (!jQuery.iDialog.opened) {
			if (jQuery.browser.msie) {
				jQuery('#DialogBoxOverlayIframe')
					.css ('height', Math.max(pageSize.ih,pageSize.h) + 'px')
					.css ('width', Math.max(pageSize.iw,pageSize.w) + 'px')
					.show();
			}
			overlay
				.css ('height', Math.max(pageSize.ih,pageSize.h) + 'px')
				.css ('width', Math.max(pageSize.iw,pageSize.w) + 'px')
				.show()
				.fadeTo( 
					jQuery.iDialog.options.fadeDuration,
					jQuery.iDialog.options.overlayOpacity
				);
		};
		
		// Center the box in the page
		jQuery.iDialog._positionElements();
		
		if (container.css('display') == 'none') {
		    container
				.show()
				.fadeIn(									
    				jQuery.iDialog.options.fadeDuration, 
    				function()
    				{
    					jQuery.iDialog.animationInProgress = false;
    					jQuery.iDialog.opened = true;
    					
    					if (jQuery.iDialog.showCallback !== null) {
    					    if (typeof jQuery.iDialog.showCallback == 'function') {
    					       eval(jQuery.iDialog.showCallback());
    					    } else {
    					        eval(jQuery.iDialog.showCallback);
    					    }
    					}
    				});
    	    
		};
		return false;
    },
    
    /**
	 * Hide the Dialog Box
	 * 
	 * @params  void
     * @return  void
     * @access  private
     * @author 	David Michon <david.michon@kaywa.com>
	 */
    hide : function()
	{
		jQuery('#DialogBoxContainer').remove();
		
		jQuery('#DialogBoxOverlay').fadeTo(
			jQuery.iDialog.options.fadeDuration, 
			0, 
			function(){
			    if (jQuery.browser.msie) {
					jQuery('#DialogBoxOverlayIframe').remove();
				}
				jQuery(this).remove();
			}
		);
		
		
		jQuery.iDialog.opened = false;
		jQuery.iDialog.animationInProgress = false;
		
		if (jQuery.iDialog.hideCallback !== null) {
		    if (typeof jQuery.iDialog.hideCallback == 'function') {
		       eval(jQuery.iDialog.hideCallback());
		    } else {
		        eval(jQuery.iDialog.hideCallback);
		    }
		}
		
		return false;
	},
	
	/**
	 * Callback function to execute on Dialog Show
	 * 
	 * @params  function callback  Callback function to execute
     * @return  void
     * @access  private
     * @author 	David Michon <david.michon@kaywa.com>
	 */
	onShowCallback : function (callback) {
        jQuery.iDialog.showCallback = callback;
    },
    
    /**
	 * Callback function to execute on Dialog Hide
	 * 
	 * @params  function callback  Callback function to execute
     * @return  void
     * @access  private
     * @author 	David Michon <david.michon@kaywa.com>
	 */
    onHideCallback : function (callback) {
        jQuery.iDialog.hideCallback = callback;
    },
    
    /**
	 * Reposition elements on screen
	 * 
	 * @params  void
     * @return  void
     * @access  private
     * @author 	David Michon <david.michon@kaywa.com>
	 */
    _positionElements : function () {
        pageSize = jQuery.iUtil.getClient();
		pageScroll = jQuery.iUtil.getScroll();
		
        overlay = jQuery('#DialogBoxOverlay');
        var overlayStyle = {width: pageScroll.l + pageSize.w, height: pageScroll.t + pageSize.h};
        overlay.css(overlayStyle);
        
        overlayIframe = jQuery('#DialogBoxOverlayIframe');
        var overlayIframeStyle = {width: pageScroll.l + pageSize.w, height: pageScroll.t + pageSize.h};
        overlayIframe.css(overlayIframeStyle);
        
        container = jQuery('#DialogBoxContainer');
        containerW = jQuery.iDialog.options.width;
		containerH = jQuery.iDialog.options.height;    
        
        var containerStyle = {width: containerW, left: (pageScroll.l + (pageSize.w - containerW)/2), top: (pageScroll.t + (pageSize.h - containerH)/2)};
        container.css(containerStyle);
    }
};


/**
 * Available method to use externally
 * 
 * UIDialog       : Show the Dialog Box 
 * UIDialogClose  : Close the Dialog Box
 * UIDialogOnShow : Register a callback function on Dialog Box show
 * UIDialogOnHide : Register a callback function on Dialog Box hide
 */
jQuery.fn.extend({UIDialog : jQuery.iDialog.init});
jQuery.fn.extend({UIDialogClose : jQuery.iDialog.hide});
jQuery.fn.extend({UIDialogOnShow : jQuery.iDialog.onShowCallback});
jQuery.fn.extend({UIDialogOnHide : jQuery.iDialog.onHideCallback});