/**
 *	Reading Festival - Javascript Tools
 *
 *	@date		2008-01-08
 *	@author		Michael Giuliano
 *	@copyright	Live Nation (Music) UK
 */
 

   
/**
 *  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
 *
 *  param   divId               [string]    Accodion Block ID
 *  param   displayClass        [string]    Trigger Class Name
 *  param   stretcherClass      [string]    Stretcher Class Name
 *  param   onActive            [function]  Function called after the Accordion effect
 *  param   onActiveArgs        [array]     onActive Function Parameters
 *  param   disableIE6          [boolean]   enable/disable accordion effect for IE<=6 browser
 *  param   openFirstElement    [boolean]   open first accordion stretcher
 */
var StartEffects = {

    divId: null,
    displayClass: null,
    stretcherClass: null,
    onActive: null,
    onActiveArgs: new Array(),
    disableIE6: false,
    openFirstElement: false,
    
    init: function(args) {
    
        this.disableIE6 = args[5];
        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];
            this.displayClass = args[1];
            this.stretcherClass = args[2];
            this.onActive = args[3];
            this.onActiveArgs = args[4];
            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");
                Content.setParam(aTags[0], 'originalClassName', aTags[0].className);
                aTags[0].onclick = function() {
                    return false;
                };
            }
            
            // accordion effect
	        var myAccordion = new fx.Accordion(
		        toggles, 
		        stretchers, 
		        {opacity: true, duration: 300}, 
		        this.onActive,
		        this.onActiveArgs
	        );
	        
	        // open first div
	        var qs = location.href.split('?')[1];
	        if(qs != 'undefined' && qs != null) {
                var acc_id;
                if(qs.indexOf('&') > 0) { // Categories present
                    acc_id = qs.split('&')[0].split('=')[1];
                } else {
                    acc_id = qs.split('=')[1];
                }
                myAccordion.showThisHideOpen(stretchers[acc_id-1]);
            } else {
                // Default - open first div
                this.openFirstElement = args[6];
	            if(this.openFirstElement) {
	                myAccordion.showThisHideOpen(stretchers[0]);
	            } else {
	                // Hide loader
                    if($('loader') != null) {
                        Content.hide($('loader'));
                    }
                }
            }
            
	    }
	}
	
};


/**
 *  Activates the link with the "active" class
 *  And initialises the Content functions 0689295381
 */
var ActiveEffects = {
    
    mode: '',
    obj: null,
    params: new Array(),
    tracker: 0,    
    
    init: function(m, o, a) {
        this.mode = m;
        this.obj = o;
        this.params = a;
        var aTag = this.obj.getElementsByTagName('a')[0];
        switch(this.mode) {
            case 'open':
                aTag.className = 'active';
                break;
            case 'close':
                aTag.className = aTag.originalClassName;
                break;
            case 'closeAndOpen':
                aTag.className = aTag.originalClassName;
                break;
        }
        
        // Call the Content init function - only once
        if(this.params != null) {
            if(this.tracker <= 0) {
                this.tracker++;
                var args = new Array();
                args = this.params[1];
                var func = this.params[0];
                func(args);
                
                // Hide loader
                if($('loader') != null) {
                    Content.hide($('loader'));
                }
            }
        }
        
        
    }
    
};




/**
 *  Content page Functions
 *  Reveals content when header is clicked
 *
 *  param   blockId         [string]    ID containing all the contents blocks
 *  param   blockClass      [string]    Block Class Name
 *  param   headersId       [string]    ID containing all the headers
 *  param   headersClass    [string]    Header Class Name (optional)
 *
 *  Headers and Contents format:
 *      - name_id
 *      - name_cid::id
 */
