/**
 *	O2 Wireless 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'));
                }
            }
        }
        
        
    }
    
};



/**
 *  Home page Functions
 */
var Home = {
    
    blockId: null,      // ID containing all the home blocks [Details and Video]
    titlesClass: null,
    detailsClass: null,
    videosClass: null,
    
    titles: new Array(),
    details: new Array(),
    videos: new Array(),
    
    init: function(args) {
        
        this.blockId = args[0];
        this.titlesClass = args[1];
        this.detailsClass = args[2];
        this.videosClass = args[3];
        this.aClass = args[4];
                
        // Build 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.titlesClass) {
                Home.setParam(el[i], 'bid', el[i].id.split('_')[1]);
                Home.titles.push(el[i]);
            } else if(el[i].className == this.detailsClass) {
                Home.setParam(el[i], 'bid', el[i].id.split('_')[1]);
                Home.details.push(el[i]);
            } else if(el[i].className == this.videosClass) {
                Home.setParam(el[i], 'bid', el[i].id.split('_')[1]);
                Home.videos.push(el[i]);
            }
        }
        
        // Disable switch links & add click events
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            if(aTags[i].className == 'home_title') {
                Home.setParam(aTags[i], 'bid', aTags[i].id.split('_')[1]);
                aTags[i].onclick = function() {
                    Home.showInfo(this);
                    return false;
                }
            } else if(aTags[i].className == 'home_video') {
                Home.setParam(aTags[i], 'bid', aTags[i].id.split('_')[1]);
                aTags[i].onclick = function() {
                    Home.showInfo(this);
                    return false;
                }
            } else if(aTags[i].className == 'home_back') {
                Home.setParam(aTags[i], 'bid', aTags[i].id.split('_')[1]);
                aTags[i].onclick = function() {
                    Home.showInfo(this);
                    return false;
                }
            }
        }
        
        // Display First Disposition
        Home.showDefault();
        
    },
    
    showInfo: function(el) {
        var el_action = el.id.split('_')[0];
        var el_id = el.id.split('_')[1];
        switch(el_action) {
            case 'title':
                var l = Home.titles.length;
                for(i=0;i<l;i++) {
                    if(Home.getParam(Home.titles[i], 'bid') == el_id) {
                        Home.hideBlock(Home.titles[i]);
                        Home.showBlock(Home.details[i]);
                    } else {
                        Home.showBlock(Home.titles[i]);
                        Home.hideBlock(Home.details[i]);                        
                    }
                }
                l = Home.videos.length;
                for(i=0;i<l;i++) {
                    Home.hideBlock(Home.videos[i]);
                }
                break;
            case 'click':
                var l = Home.videos.length;
                for(i=0;i<l;i++) {
                    var ll = Home.details.length;
                    for(j=0;j<ll;j++) {
                        if(Home.details[j].bid == el_id) {
                            Home.showBlock(Home.titles[j]);
                            Home.hideBlock(Home.details[j]);
                        } else {
                            Home.hideBlock(Home.titles[j]);
                            Home.hideBlock(Home.details[j]);
                        }                        
                    }
                    if(Home.videos[i].bid == el_id) {
                        Home.showBlock(Home.videos[i]);
                    } else {
                        Home.hideBlock(Home.videos[i]);
                    }
                }
                break;
            case 'back':
                var l = Home.videos.length;
                for(i=0;i<l;i++) {
                    var ll = Home.details.length;
                    for(j=0;j<ll;j++) {
                        if(Home.details[j].bid == el_id) {
                            Home.hideBlock(Home.titles[j]);
                            Home.showBlock(Home.details[j]);
                            Home.hideBlock(Home.videos[i]);
                        } else {
                            Home.showBlock(Home.titles[j]);
                        }
                    }
                }
                break;               
        }   
        
    },
    
    showDefault: function() {
        var d = new Date();
        var day = d.getDay();
        var date = d.getDate();
        
        /** To remove post festival **/
        if(day == 4 && date == 3) {
            Home.showBlock(Home.titles[0]);
		    Home.showBlock(Home.titles[1]);
		    Home.showBlock(Home.titles[2]);
            Home.showBlock(Home.details[3]);
        } else if(day == 5 && date == 4) {
            Home.showBlock(Home.titles[0]);
		    Home.showBlock(Home.titles[1]);
		    Home.showBlock(Home.titles[3]);
            Home.showBlock(Home.details[2]);
        } else if(day == 6 && date == 5) {
            Home.showBlock(Home.titles[0]);
		    Home.showBlock(Home.titles[2]);
		    Home.showBlock(Home.titles[3]);
            Home.showBlock(Home.details[1]);
        } else if(day == 0 && date == 6) {
            Home.showBlock(Home.titles[1]);
		    Home.showBlock(Home.titles[2]);
		    Home.showBlock(Home.titles[3]);
            Home.showBlock(Home.details[0]);
        } else {
            Home.showBlock(Home.titles[0]);
		    Home.showBlock(Home.titles[1]);
		    Home.showBlock(Home.titles[2]);
            Home.showBlock(Home.details[3]);
        }
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = 'none';
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};



