function newVideoPlayer(videoURL, movieWidth, movieHeight, waterMarkURL, agegate) {
    videoPlayer(videoURL, movieWidth, movieHeight, waterMarkURL, agegate);
}

function videoPlayer(videoURL, movieWidth, movieHeight, waterMarkURL, agegate) {
    var vidId = Math.round(Math.random() * 10000);
    agegate = agegate ? agegate : (typeof post_age_limit != 'undefined' ? post_age_limit : null);
    agegate = agegate ? agegate : (typeof videoURL == 'object' ? videoURL.agegate : null);
    if (videoURL.source) {
        video = createmotionboxPlayer(videoURL, movieWidth, movieHeight, waterMarkURL);
    } else {
        video = createGawkerPlayer(videoURL, movieWidth, movieHeight, waterMarkURL);
    }
    document.writeln('</p><div id="obj_' + vidId + '"></div><p>');
    //if (agegate) {
    //    GawkerClientside.pushWidget('agegate', jQuery('#obj_' + vidId).AgeGate({
    //        id: vidId,
    //        object: video,
    //        limit: agegate
    //    }).data('AgeGate'));
    //} else {
        video.handler(vidId, video);
    //}
}

function createmotionboxPlayer(video) {
    video.handler = displayMotionBoxPlayer;
    return video;
}

function createGawkerPlayer(videoURL, movieWidth, movieHeight, waterMarkURL) {
    if (!parseInt(movieWidth)) movieWidth = 500;
    if (!parseInt(movieHeight)) movieHeight = 375;
    if (movieWidth > 500) {
        var oldMovieWidth = movieWidth;
        movieWidth = 500;
        movieHeight = Math.round(movieHeight / oldMovieWidth * movieWidth) + 20;
    } else {
        movieHeight += 20;
    }
    var flashvars = {
        bgcolor: "#000000",
        videoURL: videoURL,
        stageWidth: movieWidth,
        stageHeight: movieHeight,
        waterMarkImageURL: waterMarkURL
    };
    var params = {
        quality: 'best',
        scale: 'noscale',
        salign: 'tl',
        allowScriptAccess: 'always'
    };
    var attributes = {
        'class': "flv gawkerVideo"
    };
    if (window.permalink) flashvars.permalink = permalink;
    else flashvars.permalink = "undefined";
    if (window.autoplay) {
        flashvars.autoplay = autoplay;
        window.autoplay = false;
    } else flashvars.autoplay = "undefined";
    return {
        width: movieWidth,
        height: movieHeight,
        flashvars: flashvars,
        params: params,
        attributes: attributes,
        handler: displayGawkerPlayer
    };
}

function displayMotionBoxPlayer(id, videoData) {
    if (/[&?]autoplay=true/.test(location.search) && videoData.source == 'motionbox') {
        videoData.player += '&autoPlay=true';
    }
    var vd = videoData;
    var classId = '';
    //if (jQuery('html').hasClass('ie')) {
        if (vd.flashData) {
            vd.player = vd.flashData;
            vd.flashData = false;
        }
        classId = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
    //}
    var objectString = '<object width="' + vd.width + '" height="' + vd.height + '"';
    if (vd.flashData) objectString += ' data="' + vd.flashData + '"';
    if (vd.objectId) objectString += ' id="' + vd.objectId + '"';
    objectString += ' classid="' + classId + '" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="movie" value="' + vd.player + '" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" />';
    var flashVars = '';
    for (var i = 0, l = vd.customParams.length; i < l; i++) {
        var item = vd.customParams[i];
        var lcname = item.name.toLowerCase();
        if (lcname == 'flashvars') flashVars = item.value;
        else if (lcname == 'allowfullscreen' || lcname == 'allowscriptaccess') continue;
        objectString += '<param name="' + item.name + '" value="' + item.value + '" />';
    }
    if (!vd.noEmbed) {
        objectString += '<embed src="' + vd.player + '" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" allowscriptaccess="always" allowfullscreen="true" width="' + vd.width + '" height="' + vd.height + '"';
        if (flashVars) objectString += ' flashvars="' + vd.flashVars + '"';
        if (vd.embedName) objectString += ' name="' + vd.embedName + '"';
        objectString += '></embed>';
    }
    objectString += '</object>';
    jQuery('#obj_' + id)[0].innerHTML = objectString;
}

function displayGawkerPlayer(id, videoData) {
    jQuery('#obj_' + id)[0].innerHTML = '<object class="flv gawkerVideo" style="height: ' + videoData['height'] + 'px; width: ' + videoData['width'] + 'px;" id="flv_' + id + '"></object>';
    swfobject.embedSWF("http://cache.gawkerassets.com/assets/util/videoModule.008.2.swf", 'flv_' + id, videoData['width'], videoData['height'], "9.0.0", "expressInstall.swf", videoData['flashvars'], videoData['params'], videoData['attributes']);
}
