(function(){

    var tag_line = {
        config: {
            id: 'tagline',
            classname: 'tagline'
        },
        init: function() {
            try {
                Selector.query('div.' + this.config.classname)[0].style.display = 'none';
            } catch(ex) {
                // Do Nothing
            }
        }
    };
    Event.onAvailable(tag_line.config.id, tag_line.init, tag_line, true);

    var marketing_panel = {
        config: {
            start_at: 0,
            interval: 8500,
            anim_speed: 1.75,
            trigger_id: 'hp_mk_ldr',
            tab_class_name: 'mk-tab',
            pnl_class_name: 'mk-pnl',
            default_class_name: 'default',
            current_class_name: 'current'
        },
        init: function() {
            this.tabs = Selector.query('.' + this.config.tab_class_name, this.config.trigger_id);
            this.panels = Selector.query('.' + this.config.pnl_class_name, this.config.trigger_id);
            this.total = this.panels.length;
            // Only start the fading effect if there is more than 1 panel to show
            if (this.total > 1) {
                this.current_item = this.config.start_at;
                this.next_item = this.current_item + 1;
                // If we want to start the animations from a different panel we need to change some classnames around
                if (this.config.start_at != 0) {
                    Dom.removeClass(this.panels, this.config.default_class_name);
                    Dom.addClass(this.panels[this.config.start_at], this.config.default_class_name);
                };
                // Hide the other panels by setting there opacity to '0'
                Dom.setStyle(Selector.query('.' + this.config.pnl_class_name + ':not(.' + this.config.default_class_name + ')', this.config.trigger_id), 'opacity', '0');
                // Call the fade panel function after a preset time
                setTimeout(this.fade_panel, this.config.interval);
                // Add the current tabs class anem to the current selected panel
                this.set_tab(this.config.start_at);
            };
        },
        fade_panel: function() {
            var _self = marketing_panel;
            // Set the current items zindex to be one more than the next items, then set the next items
            // opacity to 1 so it will be visible when the current item is faded out. Did it this way as
            // didnt want 2 animations (fadein & fadeout) to be running at the same time.
            Dom.setStyle(_self.panels[_self.current_item], 'z-index', _self.total);
            Dom.setStyle(_self.panels[_self.next_item], 'z-index', _self.total - 1);
            Dom.setStyle(_self.panels[_self.next_item], 'opacity', '1');
            // Create a new animation to fade out the current item
            var _fadeout = new YAHOO.util.Anim(_self.panels[_self.current_item], { 
                opacity: { to: 0 }  
            }, _self.config.anim_speed, YAHOO.util.Easing.easeOut);
            // Subscribe to the animation oncomplete event, change tab selected, but only after panel has faded
            _fadeout.onStart.subscribe(function() {
                setTimeout(_self.set_tab(_self.next_item), _self.config.interval * 0.75);
            });
            _fadeout.animate();
            // Once we've reached the last item, reset to the first item
            _self.current_item++;
            if (_self.current_item > _self.total - 1) {
                _self.current_item = 0;
            };
            // before we reach the last item, reset this next item to the first item
            _self.next_item = _self.current_item + 1;
            if (_self.next_item == _self.total) {
                _self.next_item = 0;
            }
            // Call the fade panel function - callback
            setTimeout(_self.fade_panel, _self.config.interval);
        },
        set_tab: function(t) {
            var _self = marketing_panel;
            Dom.removeClass(_self.tabs, _self.config.current_class_name);
            Dom.addClass(_self.tabs[t], _self.config.current_class_name);
        }
    }
    Event.onAvailable(marketing_panel.config.trigger_id, marketing_panel.init, marketing_panel, true);


    var tabbed_ads = {
        config: {
            list_classname: 'advert-list',
            img_height: 65,
            img_width: 65
        },
        init: function() {
            var _lists = Selector.query('.' + this.config.list_classname), // Cache the advert lists
                _parentEl = _lists[0].parentNode, // Cache the parent node of the existing ad lists
                _divEl = document.createElement('div'); // Create placeholder for new ad list
                _divEl.id = 'advert_gallery';
                _divEl.className = 'gallery';

            Dom.insertBefore(_divEl, _lists[0]); // insert the new placholder above the existing ad lists

            try {
                var _newAdsList = new YAHOO.widget.TabView(); // create new yui tabview control

                for (i = 0; i < _lists.length; i++) {
                    // Resize the gallery images y updating image src attribute rather than strecthing with css.
                    var _images = _lists[i].getElementsByTagName('img');

                    for (j = 0; j < _images.length; j++) {
                        var _image = _images[j],
                            _src = _image.src.split('?'),
                            _newSrc = _src.length > 1 ? _src[0] + '?t=tr/w:' + this.config.img_width + '/h:' + this.config.img_height + '/m:FitPad' : _src[0];

                        _image.src = _newSrc;
                        _image.title = _image.alt.split('-')[1];
                    };
                    // Add new tabs to the tabview control
                    _newAdsList.addTab( new YAHOO.widget.Tab({
                        label: _lists[i].getElementsByTagName('h2')[0].innerHTML,
                        content: '<ul class="content-wrapper">' + _lists[i].getElementsByTagName('ul')[0].innerHTML + '</ul>',
                        active: i == 0
                    }));
                    // remove the existing advert lists from the DOM
                    _parentEl.removeChild(_lists[i]);
                };
                Dom.addClass(_newAdsList, 'rhs-list advert-list'); // Add class names to style tabview in same way as existing advert lists
                _newAdsList.appendTo(_divEl); // add the tabview control to the placholder

            } catch (ex) {
                //console.log(ex);
            };
        }
    }
    Event.onDOMReady(tabbed_ads.init, tabbed_ads, true);

//    var link_test = {
//        config: {
//            id: 'tagline' 
//        },
//        init: function() {
//            anchors = Selector.query('ul.crawl li a');
//            
//            //console.log(anchors.length);

//            try {
//                this.build_placeholder();
//            } catch(ex) { };

//            for (var i = 0; i < anchors.length; i++) {
//                var _callback = {
//                    success: this.handle_success,
//                    failure: this.handle_failure,
//                    scope: this,
//                    argument: anchors[i].href
//                };
//                Connect.asyncRequest('GET', anchors[i].href, _callback, null);
//            };
//        },
//        build_placeholder: function() {
//            var body = document.getElementById('homepage');
//            var div = document.createElement('div');
//                div.style.border = '1px solid #000';
//                div.style.background = '#fff';
//                div.style.position = 'absolute';
//                div.style.padding = '10px';
//                div.style.top = '100px'
//                div.style.left = '10px'
//                div.style.textAlign = 'left';
//            var sUl = document.createElement('ul');
//                sUl.id = 'sUl';
//                sUl.style.marginTop = '15px';
//            var sLi = document.createElement('li');
//                sLi.style.fontWeight = '900';
//                sLi.appendChild(document.createTextNode('Success:'));
//                sUl.appendChild(sLi);
//            var fUl = document.createElement('ul');
//                fUl.id = 'fUl';
//            var fLi = document.createElement('li');
//                fLi.style.fontWeight = '900';
//                fLi.appendChild(document.createTextNode('Failure:'));
//                fUl.appendChild(fLi);

//                div.appendChild(fUl);
//                div.appendChild(sUl);

//            body.appendChild(div);
//        },
//        handle_success: function(o) {
//            var list = document.getElementById('sUl');
//            var el = document.createElement('li');
//                el.style.color = '#0f0';
//                el.appendChild(document.createTextNode(o.argument.substring(21)));
//                
//                list.appendChild(el);
//        },
//        handle_failure: function(o) {
//            var list = document.getElementById('fUl');
//            var el = document.createElement('li');
//                el.style.color = '#f00';
//                el.appendChild(document.createTextNode(o.argument.substring(21) + ' - (' + o.status + ')'));
//                
//                list.appendChild(el);
//        }
//    };
//    Event.onDOMReady(link_test.init, link_test, true);

})();