﻿// FLASH VIDEO PLAYER CODE
// Require jQuery 1.3.1+
//
// Author: Joel Nagy
//------------------------------------------------------------------
// Links to play a video should be called: goVplay('GUID-STRING-GOES-HERE')

// VIDEO GLOBALS
var $videoGuid, $sortCat, $showNextPrev, $baseUrl, $namodelCodeQS = '';
var __winHeight = 0;
var __opacity = 1; // set BG to 1% for browser compliant modal bg, let Flash handle the official design sanctioned opacity level

// BROWSER GLOBALS
var __nav = navigator.userAgent.toLowerCase();
var $ready = false;

$(document).ready(function() {
    window.ie6 = $.browser.msie && parseInt($.browser.version) < 7;
    window.ie = $.browser.msie;
    window.gecko = $.browser.mozilla;
    window.macppc = __nav.indexOf('ppc') > 0 && __nav.indexOf('mac') > 0;
});

// Dimensions of the Viewable Window
window.getHeight = function() {
    if (window.innerHeight) return window.innerHeight;
    else if (document.documentElement.clientHeight) return document.documentElement.clientHeight;
    else if (document.body.clientHeight) return document.body.clientHeight;
    else return document.body.offsetHeight;
};
window.getWidth = function() {
    if (window.innerWidth) return window.innerWidth;
    else if (document.documentElement.clientWidth) return document.documentElement.clientWidth;
    else if (document.body.clientWidth) return document.body.clientWidth;
    else return document.body.offsetWidth;
};

// API: launch video
// sends input to flash; loads player swf using swfload(swfobject)
// Example HTML: <a href="javascript:goVplay('http://resource.bmwusa.com/Swf_00000000-0000-0000-0000-000000000000.arox', 'e0b74591-9a91-47b4-9675-6332482cae76', 'none', false, 'www.bmwusa.com/index.html', 'namodelcode=0920');">THUMBNAILorTEXT</a>

// new param nmaes
// swfUrl, videoGuid, sortCat, showNextPrev, baseUrl, namodelCodeQS

function goVplay(swfUrl, videoGuid, sortCat, showNextPrev, baseUrl, namodelCodeQS) {
    if (!$ready) {
        return;
    }
    //    try {
    // hide Selects for ie6
    if (window.ie6)
        toggleSelects('off');

    // launch/show vplayer
    if (_$('vplayswf') == null) {
        // Main Container

        var $vplay = $('<div id="vplayContainer" style="display: block; z-index: 999999; top: 0; left: 0; width: 100%; height: 0; position: ' + (window.ie6 ? 'absolute' : 'fixed') + '; overflow: hidden; background: transparent"></div>').prependTo($(document.body));
        // SWF container
        var $vplayInner = $('<div id="vplay" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: transparent"></div>').prependTo($vplay);
        // Close button
        $('<div style="display: block; z-index: 1000000; top: 5px; right: 30px; overflow: hidden; position: absolute; height: 22px; width: 100px"><a href="javascript:noVplay(true)" style="font-family: sans-serif; text-decoration: none; color: black; font-size: 10px; float: right;">[close]</a></div>').prependTo($vplay);
        // Background
        var $vplayBG = $('<div id="vplayBG" style="display: block; z-index: 999990; top: 0; left: 0; width: 100%; height: 0; position: ' + (window.ie6 ? 'absolute' : 'fixed') + '; overflow: hidden; background: white; opacity: 0.01"></div>').prependTo($(document.body));

        var wm = 'transparent';
        if (window.gecko && window.macppc)
            wm = 'opaque';

        // Insert SWF
        swfload('vplay', 'vplayswf', swfUrl, '100%', '100%', { xml_path: '/data/player_default_config.xml' }, { wmode: wm, allowscriptaccess: 'always' });

        if (!window.ie6)
            $vplayBG.css('opacity', __opacity / 100);
        else
            $vplayBG.css('filter', 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + __opacity + ')');
    }

    // set vplayer (bg,container) to be seen
    _$('vplayContainer').style.display = 'block';
    _$('vplayBG').style.display = 'block';


    // set vplayer (bg,container,self) to current viewable area
    var h = window.getHeight();
    var w = window.getWidth();

    _$('vplayContainer').style.height = h + 'px';
    _$('vplayContainer').style.width = w + 'px';
    _$('vplay').style.height = h + 'px';
    _$('vplay').style.width = w + 'px';
    if (window.ie6) {
        _$('vplayBG').style.height = h + 'px';
        _$('vplayBG').style.width = w + 'px';
    }

    if (window.ie6) { // ie 6 specific stuff to remove scrolling
        __winHeight = parseInt($(window).height()); // the full height of the scrollable content
        _$('vplayContainer').style.top = parseInt($(window).scrollTop()) + 'px';
    }

    // set params
    $videoGuid = videoGuid;
    $sortCat = sortCat;
    $showNextPrev = showNextPrev;
    $baseUrl = 'http://' + baseUrl;
    $namodelCodeQS = namodelCodeQS;

    var $flash = $('#vplayswf');
    if ($flash.length > 0 && $flash[0].setGuid)
        $flash[0].setGuid(videoGuid, sortCat, showNextPrev, $baseUrl, namodelCodeQS);
    //    } catch (e) { }
};

