GLDEModalWindow = Class.create({
	initialize: function(opt) {
		this.input = null; 
		this.myEffects = {
			closeButtonDuration: 0.1	
		};
		this.def = {
			closeOnClick: true,
            fade: true,
            fadeDuration: 0.4,
            overlayOpacity: 0.8,
            afterOpen: this.initClose.bind(this)
		}	
        this.modal = new Control.Modal(false, Object.extend(this.def, opt) );
	},
	show: function() {
        this.modal.open();
	},
	
	initClose: function() {
		this.input = $('modal_container').down('input');
		this.input.observe('mouseover', this.closeButtonEffect.bind(this));
		this.input.observe('mouseout',  this.closeButtonEffect.bind(this));
		this.input.observe('click', function() { this.modal.close(); }.bind(this));
	},
	closeButtonEffect: function() {
		new Effect.Opacity(this.input, { from: 1.0, to: 0.2, duration: this.myEffects.closeButtonDuration,
			afterFinish: function() {
				this.input.src = this.input.src.replace(/(.*?_)(un)?active\.(.+)/, function(str, p1, p2, p3, offset, s) { 
					return p1 + (p2 ? '' : 'un') + 'active.' + p3;
				});
				new Effect.Opacity(this.input, { from: 0.2, to: 1.0, duration: this.myEffects.closeButtonDuration});
			}.bind(this)
		});
	}
});

GLDE = Class.create({
	initialize: function() {
		this.modal = null;
		$$('a.externalhref').each(function(el) {
			el.observe('click', function(event) {
				Event.stop(event);
                this.login(el.href);
			}.bind(this));
		}.bind(this));
		if ($('gldeLogout')) {
			Event.observe('gldeLogout', 'click', this.logout.bind(this));
		}
	},
    login: function(href) {
        var urlLw = new UrlConstructor('glde/login_window');
        new Ajax.Request(urlLw.construct(), {
            method: 'post',
            parameters: 'src=' + encodeURIComponent(href),
            onComplete: function(transport) {
                this.modal = new GLDEModalWindow({
                    contents: transport.responseText
                }); 
                this.modal.show();
            }.bind(this)
        });
    },
	logout: function(event) {
		var el = Event.element(event);
		Event.stop(event);
        var url = new UrlConstructor('logout/glde');
		url.setAction(true);
		new Ajax.Request(url.construct(), {
                method: 'get',
                onComplete: function(transport){
					var obj = transport.responseText.evalJSON();
					var er = new ErrorManager(obj);
					if (!er.getErrors()) {
					   this.doExternalLogout(el).bind(this);
					}
				}.bind(this)
	    });
		return false;
	},
    doExternalLogout: function(el) {
		if (!el.href) {
			el = el.up('a');
		}
		var urlLw = new UrlConstructor('glde/logout_window');
		new Ajax.Request(urlLw.construct(), {
            method: 'post',
            parameters: 'src=' + encodeURIComponent(el.href),
            onComplete: function(transport) {
                this.modal = new GLDEModalWindow({
                	contents: transport.responseText,
                    afterClose: function () {
	                    setTimeout(function() {
	                        var url = new UrlSeoConstructor('HOME');
	                        window.location = url.construct();
	                    }.bind(this), 500);
                	}
                }); 
                this.modal.show();
			}.bind(this)
		});
    }
});

GLDEAvs = Class.create({
    initialize: function(info) {
        this.info = info;
        this.modal = null;
        this.avs();
    },
    avs: function() {
        var urlGldeAvs = new UrlConstructor('Glde/Avs');
        new Ajax.Request(urlGldeAvs.construct(), {
            onComplete: function(request) {
                this.modal = new Control.Modal(false, {
                    contents: request.responseText,
                    fade: true,
                    fadeDuration: 0.4,
                    opacity: 0.8,
                    width: 740
                });
                this.modal.open();
                this.initClick();
            }.bind(this)
        });
    },
    initClick: function() {
        Event.observe('avsButton', 'click', function() {
            var exteralUrl = getExternalUrl('https') + 'gamesload/gsp/login.aspx?provider=metaboli&avs=true&metaboli_avs=true' +
                    '&act=' + this.info.process + 
                    '&technology=' + this.info.technology + 
                    '&product_id=' + this.info.gameId + 
                    '&MasterID=' + this.info.technoId + 
                    '&commandLine=' + this.info.commandLine + 
                    '&st2=' + this.info.st2;
            var urlAvs = new UrlConstructor('glde/avs_window');
            new Ajax.Request(urlAvs.construct(), {
                method: 'post',
                parameters: 'src=' + encodeURIComponent(exteralUrl),
                onComplete: function(transport) {
                    this.modal = new GLDEModalWindow({
                        contents: transport.responseText
                    }); 
                    this.modal.show();
                }.bind(this)
            });
        }.bind(this));
    }

});

GLDELogin = Class.create({
    initialize: function() {
        if (!$('loginIFrame')) {return false;}
        if ($('loginIFrame').value == 1) {
            glde.login($('loginButtons').down().href);
        };
    }
});

function gldeNavigator() {
    var navigator = $('navigator');
    if (!navigator) return ;
    navigator.select('img.pngFix').each(function(el) {
         el.observe('mouseover', function() { this.src = $(this).next().value.replace(/(.+)\/(.+)(?=\.png)/, '$1/$2_hover'); });
         el.observe('mouseout', function()  { this.src = $(this).next().value });
    });
}

function GldeSizeScreen() {
    widthRightDecorElement = 0;
    switch (screen.width) {
        case 1152:
            widthRightDecorElement = 65;
            break;
        case 1024:
            widthRightDecorElement = 0;
            break;
        default:
            widthRightDecorElement = 104;
    }
    $('decorationRight').setStyle({
        width: widthRightDecorElement + 'px'
    });
}

Event.observe(window, 'load', function() {
    gldeNavigator();
    glde = new GLDE();
    new GLDELogin();
});
