﻿/// <reference name="MicrosoftAjax.js"/>


var ns = "local.gigya.controls";
if (!Type.isNamespace(ns)) {
    Type.registerNamespace(ns);
}

local.gigya.controls.LightBox = function(element) {
    ///<summary>Initialize a new instance of the Notification class</summary>

    this._options = new Array();
    
    ///add canvas.
    this._canvas = new Element('div');
    this._canvas.id = element.id + '_canvas';
    this._canvas.setStyle({
        'background':'#fff',
        'position': 'absolute',
        'opacity': 0.75,
        'display': 'none',
        'zIndex': 3000
    });
    document.body.appendChild(this._canvas);
    
    
     ///add lightbox.
    this._lightbox = new Element('div');
    this._lightbox.id = element.id + '_lightbox';
    this._lightbox.setStyle({
        'position': 'absolute',
        'display':'none',
        'zIndex': 3000
    });
    document.body.appendChild(this._lightbox);

    Array.add(local.gigya.controls.LightBox.Collection,this);
    local.gigya.controls.LightBox.initializeBase(this, [element]);
}


local.gigya.controls.LightBox.prototype =
{

    get_key: function() {
        ///<summary>get the LightBox key</summary>
        return this._key;
    }

    , set_key: function(value) {
        ///<summary>Set the LightBox key</summary>
        this._key = value;
    }

    , get_text: function() {
        ///<summary>get the LightBox text</summary>
        return this._text;
    }

    , set_text: function(value) {
        ///<summary>Set the LightBox text</summary>
        this._text = value;
    }
    

    , get_error: function() {
        ///<summary>get the LightBox error</summary>
        return this._error;
    }

    , set_error: function(value) {
        ///<summary>Set the LightBox error</summary>
        this._error = value;
    }
    
    , get_theme: function() {
        ///<summary>get the LightBox theme</summary>
        return this._theme;
    }

    , set_theme: function(value) {
        ///<summary>Set the LightBox minTopOffset</summary>
        this._theme = value;
    }
    , get_minTopOffset: function() {
        ///<summary>get the LightBox minTopOffset</summary>
        
        var f = undefined;
        try
        {
            f = parseFloat(this._minTopOffset);
        }
        catch(e)
        {}
       
        return f;
    }

    , set_minTopOffset: function(value) {
        ///<summary>Set the LightBox minTopOffset</summary>
        this._minTopOffset = value;
    }
    
    ///Methods
    ,initialize : function() {
        local.gigya.controls.LightBox.callBaseMethod(this, 'initialize');
        
        var theme = this.get_theme();
        if(theme == undefined)
            theme = "";
        else
            theme += "_";
        this._canvas.addClassName(theme + 'lightbox_canvas');
        this._lightbox.addClassName(theme + 'lightbox_lightbox');
        
    }
    
    ,dispose : function() {
        local.gigya.controls.LightBox.callBaseMethod(this, 'dispose');
    }
    ,display: function()
    {
        this._display(this.get_text());
    }
    
    ,error: function()
    {
        this._display(this.get_error());
    }
    
    , _display: function(str) {
    
        ///fix ie bug (some how the first call fails with exception).
        try
        {
            $(this.get_element().id + '_canvas').clonePosition($(this.get_element()));
        }
        catch(e)
        {}
        
        $(this.get_element().id + '_canvas').clonePosition($(this.get_element()));
        $(this.get_element().id + '_canvas').show();

        $(this.get_element().id + '_lightbox').clonePosition($(this.get_element().id + '_canvas'),{
            setWidth:false,setHeight:false
        });
        
        $(this.get_element().id + '_lightbox').update(str);
        
        var left = (($(this.get_element().id + '_canvas').getWidth() - $(this.get_element().id + '_lightbox').getWidth())/ 2) + $(this.get_element().id + '_canvas').cumulativeOffset().left ;
        var top =  ((document.viewport.getDimensions().height - $(this.get_element().id + '_lightbox').getHeight()) / 2);
        var canvasTop = (($(this.get_element().id + '_canvas').getHeight() - $(this.get_element().id + '_lightbox').getHeight()) /2) + $(this.get_element().id + '_canvas').cumulativeOffset().top;
        
         
        ///make sure its inside the canvas.
        if(canvasTop<top)
        {
            top = canvasTop;
        }
        
        ///minmum top offset. 
        if(this.get_minTopOffset()!=undefined && this.get_minTopOffset()>top)
        {
            top = this.get_minTopOffset();
        }
        
        top += document.viewport.getScrollOffsets().top;
        
        $(this.get_element().id + '_lightbox').setStyle
        ({
            'left': left + 'px',
            'top': top + 'px'
        });
        
        $(this.get_element().id + '_lightbox').show();
    }

    ,hide: function() {
        $(this.get_element().id + '_canvas').hide();
        $(this.get_element().id + '_lightbox').hide();
    }



}


local.gigya.controls.LightBox.registerClass('local.gigya.controls.LightBox', Sys.UI.Behavior);


///Static
local.gigya.controls.LightBox.Collection = new Array();
local.gigya.controls.LightBox.getByKey = function(key)
{
    var lb = null;
    Array.forEach(this.Collection,function(item)
    {
        if(item.get_key() == key)
            lb = item;
    },null);
    
    return lb;
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();