﻿
var CTPopupWindow = Class.create({

    initialize: function(options) {
        this.options = Object.extend({
            loaded: false
        }, options || {});

        this.popupBackLay = new Element('div', { 'id': 'ctpopupwindowbacklay' });
        document.getElementsByTagName('body')[0].appendChild(this.popupBackLay);

        this.popup = new Element('div', { 'id': 'ctpopupwindow' });
        document.getElementsByTagName('body')[0].appendChild(this.popup);

        this.popupClose = new Element('div', { 'class': 'close' });
        this.popup.appendChild(this.popupClose);
        this.popupCloseButton = new Element('a', { 'href': '#', 'class': 'close' });
        this.popupClose.appendChild(this.popupCloseButton);

        this.popupContent = new Element('div', { 'class': 'content' });
        this.popup.appendChild(this.popupContent);
        this.popupImg = new Element('img', { 'class': 'popupimage' });
        this.popupContent.appendChild(this.popupImg);

        Event.observe(this.popupImg, 'load', this.resizeImageBox.bind(this), false);
        Event.observe(this.popupBackLay, 'click', this.hide.bind(this), false);
        Event.observe(this.popupImg, 'click', this.hide.bind(this), false);
        Event.observe(this.popupCloseButton, 'click', this.hide.bind(this), false);
    },

    openImage: function(url) {
        this.popupBackLay.style.display = 'block';
        this.popup.style.display = 'block';

        this.popupBackLay.style.visibility = 'hidden';
        this.popup.style.visibility = 'hidden';

        this.popup.style.height = '220px';
        this.popup.style.width = '200px';
        this.popup.style.marginLeft = ' -90px';

        this.loaded = true;
        this.popupImg.src = url;
    },

    hide: function() {
        this.popup.style.display = 'none';
        this.popupBackLay.style.display = 'none';
    },

    resizeImageBox: function() {
        if (!this.loaded) return;

        this.popup.style.height = this.popupImg.height + 20 + 'px';
        this.popup.style.width = this.popupImg.width + 'px';

        this.popup.style.marginLeft = (-this.popupImg.width / 2) - 10 + 'px';
        this.popup.style.marginTop = (-this.popupImg.height / 2) - 30 + 'px';

        this.popupBackLay.style.visibility = 'visible';
        this.popup.style.visibility = 'visible';

    }

});




/*-----------------------------------------------------------------------------------------------*/
Event.observe(window, 'load', CTPopupWindowInit, false);

var myCTPopupWindow = null;
function CTPopupWindowInit() {
    myCTPopupWindow = new CTPopupWindow();
}