
function openWindow(file,name,width,height) {
	t=window.open(file,name, 'toolbar=no,menubar=no,scrollbars=no,resizable=yes,status=no,location=no,directories=no,top=60, left=100,width='+width+',height='+height+'');
	t.focus();
}

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{
		countfield.value = maxlimit - field.value.length;
	}
}


var _Browser = {
	ie: Browser.Engine.trident,
	ie6: Browser.Engine.trident4,
	ie7: Browser.Engine.trident5
};


var ModalWindow = new Class({
    initialize: function(o) {
        this.oContent = o.oContent;
        this.sContent = o.sContent;
        this.oWindowCss = o.oWindowCss;
    	this.sWindowClassName = 'modal-window';
        this.window = {};
        this.bg = null;
        this.width = 0;
        this.height = 0;

        this.createWindow();
    },
    
    show: function() {
        this.appendWindow();
        this.getPosition();
        this.setPosition();     
                
        this.scrollEvent = window.addEvent('scroll', function() {
            this.setPosition();
            this.setBackgroundPosition();
        }.bind(this));
        
        this.resizeEvent = window.addEvent('resize', function() {
            this.setPosition();
            this.setBackgroundPosition();
        }.bind(this));
    },
    
    close: function() {
        this.bg.dispose();
        this.window.dispose();
        window.removeEvent('scroll', this.scrollEvent);
        window.removeEvent('resize', this.resizeEvent);
    },
    
    insertContent: function(content) {
    	this.window.innerHTML = content;
    },

    getPosition: function() {
        this.width = this.window.getStyle('width').toInt();
    	this.height = this.window.getStyle('height').toInt();
        
        if (isNaN(this.width) || isNaN(this.hieght)) {
        	var size = this.window.getSize();
            this.width = size.x;
            this.height = size.y;
        }
    },
    
    setPosition: function() {
        this.placeCenter();
    },
      
    placeCenter: function() {
    	var obszar = window.getSize();
        var scroll = window.getScroll();
        
        var l = Math.ceil(obszar.x / 2 - (this.width/2));
        var t = Math.ceil(obszar.y / 2 - (this.height/2));
             
        this.window.style.top = t + scroll.y + 'px';
        this.window.style.left = l + scroll.x + 'px';
    },
    
    createWindow: function() {
    	this.window = new Element('div', {'class': this.sWindowClassName});
        if (this.oContent) this.window.appendChild(this.oContent);
        else if (this.sContent) this.insertContent(this.sContent);
        else this.insertContent('');
        
        if (this.oWindowCss) {
            for (var key in this.oWindowCss) {
                this.window.style[key] = this.oWindowCss[key];
            }
            this.window.style.zIndex = '9999';
        }
    },
    
    appendWindow: function() {
    	var place = document.body;
        this.createBackground();
        place.insertBefore(this.window, place.firstChild);
    },
    
    createBackground: function() {
    	this.bg = new Element('div', {
            'styles': {
                'position' : 'absolute',
                'background-color' : '#000',
                'z-Index' : '9998',
                'overflow' : 'hidden'
            }
        });
        
        this.setBackgroundPosition();
        
        if (_Browser.ie)
        	this.bg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+60+')';
        else
            this.bg.style.opacity = 0.6;
        
        this.bg.inject(document.body, 'top');
    },
    
    setBackgroundPosition: function() {
        var zxc = window.getScrollSize();
        this.bg.setStyles({
            'top': 0,
            'left': 0,
            'width': zxc.x + 'px',
            'height': zxc.y + 'px'
        });
    }
});


window.addEvent('domready', function() {
    if (document.getElementById('prezentacja-trigger')) {
        $('prezentacja-trigger').addEvent('click', function(e) {
            e.stop();
            var okno = new Element('iframe', {
                'class': 'prezentacja',
                'src': 'http://www.tourdesk.pl/index-prezentacja.html',
                'styles': {
                    'width': '100%',
                    'height': '100%'
                }
            });  
           
            var asd = new ModalWindow({
                oContent: okno,
                oWindowCss: {
                    width: '980px',
                    position: 'absolute',
                    height: '600px',
                    backgroundColor: '#fff'
                }
            });
            
            var close = new Element('a', {
                'href': 'javascript:void(0)',
                'class': 'prezentacja' + ((_Browser.ie6) ? ' prezentacja-ie' : ''),
                'events': {
                    'click': function() {
                    	asd.close();
                    }
                }
            }).inject(asd.window, 'top');   
                
            asd.show();
        });
    }
});