window.addEvent("domready", function(){
	//protected/self executing funtion to keep this script from interfering
	(function(){
		
		/*
		 * The big object ---> this simply wraps the SWF OBJECT and adds QVC speccific functionality,
		 * this is currently set up only to except default SO parameters but can be extended as needed.
		 * 
		 * video=new SWFObject and video=new _v are interchangable
		 */
		
		var _v=new Class({
			initialize: function(swf, element, width, height, version, backgroundColor, construct){
				this.name=element;
				this.backgroundColor=backgroundColor;
				var version = version || '8';
				this.so=new SWFObject(swf, this.name, width, height, version, backgroundColor);
				this.addVariable("objName", this.name);
				this.addParam("class", 'qvc_video');
				//
				construct(this);
			},
			write:function(element, videoWidth, videoHeight, vars, params, videoWeekly){

				(function purge(d){
					var a = d.attributes, i, l, n;
					if(a){
						l = a.length;
						for(i = 0; i < l; i += 1){
							n = a[i].name;
							if(typeof d[n] === 'function') {
								d[n] = null;
							}
						}
					}
					a = d.childNodes;
					if(a){
						l = a.length;
						for (i = 0; i < l; i += 1){
							purge(d.childNodes[i]);
						}
					}
				})(element);

				this.addVariable("totalHeight", videoHeight);
				this.addVariable("videoWidth", videoWidth);
				
				//add dynamic parameters and variables
				
				for(var i=0; i<vars.length; i++){
					this.addVariable(vars[i][0], vars[i][1]);
				}
				
				for(i=0; i<params.length; i++){
					this.addParam(params[i][0], params[i][1]);
				}
				
				//Build Holder Element -- to provide Browser agnostic wrapper to house functions
				
				var holder=new Element('div',{
						'styles':{'backgroundColor':this.backgroundColor},
						'id':this.name+'_holder'
				});
			
				holder.pause=function(){
					var videoName=this.getElements('*')[0].id;
					thisMovie(videoName).stopVideo();
				};
				holder.play=function(){
					var videoName=this.getElements('*')[0].id;
					thisMovie(videoName).stopVideo();
				};
				holder.changeContent=function(ObjRef, url, isPlaying, shouldMute, imageName){
					var videoName=this.getElements('*')[0].id;
					var parentElement=this.parentNode;
					//
					if (!window.ie) {
						var videoName=this.getElements('*')[0].id;
						thisMovie(videoName).changeContent(url, isPlaying, shouldMute, imageName);
					}else{
						parentElement.innerHTML='';
						ObjRef.write(parentElement, 684, 376,
							[['videoURI', url], ['autoPlay',isPlaying], ['doesMute', shouldMute], ['imageName',imageName]],
							[]
						);
					}
				};
				holder.changeContentWithTime=function(ObjRef, url, isPlaying, shouldMute, freezeTime){
					var videoName=this.getElements('*')[0].id;
					var parentElement=this.parentNode;
					//
					if (!window.ie) {
						var videoName=this.getElements('*')[0].id;
						thisMovie(videoName).changeContent(url, isPlaying, shouldMute, freezeTime);
					}else{
						parentElement.innerHTML='';
						ObjRef.write(parentElement, 684, 376,
							[['videoURI', url], ['autoPlay',isPlaying], ['doesMute', shouldMute], ['previewTime',previewTime]],
							[]
						);
					}
				};
				holder.changeContentWithSegments=function(ObjRef, url, isPlaying, shouldMute, imageName, previewTime, segmentStart, segmentEnd){
					var videoName=this.getElements('*')[0].id;
					var parentElement=this.parentNode;
					//
					if (!window.ie) {
						var videoName=this.getElements('*')[0].id;
						thisMovie(videoName).changeContentWithSegments(ObjRef, url, isPlaying, shouldMute, imageName, previewTime, segmentStart, segmentEnd);
					}else{
						parentElement.innerHTML='';
						ObjRef.write(parentElement, 684, 376,
							[['videoURI', url], ['autoPlay',isPlaying], ['doesMute', shouldMute], ['imageName',imageName], ['previewTime',previewTime], ['segmentStart',segmentStart],['segmentEnd',segmentEnd]],
							[]
						);
					}
				};
				holder.removeFromDOM=function(){
					this.remove();
				}			
                
                if (videoWeekly != undefined){
                    if(window.ie){
				        if (videoWeekly == true){
				            if (this.so.getVariablePairs().join("&").indexOf('NO-MOVIE') == -1){
				                this.so.attributes.swf = this.so.attributes.swf + "?" + this.so.getVariablePairs().join("&");
				            }
				        }
				    }
				}   
				//preform .write: this function is native to the SO object
				holder.innerHTML=element.innerHTML;
				element.innerHTML='';
				//
				this.so.write(holder);
				
				//put it on the page
				holder.injectInside(element);

				//no flash
				if((holder.innerHTML == '') || (holder.innerHTML == '<!-- No Flash Installed -->') || (holder.innerHTML.indexOf('\n') > -1)){
					var noFlash = new Element('div',{'id':'noflash','class':'noflash'}).setHTML('<div>Flash 8 or higher is required to view QVC videos.<br /><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash?rewrite=no" title="Download Flash Player" target="_blank">Download</a> the latest version of Flash.</div>').injectInside(element);
				}
				
				/*
				 * IE is not that great at updating <OBJECT> eg ...
				 * object dosen't support this blah blah error so be nice to ie with this conditional.
				*/
				if(document.getElementById(this.name) != null)
				{
					if(!window.ie){
						document.getElementById(this.name).setProperties({
							'width':videoWidth,
							'height':videoHeight
						});
					}else{
						document.getElementById(this.name).setAttribute('width', videoWidth);
						document.getElementById(this.name).setAttribute('height', videoHeight);
					}
				}
				//
				
				function thisMovie(movieName) {
				    if (window.ie) {
				        return window[movieName]
				    }
				    else {
				        return document[movieName]
				    }
				}
				
			},
			//Wrap SO functions inside Class
			addVariable:function(name, value){
				this.so.addVariable(name, value)
			},
			addParam:function(name, value){
				this.so.addParam(name, value)
			}
		});
		//This function called upon user interaction and on video load
		
		function dispatchEvent(eventName, eventObj){
			function handleEvent(eventName, video, execute, callback, args){
				var args = args || [];
				var callback=callback || function(){return true};
				var execute=execute || true;
				switch(eventName){
					case 'pause':
						if(!eventObj.isPaused){
							video.pause();
						}
						//callback page speciffic action and don't break if it's not there.
						callback = pause || callback;
						break;
					case 'play':
						if(eventObj.isPaused){
							//video.play();
						}
						callback = play || callback;
						break;
					case 'expandButtonPress':
						callback = expandButtonPress || callback;
						break;
					case 'changeContent':
						video.switchVideo(args);
						//callback = changeContent || callback;
						break;
					default:
						callback=callback;
						break;
				}
				//
				if(execute){callback()};
			}

			handleEvent(eventName, $(eventObj.objName));
			
			//put internal function in 'video namespace when the first event is fired'
			if(!window['video']['executeEvent']){
				window['video']['executeEvent']=handleEvent;
			}
		}
		
		//set up a protected namespace to store video functions/classes
		window['video']={};
		window['video']['_v']=_v;
		
		//make dispatch event avaliable to the flash object
		window['dispatchEvent']=dispatchEvent;
	})();
});