/*jslint browser: true */
/*global $, $$, Control, Effect, Element, Event, imagestripScrollInit, last, scrollHorizontal, 
sfHover, slideshow */


/* son of suckerfish enabling for ie */

last = null;
sfHover = function() {
    if (navigator.userAgent.indexOf('MSIE') == -1) {
        //only IE needs JS for dropdown menus
        return;
    }
    var removeHover = function() {
        if (last) {
            last.removeClassName("sfhover");
        }
        last = null;
        //$$('li').each(function(el) { el.removeClassName("sfhover") });
    };
    $$('ul.navigation > li').each(function(el) {
        Event.observe(el, 'mouseover', function() { removeHover(); el.addClassName("sfhover"); last = el; });
        Event.observe(el, 'mouseout', function() { el.removeClassName("sfhover"); });
    });
    //we have to observe head and content because IE tends to miss mouseout events
    Event.observe('head', 'mouseover', removeHover);
    Event.observe('content', 'mouseover', removeHover);
};


/* javascript scroll slider */

imagestripScrollInit = function(imagestripId, scrollbarId, scrollhandleId) {
    var imagestrip = $(imagestripId);
    var scrollbar = $(scrollbarId);
    
    if (!imagestrip || !scrollbar) {
        return;
    }
    
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('safari') > -1 && ua.indexOf('version') == -1) {
        //safari 1.x doesn't allow switching the overflow property.
        //so we just replace the imagestrip with a new one that has overflow:hidden set
        var newstrip = new Element('div', {'id':'imagestrip', 'style':'overflow:hidden'});
        newstrip.innerHTML = imagestrip.innerHTML;
        Element.replace(imagestrip, newstrip);
        imagestrip = newstrip;
        //return;
    }
    
    imagestrip.addClassName('enhanced');
    scrollbar.setStyle({display:'block'});

    var slider = new Control.Slider(scrollhandleId, scrollbarId, {
        onSlide: function(v) { scrollHorizontal(v, imagestrip, slider);  },
        onChange: function(v) { scrollHorizontal(v, imagestrip, slider); }
    });
			
};

scrollHorizontal = function(value, element, slider) {
    element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
};

/* slideshow */

slideshow = {
    SLIDEPAUSE : 3000,
    SLIDEFADE : 100,
    SLIDECONTAINER : 'slideshowContainer',
    
    imgs : [],
    current : 0,
    running : null,
    
    start : function() {
        if (slideshow.running) {
            window.clearTimeout(slideshow.running);
            slideshow.running = null;
        }

        var container = $(slideshow.SLIDECONTAINER);
        if (!container) {
            return;
        }

        slideshow.imgs = container.getElementsByTagName("img");
        if (!slideshow.imgs || slideshow.imgs.length === 0) {
            return;
        }
        
        slideshow.current = 0;
        
        slideshow.running = setTimeout(slideshow.slide, slideshow.SLIDEPAUSE);
    },

    slide : function() {
        if (slideshow.current == (slideshow.imgs.length - 1)) {
            for(var i = 0; i < slideshow.imgs.length; i += 1) {
                $(slideshow.imgs[i]).show();
            }
        }
        if (slideshow.current > 0) {
            Effect.Fade(slideshow.imgs[slideshow.current]);
            slideshow.current -= 1;
        } else {
            slideshow.current = slideshow.imgs.length - 1;
            Effect.Appear(slideshow.imgs[slideshow.current]);
        }
        
        slideshow.running = setTimeout(slideshow.slide, slideshow.SLIDEPAUSE);
    }
};


/* global init */

Event.observe(window, 'load', function() {
    
    sfHover();
    
    imagestripScrollInit('imagestrip', 'scrollbar', 'scrollhandle');
    
    slideshow.start();
    
    $$('a').invoke('observe', 'focus', function(event) {
        var el = Event.element(event);
        el.blur();
    });
    
});