﻿/*
GLOBAL UI FUNCTIONS /CLASSES
Namespaces
- MindTheGap.Web.UI.Helpers

Classes
- MindTheGap.Web.UI.Helpers.ImageManager
- MindTheGap.Web.UI.Helpers.Hashtable

*/
Type.registerNamespace('MindTheGap.Web.UI.Helpers');

/* BEGIN: MindTheGap.Web.UI.Helpers.ImageManager 

Methods: 
- Initialize(bool useCache)
- EnableCache()
- DisableCache()
- ClearCache() 
- GetImage(string imageUrl) >> case insensitive url 
- IsImageLoaded(Image imageToCheck) >> checks if the image has been loaded or not

*/
MindTheGap.Web.UI.Helpers.ImageManager = function()
{
    this._cache = new Array(); //Initializes the cache object
    this._useCache = true; // sets the initial cache usage to enables
}

MindTheGap.Web.UI.Helpers.ImageManager.prototype =
{
    Initialize: function(bUseCache) {
        this._useCache = bUseCache;

    },
    EnableCache: function() {
        this._useCache = true;
    },
    DisableCache: function() {
        this._useCache = false;
    },
    ClearCache: function() {
        this._cache = new Array();
    },
    GetImage: function(sUrl) {
        var lUrl = sUrl.toLowerCase();
        var img = this._cache[lUrl];
        if (img == null) {
            //img = new Image();
            img = document.createElement('img');
            img.src = sUrl;

            if (this._useCache)
                this._cache[lUrl] = img;
        }

        return img;
    },
    LoadImage: function(sUrl) {
        var img = this.GetImage(sUrl);
    },
    IsImageUrlLoaded: function(sImageUrl) {
        var img = this.GetImage(sImageUrl);
        return this.IsImageLoaded(img);
    },
    SetAlpha: function(oImage, iAlpha) {
        if (oImage == null)
            return;

        var absoluteAlpha = Math.max(Math.min(iAlpha, 100), 0);
        var percAlpha = absoluteAlpha / 100;

        if (typeof (oImage.style.filter) != 'undefined') {
            if (absoluteAlpha < 100)
                oImage.style.filter = 'alpha(opacity=' + absoluteAlpha + ')';
            else
                oImage.style.filter = null;
        }


        if (typeof (oImage.style.MozOpacity) != 'undefined') {
            if (absoluteAlpha < 100)
                oImage.style.MozOpacity = percAlpha;
            else
                oImage.style.MozOpacity = null;

        }

        if (typeof (oImage.style.opacity) != 'undefined') {
            if (absoluteAlpha < 100)
                oImage.style.opacity = percAlpha;
            else
                oImage.style.opacity = null;
        }

    },
    IsImageLoaded: function(img) {


        // During the onload event, IE correctly identifies any images that
        // weren't downloaded as not complete. Others should too. Gecko-based
        // browsers act like NS4 in that they report this incorrectly.
        if (!img.complete) {
            return false;
        }

        // However, they do have two very useful properties: naturalWidth and
        // naturalHeight. These give the true size of the image. If it failed
        // to load, either of these should be zero.
        if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
            return false;
        }

        // No other way of checking: assume it's ok.
        return true;
    }
}

MindTheGap.Web.UI.Helpers.ImageManager.registerClass('MindTheGap.Web.UI.Helpers.ImageManager');

/* END: MindTheGap.Web.UI.Helpers.ImageManager */

/* BEGIN: MindTheGap.Web.UI.Helpers.Hashtable */


MindTheGap.Web.UI.Helpers.Hashtable = function() 
{ 
    this._hash = new Array(); 
    this._keys = new Array(); 
    this._getEnum = new Array(); 
    this._count = 0;
}

