Ext.namespace("Flowz");

Flowz.Player = function(config) {
    Ext.apply(this, config);
  	//Flowz.Player.superclass.constructor.call(this, config);
	this.players = new Array();
	for(var e in this.extensions){
		//this.players[e] = new RegExp('\.(' + this.extensions[e].join('|') + ')\s*$', 'i');
		this.players[e] = new RegExp('(' + this.extensions[e].join('|') + ')', 'i');
	}

	this.plugins = null;

	// detect plugin support
	if(navigator.plugins && navigator.plugins.length) {
		var detectPlugin = function(plugin_name) {
			var detected = false;
			for(var i = 0, len = navigator.plugins.length; i < len; ++i) {
				if(navigator.plugins[i].name.indexOf(plugin_name) > -1) {
					detected = true;
					break;
				}
			}
			return detected;
		};
		var f4m = detectPlugin('Flip4Mac');
		plugins = {
			fla:	detectPlugin('Shockwave Flash'),
			qt:		detectPlugin('QuickTime'),
			wmp:	!f4m && detectPlugin('Windows Media'), // if it's Flip4Mac, it's not really WMP
			f4m:	f4m
		};
	} else {
		var detectPlugin = function(plugin_name) {
			var detected = false;
			try {
				var axo = new ActiveXObject(plugin_name);
				if(axo) detected = true;
			} catch(e) {
			}
			return detected;
		};
		plugins = {
			fla:	detectPlugin('ShockwaveFlash.ShockwaveFlash'),
			qt:		detectPlugin('QuickTime.QuickTime'),
			wmp:	detectPlugin('wmplayer.ocx'),
			f4m:	false
		};
	}
	this.options = {
		flashBgColor: '#000000',
		autoplayMovies:     true,
		showMovieControls:  true
	};
};

Flowz.Player = Ext.extend(Flowz.Player, {
	extensions: {
		img:        ['png', 'jpg', 'jpeg', 'gif', 'bmp'],
		swf:        ['swf'],
		mp3:		['mp3'],
		flv:        ['flv'],
		qt:         ['dv', 'mov', 'moov', 'movie', 'mp4'],
		wmp:        ['asf', 'wm', 'wmv'],
		qtwmp:      ['avi', 'mpg', 'mpeg'],
		iframe:     ['asp', 'aspx', 'cgi', 'cfm', 'css', 'js', 'htm', 'html', 'pl', 'php',
					'php3', 'php4', 'php5', 'phtml', 'rb', 'rhtml', 'shtml',
					'txt', 'vbs']
	},

	/**
	 * Requires an object with at least:
	 * id, extension, name
	 * and optionally:
	 * width, height
	 * @param file
	 */
	getPlayer: function(file) {
		for(var p in this.players) {
			if(!this.players[p].test)
				continue;
			if(this.players[p].test(file.exts)) {
				var player = this.checkSupported(p);
				if(player != "unsupported") {
					//console.log("new Flowz.Player." + player + "(file);");
					return eval("new Flowz.Player." + player + "(file);");
				}
			}
		}
		//console.log("player not found, returning default.");
		return new Flowz.Player.img(file);
	},

	checkSupported: function(player) {
		if(player == "img") return 'img';
		if(player == "swf") return plugins.fla ? 'swf' : 'unsupported-swf';
		if(player == "flv") return plugins.fla ? 'flv' : 'unsupported-flv';
		if(player == "qt") return plugins.qt ? 'qt' : 'unsupported-qt';
		if(player == "wmp") {
			if(plugins.wmp) return 'wmp';
			if(plugins.f4m) return 'qt';
			if(Ext.isMac) return plugins.qt ? 'unsupported-f4m' : 'unsupported-qtf4m';
			return 'unsupported-wmp';
		} else if(player == "qtwmp") {
			if(plugins.qt) return 'qt';
			if(plugins.wmp) return 'wmp';
			return Ext.isMac ? 'unsupported-qt' : 'unsupported-qtwmp';
		} else if(player == "iframe") {
			return 'iframe';
		} else if(player == "mp3") {
			return 'mp3';
		}
		return 'unsupported'; // same domain, not supported
	},

	getOptions: function() {
		return this.options;
	}
});

/**
 * Image Player
 *
 */
