/* Terms and conditions Popup Javascript */

function Terminos_Popup(popup_id, link_id, close_link_search, close_callback, open_callback)
{
    this.popup_id = popup_id;
    this.popup = $(popup_id);
    this.link_id = link_id;
    this.link = $(link_id);
    this.close_link_search = close_link_search;

    if(typeof close_callback != 'undefined') {
        this.close_callback = close_callback;
    }
    else    {
        this.close_callback = false;
    }

    if(typeof open_callback != 'undefined') {
        this.open_callback = open_callback;
    }
    else    {
        this.open_callback = false;
    }  

    var onestepcheckout_popup = this;

    if(this.link != null)    {
        this.link.observe('click', function(e)    {

            e.preventDefault();
            onestepcheckout_popup.show();
        });
    }

    var close_link = $$(this.close_link_search);
    if(close_link.length > 0)    {
        close_link[0].observe('click', function(e)    {
            e.preventDefault();
            onestepcheckout_popup.hide();
        });
    }

    this.overlay = $('onestepcheckout_popup_overlay');

    this.overlay.observe('click', function(e)   {
        onestepcheckout_popup.hide();
    });

}

Terminos_Popup.prototype.show_overlay = function()
{
    this.overlay.show();

    var dimensions = document.viewport.getDimensions();
    var offset = document.viewport.getScrollOffsets();

    var dimensions_new = {
            height: (dimensions.height + offset.top) + 'px',
            width: (dimensions.width) + 'px'
    }

    this.overlay.setStyle(dimensions_new);

    /* IE Hack */
    var IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf('MSIE')+5)) == 6;

    if(IE6)    {
        $$('select').invoke('hide');
    }

}

Terminos_Popup.prototype.show = function()
{
    this.show_overlay();
    this.popup.show();
    this.center();

    if(this.open_callback) {
        this.open_callback();
    }
}

Terminos_Popup.prototype.hide = function()
{
    this.overlay.hide();

    /* IE Hack */
    var IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf('MSIE')+5)) == 6;

    if(IE6)    {
        $$('select').invoke('show');
    }

    this.popup.hide();

    if(this.close_callback) {
        this.close_callback();
    }
}

Terminos_Popup.prototype.center = function()
{
    var dimensions = document.viewport.getDimensions();
    var popup_width = this.popup.getWidth();
    var popup_height = this.popup.getHeight();
    var offset = document.viewport.getScrollOffsets();

    var width = dimensions.width;
    var left = (width / 2) - (popup_width / 2);

    var height = dimensions.height;
    var top = (height / 2) - (popup_height / 2) + offset.top;

    this.popup.setStyle({
        top: top + 'px',
        left: left + 'px'
    });
}
