var Site = {

    options:
	{
	    noDuration: 0,
	    splashDuration: 800,
	    splashDelay: 5000,
	    splashSlideDuration: 300,
	    projectDuration: 500,
	    formTips: 'dl.formSplit .tooltip'
	},

    domready: function() {
        if ($$('#splash .splashNav a').length > 1)
            Site.initializeSplashes();

        if ($$('ul.listVari li a.toggler').length > 0)
            Site.initializeShowHide();

        var tips = $$(Site.options.formTips);
        if (tips.length > 0)
            Site.initializeFormTips(tips);
    },

    load: function() {
        Site.mainNavFallback();
    },

    mainNavFallback: function() {
        var elements = $$('#mainnav li a');

        elements.each(function(link, i) {
            if ($ES('img', link).length != 1) {
                $E('span', link).setStyles({
                    'position': 'static',
                    'text-indent': 0
                });
            }
        });
    },

    initializeShowHide: function() {
        $$('.wide .group').each(function(e, i) {
            var ShowHide = new Accordion($ES('ul.listVari li a.toggler', e), $ES('ul.listVari li div.element', e), {
                display: false,
                opacity: false,
                height: true,
                alwaysHide: true,
                duration: Site.options.projectDuration,
                transition: Fx.Transitions.Quad.easeInOut,
                onActive: function(toggler, element) {
                    toggler.addClass('selected');
                },
                onBackground: function(toggler, element) {
                    toggler.removeClass('selected');
                }
            }, e);

            $ES('ul.listVari li a.toggler', e).each(function(element, i) {
                element.addEvent('click', function(c) {
                    new Event(c).stop();
                });
            });
        });
    },

    initializeSplashes: function() {
        var counter = new Element('div', { 'class': 'splashCounter' }).appendText(0);
        var splash = new Accordion('#splash .splashNav a', '#splash .splashContent .splashItem', {
            show: 0,
            opacity: true,
            height: false,
            duration: Site.options.splashDuration,
            transition: Fx.Transitions.Quad.easeInOut,
            onActive: function(toggler, element) {
                toggler.addClass('selected');
                var changeHeight = new Fx.Styles(element.getParent(), { duration: 300, wait: false });
                changeHeight.start({
                    height: element.offsetHeight
                });
            },
            onBackground: function(toggler, element) {
                toggler.removeClass('selected');
            }
        }, $('splash').adopt(counter));

        $$('#splash .splashNav a').each(function(link, i) {
            link.addEvent('click', function(e) {
                new Event(e).stop();
                var counter = $E('.splashCounter', splash);
                if (counter.innerHTML != 1)
                    counter.setHTML(1);
            });
        });

        var nextButton = $ES('a.next', splash);
        if (!(nextButton.length == 1))
            Site.nextSplashFocus(0, splash);
        else
            Site.initializeNextSplashButton(nextButton, splash);

        var previousButton = $ES('a.previous', splash);
        if (!(previousButton.length == 0))
            Site.initializePreviousSplashButton(previousButton, splash);
    },

    initializeNextSplashButton: function(nextButton, splash) {
        var elements = $ES('.splashNav a', splash);
        var length = elements.length;

        nextButton.addEvent('click', function(e) {
            new Event(e).stop();

            var next;

            elements.each(function(link, i) {
                if (link.hasClass('selected'))
                    next = i + 1;
            });

            if (next != length)
                splash.display(next);
        });

        splash.addEvent('onActive', function(toggler, element) {
            elements.each(function(link, i) {
                if (link == toggler) {
                    var changeOpacity = new Fx.Style(nextButton[0], 'opacity', { duration: Site.options.splashSlideDuration });

                    if (i == length - 1)
                        changeOpacity.start(0);
                    else if (nextButton[0].getStyle('opacity') != 1)
                        changeOpacity.start(1);
                }
            });
        });
    },

    nextSplashFocus: function(i, splash) {
        var length = $ES('.splashNav a', splash).length - 1;
        var first = 0;
        var next = i + 1;

        if ($E('.splashCounter', splash).innerHTML != 1) {
            if (i == length) {
                (function() {
                    if ($E('.splashCounter', splash).innerHTML != 1) {
                        splash.display(first);
                        Site.nextSplashFocus(first, splash);
                    }
                }).delay(Site.options.splashDelay);
            } else {
                (function() {
                    if ($E('.splashCounter', splash).innerHTML != 1) {
                        splash.display(next);
                        Site.nextSplashFocus(next, splash);
                    }
                }).delay(Site.options.splashDelay);
            }
        }
    },

    initializePreviousSplashButton: function(previousButton, splash) {
        var elements = $ES('.splashNav a', splash);

        previousButton.addEvent('click', function(e) {
            new Event(e).stop();
            var previous;

            elements.each(function(link, i) {
                if (link.hasClass('selected'))
                    previous = i - 1;
            });

            if (previous != -1)
                splash.display(previous);
        });

        splash.addEvent('onActive', function(toggler, element) {
            elements.each(function(link, i) {
                if (link == toggler) {
                    var changeOpacity = new Fx.Style(previousButton[0], 'opacity', { duration: Site.options.splashSlideDuration });

                    if (i == 0) {
                        changeOpacity.start(0);
                        previousButton[0].setStyle('cursor', 'default');
                    }
                    else {
                        changeOpacity.start(1);
                        previousButton[0].setStyle('cursor', 'pointer');
                    }
                }
            });
        });
    },

    previousSplashFocus: function(i, splash) {
        var length = $ES('.splashNav a', splash).length - 1;
        var last = length;
        var previous = i - 1;

        if ($E('.splashCounter', splash).innerHTML != 1) {
            if (i == 0) {
                (function() {
                    if ($E('.splashCounter', splash).innerHTML != 1) {
                        splash.display(last);
                        Site.previousSplashFocus(last, splash);
                    }
                }).delay(Site.options.splashDelay);
            } else {
                (function() {
                    if ($E('.splashCounter', splash).innerHTML != 1) {
                        splash.display(previous);
                        Site.previousSplashFocus(previous, splash);
                    }
                }).delay(Site.options.splashDelay);
            }
        }
    },

    initializeFormTips: function(elements) {
        var formTips = new Tips(elements, {
            showDelay: 0,
            hideDelay: 0,
            fixed: true,
            offsets: { 'x': 490, 'y': 0 },
            initialize: function() {
                this.fx = new Fx.Style(this.toolTip, 'opacity', { duration: 350, wait: false }).set(0);
            },
            onShow: function(toolTip) {
                this.fx.start(1);
            },
            onHide: function(toolTip) {
                this.fx.start(0);
            }
        });

        elements.each(function(element, i) {
            element.addEvents({
                'focus': function() {
                    element.addClass('focus');
                },
                'blur': function() {
                    element.removeClass('focus');
                }
            });
        });
    }
};

