var video_bitrate    = 650;
var video_sizes      = {};
var video_basepath   = "";
var video_extension  = "";
var stream_extension = "auto";
var video_fullscreen = 0;
var enable_play_hint = 0;
var loop_video       = 0;
var so;

var FlashVideo = {
    MakeSWFObject : function () {
        // Pick best video size and type from available videos
        var width      = 640;
        var height     = 480;
        var bitrate    = video_bitrate;
	var stream_ext = stream_extension;
	var exts       = stream_extension == "auto" ? ["flv", "mp4"] : [stream_extension] ;
        for (var i in exts) {
	     var ext   = exts[i];
             var sizes = video_sizes[ext];
             if (!sizes)
	         continue;

             for (var i in sizes) {
                  var size_info = sizes[i];
                  if (size_info[0] <= video_bitrate) {
                      stream_ext = ext;

                      bitrate = size_info[0];
                      width   = size_info[1];
                      height  = size_info[2] + 20;
                  }
             }
        }
        if (video_fullscreen) {
            width  = "100%";
            height = "100%";
            $('imageinfo').style.display = "none";
        }

        var flash_version_required = stream_ext == "flv" ? "7" : "9.0.115";

        so = new SWFObject("/flvplayer.swf", "flashplayer", width, height,
                           flash_version_required, "#FFFFFF");

        if (so && so.installedVer.versionIsValid(so.getAttribute('version'))) {
            var stream_file = video_basepath + "_" + bitrate + "." + stream_ext;
            stream_file = escape(escape(escape(stream_file)));

            if (video_extension == "xspf") {
                stream_file += ".xspf?";
                stream_file = escape(stream_file);
                so.addVariable("linkfromdisplay", "true");
                so.addVariable("showfsbutton",    "false");
            }
            else {
                var repeat   = loop_video != 0 ? "true" : "false";
                so.addVariable("repeat",          repeat);

                var fs_page  = "/fullscreen.html?e=" + video_extension + escape("&f=") + stream_file;

                so.addVariable("showfsbutton",    "true");
                so.addVariable("fsreturnpage",    window.location);
                so.addVariable("fsbuttonlink",    fs_page);
                so.addVariable("fullscreenpage",  fs_page);
                so.addParam   ("allowFullScreen", "true");
            }

            so.addVariable("file",        stream_file);
            so.addVariable("enablejs",    "true");
            so.addVariable("autostart",   "true");
            so.addVariable("showdigits",  "true");
            so.addVariable("overstretch", "false");

            enable_play_hint = 1;
            var flash_controls = [ $('playbutton1'),  $('playbutton2'),
                                   $('ratecontrol1'), $('ratecontrol2'),
                                   $('loopvid'),      $('typebutton') ];
            for (var i in flash_controls) {
                if (flash_controls[i]) {
                    flash_controls[i].style.display = 'inline';
                }
            }

            var version_8  = new deconcept.PlayerVersion([8,0,0]);
            var at_least_8 = so.installedVer.versionIsValid(version_8);
            var controls   = at_least_8 ? $('static_controls_9')
                                        : $('static_controls_7') ;
            if (controls) controls.style.display = 'inline';
        }
        else if (so && so.installedVer.versionIsValid(new deconcept.PlayerVersion([6,0,65]))) {
            so.useExpressInstall("/expressinstall.swf");
            so.write('flashreplace');
        }
        else {
            if ($('static_controls_9')) {
                $('pushflash').style.display = 'inline';
            }
        }
    },

    DoReplace : function () {
        // $('debug').innerHTML += 'DoReplace ';
        if (timeout != 0) clearTimeout(timeout);
        timeout = 0;
        want_pop = -1;

        $('pop').style.display = "none";
        so.write('flashreplace');
    },

    GoFullScreen : function () {
        // $('debug').innerHTML += 'GoFullScreen ';
        video_fullscreen = 1;
        FlashVideo.DoReplace();
    },

    LoopVideo : function () {
        // $('debug').innerHTML += 'LoopVideo ';
        loop_video = 1;
        FlashVideo.MakeSWFObject();
    },

    SetVideoLooping : function (button) {
        if (loop_video != 0) {
            loop_video = 0;
            button.innerHTML = 'Looping is Off';
        }
        else {
            loop_video = 1;
            button.innerHTML = 'Looping is On';
        }

        document.cookie = 'loopvideo=' + loop_video + ';path=/;max-age=631152000';

        FlashVideo.MakeSWFObject();
        if (! $('mainpic')) {
            FlashVideo.DoReplace();
        }
    },

    MaybeSetVideoLooping : function () {
        var cookies = document.cookie.split(/;\s*/);

        for (var i in cookies) {
            var loop_match = cookies[i].match(/^loopvideo=(\d+)/);
            if (loop_match && loop_match.length > 0) {
                loop_video = loop_match[1];
            }
        }

        if (loop_video != 0) {
            $('loopvid').innerHTML = 'Looping is On';
        }
    },

    SetTranscodeType : function (button) {
        if      (stream_extension == 'auto') {
                 stream_extension  = 'flv';
        }
        else if (stream_extension == 'flv') {
                 stream_extension  = 'mp4';
        }
	else {
                 stream_extension  = 'auto';
        }

        button.innerHTML = stream_extension.toUpperCase();

        document.cookie = 'transcode_type=' + stream_extension + ';path=/;max-age=631152000';

        FlashVideo.MakeSWFObject();
        if (! $('mainpic')) {
            FlashVideo.DoReplace();
        }
    },

    MaybeSetTranscodeType : function () {
        var cookies = document.cookie.split(/;\s*/);

        for (var i in cookies) {
            var xc_match = cookies[i].match(/^transcode_type=(\w+)/);
            if (xc_match && xc_match.length > 0) {
                stream_extension = xc_match[1];
            }
        }

        if (stream_extension != 'flv') {
            $('tctype').innerHTML = stream_extension.toUpperCase();
        }
    },

    SetBitrate : function () {
        var br_select = $('bitrate');
        var old_br    = video_bitrate;
        var new_br    = br_select.options[br_select.selectedIndex].value;

        if (new_br != old_br) {
            video_bitrate   = new_br;
            document.cookie = 'bitrate=' + video_bitrate + ';path=/;max-age=631152000';
            FlashVideo.MakeSWFObject();
        }
    },

    MaybeSetBitrate : function () {
        var cookies = document.cookie.split(/;\s*/);
        var cookie_bitrate = 0;

        for (var i in cookies) {
            var br_match = cookies[i].match(/^bitrate=(\d+)/);
            if (br_match && br_match.length > 0) {
                cookie_bitrate = br_match[1];
            }
        }

        var br_select  = $('bitrate');
        var br_options = br_select.options;
        for (var i = 0; i < br_select.length; i++) {
            if (cookie_bitrate == br_options[i].value) {
                br_select.selectedIndex = i;
                video_bitrate = cookie_bitrate;
                break;
            }
        }
    }
};

Pop.RevealPlayHint = function () {
    if (want_pop > 0 || !enable_play_hint) return;

    // $('debug').innerHTML += 'RevealPlayHint ';
    var pop    = $('pop');
    var hint   = $('hint');
    var story  = $('mainpic');
    var left   = Pop.FindLeft(story);
    var top    = Pop.FindTop(story);
    var width  = 645;
    var height = 392;

    hint.innerHTML  = '<img src="/images/play.png" alt="play" border="0" style="padding:6px; vertical-align:middle" />';
    pop.style.left  = (left + Math.floor(width  / 2) - 30) + 'px';
    pop.style.top   = (top  + Math.floor(height / 2) - 20) + 'px';
    pop.style.width = '50px';

    want_pop++;
    if (timeout != 0) clearTimeout(timeout);
    timeout = setTimeout("Pop.MaybeReveal();", 500);
};
