/**
 * NAME: music_player.js
 * PURPOSE: Constructs an instance of flow player in the footer specifically for
 * playing music and creates the functionality for the ON and OFF anchor tags
 * in order to control it
 */

var MusicPlayer = {

    // The flow player object
    music_player : Object,

    // The anchor tag wrapping the flowplayer
    music_player_container : Object,

    // The anchor tag responsible for turning on the music
    on_switch : Object,

    // The anchor tag responsible for turning off the music
    off_switch : Object,

    // CSS properties to apply when one of the switches has been selected
    selected_state_css : {
        'color' : 'white'
    },

    // CSS properties to apply when one of the switches has been deselected
    not_selected_state_css : {
        'color' : '#73694A'
    },

    // CSS to apply to the music player to container.

    // Boolean variable for tracking the state of the player
    playing : false,

    // Builds the music player in the footer and performs variable assignment
    construct_music_player : function() {

        jQuery(document).ready(function() {

            // Find the on and off switch
            MusicPlayer.on_switch = jQuery('#music_player_on');
            MusicPlayer.off_switch = jQuery('#music_player_off');

            // Assign the play and stop events to the switches
            MusicPlayer.on_switch.bind('click', function() {
                MusicPlayer.play();
            });
            MusicPlayer.off_switch.bind('click', function() {
                MusicPlayer.stop();
            });

            // Always off by default
            MusicPlayer.off_switch.css(MusicPlayer.selected_state_css);
            MusicPlayer.on_switch.css(MusicPlayer.not_selected_state_css);

            // Construct the container for the music player
            music_player_container_html =
                '<div style=""><a id="music_player" style="width:1px; height:1px; position: absolute; margin: -1000px 0 0 0"></a></div>';

            // Inject the container into the footer
            jQuery('#footer').append(music_player_container_html);

            // Find our newly created container
            MusicPlayer.music_player_container = jQuery('#music_player');

            // Load the Flow Player into the container
            MusicPlayer.music_player = flowplayer(
                'music_player', "resources/audio/flowplayer-3.2.5.swf",
                {
                    // player configuration goes here
                    clip: {
                        url: 'resources/audio/music.mp3',
                        onBeforeFinish: function () {
                            // do not finish, but repeat
                            return false;
                        },
                        autoPlay: false
                    },
                    plugins: {
                        audio: {
                        	url: 'resources/audio/flowplayer.audio-3.2.1.swf'
                        }
                    }

                }
            );

            // Don't stop playing this music when a video starts
            MusicPlayer.music_player.onBeforeUnload(function() {
                return false;
            });


            // Only play music if we are on home page
            if (jQuery('#site-intro').length != 0) {
                MusicPlayer.play();
                MusicPlayer.playing = true;
            }
        });
    },

    // You know what this does.
    play : function() {

        // We do nothing if the music player is already playing
        if (MusicPlayer.playing)
            return;

        // Start the music, apply the CSS to the switch, and record the music
        // player as playing
        MusicPlayer.music_player.play();
        MusicPlayer.on_switch.css(MusicPlayer.selected_state_css);
        MusicPlayer.off_switch.css(MusicPlayer.not_selected_state_css);
        MusicPlayer.playing = true;
    },

    // And what this does too.
    stop : function() {

        // We do nothing if the music player is not playing
        if (!MusicPlayer.playing)
            return;

        // Start the music, apply the CSS to the switch, and record the music
        // player as playing
        MusicPlayer.music_player.pause();
        MusicPlayer.off_switch.css(MusicPlayer.selected_state_css);
        MusicPlayer.on_switch.css(MusicPlayer.not_selected_state_css);
        MusicPlayer.playing = false;
    },

    // Determines whether or not the user is on the iphone
    is_iphone : function() {
        //agent = navigator.userAgent.toLowerCase();
        return (navigator.userAgent.match(/like Mac OS X/i));
    }

};