/**
 *  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 03 July
        //Tickets.showDefault();
        
    },
    
    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];
    }
    
};*/



/**
 *  Weekender page Functions
 */
var Weekender = {
    
    blockId: null,
    blockClass: null,
    headersId: null,
    blurbId: null,
    
    weBlocks: new Array(),
    weDays: new Array(),
    weBlurb: null,
    
    init: function(args) {
        
        this.blockId = args[0];
        this.blockClass = args[1];
        this.headersId = args[2];
        this.blurbId = args[3];
        
        // Create Blurb div
        this.weBlurb = document.createElement('div');
        $(this.blockId).appendChild(this.weBlurb);
        
        // 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];
                var tmpId = tmp.id.split('_')[1];
                Weekender.setParam(tmp, 'id', tmpId);
                Weekender.weBlocks.push(tmp);
            }
        }
        
        // Disable headers links & add click events
        var aTags = $(this.headersId).getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            var tmp = aTags[i];
            var tmpId = tmp.id.split('_')[1];
            Weekender.setParam(tmp, 'id', tmpId);
            tmp.onclick = function() {
                Weekender.showInfo(this);
                return false;
            }
            Weekender.weDays.push(tmp);
        }
        
        
        // 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
            var iid = qs.split('=')[1];
            Weekender.showInfo(iid);
        } else { // Default - Show blurb block
            // Get content from existing hidden element on the page
            this.weBlurb.innerHTML = $(this.blurbId).innerHTML;
        }
        
    },
    
    showInfo: function(el) {
        Weekender.hideBlock(parent.weBlurb);
        var l = Weekender.weBlocks.length;
        var activeEl = null;
        if(typeof(el) == 'object') { // The function is called by a header on the page
            for(i=0;i<l;i++) {
                if(Weekender.getParam(Weekender.weBlocks[i], 'id') == el.id) {
                    Weekender.showBlock(Weekender.weBlocks[i]);
                    activeEl = el;
                } else {
                    Weekender.hideBlock(Weekender.weBlocks[i]);
                }
            }
        } else { // The ID is present in the QueryString
            for(i=0;i<l;i++) {
                if(Weekender.getParam(Weekender.weBlocks[i], 'id') == el) {
                    Weekender.showBlock(Weekender.weBlocks[i]);
                    activeEl = el;
                } else {
                    Weekender.hideBlock(Weekender.weBlocks[i]);
                }
            }
        }
        if(activeEl != null) {
            Weekender.activate(el);
        }
        Weekender.activate(activeEl);
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = 'none';
    },
    
    activate: function(el) {
        var l = Weekender.weDays.length;
        if(typeof(el) == 'object') { // The function is called by a header on the page
            for(i=0;i<l;i++) {
                if(Weekender.weDays[i].id == el.id) {
                    Weekender.weDays[i].className = 'active';
                } else {
                    Weekender.weDays[i].className = '';
                }
            }
        } else { // The ID is present in the QueryString
            for(i=0;i<l;i++) {
                if(Weekender.weDays[i].id == el) {
                    Weekender.weDays[i].className = 'active';
                } else {
                    Weekender.weDays[i].className = '';
                }
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};



/**
 *  Partners page Functions
 */
var Partners = {
    
    blockId: null,      // ID containing all the info blocks
    blockClass: null,   // Class of a info block
    headersId: null,    // ID containing the info headers
    
    partnersBlocks: new Array(),
    partnersHeaders: 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) {
                Partners.setParam(el[i], 'pId', el[i].id);
                Partners.partnersBlocks.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++) {
            Partners.setParam(aTags[i], 'pId', aTags[i].id);
            aTags[i].onclick = function() {
                Partners.showInfo(this);
                return false;
            }
            Partners.partnersHeaders.push(aTags[i]);
        }
        
    },
    
    showInfo: function(el) {
        var l = Partners.partnersBlocks.length;
        for(i=0;i<l;i++) {
            if(Partners.getParam(Partners.partnersBlocks[i], 'pId') == Partners.getParam(el, 'pId')) {
                Partners.showBlock(Partners.partnersBlocks[i]);
            } else {
                Partners.hideBlock(Partners.partnersBlocks[i]);
            }
        }
    },
    
    showBlock: function(el) {
        el.style.display = 'block';
    },
    
    hideBlock: function(el) {
        el.style.display = '';
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    },
    
    getParam: function(e, param) {
        return e[param];
    }
    
};



