window.addEvent('domready', function(){
    
    /* accordion
    *************************************************************************/
    if($('accordion')){
        // set a default for the accordion function
        var aIndex = false;
        // get the current URL
        var currenturl = document.location.href;
        // get a reference to all <h4 class="atStart"> and <ul class="atStart"> tags
        var togglers = $$('h4.toggler');
        var elements = $$('ul.element');
        // loop through each of the <ul> tags
        elements.each(function(element, index){
            // get all <a> tags within the <ul>
            var aTags = element.getElements('a');
            // loop through all of the <a> tags
            aTags.each(function(aTag){
                // get the URL for the current link
                var linkurl = aTag.getProperty('href');
                // see if current URL matches the href of the current link
                if(currenturl.indexOf('/'+linkurl) != -1 || currenturl.indexOf(linkurl) != -1){
                    aIndex = index; // set aIndex to the current index number
                }
            });
        });
        // create the accordion with necessary options
        var accordion = new Accordion(togglers, elements, {
            opacity: false,
            display: false,
            show: false,
            duration: 0,
            alwaysHide: true,
    		onActive: function(toggler, element){
    		    toggler.setStyle('background', 'transparent url(http://corporate.discovery.com/media/static/images/toggler-open.png) no-repeat 97% 50%')
    		    toggler.setStyle('color', '#99cc00')
    		    toggler.setStyle('border-bottom', '1px solid #254656')
    		},
    		onBackground: function(toggler, element){
    		    toggler.setStyle('background', 'transparent url(http://corporate.discovery.com/media/static/images/toggler-closed.png) no-repeat 97% 50%')
    		    toggler.setStyle('color', '#84cffe')
    		    toggler.setStyle('border-bottom', '1px solid #041D2B')
    		}
        });
        // show the correct accordion menu
        if(aIndex !== false) accordion.display(aIndex);
    }
    
    /* open external links in a new window
    *************************************************************************/
    var aTags = $$('a');
    aTags.each(function(aTag, index){
        var url = aTag.get('href');
        if(url !== null){
            //console.log(url.contains('corporate.discovery.com'));
            if(url.contains('http://') && !url.contains('corporate.discovery.com')){
                aTag.addEvent('click', function(e){
                    e.preventDefault();
                    window.open(url);
                });
            }
        }
    });
    
    /* simple search slide-open box
    *************************************************************************/
    if($('simple-search') && $('nav-search') && $('search-close')){
		var searchActive = false;
	        
        
        // reference some elements
        var searchBox = $('simple-search');
		var searchGutter = $$('#simple-search .gutter');
        var searchNav = $('nav-search');
        var closeLink = $('search-close');
        var searchButton = $$('#nav .search');
        
        //searchButton.removeClass('activated');
		
        
        // set the tween options for the search box
        searchBox.set('tween', {
            duration: 250,
            transition: Fx.Transitions.Quad.easeOut
        });
        
        // get the initial height
        var initialHeight = searchBox.getStyle('height').toInt();
        
        // tell the nav search btn what to do
        searchNav.addEvent('click', function(e){
            e.preventDefault();
            
            if (searchActive === false) {
                searchBox.tween('height', 0, 52);
                searchButton.addClass('activated');
            	searchActive = true;
            } else if (searchActive === true) {
                searchBox.tween('height', 52, 0);
                searchButton.removeClass('activated');
            	searchActive = false;
            }
            
            
            
        });
        // locate the search terms <input> tag
        var searchTerms = $('id_search_terms');

        // get the initial value of the search field
        var termsText = searchTerms.value;

        // fade in the color on the search terms field when focused
        searchTerms.addEvent('focus', function(e){
            if(this.value == termsText) this.value = '';
        });

        // fade out the color on the search terms field when blurred
        searchTerms.addEvent('blur', function(e){
            if(this.value == '') this.value = termsText;
        });
        
        // close the box on link click
        closeLink.addEvent('click', function(e){
            searchBox.tween('height', 52, 0);
            searchButton.removeClass('activated');
			// var myFx = new Fx.Style(searchBox, 'height').start(52, 0);
			searchActive = false;
        });
    }
    
    /* news search sidebar features
    *************************************************************************/
    if($('news-search')){
        var newsSearch = $('news-search');
        var frm = newsSearch.getElement('form');
        var label = newsSearch.getElement('label');
        label.set('tween', {duration: 250});
        var resultsDiv = $('news-ajax-results');
        var initLabelPosition = label.getStyle('left');
        var terms = $('news-search-field');
        terms.value = '';
        // fade in the color on the search terms field when focused
        terms.addEvent('focus', function(e){
            if(this.value == '') {
                label.fade('out');
                this.value = '';
            }
        });

        // fade out the color on the search terms field when blurred
        terms.addEvent('blur', function(e){
            if(this.value == '') label.fade('in');
        });
        
        // tell the form what to do when submit is attempted
        frm.addEvent('submit', function(e){
            e.preventDefault();
            if(terms.value != ''){
                var ajaxURL = frm.getProperty('action') + 'ajax/';
                frm.set('send', {
                    url: ajaxURL,
                    method: 'get',
                    onComplete: function(response){
                        resultsDiv.empty();
                        resultsDiv.set('html', response);
                    }
                });
                frm.send();
            }
        });
    }
    
    /* site Swiff injection
    *************************************************************************/
    if($('logo-train')){
        var trainOptions = {
            'container': $('logo-train'),
            'id': 'logo-train-swf',
            'width': 650,
            'height': 60,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var trainSWF = new Swiff('http://corporate.discovery.com/media/static/swf/logo-train.swf', trainOptions);
    }
    
    if($('banner')){
        var bannerOptions = {
            'container': $('banner'),
            'id': 'banner-swf',
            'width': 881,
            'height': 204,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var bannerSWF = new Swiff('/media/static/swf/billboard/billboard.swf', bannerOptions);
    }
    
    if($('photo-wall')){
        var photoWallOptions = {
            'container': $('photo-wall'),
            'id': 'photo-wall-swf',
            'width': 541,
            'height': 400,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var photoWallSWF = new Swiff('/media/static/swf/photo-wall/photo-wall.swf', photoWallOptions);
    }
    
    if($('vplayer')){
        var vplayerOptions = {
            'container': $('vplayer'),
            'id': 'vplayer-swf',
            'width': 352,
            'height': 385,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var photoWallSWF = new Swiff('/media/static/swf/vplayer/vplayer.swf', vplayerOptions);
    }
    
    if($('timeline-flash')){
        var timelineOptions = {
            'container': $('timeline-flash'),
            'id': 'timeline-flash-swf',
            'width': 550,
            'height': 390,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var photoWallSWF = new Swiff('/media/static/swf/timeline.swf', timelineOptions);
    }
    
    if($('timeline-flash-test')){
        var timelineOptions = {
            'container': $('timeline-flash-test'),
            'id': 'timeline-flash-swf',
            'width': 550,
            'height': 390,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var photoWallSWF = new Swiff('/media/static/swf/timeline-test.swf', timelineOptions);
    }
    
    if($('logowall-flash')){
        var logowallOptions = {
            'container': $('logowall-flash'),
            'id': 'logowall-flash-swf',
            'width': 550,
            'height': 400,
            'params': {
                'wmode': 'transparent'
            },
            'vars': {
                
            }
        };
        var photoWallSWF = new Swiff('/media/static/swf/logo-wall.swf', logowallOptions);
    }
    
});