// API: Flash calls this function to retreive current params
function getVplayID() { // send to flash if requested
    return [$videoGuid, $sortCat, $showNextPrev, $baseUrl, $namodelCodeQS];
}

// API: Call from JS or Flash to handle closing of the video player
// If called from JavaScript/HTML then fromJS must be set to true
// so that a call to Flash can be made to stop videos and transition out
function noVplay(fromJS) {
    //    try {
    fromJS = fromJS == null ? false : fromJS;
    var $vplayswf = _$('vplayswf')
    _$('vplayContainer').style.height = 0;
    _$('vplayBG').style.height = 0;
    if ((!fromJS && window.ie6) || ($vplayswf.closeVideo == null && fromJS && window.ie6))
        toggleSelects('on');
    if (fromJS && $vplayswf.closeVideo != null)
        $vplayswf.closeVideo();
    //    } catch (e) { }
}

// API (possible future enhancement): Tell Flash to pause Video
function pauseVideo() {
}
// API (possible future enhancement): Tell Flash to resume/play Video
function resumeVideo() {
}
// API (possible future enhancement): Tell Flash to set auto play
function setAutoPlay() {
}

// EVENTS
// Window Event to resize the player area based on the Viewable area.
function resizeVplay() {
    //    try {
    var $vplayBG = $('#vplayBG');
    var $vplay = $('#vplayContainer');
    var $vplayInner = $('#vplay');
    var $vplayswf = $('#vplayswf');

    var h = window.getHeight();
    var w = window.getWidth();

    if ($vplay != null && parseInt($vplay.css('height')) > 1 && $vplayswf != null) {
        if (window.ie6) {
            $vplayBG.css('height', h + 'px');
            $vplayBG.css('width', w + 'px');
            $vplay.css('top', parseInt($(window).scrollTop()) + 'px');
        }
        $vplay.css('height', h + 'px');
        $vplay.css('width', w + 'px');
        $vplayInner.css('height', h + 'px');
        $vplayInner.css('width', w + 'px');
    }
    //    } catch (e) { }
}

function scrollVplay() {
    _$('vplayContainer').style.top = parseInt($(window).scrollTop()) + 'px';
    _$('vplayBG').style.top = parseInt($(window).scrollTop()) + 'px';
}

// Window events to handle styling of intitial and resized windows
$(document).ready(function() {
    try {
        if (window.ie6)
            if (_$('vplayswf') != null && parseInt(_$('vplayContainer').style.height) > 0) scrollVplay()

        resizeVplay();
        $(window.body).css('height', '100%');
        $(window.html).css('height', '100%');
    } catch (e) { }
});

$(document).ready(function() {
    $(window).resize(function() { resizeVplay(); });
    $ready = true;
});

function toggleSelects(offOrOn) {
    try {
        var S = document.getElementsByTagName('SELECT');
        for (var i = 0, l = S.length; i < l; i++) {
            var s = S[i];
            var visi = offOrOn == 'off' ? 'hidden' : 'visible';
            s.style.visibility = visi;
        }
    } catch (e) { }
}
