/*
#        Pixelsilk jQuery Video Replacement Script v1.0
#        Use this script to replace Achor tags with the class of 'replaceVideo' with the Pixelsilk Flash Video Player.

#        Examples:

#        Text in the Anchor. The video will be inserted at default dimensions (400 x 300) and a generic "Play Video"
#        still image will be used as the "first frame" image.
#        <a href="/theVideo.flv" class="replaceVideo">The Video Text (will be replace)</a>

#        Image in the Anchor. In this example, the image will be used as the still "first frame" of the video and
#        the dimensions of the image designate the dimensions of the Flash Video inserted.
#        <a href="/theVideo.flv" class="replaceVideo"><img src="/videoStill.jpg" width="320" height="240" alt="" /></a>
*/

$(document).ready(function() {
    var anchorSelector = $('.replaceVideo');
    var videoPlayer = '/videoPlayer.swf';
    var vidWidth = 400;
    var vidHeight = 300;
    
    anchorSelector.each(function(i) {
        var firstFrame = '';
        var videoFile = $(this).attr('href');
        
        var img = $(this).find('img');
        if (img.length) {
            if (img.attr('width')) {
                vidWidth = img.attr('width');
            }
            if (img.attr('height')) {
                vidHeight = img.attr('height');
            }
            firstFrame = img.attr('src');
        } else {
            firstFrame = '/play-video.jpg?w='+vidWidth+'&h='+vidHeight+'&padded=true';
        }
        
        //var code = '<object type="application/x-shockwave-flash" height="' + vidHeight + '" width="' + vidWidth + '" data="' + videoPlayer + '" id="flashVideoPlayer'+i+'"><param name="Movie" value="' + videoPlayer + '"><param name="Src" value="' + videoPlayer + '"><param name="Quality" value="High"><param name="wmode" value="transparent"><param name="flashvars" value="playVid=' + videoFile + '&vidWidth=' + vidWidth + '&vidHeight=' +vidHeight + '&img=' + firstFrame + '"></object>';
        var code = '';
        if (jQuery.browser.msie && jQuery.browser.version >8) {
            // for IE 9 and above:
            code = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="visibility:visible;" type="application/x-shockwave-flash" height="' + vidHeight + '" width="' + vidWidth + '" data="' + videoPlayer + '" id="flashVideoPlayer'+i+'"><param name="quality" value="High"><param name="wmode" value="transparent"><param name="flashvars" value="playVid=' + videoFile + '&vidWidth=' + vidWidth + '&vidHeight=' + vidHeight + '&img=' + firstFrame + '"></object>';
        } else {
            // all other browsers:
            code = '<object style="visibility:visible;" type="application/x-shockwave-flash" height="' + vidHeight + '" width="' + vidWidth + '" data="' + videoPlayer + '" id="flashVideoPlayer'+i+'"><param name="Movie" value="' + videoPlayer + '"><param name="Src" value="' + videoPlayer + '"><param name="quality" value="High"><param name="wmode" value="transparent"><param name="flashvars" value="playVid=' + videoFile + '&vidWidth=' + vidWidth + '&vidHeight=' + vidHeight + '&img=' + firstFrame + '"></object>';
        }
        
        $(this).replaceWith(code);
    });
});
