﻿/*
* jQuery Media plugin 0.1.0
* http://plugins.jquery.com/project/jqmedia
* Copyright (c) 2008 Leonardo Rossetti motw.leo@gmail.com
*  GPL License: http://gplv3.fsf.org/
*
*Adds media object in the page.
*/

jQuery.fn.jqmedia = function(options) {
    var $this;
    var width = options.width;
    var height = options.height;
    var src = options.src;
    var alt = options.alt ? options.alt : "";
    var wmode = options.wmode ? options.wmode : "transparent";
    var quality = options.quality ? options.quality : "high";
    var mediaObject;
    //mime type and extension list
    var type = {
        "application/x-shockwave-flash": "swf"
    };
    //activex object reference according to the mimetype
    var msieMedia = {
        "application/x-shockwave-flash": "ShockwaveFlash.ShockwaveFlash"
    };
    //get mime type according to the file extension
    var getData = function(data) {
        var dataType;
        var mimeType;
        for (var mimeType in type) {
            if (type.hasOwnProperty(mimeType)) {
                if (data.indexOf(type[mimeType]) !== -1) {
                    dataType = mimeType;
                }
            }
        }
        return dataType;
    };
    //checks if mimetype is supported
    var isAvailable = function(data) {
        var ieObj;
        var results = false;
        if (navigator.mimeTypes[data]) {
            results = true;
        } else if (window.ActiveXObject) {
            try {
                ieObj = new ActiveXObject(msieMedia[data]);
                ieObj = null;
                results = true;
            } catch (e) {
                results = false;
            }
        }
        return results;
    };
    //html object
    mediaObject = "<object data=\"" + src + "\" type=\"" + getData(src) + "\"";
    mediaObject += "width=\"" + width + "\" height=\"" + height + "\">";
    mediaObject += "<param name=\"movie\" value=\"" + src + "\" />";
    mediaObject += "<param name=\"quality\" value=\"" + quality + "\" />";
    mediaObject += "<param name=\"wmode\" value=\"" + wmode + "\" />";
    mediaObject += "</object>";
    //if plugin is supported, displays the html object in the element, otherwise displays the options.alt content in the element
    return this.each(function() {
        $this = jQuery(this);
        if (isAvailable(getData(src))) {
            $this.html(mediaObject);
        } else {
            $this.html(alt);
        }
    });
};
