﻿/***
****  Loader function
****  Add a function to window.onload event
***/

function addLoadEvent(func, args) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = function() {
            func(args);
        }
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func(args);
        }
    }
}

/**
 *  Accordion Effects
 *  Initialises accordion, and desactivate links
 */
var StartEffects = {

    divId: null,
    displayClass: null,
    stretcherClass: null,
    specialEffects: null,
    disableIE6: false,
    openFirstElement: false,
    
    init: function(args) {
    
        this.disableIE6 = args[4];
        if(this.disableIE6) {
            var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
            var isIE6 = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
        } else {
            var isIE6 = false;
        }
        if (!isIE6) {
        
            this.divId = args[0];           // Block ID
            this.displayClass = args[1];    // Display block class name
            this.stretcherClass = args[2];  // Stretcher block class name
            this.specialEffects = args[3];  // Special effect to apply after the Accordion affect
            
            var stretchers = document.getElementsByClassName(this.stretcherClass);
            var toggles = document.getElementsByClassName(this.displayClass);
            
            // disable link
            var tl = toggles.length;
            for(i=0; i<tl; i++) {
                var aTags = toggles[i].getElementsByTagName("a");
                aTags[0].onclick = function() {
                    return false;
                };
            }
        
	        // accordion effect
	        var myAccordion = new fx.Accordion(
		        toggles, stretchers, {opacity: true, duration: 200}, this.specialEffects
	        );
	        
	        // open first div
	        this.openFirstElement = args[5];
	        if(this.openFirstElement) {
	            if(this.openFirstElement == 'second') {
	                myAccordion.showThisHideOpen(stretchers[1]);
	            } else {
	                myAccordion.showThisHideOpen(stretchers[0]);
	            }
	        }
	        
	    }
	}
	
};


/**
 *  Activates the link with the "active" class
 */
var ActiveEffects = {
    
    mode: '',
    obj: null,
    
    init: function(m, o) {
        this.mode = m;
        this.obj = o;
        var aTag = this.obj.getElementsByTagName('a')[0];
        switch(this.mode) {
            case 'open':
                aTag.className = 'active';
                break;
            case 'close':
                aTag.className = '';
                break;
            case 'closeAndOpen':
                aTag.className = '';
                break;
        }
    }
    
};



/**
 *  Content Hide/Show Class
 *  Reveals content when header is clicked
 *
 *  @param  blockId         content blocks container ID
 *  @param  blockClass      content block Classname
 *  @param  heardersId      headers container ID
 *  @param  l_mode          loading mode: null, 'loader' or 'blurb'
 *  @param  loaderId        loader ID
 *  @param  blurbId         blurb ID
 */
var Content = Class.create();
Content.prototype = {
    
    blockId: '',
    blockClass: '',
    headersId: '',
    lmode: null,
    loaderId: '',
    blurbId: '',
    
    blocks: new Array(),
    headers: new Array(),
    
    initialize: function(args) {
        
        var caller = this;
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = args[2];
        this.lmode = args[3];
        this.loaderId = args[4];
        this.blurbId = args[5];
                
        /** Build info blocks objects **/
        var block = $(this.blockId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for(i=0;i<el_length;i++) {
            if(el[i].className == this.blockClass) {
                this.blocks.push(el[i]);
            }
        }
        
        /** Disable headers links & add click events **/
        var block = $(this.headersId);
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            aTags[i].onclick = function() {
                caller.showBlock(this);
                return false;
            }
            this.headers.push(aTags[i]);
        }
        
        /** Show correct block **/
        var offset = 0;
        var qs = location.href.split('?')[1];
        if(qs != 'undefined' && qs != null) { // The info ID is present in the QueryString
            var contentId = qs.split('=')[1];
            this.showBlock(contentId);
        } else { 
            if(this.lmode != null) { // Default - Show first block            
                switch(this.lmode) { // Loader or blurb present                
                    // If loader - display first block
                    case 'loader':
                        this.showBlock(this.headers[0]);
                        break;
                    // If blurb - Insert content of blurb in loader
                    case 'blurb':
                        var txt = $(this.blurbId).innerHTML;
                        $(this.loaderId).className = '';
                        $(this.loaderId).innerHTML = txt;
                        break;
                }      
            } else {
                // No loader or blurb - only display block
                this.showBlock(this.headers[0]);
            }
        }
        
    },
    
    showBlock: function(el) {
        var l = this.blocks.length;
        var activeEl = null;
        if(typeof(el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for(i=0;i<l;i++) {
            if(this.blocks[i].id == obj) {
                this.display(this.blocks[i]);
                activeEl = obj;
            } else {
                this.hide(this.blocks[i]);
            }
        }
        
        // Activate active header
        if(activeEl != null) this.activate(activeEl);
        
        // Hide loader
        if(this.lmode != null) this.hide($(this.loaderId));
        
    },
    
    display: function(el) {
        el.style.display = 'block';
    },
    
    hide: function(el) {
        el.style.display = 'none';
    },
    
    activate: function(el) {
        var l = this.headers.length;
        if(typeof(el) == 'object') { // Action triggered by a click event
            var obj = el.id;
        } else {
            var obj = el;
        }
        for(i=0;i<l;i++) {
            if(this.headers[i].id == obj) {
                this.headers[i].className = 'active';
            } else {
                this.headers[i].className = '';
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    }
    
};