var Content = {
    
    blockId: null,
    blockClass: null,
    headersId: null,
    headersClass: null,
    
    blocks: new Array(),
    headers: new Array(),
    hasSubCat: false,
    loaderEl: null,
    blurbEl: null,
    
    init: function(args) {
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = args[2];
        this.headersClass = args[3];
        
        // defines whether a header is within a category (ie: in an accordion)
        if(this.headersClass != null) this.hasSubCat = true;
        
        this.loaderEl = $('loader');
        this.blurbEl = $('blurb');
        
        // 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) {
                Content.setParam(el[i], 'id', el[i].id.split('_')[1]);
                Content.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++) {
            if(this.hasSubCat) {
                if(aTags[i].className == this.headersClass) {
                    Content.setParam(aTags[i], 'id', aTags[i].id.split('_')[1]);
                    aTags[i].onclick = function() {
                        Content.showBlock(this);
                        return false;
                    }
                    Content.headers.push(aTags[i]);
                }
            } else {
                Content.setParam(aTags[i], 'id', aTags[i].id.split('_')[1]);
                aTags[i].onclick = function() {
                    Content.showBlock(this);
                    return false;
                }
                Content.headers.push(aTags[i]);
            }
        }
        
        // Show correct block function of query string
        var offset = 0;
        var qs = location.href.split('?')[1];
        
        if(qs != 'undefined' && qs != null) { 
            // The ID is present in the QueryString - show the corresponding block
            var contentId;
            if(this.hasSubCat) {
                var priId = qs.split('&')[0].split('=')[1];
                var secId = qs.split('&')[1].split('=')[1];
                contentId = priId + "::" + secId;
                Content.showBlock(contentId);
            } else {
                contentId = qs.split('=')[1];
                Content.showBlock(contentId);
            }
        } else if(this.blurbEl == null) {
            // The ID is not present and there is no blurb - show the 1st block
            Content.showBlock(Content.headers[0]);
        } else {
            // The ID is not present and there is a blurb
            //  - show the blurb (already showing => nothing to do)
            //  - hide the loader
            if(this.loaderEl != null) Content.hide(this.loaderEl);
        }
        
    },
    
    showBlock: function(el) {
        var l = Content.blocks.length;
        if(parent.hasSubCat) {
            // Headers are inside an accordion: ie: subcategory of a main category
            var activeEl = new Array(null);        
            if(typeof(el) == 'object') { // Action triggered by a click event
                var cat_obj = el.id.split('::')[0];
                var obj = el.id.split('::')[1];
            } else {
                var cat_obj = el.split('::')[0];
                var obj = el.split('::')[1];
            }
            for(i=0;i<l;i++) {
                if(Content.blocks[i].id.split('::')[0] == cat_obj) {
                    if(Content.blocks[i].id.split('::')[1] == obj) {
                        Content.display(Content.blocks[i]);
                        activeEl = cat_obj + "::" + obj;
                    } else {
                        Content.hide(Content.blocks[i]);
                    }
                } else {
                    Content.hide(Content.blocks[i]);
                }                
            }        
        } else {
            // Headers are singletons
            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(Content.blocks[i].id == obj) {
                    Content.display(Content.blocks[i]);
                    activeEl = obj;
                } else {
                    Content.hide(Content.blocks[i]);
                }
            }
        }
        
        // Activate active header
        if(activeEl != null) Content.activate(activeEl);
        
        // Hide blurb
        if(Content.blurbEl != null) Content.hide(Content.blurbEl);
        
        // Hide loader
        if(Content.loaderEl != null) Content.hide(Content.loaderEl);
        
    },
    
    display: function(el) {
        el.style.display = 'block';
    },
    
    hide: function(el) {
        el.style.display = 'none';
    },
    
    activate: function(el) {
        var l = Content.headers.length;
        if(parent.hasSubCat) {
            if(typeof(el) == 'object') { // Action triggered by a click event
                var cat_obj = el.id.split('::')[0];
                var obj = el.id.split('::')[1];
            } else {
                var cat_obj = el.split('::')[0];
                var obj = el.split('::')[1];
            }
            for(i=0;i<l;i++) {
                if(Content.headers[i].id.split('::')[0] == cat_obj) {
                    if(Content.headers[i].id.split('::')[1] == obj) {
                        Content.headers[i].className = 'active';
                    } else {
                        Content.headers[i].className = '';
                    }
                } else {
                    Content.headers[i].className = '';
                }
            }
        } else {
            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(Content.headers[i].id == obj) {
                    Content.headers[i].className = 'active';
                } else {
                    Content.headers[i].className = '';
                }
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    }
    
};






























