/*
*	Premier.js
*	
*	DPAC 2008
*/

var Premier = function() {};
(function() {
    this.domready = function() {
        this.font();
    }.bind(this);
    window.addEvent('domready', this.domready);
    this.slideshow = function(imgs) {
        new Silk.Slideshow($$('.headImg')[0], imgs, {
            displayDuration: 20
        });
    }
    this.font = function() {
        var font = new Silk.Font($$('#home', '#right_cols'), [8, 10, 13], {
            fx: {
                unit: 'pt'
            }
        });
        $('font_controls').setStyle('display', 'block');
        $$('#font-small', '#font-medium', '#font-large').each(function(el, i) {
            el.addEvent('click', function(e) {
                e.stop();
                font.set(i);
                this.blur();
            });
        });
    }
    this.sideshow = function(imgs) {
        new Silk.Sideshow($('sideshow'), imgs, {
            pause: 'sideshow_pause',
            loadingHTML: "<p>Loading images...</p>"
        });
        $('sideshow').setStyle('height', '134px');
        $('sideshowControls').setStyles({
            display: 'block',
            opacity: 0.6
        });
        var paused = false;
        $('sideshow_pause').addEvent('click', function(e) {
            e.stop();
            this.toggleClass('paused');
            this.blur();
        });
    }
    this.videoPlayer = function() {
        this.player = new Silk.Player('video', {
            src: './?a=58387',
            width: 255,
            height: 191
        },
        '.play', {
            playerVars: {
                showPlayButton: false,
                controlsOverVideo: 'ease',
                controlBarBackgroundColor: -1,
                controlBarGloss: 'none',
                timeDisplayFontColor: 0xcccccc
            },
            controls: {
                toggle: 'play-pause',
                next: 'next',
                prev: 'previous'
            },
            onStopPlaying: function() {
                if (Browser.Engine.trident4) {
                    $('play-pause').setStyle('filter', filter + '"./?a=63241")');
                } else {
                    $('play-pause').setStyle('background-image', 'url(./?a=63241)');
                }
            },
            onStartPlaying: function() {
                if (Browser.Engine.trident4) {
                    $('play-pause').setStyle('filter', filter + '"./?a=63240")');
                } else {
                    $('play-pause').setStyle('background-image', 'url(./?a=63240)');
                }
            }
        });
        var filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=";
        var playing = false;
        var pause = new Element('img', {
            src: "./?a=63240"
        });
        var prev_hover = new Element('img', {
            src: "./?a=63243"
        });
        var next_hover = new Element('img', {
            src: "./?a=63239"
        });
        $('previous').setStyle('cursor', 'pointer').addEvents({
            mouseenter: function() {
                if (Browser.Engine.trident4) {
                    this.setStyle('filter', filter + '"./?a=63243")');
                } else {
                    this.setStyle('background-image', "url(./?a=63243)");
                }
            },
            mouseleave: function() {
                if (Browser.Engine.trident4) {
                    this.setStyle('filter', filter + '"./?a=63242")');
                } else {
                    this.setStyle('background-image', "url(./?a=63242)");
                }
            }
        }).fireEvent('mouseleave').getElement('img').setStyle('visibility', 'hidden');
        $('next').setStyle('cursor', 'pointer').addEvents({
            mouseenter: function() {
                if (Browser.Engine.trident4) {
                    this.setStyle('filter', filter + '"./?a=63239")');
                } else {
                    this.setStyle('background-image', "url(./?a=63239)");
                }
            },
            mouseleave: function() {
                if (Browser.Engine.trident4) {
                    this.setStyle('filter', filter + '"./?a=63238")');
                } else {
                    this.setStyle('background-image', "url(./?a=63238)");
                }
            }
        }).fireEvent('mouseleave').getElement('img').setStyle('visibility', 'hidden');
        $('play-pause').setStyle('cursor', 'pointer').getElement('img').setStyle('visibility', 'hidden');
        if (Browser.Engine.trident4) {
            $('play-pause').setStyle('filter', filter + '"./?a=63241")');
        } else {
            $('play-pause').setStyle('background-image', 'url(./?a=63241)');
        }
    };
    this.videoLibrary = function() {
        var scroll = new Fx.Scroll('video_list');
        $('video_list_up').setStyle('cursor', 'pointer').addEvent('click', function() {
            scroll.toTop()
        });
        $('video_list_down').setStyle('cursor', 'pointer').addEvent('click', function() {
            scroll.toBottom();
        });
    }
    this.walk = function() {
        this.walkVid = new Silk.Player('introVideo', {
            src: './?a=58387',
            width: 268,
            height: 152,
            wmode: 'transparent'
        },
        '?a=63099', {
            playerVars: {
                usePlayOverlay: false,
                hideControls: true,
                backgroundColor: -1
            },
            onFlowPlayerReady: function() {
                this.walkVid.play();
            }.bind(this),
            onClipDone: function() {
                var slideshow_target = $$('.headImg')[0];
                slideshow_target.set('morph', {
                    duration: 1200,
                    onComplete: function() {
                        this.slideshow(slideshowImgs);
                        $('headImg').getElements('div').destroy();
                    }.bind(this)
                }).morph.delay(1500, slideshow_target, {
                    marginTop: [ - 165, 0],
                    opacity: [0, 1]
                });
            }.bind(this)
        });
        window.addEvent("load", function() {
            this.walkVid.play();
        }.bind(this));
    };
    var currentTopic;
    var topicTimer = false;
    var topicDelay = 5000;
    this.topics = function() {
        var topics = $$('.hotTopics_list_item');
        var topicPositions = [];
        var bulletSize = $('topicBullet').set('tween', {
            link: 'cancel',
            duration: 300,
            property: 'top'
        }).getSize();
        var containerCoords = $('hot_topics').getCoordinates();
        var container = $$('.hotTopics_list_item')[0].getOffsetParent();
        var topicOver = function(target) {
            $clear(topicTimer);
            var top = target.getCoordinates()['top'] - containerCoords.top + (target.getCoordinates()['height'] / 2) - 9;
            if (Browser.Engine.trident) {
                top = target.getBetterPosition()['y'] - $(target.getOffsetParent()).getBetterPosition()['y'] + (target.getSize()['y'] / 2) - 9;
            }
            $('topicBullet').tween(top);
            if (target != currentTopic) {
                currentTopic.setStyles({
                    backgroundColor: '#57a820',
                    color: '#ffffff'
                });
                target.setStyles({
                    backgroundColor: '#6eaf41',
                    color: '#000000'
                });
                var currentImg = currentTopic.getNext();
                if (currentImg.get('tag') == 'img') {
                    currentImg.setStyle('display', 'none');
                }
                var img = target.getNext();
                if (img.get('tag') == 'img') {
                    img.setStyle('display', 'block');
                }
                currentTopic = target;
            }
            topicTimer = advance.delay(topicDelay);
        };
        topics.each(function(topic, i) {
            if (!i) {
                var coords = topic.getCoordinates();
                var left = coords.left - containerCoords.left - 5;
                var top = coords.top - containerCoords.top + (coords.height / 2) - 9;
                if (Browser.Engine.trident) {
                    left = $(topic.getOffsetParent()).getBetterPosition()['x'] - $('frame').getBetterPosition()['x'] - (topic.getSize()['x'] / 2) + 3;
                    top = topic.getBetterPosition()['y'] - $(topic.getOffsetParent()).getBetterPosition()['y'] + (topic.getSize()['y'] / 2) - 9;
                }
                $('topicBullet').setStyles({
                    display: 'block',
                    top: top,
                    left: left
                });
                topic.setStyles({
                    backgroundColor: '#6eaf41',
                    color: '#000000'
                });
                currentTopic = topic;
            }
            topic.addEvent('mouseover', topicOver.pass(topic));
        });
        advance = function() {
            var i = (topics.indexOf(currentTopic) + 1).mod(topics.length);
            topicOver(topics[i]);
        };
        topicTimer = advance.delay(topicDelay);
    }
}).call(Premier);
Element.implement({
    getBetterPosition: function() {
        if (!Browser.Engine.trident) return this.getPosition();
        var b = this.getBoundingClientRect(),
        html = this.getDocument().documentElement;
        return {
            x: b.left + html.scrollLeft - html.clientLeft,
            y: b.top + html.scrollTop - html.clientTop
        };
    }
});