/*

DONG Energy
JavaScript functions
	
Author: Creuna Danmark A/S / www.creuna.dk
Copyright: 2009, Creuna Danmark A/S. All rights resevered
Version: 8 - 03.02.09 - 13:00

-----------------------------------------------------------------------*/

var Site = {

    options:
	{
	    tools: 'tools',
	    print: '.print',
	    enlarge: '.enlarge-text',

	    splashItems: '#splash dl',
	    splashLinks: 'splash-links',
	    splashDefault: 0,

	    ie: Browser.Engine.trident,
	    ie6: Browser.Engine.trident4
	},

    domready: function() {
        if ($(Site.options.tools))
            Site.initTools();

        if ($$('.tabs .tab-toggler a').length > 1)
            Site.Tabs.initialize();

        if ($$('dl.accordion').length > 0)
            Site.initAccordion();
    },

    load: function() {
        if ($(Site.options.splashLinks))
            Site.initSplash();
    },

    initSplash: function() {
        var links = $(Site.options.splashLinks).getElement('.bg').setProperty('class', 'bg clearfix pos' + Site.options.splashDefault);
        var linkbg = links.getStyle('background-image');
        // Variable used to ensure that default tab 1 in dongenergylevel2frontpage/erhverv is deselected when mouseover on other tabs
        var tabID = 1;

        $$(Site.options.splashItems).each(function(item, i) {

            // Add mouse events
            item.addEvents({
                'mouseover': function() {
                var controlWith = 100;
                    // variable to specify which tab is selected
                    var j = i + 1;

                    if ($('splash').getElement('img.splash') != null) {
                        controlWith = $('splash').getElement('img.splash').getStyle('width').toInt();
                    } else if ($('flashBackround' + j) != null) {
                        controlWith = $('flashBackround' + j).style.width.toInt()
                    }

                    if (controlWith > 695) {
                        links.setStyles({
                            'background-image': 'none',
                            'background-color': '#ffffff'
                        });
                    } else {
                        if (links.getStyle('background-color') != 'transparent')
                            links.setStyle('background-color', 'transparent');

                        if (links.getStyle('background-image') != linkbg)
                            links.setStyle('background-image', linkbg);
                    }
                    
                    Site.changeBackground(tabID, j);
                    links.setProperty('class', 'bg clearfix pos' + i);
                    tabID = i + 1;
                },
                'mouseout': function() {
                    Site.rewindMovie();
                    links.setProperty('class', 'bg clearfix');
                }
            });
        });
    },
    // function to change style.display for the different backgrounds, depending on tab mouseover
    changeBackground: function(tabID, j) {
        if (document.getElementById('tabBackground')) {
            //var j = i + 1;
            document.getElementById('tabBackground' + tabID).style.display = 'none';
            document.getElementById('tabBackground' + j).style.display = 'block';
        }
    },
    // function that stops and rewinds flashfiles when the tab is deselected
    rewindMovie: function() {
        if (document.getElementById('tabBackground')) {
            var alltabs = document.getElementById('tabBackground').childNodes;
            for (i = 0; i < alltabs.length; i++) {

                var j = i + 1;
                if (document.getElementById('flashBackround' + j)) {
                    var movie = document.getElementById('flashBackround' + j);
                    movie.rewind();
                }
            }
        }
    },

    initTools: function() {
        var print = $(Site.options.tools).getElement(Site.options.print);
        var enlarge = $(Site.options.tools).getElement(Site.options.enlarge);

        if (print) {
            // Print page
            print.addEvent('click', function(e) {
                new Event(e).stop(window.print());
            });
        }

        if (enlarge) {
            // Enlarge body text
            enlarge.addEvent('click', function(e) {
                new Event(e).stop();

                var body = $(document.body);

                if (body.getStyle('font-size') == '62.5%' || body.getStyle('font-size') == '10px') {
                    body.setStyle('font-size', '72.5%');
                    enlarge.addClass('enlarged');
                }
                else {
                    body.setStyle('font-size', '62.5%');
                    enlarge.removeClass('enlarged');
                }
            });
        }
    },

    Tabs:
	{
	    initialize: function() {
	        $$('.tabs').each(function(tabs, t) {
	            tabs.getElement('.tab-toggler li').addClass('selected');
	            tabs.getElement('.tab-content .element').setStyle('display', 'block');

	            // Hide tabs in IE
	            if (Site.options.ie == true) {
	                Site.Tabs.showHideTabs(0, tabs);
	                Site.Tabs.addRemoveSelectedTab(0, tabs);
	            }

	            // Add click event
	            tabs.getElements('.tab-toggler a').each(function(toggler, i) {
	                toggler.addEvent('click', function(event) {
	                    event.stop();

	                    Site.Tabs.showHideTabs(i, tabs);
	                    Site.Tabs.addRemoveSelectedTab(i, tabs);
	                });
	            });

	            // Check for anchor in url in first tab container
	            if (t == 0) {
	                var regex = new RegExp("[#]([^$]*)");
	                var href = regex.exec(location.href);

	                if (href[1]) {
	                    var anchor = href[1].split('tab')[1] - 1;

	                    if (!(anchor > ($$('.tab-toggler li').length - 1))) {
	                        Site.Tabs.showHideTabs(anchor, tabs);
	                        Site.Tabs.addRemoveSelectedTab(anchor, tabs);
	                    }
	                }
	            }
	        });
	    },

	    showHideTabs: function(current, tabs) {
	        // Show/hide element
	        tabs.getElements('.tab-content .element').each(function(element, i) {
	            if (i == current)
	                element.setStyle('display', 'block');
	            else
	                element.setStyle('display', 'none');
	        });
	    },

	    addRemoveSelectedTab: function(current, tabs) {
	        // Add/remove selected classname
	        tabs.getElements('.tab-toggler a').each(function(toggler, i) {
	            if (i == current)
	                toggler.getParent().addClass('selected');
	            else
	                toggler.getParent().removeClass('selected');
	        });
	    }
	},

    initAccordion: function() {
        Site.removeClickEvent($$('#content dl.accordion dt a'));

        var accordion = new Accordion('#content dl.accordion dt a', '#content dl.accordion dd', {
            opacity: false,
            show: 'none',
            alwaysHide: true,
            start: 'all-closed',
            duration: 400,
            onActive: function(toggler, element) {
                toggler.addClass('selected');
            },
            onBackground: function(toggler, element) {
                toggler.removeClass('selected');
            }
        });
    },

    removeClickEvent: function(elements) {
        $$(elements).each(function(element) {
            element.addEvent('click', function(e) {
                e = new Event(e).stop();
            });
        });
    }
};

window.addEvent('domready', Site.domready);
window.addEvent('load', Site.load);