/**
 *  Ticket page Functions
 *
var Tickets = {
    
    blockId: null,
    blockClass: null,
    headersId: null,
    
    ticketBlocks: new Array(),
    ticketDays: new Array(),
    
    init: function(args) {
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = $(args[2]);
                
        // 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) {
                var tmp = el[i];
                Tickets.setParam(tmp, 'name', el[i].title);
                Tickets.ticketBlocks.push(tmp);
            }
        }
        
        // Disable headers links & add click events
        var aTags = this.headersId.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            aTags[i].onclick = function() {
                Tickets.showInfo(this);
                return false;
            }
            Tickets.ticketDays.push(aTags[i]);
        }
        
        // Show first info block & activate 05 July
        Tickets.showBlock(Tickets.ticketBlocks[2]);
        Tickets.activate(Tickets.ticketDays[2]);
        
    },
    
    showInfo: function(el) {
        var l = Tickets.ticketBlocks.length;
        var activeEl = null;
        for(i=0;i<l;i++) {
            if(Tickets.getParam(Tickets.ticketBlocks[i], 'name') == el.title) {
                Tickets.showBlock(Tickets.ticketBlocks[i]);
                activeEl = el;
            } else {
                Tickets.hideBlock(Tickets.ticketBlocks[i]);
            }
        }
        Tickets.activate(activeEl);
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = '';
    },
    
    activate: function(el) {
        var l = Tickets.ticketDays.length;
        for(i=0;i<l;i++) {
            if(Tickets.ticketDays[i].title == el.title) {
                Tickets.ticketDays[i].className = 'active';
            } else {
                Tickets.ticketDays[i].className = '';
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};



/**
 *  Info page Functions
 *
var Info = {
    
    blockId: null,      // ID containing all the info blocks
    blockClass: null,   // Class of a info block
    headersId: null,    // ID containing the info headers
    
    infoBlocks: new Array(),
    infoHeaders: new Array(),
    
    init: function(args) {
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = $(args[2]);
                
        // 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) {
                var tmp = el[i].id.split('::');
                Info.setParam(el[i], 'catId', tmp[0]);
                Info.setParam(el[i], 'contentId', tmp[1]);
                Info.infoBlocks.push(el[i]);
            }
        }
        
        // Disable headers links & add click events
        var aTags = this.headersId.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            if(aTags[i].className == 'faq_clickable') {
                var tmp = aTags[i].id.split('::');
                Info.setParam(aTags[i], 'catId', tmp[0]);
                Info.setParam(aTags[i], 'contentId', tmp[1]);
                aTags[i].onclick = function() {
                    Info.showInfo(this);
                    return false;
                }
                Info.infoHeaders.push(aTags[i]);
            }
        }
        
        // Show first info block & activate first day
        Info.showBlock(Info.infoBlocks[0]);
        Info.activate(Info.infoHeaders[0]);
        
    },
    
    showInfo: function(el) {
        var l = Info.infoBlocks.length;
        var activeEl = null;
        for(i=0;i<l;i++) {
            if(Info.getParam(Info.infoBlocks[i], 'catId') == Info.getParam(el, 'catId')) {
                if(Info.getParam(Info.infoBlocks[i], 'contentId') == Info.getParam(el, 'contentId')) {
                    Info.showBlock(Info.infoBlocks[i]);
                    activeEl = el;
                } else {
                    Info.hideBlock(Info.infoBlocks[i]);
                }                
            } else {
                Info.hideBlock(Info.infoBlocks[i]);
            }
        }
        Info.activate(activeEl);
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = '';
    },
    
    activate: function(el) {
        var l = Info.infoHeaders.length;
        for(i=0;i<l;i++) {
            if(Info.getParam(Info.infoHeaders[i], 'catId') == Info.getParam(el, 'catId')) {
                if(Info.getParam(Info.infoHeaders[i], 'contentId') == Info.getParam(el, 'contentId')) {
                    Info.infoHeaders[i].className = 'active';
                } else {
                    Info.infoHeaders[i].className = '';
                }                
            } else {
                Info.infoHeaders[i].style.color = '';
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};



/**
 *  Lineup page Functions
 *
var Lineup = {
    
    blockId: null,      // ID containing all the info blocks
    blockClass: null,   // Class of a info block
    headersId: null,    // ID containing the info headers
    
    lineupBlocks: new Array(),
    lineupHeaders: new Array(),
    
    init: function(args) {
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = $(args[2]);
                
        // 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) {
                Lineup.setParam(el[i], 'dayId', el[i].id);
                Lineup.lineupBlocks.push(el[i]);
            }
        }
        
        // Disable headers links & add click events
        var aTags = this.headersId.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            Lineup.setParam(aTags[i], 'dayId', aTags[i].id);
            aTags[i].onclick = function() {
                Lineup.showInfo(this);
                return false;
            }
            Lineup.lineupHeaders.push(aTags[i]);
        }
        
        // Show correct block function of query string
        var offset = 0;
        var qs = location.href.split('?')[1];
        if(qs != 'undefined' && qs != null) {
            var day = qs.split('=')[1];
            switch(day) {
                case '03': offset = 0; break;
                case '04': offset = 1; break;
                case '05': offset = 2; break;
                case '06': offset = 3; break;
            }
            Lineup.showBlock(Lineup.lineupBlocks[offset]);
            Lineup.activate(Lineup.lineupHeaders[offset]);
        } else {
            // Default
            Lineup.showBlock(Lineup.lineupBlocks[2]);
            Lineup.activate(Lineup.lineupHeaders[2]);
        }
        
    },
    
    showInfo: function(el) {
        var l = Lineup.lineupBlocks.length;
        var activeEl = null;
        for(i=0;i<l;i++) {
            if(Lineup.getParam(Lineup.lineupBlocks[i], 'dayId') == Lineup.getParam(el, 'dayId')) {
                Lineup.showBlock(Lineup.lineupBlocks[i]);
                activeEl = el;   
            } else {
                Lineup.hideBlock(Lineup.lineupBlocks[i]);
            }
        }
        Lineup.activate(activeEl);
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = '';
    },
    
    activate: function(el) {
        var l = Lineup.lineupHeaders.length;
        for(i=0;i<l;i++) {
            if(Lineup.getParam(Lineup.lineupHeaders[i], 'dayId') == Lineup.getParam(el, 'dayId')) {
                Lineup.lineupHeaders[i].className = 'active';
            } else {
                Lineup.lineupHeaders[i].className = '';
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};*/