(function(){
    var preloader;

    Flowz.Player.img = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
        this.file = file;


        // preload the image
        this.ready = false;
        var self = this; // needed inside preloader callback
        this.preloader = new Image();
        this.preloader.onload = function(){
            // height defaults to image height
            self.height = self.file.height ? parseInt(self.file.height, 10) : preloader.height;

            // width defaults to image width
            self.width = self.file.width ? parseInt(self.file.width, 10) : preloader.width;

            // ready to go
            self.ready = true;

            // clean up to prevent memory leak in IE
            self.preloader.onload = '';
            self.preloader = null;
			if(self.onLoad)
				self.onLoad();
        };
        this.preloader.src = this.file.url;
    };

    Flowz.Player.img.prototype = {

		/**
		 * The html to display the image
		 *
		 * @param override
		 */
        markup: function() {

            return {
                tag:    'img',
                id:     this.id,
				width:  this.file.width,
				height: this.file.height,
				style: 'display: block; margin-left: auto; margin-right: auto; position: relative',
                src:    this.file.url
            };
        },

		getWidth: function() {
			return this.file.width;
		},

		getHeight: function() {
			return this.file.height;
		},

        /**
         * Call back after image is loaded
         */
        onLoad: function(){

        },

        /**
         * Removes this image from the document.
         */
        remove: function(anim) {
			var el = Ext.get(this.id);

			if(anim && el) {
				var cb = anim.callback.createDelegate(anim.scope);
				anim.callback = function() {
					el.remove();
					if(this.preloader) {
						this.preloader.onload = '';
						this.preloader = null;
					}
					cb();
				}
				anim.scope = this;
				el.setOpacity(0, anim);
				return;
			} else if(el)
				el.remove();

            // prevent old image requests from loading
            if(this.preloader) {
                this.preloader.onload = '';
                this.preloader = null;
            }
        }
    };

})();
(function(){
    Flowz.Player.flv = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
        this.file = file;
		this.defaultWidth = 320;
		this.defaultHeight = 240;

        this.height = this.file.height ? parseInt(this.file.height, 10) : this.defaultHeight;
		if(this.height == 0)
			this.height = this.defaultHeight;
		//if(Flowz.Player.options.showMovieControls){
            this.height += 20; // height of JW FLV player controller
        //}

        this.width = this.file.width ? parseInt(this.file.width, 10) : this.defaultWidth;
		if(this.width == 0)
			this.width = this.defaultWidth;
		if(this.width > 800)
			this.width = 800;
    };

    Flowz.Player.flv.prototype = {
		interactive: true,
		
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},

		getDefaultWidth: function() {
			return this.defaultWidth;
		},

		getDefaultHeight: function() {
			return this.defaultHeight;
		},
		
        markup: function(){

            var displayheight = this.height;// - (true ? 20 : 0); // subtract controller height
            var flashvars = [
                'file=' + this.file.url,
                'height=' + this.height,
                'width=' + this.width,
                'autostart=true',
                'displayheight=' + displayheight,
                'showicons=true',
                'backcolor=0x000000',
                'frontcolor=0xCCCCCC',
                'lightcolor=0x557722'
            ];

            return {
                tag:        'object',
                id:         this.id,
                name:       this.id,
                type:       'application/x-shockwave-flash',
                data:       '/scripts/shadowbox/flvplayer.swf',
                children:   [
                    { tag: 'param', name: 'movie', value: '/scripts/shadowbox/flvplayer.swf' },
                    { tag: 'param', name: 'flashvars', value: flashvars.join('&amp;') },
                    { tag: 'param', name: 'allowfullscreen', value: 'true' }
                ],
                height:     this.height, // new height includes controller
                width:      this.width
            };
        },

        remove: function(){
            var el = Ext.get(this.id);
            if(el) el.remove();
        }
    };
})();(function(){
    Flowz.Player.iframe = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
        this.file = file;
		this.defaultWidth = 640;
		this.defaultHeight = 480;

		this.width = this.file.width ? parseInt(this.file.width, 10) : this.defaultWidth;
		if(this.width > 800)
			this.width = 800;
		this.height = this.file.height ? parseInt(this.file.height, 10) : this.defaultHeight;
		if(this.height > 800)
			this.height = 800;

		if(this.height == 0)
			this.height = this.defaultHeight;
		if(this.width == 0)
			this.width = this.defaultWidth;
		if(this.width > 800)
			this.width = 800;
    };

    Flowz.Player.iframe.prototype = {
		interactive: true,
		
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},

		getDefaultWidth: function() {
			return this.defaultWidth;
		},

		getDefaultHeight: function() {
			return this.defaultHeight;
		},

        markup: function(){
            var markup = {
                tag:            'iframe',
                id:             this.id,
                name:           this.id,
				width:			this.width,
				height:			this.height,
                frameborder:    '0',
                marginwidth:    '0',
                marginheight:   '0',
                scrolling:      'auto'
            };

            if(Ext.isIE){
                // prevent brief whiteout while loading iframe source
                markup.allowtransparency = 'true';

                if(!Ext.isIE7){
                    // prevent "secure content" warning for https on IE6
                    // see http://www.zachleat.com/web/2007/04/24/adventures-in-i-frame-shims-or-how-i-learned-to-love-the-bomb/
                    markup.src = 'javascript:false;document.write("");';
                }
            }

            return markup;
        },

        onLoad: function(){
            var win = (Ext.isIE) ? Ext.get(this.id).dom.contentWindow : window.frames[this.id];
            win.location = this.file.url; // set the iframe's location
        },

        remove: function(){
            var el = Ext.get(this.id);
            if(el){
                el.remove();
                if(Ext.isGecko) delete window.frames[this.id]; // needed for Firefox
            }
        }
    };

})();(function(){
    Flowz.Player.qt = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
		this.options = {
			autoPlayMovies: true,
			showMovieControls: true
		};

        this.file = file;
		this.defaultWidth = 640;
		this.defaultHeight = 480;

        this.height = this.file.height ? parseInt(this.file.height, 10) : this.defaultHeight;
        if(this.options.showMovieControls) {
            this.height += 16; // height of QuickTime controller
        }
		if(this.height == 0)
			this.height = 480;

        this.width = this.file.width ? parseInt(this.file.width, 10) : this.defaultWidth;
		if(this.width == 0)
			this.width = 640;
		if(this.width > 800)
			this.width = 800;
    };

    Flowz.Player.qt.prototype = {
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},

		getDefaultWidth: function() {
			return this.defaultWidth;
		},

		getDefaultHeight: function() {
			return this.defaultHeight;
		},

		interactive: true,
		
        markup: function(){

            var markup = {
                tag:        'object',
                id:         this.id,
                name:       this.id,
                width:      this.width,
				height:     this.height,
				children:   [
                    { tag: 'param', name: 'src', value: this.file.url },
                    { tag: 'param', name: 'scale', value: 'aspect' },
                    { tag: 'param', name: 'controller', value: "true" },
                    { tag: 'param', name: 'autoplay', value: "true" }
                ],
                kioskmode:  'true'
            };
            if(Ext.isIE){
                markup.classid = 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
                markup.codebase = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
            }else{
                markup.type = 'video/quicktime';
                markup.data = this.file.url;
            }

            return markup;
        },

        /**
         * Removes this movie from the document.
         *
         * @return  void
         * @public
         */
        remove: function(){
            try{
                document[this.id].Stop(); // stop QT video stream
            }catch(e){}
            var el = Ext.get(this.id);
            if(el){
				el.remove();
            }
        }

    };

})();
(function() {
	Flowz.Player.swf = function(file) {
		this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
		this.file = file;
		this.defaultWidth = 320;
		this.defaultHeight = 240;
		this.width = this.file.width ? parseInt(this.file.width, 10) : this.defaultWidth;
		this.height = this.file.height ? parseInt(this.file.height, 10) : this.defaultHeight;
		if(this.height == 0)
			this.height = this.defaultHeight;
		if(this.width == 0)
			this.width = this.defaultWidth;
		if(this.width > 800)
			this.width = 800;
	};

	Flowz.Player.swf.prototype = {
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},
		
		getDefaultWidth: function() {
			return this.defaultWidth;
		},

		getDefaultHeight: function() {
			return this.defaultHeight;
		},

		interactive: true,
		
		markup: function() {
			//var bgcolor = Flowz.Player.getOptions().flashBgColor;
			var bgcolor = '#000000';
			return {
				tag:		'object',
				id:		 this.id,
				name:	   this.id,
				type:	   'application/x-shockwave-flash',
				data:	   this.file.url,
				children:   [
					{ tag: 'param', name: 'movie', value: this.file.url },
					{ tag: 'param', name: 'bgcolor', value: bgcolor }
				],
				width:	  this.width,
				height:	 this.height
			};
		},

		remove: function() {
			var el = Ext.get(this.id);
			if(el) el.remove();
		}
	};
})();
(function(){
    Flowz.Player.wmp = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
        this.file = file;
		this.defaultWidth = 320;
		this.defaultHeight = 240;

		this.options = {
			autoPlayMovies: true,
			showMovieControls: true
		};

        this.height = this.file.height ? parseInt(this.file.height, 10) : this.defaultHeight;
        //if(this.options.showMovieControls){
            // add height of WMP controller in IE or non-IE respectively
            this.height += (Ext.isIE ? 70 : 45);
        //}
        this.width = this.file.width ? parseInt(this.file.width, 10) : this.defaultWidth;

		if(this.height == 0)
			this.height = this.defaultHeight;
		if(this.width == 0)
			this.width = this.defaultWidth;
		if(this.width > 800)
			this.width = 800;
    };

    Flowz.Player.wmp.prototype = {
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},

		getDefaultWidth: function() {
			return this.defaultWidth;
		},

		getDefaultHeight: function() {
			return this.defaultHeight;
		},

		interactive: true,
		
        markup: function() {
			/*
			this.options = {
				autoPlayMovies: true,
				showMovieControls: true
			};
			*/
            //var options = Flowz.Player.getOptions();
            var autoplay = this.options.autoPlayMovies ? 1 : 0;
			
            var markup = {
                tag:        'object',
                id:         this.id,
                name:       this.id,
                height:     this.height, // height includes controller
                width:      this.width,
                children:   [
                    { tag: 'param', name: 'autostart', value: autoplay }
                ]
            };
            if(Ext.isIE){
                var controls = this.options.showMovieControls ? 'full' : 'none';
                // markup.type = 'application/x-oleobject';
                markup.classid = 'clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6';
                markup.children[markup.children.length] = { tag: 'param', name: 'url', value: this.file.url };
                markup.children[markup.children.length] = { tag: 'param', name: 'uimode', value: controls };
            }else{
                var controls = this.options.showMovieControls ? 1 : 0;
                markup.type = 'video/x-ms-wmv';
                markup.data = this.file.url;
                markup.children[markup.children.length] = { tag: 'param', name: 'showcontrols', value: controls };
            }

            return markup;
        },

        remove: function(){
            if(Ext.isIE){
                try{
                    window[this.id].controls.stop(); // stop the movie
                    window[this.id].URL = 'non-existent.wmv'; // force player refresh
                    window[this.id] = function(){}; // remove from window object
                }catch(e){}
            }
            var el = Ext.get(this.id);
            if(el){
                setTimeout(function(){ // using setTimeout prevents browser crashes with WMP
                    el.remove();
                }, 10);
            }
        }
    };
})();
/**
 * play mp3s via odeo player :/
 */
