var Merlin = new function() {

    $(document).ready(function() {
        loadMenu();
        loadHomePageHighlights();
        
    	$( ".accordion" ).accordion({ header: 'h4', collapsible: true, autoHeight: false, icons: false });
    	
    	var hostname = window.location.hostname.toLowerCase();
    	$('a').each( function() {
    	    var href = $(this).attr('href').toLowerCase();
    	    if (href.indexOf('http://') != -1 && href.indexOf(hostname) == -1) {
    	        $(this).attr('target', '_blank');
    	        $(this).attr('rel', 'external');
	        }
	    });
	    
        // scaleBackground();
    });
    
    var rotate_highlights = true;
    
    this.showHighlight = function(number, user_action) {
        if (!user_action && !rotate_highlights) {
            return;
        }
        if (user_action) {
            rotate_highlights = false;
        }
        if (number == 'next') {
            number = $('.highlight-active').attr('id').split('-')[1];
            number++;
            if (number > $('.highlight').size()) {
                number = 1;
            }
        }
        $('.highlight-active').fadeOut('fast', function() {
            $('.highlight-active').removeClass('highlight-active');
            $('#highlight-indicator img').attr('src', '/media/images/icon-indicator.png');
            highlight = $('#highlight-'+number);
            highlight.fadeIn('fast', function() {
                highlight.removeClass('highlight-inactive');
                highlight.addClass('highlight-active');
                $('#indicator-'+number).attr('src', '/media/images/icon-indicator-selected.png');
            });
        });
        if (rotate_highlights) {
            window.setTimeout( function() {
                Merlin.showHighlight('next', false);
            }, 7000);
        }
    };
    
    var loadHomePageHighlights = function() {
        if ($('#highlights').length) {
            if (!ie7) {
                window.setTimeout( function() {
                    Merlin.showHighlight('next', false);
                }, 7000);
            }
            else {
                Merlin.showHighlight(1, false);
            }
        }
    };
    
    var loadMenu = function() {
    	var levelOneItems = document.getElementById("global-nav-menu").getElementsByTagName("A");
    	var levelTwoItems = document.getElementById("global-nav-level2-menu").getElementsByTagName("UL");
	
    	var prevIndex;
	
    	// var headlines dynamically populated in site header template
	
    	$("ul#global-nav-menu > li > a").hover(function() {
    		var itemIndex = jQuery.inArray(this, levelOneItems);
    		var itemHeadline = headlines[itemIndex].text;

    		$(this).addClass('l1hover');
    		$("div#global-nav-level2").addClass('menu-expanded');
		
    		if (itemIndex != prevIndex) {
				$("li#global-nav-level2-header div").text(itemHeadline);
    		}

    		$("ul#global-nav-level2-menu ul:eq("+itemIndex+")").addClass('l2hover');
    	},
    	function() {
    		var itemIndex = jQuery.inArray(this, levelOneItems);
    		prevIndex = itemIndex;
		
    		$(this).removeClass('l1hover');
    		$("div#global-nav-level2").removeClass('menu-expanded');
    		$("ul#global-nav-level2-menu ul:eq("+itemIndex+")").removeClass('l2hover');
    	});

    	$("ul#global-nav-level2-menu ul").hover(function() {
    		var itemIndex = jQuery.inArray(this, levelTwoItems);
    		var itemHeadline = headlines[itemIndex].text;

    		$(this).addClass('l2hover');
    		$("div#global-nav-level2").addClass('menu-expanded');
		
    		if (itemIndex != prevIndex) {
				$("li#global-nav-level2-header div").text(itemHeadline);
    		}

    		$("ul#global-nav-menu > li:eq("+itemIndex+") > a").addClass('l1hover');
    	},
    	function() {
    		var itemIndex = jQuery.inArray(this, levelTwoItems);
    		prevIndex = itemIndex;
		
    		$(this).removeClass('l2hover');
    		$("div#global-nav-level2").removeClass('menu-expanded');
    		$("ul#global-nav-menu > li:eq("+itemIndex+") > a").removeClass('l1hover');
    	});
    };

    /*
    // Moving background image
    var background_height = 894;
    var background_direction = 'down';
    var scaleBackground = function() {
        if (background_direction == 'down') {
            background_height = background_height - 1;
        }
        else {
            background_height = background_height + 1;
        }
        if (background_height > 894) {
            background_height = 894;
            background_direction = 'down';
        }
        else if (background_height < 750) {
            background_height = 750;
            background_direction = 'up';
        }
        $('#background').height(background_height+'px');
        window.setTimeout(scaleBackground, 250);
    };
    */
    
    return this;
    
}();

