﻿var Media = {

    init: function() {
        Media.setupLinks();
        //check to see if video/sound ref passed in URL
        var hash = location.hash;
        var type = null;

        if (hash.indexOf("video") > 0) {
            type = "video";
        }
        else if (hash.indexOf("sound") > 0) {
            type = "sound";
        }

        if (type != null) {
            hash = hash.replace(/#sound=/, "");
            hash = hash.replace(/#video=/, "");
            Media.play(hash, type);
        } else {
            Media.play(7, "video");
        }
    },

    play: function(id, type) {
        id = parseInt(id);
        if (isNaN(id)) return;

        Media.turnOffAll();
        var el = null;
        var title = null;

        var flashvars = {};

        if (type == "sound") {
            el = $("#SightsSounds #audio ul li").eq(id).children("a");
            title = el.children(".desc").html();
            flashvars.image = "/images/sightssounds/audio_bg_" + id + ".jpg";
            flashvars.stretching = "fill";
        } else {
            //el = $("#SightsSounds #videosList ul li").eq(id).children("a");
            el = $("#SightsSounds #videosList ul li #video" + id);
            title = "<strong class='title'>" + el.children("strong").text() + "</strong><br />" + el.children(".desc").text();
            flashvars.image = "/images/sightssounds/video_bg_" + id + ".jpg";
            flashvars.stretching = "fill";            
        }

        flashvars.skin = "/swf/dangdang.swf";
        flashvars.file = el.attr("href");

        el.parent("li").addClass("active");
        $("#player .desc").html(title);
        Media.initSwfObject(flashvars);
        location.replace("#" + type + "=" + id);
    },

    setupLinks: function() {
        $("#SightsSounds #audio li a").click(function(e) {
            e.preventDefault();
            var el = $(this);
            var index = $("#SightsSounds #audio ul li").index(el.parent("li"));
            Media.play(index, "sound");
        });

        $("#SightsSounds #videosList li a").click(function(e) {
            e.preventDefault();
            var el = $(this);
            //var index = $("#SightsSounds #videosList ul li").index(el.parent("li"));
            var index = $(this).attr("id").replace(/video/, "");
            Media.play(index, "video");
        });
    },

    turnOffAll: function() {
        $("#SightsSounds playerWindow").html("<span id='swfPlayerContainer'></span>");
        $("#SightsSounds #videosList li").removeClass("active");
        $("#SightsSounds #audio li").removeClass("active");
    },

    initSwfObject: function(flashvars) {
        /*var flashvars = {
        file: "/flash/make-believe.flv",
        image: "/images/video/make-believe.jpg",
        duration: "93"
        };*/
        //wmode: "transparent",
        var params = {
            movie: "/swf/player.swf",
            allowScriptAccess: "sameDomain",
            wmode: "transparent",
            allowFullScreen: "true"
        };
        swfobject.embedSWF("/swf/player.swf", "swfPlayerContainer", "555", "312", "8.0.0", "", flashvars, params);
    }

};