MindTheGap.Web.UI.Helpers.Hashtable.prototype = 
{ 
    getAt : function(index) {  return this._hash[this._getEnum[index]]; },  
    get : function (key) {  return this._hash[key]; }, 
    remove : function (key) 
    {  
        for (var i = this._keys.length - 1; i >= 0; i--) 
        {   if (this._keys[i] == key) 
            {    
                this._keys.splice(i, 1);    
                this._getEnum.splice(i, 1);    
                this._hash[key] = null;    
                this._count = this._keys.length;   
            }  
        } 
    }, 
    put : function (key, value) 
    {  
        if (value == null)   return null;  
        if (this._hash[key] == null) 
        {   
            this._keys[this._keys.length] = key;   
            this._count = this._keys.length;   
            this._getEnum[this._count - 1] = key;  
        }  
        this._hash[key] = value; 
    }
}

MindTheGap.Web.UI.Helpers.Hashtable.registerClass('MindTheGap.Web.UI.Helpers.Hashtable');

/* END: MindTheGap.Web.UI.Helpers.Hashtable */

/* START GENERAL HELPER MindTheGap.Web.UI.Helpers.GlobalHelper  */
MindTheGap.Web.UI.Helpers.GlobalHelper = function()
{
    
}
MindTheGap.Web.UI.Helpers.GlobalHelper.prototype =
{
    ShowAlert: function(sMessage) {
        alert(sMessage);
    },
    ShowAlertAndCallBack: function(sMessage, fnCallback) {
        alert(sMessage);
        return fnCallback();

    },
    GetContainerWidth: function() {

        return $get('container').clientWidth;
    },
    GetContainerHeight: function() {

        return $get('container').clientHeight;
    },
    GetTopBarHeight: function() {

        return $get('topNavigator').clientHeight;
    },
    GetInnerHeight: function() {

        return this.GetContainerHeight() - this.GetTopBarHeight()-15;
    },
    GetMenuWidth: function() {
        return $get('lMenuContainer').clientWidth;
    }
    ,
    GetInnerWidth: function() {
   
        return this.GetContainerWidth() - this.GetMenuWidth()-15;
    }
    ,
    //Thank you, Quirksmode
    FindOffsetTop: function(o) {
        if (o == null)
            return 0;
        var t = 0;
        if (o.offsetParent) {
            while (o.offsetParent) {
                t += o.offsetTop;
                o = o.offsetParent;
            }
        }
        return t;
    }

}

var _lm = null;
var _maxWidth = 950;
var h = 0;
function InitLayoutManager() {
    h = 0;
    if (_lm == null) {
  
        _lm = setInterval(LayoutPerform, 100);
     }
 }
 function LayoutPerform() {
    
    var div = $get('layoutArea');
    if (div != null) {
       
        if (GlobalHelper.GetInnerHeight() != h) {
            div.style.width = Math.min(GlobalHelper.GetInnerWidth(),_maxWidth) + 'px';
            div.style.height = GlobalHelper.GetInnerHeight() + 'px';
            div.style.visibility = 'visible';
            
            CreateMtgScroller();
                
/*
            var scr = $get('ScollerContainer');
            if (scr != null) {
                var htmp = scr.offsetHeight - scr.offsetTop; ;
                //window.status = "Htmp: " + htmp;
                ScrollInit(htmp);
            }*/
            h = GlobalHelper.GetInnerHeight();
        }
        
    }


}

function SetPressScrollers() {

    var el1 = $get("Scroller-1"); // Ad Campaign
    var el2 = $get('Scroller-1Bis'); // News

    if (el1 != null && el2!=null) {
        ScrollInitForce2(); 
        DoInitScroll('Scroller-1Bis', 'Scrollbar-Container2');

        try {
            $removeHandler(window, 'resize', SetPressScrollers);
        }
        catch (e) { }
        $addHandler(window, 'resize', SetPressScrollers);
    }
    else {
        try {
            $removeHandler(window, 'resize', SetPressScrollers);
        }
        catch (e) { }
     }
 }

MindTheGap.Web.UI.Helpers.GlobalHelper.registerClass('MindTheGap.Web.UI.Helpers.GlobalHelper');

/* END GENERAL HELPER MindTheGap.Web.UI.Helpers.GlobalHelper  */

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 

var GlobalHelper = new MindTheGap.Web.UI.Helpers.GlobalHelper();
var GlobalImageManager = new MindTheGap.Web.UI.Helpers.ImageManager();
GlobalImageManager.Initialize(true);