(function(){
    Flowz.Player.mp3 = function(file){
        this.id = file.domId;
		if(!this.id) {
			this.id = Ext.id();
		}
        this.file = file;
		this.width = 300;
		this.height = 52;
    };

    Flowz.Player.mp3.prototype = {
		interactive: true,
		
		getWidth: function() {
			return this.width;
		},

		getHeight: function() {
			return this.height;
		},

        markup: function(){
            var displayheight = this.height;// - (true ? 20 : 0); // subtract controller height
            var flashvars = [
				'valid_sample_rate=true',
				'external_url=' + this.file.url,
                'autostart=true'
            ];

			//<embed src= "http://www.odeo.com/flash/audio_player_standard_gray.swf" quality="high" width="300" height="52" allowScriptAccess="always" wmode="transparent"  type="application/x-shockwave-flash" flashvars= "valid_sample_rate=true&external_url=[MP3 file address]" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed>

            return {
                tag:        'object',
                id:         this.id,
                name:       this.id,
                type:       'application/x-shockwave-flash',
                data:       'http://www.odeo.com/flash/audio_player_standard_black.swf',
                children:   [
                    { tag: 'param', name: 'movie', value: 'http://www.odeo.com/flash/audio_player_standard_black.swf' },
                    { tag: 'param', name: 'flashvars', value: flashvars.join('&amp;') },
                    { tag: 'param', name: 'allowfullscreen', value: 'false' }
                ],
                width:      this.width,
				height:     this.height
			};


//			<object type="application/x-shockwave-flash" data="http://static.odeo.com/flash/player_audio_embed_v2.swf" width="325" height="60" id="odeo_audio">
//			  <param name="movie" value="http://static.odeo.com/flash/player_audio_embed_v2.swf" />
//			  <param name="FlashVars" value="jStr=[{'id': 24350387}]" />
//			</object>
//			<embed src= "http://www.odeo.com/flash/audio_player_standard_gray.swf" quality="high" width="300" height="52" allowScriptAccess="always" wmode="transparent"  type="application/x-shockwave-flash" flashvars= "valid_sample_rate=true&external_url=[MP3 file address]" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed>

        },

        remove: function(){
            var el = Ext.get(this.id);
            if(el) el.remove();
        }
    };
})();(function() {
/**
 * Anim subclass for scrolling the window to a position defined by the "scroll"
 * member of "attributes".  All "scroll" members are arrays with x, y scroll positions.
 * <p>Usage: <code>var myAnim = new YAHOO.util.Scroll(el, { scroll: { to: [0, 800] } }, 1, YAHOO.util.Easing.easeOut);</code></p>
 * @class Scroll
 * @namespace YAHOO.util
 * @requires YAHOO.util.Anim
 * @requires YAHOO.util.AnimMgr
 * @requires YAHOO.util.Easing
 * @requires YAHOO.util.Bezier
 * @requires YAHOO.util.Dom
 * @requires YAHOO.util.Event
 * @requires YAHOO.util.CustomEvent 
 * @extends YAHOO.util.ColorAnim
 * @constructor
 * @param {String or HTMLElement} el Reference to the element that will be animated (DISREGARDED for WindowScroll)
 * @param {Object} attributes The attribute(s) to be animated.  
 * Each attribute is an object with at minimum a "to" or "by" member defined.  
 * Additional optional members are "from" (defaults to current value), "units" (defaults to "px").  
 * All attribute names use camelCase.
 * @param {Number} duration (optional, defaults to 1 second) Length of animation (frames or seconds), defaults to time-based
 * @param {Function} method (optional, defaults to YAHOO.util.Easing.easeNone) Computes the values that are applied to the attributes per frame (generally a YAHOO.util.Easing method)
 */
var WindowScroll = function(el, attributes, duration,  method) {
    if (el) { // dont break existing subclasses not using YAHOO.extend
        WindowScroll.superclass.constructor.call(this, el, attributes, duration, method);
    }
};

WindowScroll.NAME = 'WindowScroll';

// shorthand
var Y = YAHOO.util;
YAHOO.extend(WindowScroll, Y.Scroll);

var superclass = WindowScroll.superclass;
var proto = WindowScroll.prototype;

proto.doMethod = function(attr, start, end) {
       var val = null;
   
       if (attr == 'scroll') {
           val = [
               this.method(this.currentFrame, start[0], end[0] - start[0], this.totalFrames),
               this.method(this.currentFrame, start[1], end[1] - start[1], this.totalFrames)
           ];
           
       } else {
           val = superclass.doMethod.call(this, attr, start, end);
       }
       return val;
};

proto.getAttribute = function(attr) {
       var val = null;
       
       if (attr == 'scroll') {
           val = [ 
				(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft),
				(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) 
			];
       } else {
           val = superclass.getAttribute.call(this, attr);
       }
       
       return val;
   };

proto.setAttribute = function(attr, val, unit) {
       var el = this.getEl();
       
       if (attr == 'scroll') {
		window.scrollTo(val[0], val[1]);
       } else {
           superclass.setAttribute.call(this, attr, val, unit);
       }
   };

	Ext.apply(Ext.lib.Anim, { scrollWindow: function(el, args, duration, easing, cb, scope){
	        if(typeof easing == "string"){
	            easing = YAHOO.util.Easing[easing];
	        }
			var anim = new WindowScroll(el, args, duration, easing);
	        anim.animateX(function(){
	            Ext.callback(cb, scope);
	        });
	        return anim;
		}
	});
})();

