﻿var Popup = new Class({
    Implements:[Options, Events],
    options: {
        width: 500,
        height: 300,
        x: 50,
        y: 50,
        toolbar: 0,
        location: 0,
        directories: 0,
        status: 0,
        scrollbars: 'auto',
        resizable: 1,
        name: 'popup',
        onBlock: $empty
    },
    initialize: function(url, options){
        this.url = url || false;
        this.setOptions(options);
        if(this.url) this.openWin();
    },
    openWin: function(url){
        url = url || this.url;
        var options = 
            'toolbar='+this.options.toolbar+
            ',location='+this.options.location+
            ',directories='+this.options.directories+
            ',status='+this.options.status+
            ',scrollbars='+this.options.scrollbars+
            ',resizable='+this.options.resizable+
            ',width='+this.options.width+
            ',height='+this.options.height+
            ',top='+this.options.y+
            ',left='+this.options.x;
        this.window = window.open(url, this.options.name, options);
        if (!this.window) {
                this.window = window.open('', this.options.name, options);
                this.window.location.href = url;
        }
        this.focus.delay(100, this);
        return this;
    },
    focus: function(){
        if (this.window) this.window.focus();
        else if (this.focusTries < 10) this.focus.delay(100, this); //try again
        else {
                this.blocked = true;
                this.fireEvent('onBlock');
        }
        return this;
    },
    focusTries: 0,
    blocked: null,
    close: function(){
        this.window.close();
        return this;
    }
});


window.addEvent('domready', function(){
       
    var getPollVal = function(name)
    {
        var val = -1;
        var e = $$('input[name=' + name + ']');

        for (var i = 0; i < e.length; i++)
        {
            if (e[i].checked)
            {
                val = e[i].get('value');
                break;
            }
        }
        
        return val;                
    }
    
    var view = function(id, val){
        var url = 'Poll.aspx?id=' + id + '&val=' + val;
        
        var width	= window.getWidth();
        var height	= window.getHeight();
        
        var popupTop = (height - 150) / 2;
        var popupLeft = (width - 710) / 2;
        
        new Popup(url, {
            width: 710,
            height: 270,
            x: popupLeft,
            y: popupTop
        });
    }

    if($defined($('poll-voting')) && $defined($('poll-result')))
    {
        $('poll-voting').addEvent('click', function(){
            var id = $('poll-question-id').get('value');
            var val = getPollVal('poll-answer');
            
            if(val > 0)
            {
                view(id, val);
            }
            else
            {
                alert('Bạn chưa chọn câu trả lời.');
            }
        });    
    
        $('poll-result').addEvent('click', function(){
            var id = $('poll-question-id').get('value');
            var val = -1;
            
            view(id, val);        
        });
    }       
});
