/**
 * @author denniss
 */
var heroflash = null;

function StormHeroflash() {
    var self = this;

    this.updateDescriptionBox = function() {
        var box = $('.descriptionBox',self.slider);
        $('.desctext div.title',box).html($('ul li.active div.title', self.slider).html());        
        $('.desctext span.description',box).html($('ul li.active div.description', self.slider).html());
        $('.descriptionBox .desctext div').fadeIn(100);
    } 

    this.slider = $('div#heroflash');
    this.slider.anythingSlider({
        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 3000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 600,             // How long the slide transition takes
        buildButtons: false,
        hashTags: false,                 // Should links change the hashtag in the URL?
        buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Go",             // Start text
        stopText: "Stop",               // Stop text
        pageChanged: self.updateDescriptionBox
    });

    this.startPlayer = function(object, url) {
        var flashvars = {
            file:url,
            autostart:"true",
            controlbar:"over",
            enablejs:"true",
            javascriptid:"playerID",
            playerready:'playerReady'
           };
        var params = {};
        params.allowfullscreen = "true";
        params.allowscriptaccess = "always";
        params.wmode = "opaque";
        params.bgcolor = '#000000';
        var attributes = {};
        attributes.id = "heroflashPlayer";
        swfobject.embedSWF("/images/swf/mediaplayer/player.swf", "heroflashPlayer", "400", "262", "10.0.0", "/images/swf/expressInstall.swf", flashvars, params, attributes);
    }

    this.playerAddListeners = function()  {
        var player = document.getElementById('heroflashPlayer');
        var playlist = player.getPlaylist();
        if(playlist.length > 0) {    
            player.addModelListener('STATE', 'heroflashStateMonitor');
        }
        else {
            setTimeout("heroflashAddListeners();", 1000);   
        }
    }

    this.stateMonitor = function(obj) {
        var player = document.getElementById('heroflashPlayer');
        //console.log('playerState',obj.newstate);
        if(obj.newstate == 'COMPLETED' || obj.newstate == 'PAUSED' ) {
            self.destroyPlayer();
        }
    }

    this.destroyPlayer = function() {
        //$('div.video object').remove();
        swfobject.removeSWF('heroflashPlayer');
        $('div.video').append('<div id="heroflashPlayer"></div>');
        $('div.video').hide();
        $('div.wrapper', self.slider).show();
        setTimeout('heroflashShowContent();',1000);
    }
    
    this.playerReady = function(obj) {
        //console.log('playerReady',obj);
        self.playerAddListeners();
    }
    
    this.showContent = function() {
        $('div.wrapper ul li.active div.spriteImgHero', self.slider).fadeIn(500,function() {
            $('div.descriptionBox, a.playButton', self.slider).fadeIn(500);
        });
    }

    $('div.backButton', self.slider).click(function(){
        $('.descriptionBox .desctext div',self.slider).fadeOut(100);
        self.slider.anythingSlider('back');
        return false;
    });
    $('div.forwardButton',self.slider).click(function(){
        $('.descriptionBox .desctext div',self.slider).fadeOut(100);
        self.slider.anythingSlider('forward');
        return false;
    });

    $('.descriptionBox a.booknow',self.slider).click(function(){        
        window.location.href=$('ul li.active div.filmURL', self.slider).html();
        return false;
    });

    $('a.playButton',self.slider).click(function() {
        $('div.descriptionBox, a.playButton', self.slider).fadeOut(500,function() {
            $('div.wrapper ul li.active div.spriteImgHero', self.slider).fadeOut(500, function() {
                $('div.wrapper', self.slider).hide();
                $('div.video').show();
                self.startPlayer($('div.video'), $('ul li.active div.videoURL', self.slider).html());
            });
        });
    });
}

function playerReady(obj) {
    heroflash.playerReady(obj);
}

function heroflashStateMonitor(obj) {
    heroflash.stateMonitor(obj);
}

function heroflashAddListeners() {
    heroflash.playerAddListeners();
}

function heroflashShowContent() {
    heroflash.showContent();
}

$(function() {
    heroflash = new StormHeroflash();    
});


