/**
 * ATV User Interface
 */ 
var ATV = {

    /**
     * Init UI functions
     */         
    init: function () {
         ATV.initMap();
         ATV.tabs();
         ATV.emulateHover($$("#nav li"));
         ATV.preloadImages(["images/bg/support-hover.gif", "images/bg/nav-hover.png"]);
         
         if ($("diagram")) {
            ATV.overlaidElement($("diagram"), $$(".diagram-thumb a"), $$("#diagram .close"));
         }
         
         if ($("slideshow-wrapper")) {
            ATV.overlaidElement($("slideshow-wrapper"), $$(".slideshow-link"), $$("#slideshow-wrapper .close"));
            ATV.positionSlideshow();
            ATV.openCurrentSlideshow();
         }
    },
    
    initMap: function () {
        if (!$("map")) {
            return;
        }
        load(); 
        showAddress('151 E. Cliff Road, Suite 10, Burnsville, Minnesota');
    },
    
    unloadMap: function () {
        if (!$("map")) {
            return;
        }
        GUnload(); 
    },
    
    /**
     * Init sIFR 
     */         
    initsIFR: function () {
        if (typeof sIFR == "function"){
            sIFR.replaceElement(named({sSelector: "#features .feature h1", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent"}));
            sIFR.replaceElement(named({sSelector: "#features .feature .see", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent", sCase: "upper", nPaddingLeft: 17}));
            sIFR.replaceElement(named({sSelector: "#features h1.long", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent"}));                        
            sIFR.replaceElement(named({sSelector: "#features h1.blue-normal", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent"}));            
            sIFR.replaceElement(named({sSelector: "#features h1.blue", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent", sCase: "lower"}));
            sIFR.replaceElement(named({sSelector: "#features h1", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent", sCase: "lower"}));
            sIFR.replaceElement(named({sSelector: "#features .right h2", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent"}));            
            sIFR.replaceElement(named({sSelector: "#features .subtitle-long", sFlashSrc: "images/fonts/interstate.swf", sColor: "#eeeeee", sWmode: "transparent"}));             
            sIFR.replaceElement(named({sSelector: "#features .subtitle-advisor", sFlashSrc: "images/fonts/interstate.swf", sColor: "#54B948", sWmode: "transparent"}));            
            sIFR.replaceElement(named({sSelector: "#features .subtitle-blue", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent", sCase: "lower"}));
            sIFR.replaceElement(named({sSelector: "#features .subtitle-top", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent"}));            
            sIFR.replaceElement(named({sSelector: "#features .subtitle", sFlashSrc: "images/fonts/interstate.swf", sColor: "#ffffff", sWmode: "transparent", sCase: "lower"}));
            sIFR.replaceElement(named({sSelector: "#content h2 span", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent"}));
            sIFR.replaceElement(named({sSelector: "#content h2 strong", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent"}));
            sIFR.replaceElement(named({sSelector: "#content .left-col h2.sifr", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent", nPaddingLeft: 20, nPaddingBottom: 5, nPaddingTop: 5}));
            sIFR.replaceElement(named({sSelector: "#content .right-col h2.sifr", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent"}));            
            sIFR.replaceElement(named({sSelector: "#content .middle-col h2.sifr", sFlashSrc: "images/fonts/interstate.swf", sColor: "#455560", sWmode: "transparent"}));
            sIFR.replaceElement(named({sSelector: "#content .phone-email", sFlashSrc: "images/fonts/interstate.swf", sColor: "#54B948", sWmode: "transparent"}));
        }    
    },
    
    /**
     * Fade in/fade out element overlaying content 
     * @param {Element} element Element to show/hide
     * @param {Array} open_links Links which show element
     * @param {Array} close_links Links which hide element        
     */         
    overlaidElement: function (element, open_links, close_links) {
        
        open_links.each(function(el, i) {
            open_links[i].addEvent('click', function (e) {
                element.setOpacity(0);
                element.addClass('display');
                var myFx = new Fx.Style(element, 'opacity', {duration: 1000}).start(0,1);
                var event = new Event(e).stop();
            }) 
        });
        
        close_links.each(function(el, i) {
            close_links[i].addEvent('click', function (e) {
                var myFx = new Fx.Style(element, 'opacity', {duration: 1000}).start(1,0).chain(function () {
                    element.removeClass('display');
                });
                var event = new Event(e).stop();
            })
        });
    },
    
    /**
     * Position slideshow exactly over tabs
     */         
    positionSlideshow: function () {
        var top = $('content').getSize().size.y.toInt() - $E('.tabs').getSize().size.y.toInt()
        $('slideshow-wrapper').setStyle('top', top);
    },
    
    /**
     * Open current slideshow
     */          
    openCurrentSlideshow: function () {
        var links = $$('.slideshow-link');;
        var slideshows = $$('.slideshow');
        
        links.each(function(el, i) {
            links[i].addEvent('click', function (e) {
                ATV.listSlideshowItems(slideshows[i]);
                ATV.resetSlideshowItems(slideshows[i]);
                ATV.closeSlideshows(slideshows);
                slideshows[i].addClass('slideshow-display');
            });
        });
    },
    
    /**
     * Close all slideshows
     */         
    closeSlideshows: function (elements) {
         for (var i = 0; i < elements.length; i++ ) {
             elements[i].removeClass('slideshow-display');
         }
    },
    
    /**
     * Reset slideshow items
     * @param {Element} slideshow Current slideshow       
     */         
    resetSlideshowItems: function (slideshow) {
        var items = $ES('.item', slideshow);
        items.each(function(el, i) {
            if (i === 0) {
                items[i].addClass('item-display');
                items[i].setOpacity(1);
            } else {
                items[i].removeClass('item-display');
            }
        });
    },
    
    /**
     * Cycle slideshow items
     * @param {Element} slideshow Current slideshow       
     */         
    listSlideshowItems: function (slideshow) {
        
        var items = $ES('.item', slideshow);
        var previous_links = $ES('.previous', slideshow) 
        var next_links = $ES('.next', slideshow)
        var last = items.length - 1;
        
        previous_links.each(function(el, i) {
            previous_links[i].addEvent('click', function (e) {
                
                var current = getCurrent();
                var previous;
                
                if (current !== undefined) {
                     if (current > 0) {
                         previous = current - 1
                     } else {
                         previous = last;
                     }
                     fadeFromTo(current, previous);
                }
                
                var event = new Event(e).stop();
            });
        });
        
        next_links.each(function(el, i) {
            next_links[i].addEvent('click', function (e) {
                
                var current = getCurrent();
                var next;
                
                if (current !== undefined) {
                     if (current < last) {
                         next = current + 1
                     } else {
                         next = 0;
                     }
                     fadeFromTo(current, next);  
                }
                var event = new Event(e).stop();
            });
        });
        
        function fadeFromTo(from, to) {
            var myFx1 = new Fx.Style(items[from], 'opacity', {duration: 750}).start(1,0).chain(function () {
                 items[from].removeClass('item-display');
                 items[to].setOpacity(0);
                 items[to].addClass('item-display');
                 var myFx2 = new Fx.Style(items[to], 'opacity', {duration: 750}).start(0,1);
            });
        }
        
        function getCurrent() {
            for (var i = 0; i < items.length; i++ ) {
                if(items[i].hasClass('item-display')) {
                    return i;
                }
            }
        }
    },
    
    /**
     * Emulate hover on other elements than links in IE6 by adding class .hover
     * @param {Array} items Items to hover on       
     */         
    emulateHover: function (items) {
        items.each(function(el, i) {
            el.addEvents({
                mouseover: function () {
                    this.addClass('hover');
                },
                mouseout: function () {
                    this.removeClass('hover');
                }
            }) 
        });
     },
    
     /**
      * Tabs
      */           
     tabs: function () {
         var links = $$('.tabs-nav li');
         var contents = $$('.tab');
         links.each(function (el, i) {
             links[i].addEvent('click', function(e) {
                 if (contents[i] !== undefined) {
                     ATV.closeTabs(links, contents);
                     this.toggleClass('current');
                     contents[i].addClass('tab-display');
                  }
                  new Event(e).stop();
             });
         });

     },
     
     /**
      * Close all content tabs and remove active state from all nav tabs
      */      
     closeTabs: function (links, elements) {
         for (var i = 0; i < elements.length; i++ ) {
             elements[i].removeClass('tab-display');
             links[i].removeClass('current');
         }
     },

    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    },

    /**
     * Add stylesheet to document
     * @param {String} href Stylesheet url   
     * @param {String} media Stylesheet media   
     */               
    addStylesheet: function (href, media) {
        var link = new Element('link', {
            'rel': 'stylesheet',
            'type': 'text/css',
            'media': media,
            'href': href
        });
        var head = $E('head');
        link.injectInside(head);
    }
}

ATV.initsIFR();
ATV.addStylesheet("_ui/css/js.css", "screen, projection");
window.addEvent('load', ATV.init);
window.addEvent('unload', ATV.unloadMap);