/**
 *  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) {
        Content.blockId = args[0];
        Content.blockClass = args[1];
        Content.headersId = args[2];
        Content.headersClass = args[3];
        
        // defines whether a header is within a category (ie: in an accordion)
        if(Content.headersClass != null) Content.hasSubCat = true;
        
        Content.loaderEl = $('loader');
        Content.blurbEl = $('blurb');
        
        // Build info blocks objects
        var block = $(Content.blockId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for(i=0;i<el_length;i++) {
            if(el[i].className == Content.blockClass) {
                Content.setParam(el[i], 'id', el[i].id.split('_')[1]);
                Content.blocks.push(el[i]);
            }
        }
        
        // Disable headers links & add click events
        var block = $(Content.headersId);
        var aTags = block.getElementsByTagName('a');
        var aL = aTags.length;
        for(i=0;i<aL;i++) {
            if(Content.hasSubCat) {
                if(aTags[i].className == Content.headersClass) {
                    Content.setParam(aTags[i], 'id', aTags[i].id.split('_')[1]);
                    Content.setParam(aTags[i], 'originalClassName', aTags[i].className);
                    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]);
                Content.setParam(aTags[i], 'originalClassName', aTags[i].className);
                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(Content.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(Content.blurbEl == null) {
            // The ID is not present and there is no blurb - show the 1st block
            if(Content.hasSubCat) { 
                Content.showBlock(Content.headers[0]);
            } else {
                Content.showDefault();
            }
        } else {
            // The ID is not present and there is a blurb
            //  - show the blurb (already showing => nothing to do)
            //  - hide the loader
            if(Content.loaderEl != null) Content.hide(Content.loaderEl);
        }
        
    },
    
    showDefault: function() {
        var d = new Date();
        var day = d.getDay();
        var date = d.getDate();
        
        /** To remove post festival **/
        if(day == 4 && date == 3) {
            Content.showBlock(Content.headers[0]);
        } else if(day == 5 && date == 4) {
            Content.showBlock(Content.headers[1]);
        } else if(day == 6 && date == 5) {
            Content.showBlock(Content.headers[2]);
        } else if(day == 0 && date == 6) {
            Content.showBlock(Content.headers[3]);
        } else {
            Content.showBlock(Content.headers[0]);
        }
    },
    
    showBlock: function(el) {
        var l = Content.blocks.length;
        if(Content.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(Content.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 = Content.headers[i].originalClassName + 'IsActive';
                    } else {
                        Content.headers[i].className = Content.headers[i].originalClassName;
                    }
                } else {
                    Content.headers[i].className = Content.headers[i].originalClassName;
                }
            }
        } 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 = Content.headers[i].originalClassName + 'IsActive';
                } else {
                    Content.headers[i].className = Content.headers[i].originalClassName;
                }
            }
        }
    },
    
    setParam: function(e, param, value) {
        e[param] = value;
    }
    
};