// Implement tooptip - changes
Tips.implement({
	build: function(el){
		el.$tmp.myTitle = (el.href && el.getTag() == 'a') ? el.href.replace('http://', '') : (el.rel || false);
		if (el.title){
			var dual = el.title.split('::');
			if (dual.length > 1){
				el.$tmp.myTitle = dual[0].trim();
				el.$tmp.myText = dual[1].trim();
			} else {
				el.$tmp.myText = el.title;
			}
			el.removeAttribute('title');
		} else {
			el.$tmp.myText = false;
		}
		if (el.$tmp.myTitle && el.$tmp.myTitle.length > this.options.maxTitleChars) el.$tmp.myTitle = el.$tmp.myTitle.substr(0, this.options.maxTitleChars - 1) + "&hellip;";
		if ((el.getTag() == "input") || (el.getTag() == "textarea")) {
			el.addEvent('focus', function(){
				this.start(el);
				this.position(el);
			}.bind(this));
			//el.addEvent('mousemove', this.locate.bindWithEvent(this));
			var end = this.end.bind(this);
			el.addEvent('blur', end);
		}
		else {
			el.addEvent('mouseenter', function(event){
				this.start(el);
				if (!this.options.fixed) this.locate(event);
				else this.position(el);
			}.bind(this));
			if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
			var end = this.end.bind(this);
			el.addEvent('mouseleave', end);
		}
		el.addEvent('trash', end);
	}
});

window.addEvent('domready', Site.domready);
window.addEvent('load', Site.load);