/**
Copyright (c) 2008 Ângelo Ayres Camargo <uacaman at gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
if (typeof Effect == 'undefined') {
    throw ("lightwindow.js requires including script.aculo.us' effects.js library!");
}

try {
    document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

var XWindowDo = Class.create();

Object.extend(XWindowDo.prototype, {
    initialize: function () {
        this.cHeight = 50;
        this.cWidth = 200;
		this.html = '';
		
		var html = "";
		html += "<div id='XWindowHeader'>";
		html += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
		html += "<tr>";
		html += "<td height='20'><span id='XWindowTitle'></span></td>";
		html += "<td height='20' align='right'><a id='XWindowClose' href='#'>x</a></td>";
		html += "</tr>";
		html += "</table>";
		html += "</div>";
		html += "<div id='XWindowContent'>";
		html += "<iframe name='XWindowIframe' id='XWindowIframe'  src='javascript: ;' frameBorder='0'></iframe>";
		html += "</div>";
		this.html = html;

		this.eWindow = $('XWindow');
		
		if(this.eWindow === null) {
			
			this.eWindow = new Element('div', {
            	'id': 'XWindow'
	        });
			this.eWindow.hide()
/*    	    this.eWindow.setStyle({
        	    'display': 'none'
	        });*/

    	    document.body.appendChild(this.eWindow);
			this.eWindow.update(html);
			$('XWindowHeader').setStyle({
        	    'display': 'none'
	        });
			Event.observe('XWindowClose', 'click', this.doHide.bind(this));
			
			this.eOverlay = new Element('div', {
        	    'id': 'XWindowOverlay'
			});

			this.eOverlay.setStyle({
				'display': 'none'
			});
			
	        this.eOverlay.setOpacity(0.2);
    	    document.body.appendChild(this.eOverlay);
		}
    },

    doShow: function (params) {
			
		var queue = Effect.Queues.get('XWindow');
		queue.each(function(effect) { effect.cancel(); });
		
        this.options = Object.extend({
            width: 250,
            height: 50,
            url: 'http://www.camargo.inf.br',
            title: 'title',
			onClose: null
        },
        params);

		this.eWindow.update(this.html);
		$('XWindowHeader').setStyle({
			'display': 'none'
		});
		Event.observe('XWindowClose', 'click', this.doHide.bind(this));

		this.eWindow.hide();
		$('XWindowHeader').hide();
		new Effect.Opacity(this.eWindow, {from: 0,  to: 1, duration: 0, queue: { scope: 'XWindow', position: 'end' }});
		
	    var vPortSize = document.viewport.getDimensions();
        var vPortOffSet = document.viewport.getScrollOffsets();
		
        this.eOverlay.setStyle({
            'width': (vPortSize.width + vPortOffSet.left) + 'px',
            'height': $$('body')[0].getHeight() + 'px'
        });
		this.eOverlay.show();

		// update caption
        $('XWindowTitle').innerHTML = this.options.title;

		 var left 	= parseInt((vPortSize.width / 2) - (this.cWidth / 2), 10); // + vPortOffSet.left;
         var top 	= parseInt((vPortSize.height / 2) - (this.cHeight / 2), 10) + vPortOffSet.top;
		
         this.eWindow.setStyle({
            'width': this.cWidth + 'px',
            'height': this.cHeight + 'px',
            'display': '',
			'top': top + 'px',
            'left': left + 'px'			
        });

        // calculate dimensions	 %
        var pWidth = parseInt((this.options.width * 100) / this.cWidth, 10);
        var pHeight = parseInt((this.options.height * 100) / this.cHeight, 10);

		new Effect.Scale(this.eWindow, pWidth, {
            scaleFromCenter: true,
            scaleY: false,
            scaleContent: false, 
			queue: { scope: 'XWindow', position: 'end' },
			duration: 0.3,
            delay: 0.2
			}
		);
		
		new Effect.Scale(this.eWindow, pHeight, {
            scaleFromCenter: true,
            scaleX: false,
            scaleContent: false,
            afterFinish: this.doLoad.bind(this), 
			queue: { scope: 'XWindow', position: 'end' } ,
			duration: 0.3,
            delay: 0.2
			}
		);
		
        new Effect.Parallel([ 
			 new Effect.SlideDown('XWindowHeader', { sync: true }), 
			 new Effect.Appear('XWindowHeader', { sync: true, from: 0.0, to: 1.0 })], 
			 { duration: 0.2, delay: 0.3, queue: { scope: 'XWindow', position: 'end' }}
		);
		
   },

    doHide: function () {
		
		var queue = Effect.Queues.get('XWindow');
		queue.each(function(effect) { effect.cancel(); });
	
		new Effect.Parallel([ new Effect.Move('XWindow', { sync: true, x: 400, y: 0, mode: 'relative' }),  
						  new Effect.Opacity('XWindow', { sync: true, from: 1, to: 0 }) 
						], 
						{  afterFinish: function() { $('XWindow').hide(); } , duration: 1.5, queue: { scope: 'XWindow', position: 'end' }} );
		
		
		
		
		this.eOverlay.hide();
		
		if(this.options.onClose !== null) {
			this.options.onClose();
		}
		
		var xWindowIframe = $('XWindowIframe');
		xWindowIframe.src = 'javascript: ;';
    },

    doLoad: function () {

		var size = $('XWindow').getDimensions();
		var top = parseInt(this.eWindow.getStyle('top'), 10) - 20;
        var left = parseInt(this.eWindow.getStyle('left'), 10);

		var xWindowIframe = $('XWindowIframe');
		xWindowIframe.src = this.options.url;
    }
});

var XWindowHelper = Class.create();

Object.extend(XWindowHelper.prototype, {
    initialize: function () {
        this.oWindow = new XWindowDo();
    },

    show: function (param) {
        this.oWindow.doShow(param);
    },

    hide: function () {
        this.oWindow.doHide();
    }

});
var XWindow;

function XWindowInit() {
    XWindow = new XWindowHelper();
}

Event.observe(window, 'load', XWindowInit);
