(function(SinemaTv, $) {
    SinemaTv.ui = SinemaTv.ui || {};

    /**
     * Put a video player to element 
     * with function putInto()
     *
     * setPlayerSwf().setFlv() should be set
     *
     * setSkin().setImage() is optional
     */ 
    var videoPlayer = {
        playerSwf : undefined,
        width : "300",
        height : "200",
        skin : undefined,
        flv : undefined,
        image : undefined,

        setPlayerSwf : function(playerSwf){
            this.playerSwf = playerSwf;
            return this;
        },

        setWidth : function(width){
            this.width = width;
            return this;
        },

        setHeight : function(height){
            this.height = height;
            return this;
        },
      
        setSkin : function(skin){
            this.skin = skin;
            return this;
        },

        setFlv : function(flv){
            this.flv = flv;
            return this;
        },

        setImage : function(image){
            this.image = image;
            return this;
        },

        putInto : function(element){


            var flashVarsArray = [];
            if(!this.flv || !this.playerSwf){
                return false; //TODO error should be returned
            }

            flashVarsArray[flashVarsArray.length] = "file=" + this.flv;
            if(this.image) {
                flashVarsArray[flashVarsArray.length] = "image=" + this.image;
            }

            if(this.skin) {
                flashVarsArray[flashVarsArray.length] = "skin=" + this.skin;
            }

            var flashVars = flashVarsArray.join("&");
   


            var obj = $("<object>").attr("classid","clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
            obj.attr("width", this.width);
            obj.attr("height", this.height);




            $("<param>").attr("movie", this.playerSwf).appendTo(obj);
            $("<param>").attr("allowfullscreen", "true").appendTo(obj);
            $("<param>").attr("allowscriptaccess", "true").appendTo(obj);
            $("<param>").attr("wmode", "opaque").appendTo(obj);
            $("<param>").attr("flashvars", flashVars).appendTo(obj);





            var embed = $("<embed>").attr("type","application/x-shockwave-flash");
            embed.attr("src", this.playerSwf);
            embed.attr("width", this.width);
            embed.attr("height", this.height);
            embed.attr("allowscriptaccess", "true");
            embed.attr("wmode", "opaque");
            embed.attr("flashvars", flashVars);

            /*
            embed = '<embed type="application/x-shockwave-flash" src="'+this.playerSwf+'" ';
            embed += 'width="'+this.width+'" height="'+this.height+'" allowscriptaccess="true" ';
            embed += 'wmode="opaque" flashvars="'+flashVars+'" </embed>';
                
            obj.html( obj.html() + embed );
            */

            if($.browser.msie){
                obj.html( obj.html() + embed.html() ); //explorer cannot append 'embed' to an 'object'
            }
            else {
                embed.appendTo(obj);
            }

            element.html(obj);
        }
    };


    SinemaTv.ui.VideoPlayer = function(){};
    SinemaTv.ui.VideoPlayer.prototype = videoPlayer;
})(SinemaTv, jQuery);

