/* THEATRICS.COM LLC CONFIDENTIAL - Unpublished Copyright (c) 2009-2011 Theatrics.com, LLC, All Rights Reserved. */

var ViddlerEmbedCodeReplacer={findViddlerVideos:function(){var result=new Array();var objects=document.getElementsByTagName('object');if(objects==null||objects.length==0){return result;}
for(var i=0;i<objects.length;i++){if(!this.checkViddlerUrl(objects[i])){var objectId=objects[i].id;if(!this.checkViddlerValidIds(objectId)){continue;}}
if(!this.checkValidClassId(objects[i])){continue;}
result.push(objects[i]);}
return result;},log:function(text,alertIfNoConsole){if(typeof DEBUG_VIDDLER_REPLACER!="undefined"&&DEBUG_VIDDLER_REPLACER!=null&&DEBUG_VIDDLER_REPLACER){if(typeof console!="undefined"&&console!=null&&console.log!=null&&typeof console.log=='function'){console.log(text);}
else if(typeof alertIfNoConsole!='undefined'&&alertIfNoConsole!=null&&alertIfNoConsole==true)
{alert(text);}}},checkViddlerUrl:function(object){this.log("Checking URL");var url=this.findMovieUrl(object);if(url!=null&&url.length>0){if(url.match(/https?\:\/\/([a-zA-Z0-9_-]+\.)*viddler\.com.*/gi)!=null){this.log("URL matched");return true;}}
this.log("URL not matched "+(url!=null?url:''));return false;},checkViddlerValidIds:function(objectId){this.log("Checking IDs");if(objectId==null||objectId.length==0){this.log("IDs not matched (no id attribute)");return false;}else{if(objectId.substr(0,8)=="viddler_"||objectId.substr(0,14)!="viddlerplayer-"){this.log("IDs matched");return true;}
this.log("IDs not matched (invalid IDs)");}
return false;},checkValidClassId:function(object){this.log("Checking classid's");var VIDDLER_CLASS_ID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';if(object.getAttribute('classid')!=null&&object.getAttribute('classid')==VIDDLER_CLASS_ID){this.log("Classid correct");return true;}
this.log("Invalid classid");return false;},findMovieUrl:function(parentElement){this.log("Looking for movie url"
+(parentElement.id!=null?' in object with id : #'+parentElement.id:''));var paramTags=parentElement.getElementsByTagName('param');if(paramTags==null||paramTags.length==0){this.log("Movie url not found ");return null;}
for(var i=0;i<paramTags.length;i++){if(paramTags[i].name=='movie'){this.log("Movie url found: "+paramTags[i].value);return paramTags[i].value;}}},findMovieParams:function(movieObject){this.log('Finding movie params');var movieURL=this.findMovieUrl(movieObject);var result=new Object;result.parsedOk=false;if(typeof movieURL!='undefined'&&movieURL!=null){result.movieURL=movieURL;this.log("Looking for token in url: "+movieURL);var urlParts=movieURL.split('/');var initialOffset=1;if(movieURL.charAt(movieURL.length-1)=='/'){initialOffset=2;}
result.token=urlParts[urlParts.length-initialOffset];if(typeof result.token!='undefined'&&result.token!=null&&result.token.length>0){this.log("Token found: "+result.token);result.parsedOk=true;}
else{this.log("Token not found");result.error="Token not found";result.parsedOk=false;return result;}
this.log("Looking for player type in url: "+movieURL);result.playerType=urlParts[urlParts.length-initialOffset-1];if(typeof result.playerType!='undefined'&&result.playerType!=null&&result.playerType.length>0){this.log("Player type found: "+result.playerType);result.parsedOk=true;}
else{this.log("Player type not found");result.error="Player type not found";result.parsedOk=false;return result;}
this.log("Looking for host name in url: "+movieURL);result.host=urlParts[2];if(typeof result.host!='undefined'&&result.host!=null&&result.host.length>0){this.log("Host name found: "+result.host);result.parsedOk=true;}
else{this.log("Host name not found");result.parsedOk=false;result.error="Host name not found";return result;}
this.log("Looking for protocol in url: "+movieURL);if(urlParts[0].charAt(urlParts[0].length-1)==':'){result.protocol=urlParts[0].substring(0,urlParts[0].length-1);}
else{result.protocol=urlParts[0];}
if(typeof result.protocol!='undefined'&&result.protocol!=null&&result.protocol.length>0){this.log("Protocol found: "+result.protocol);result.parsedOk=true;}
else{this.log("Protocol not found");result.parsedOk=false;result.error="Protocol not found";return result;}
this.log("Looking for movie dimensions in old embed code");if(typeof movieObject.attributes!='undefined'&&movieObject.attributes!=null&&typeof movieObject.attributes.length!='undefined'&&movieObject.attributes.length!=null){for(var i=0;i<movieObject.attributes.length;i++){if(movieObject.attributes[i].nodeName=='width'){result.width=movieObject.attributes[i].nodeValue;}
else if(movieObject.attributes[i].nodeName=='height'){result.height=movieObject.attributes[i].nodeValue;}}}
else{if(movieObject.getAttribute('width')!=null){result.width=movieObject.getAttribute('width');}
if(movieObject.getAttribute('height')!=null){result.height=movieObject.getAttribute('height');}}
if(typeof result.width!='undefined'&&result.width!=null&&typeof result.height!='undefined'&&result.height!=null){this.log("Movie dimensions found. Width: "+result.width+" ; Height: "+result.height);result.parsedOk=true;}
else{this.log("Movie dimensions not found");result.error="Movie dimensions not found";result.parsedOk=false;return result;}}
else{result.parsedOk=false;result.error="Movie URL not found";return result;}
return result;},buildHTML5EmbedCode:function(movieObject,replaceOnError){var result=new Object;result.success=false;var params=this.findMovieParams(movieObject);if(typeof params!='undefined'&&params!=null&&typeof params.parsedOk!='undefined'&&params.parsedOk!=null&&params.parsedOk==true){result.embedCode=document.createElement("object");result.embedCode.setAttribute('id','viddlerOuter'+params.token);var videoObject=document.createElement("video");videoObject.setAttribute("id","viddlerVideo"+params.token);videoObject.setAttribute("width",params.width);videoObject.setAttribute("height",params.height);videoObject.setAttribute("poster",params.protocol+"://"+params.host+"/thumbnail/"+params.token);videoObject.setAttribute("controls","controls");var srcObject=document.createElement("source");srcObject.setAttribute("src",params.protocol+"://"+params.host+"/file/"+params.token+"/html5/");videoObject.appendChild(srcObject);result.embedCode.appendChild(videoObject);result.success=true;this.log("Building iPad/iPhone replacement code successfull.")
return result;}
this.log("Building iPad/iPhone replacement code unsuccessfull.")
if(typeof replaceOnError!='undefined'&&replaceOnError!=null&&replaceOnError==true){result.success=false;result.embedCode='<p>There was an error when trying to replace old embed code</p>';}
return result;},buildPS3EmbedCode:function(movieObject,replaceOnError){this.log('buildPS3EmbedCode started');var result=new Object;result.success=false;var params=this.findMovieParams(movieObject);if(typeof params!='undefined'&&params!=null&&typeof params.parsedOk!='undefined'&&params.parsedOk!=null&&params.parsedOk==true){this.log('Building object');result.embedCode=document.createElement("object");result.embedCode.setAttribute('id','viddlerOuter'+params.token);result.embedCode.setAttribute('type','application/x-shockwave-flash');result.embedCode.setAttribute('data',params.movieURL);result.embedCode.setAttribute('width',params.width);result.embedCode.setAttribute('height',params.height);this.log('Building flashvars param');var flashvars=document.createElement('param');flashvars.setAttribute('name','flashvars');flashvars.setAttribute('value','movie=autoplay=f&disablebranding=f');result.embedCode.appendChild(flashvars);this.log('Building allowScriptAccess param');var allowScriptAccess=document.createElement('param');allowScriptAccess.setAttribute('name','allowScriptAccess');allowScriptAccess.setAttribute('value','always');result.embedCode.appendChild(allowScriptAccess);this.log('Building allowFullScreen param');var allowFullScreen=document.createElement('param');allowFullScreen.setAttribute('name','allowFullScreen');allowFullScreen.setAttribute('value','true');result.embedCode.appendChild(allowFullScreen);this.log('Building name param');var name=document.createElement('param');name.setAttribute('name','name');name.setAttribute('value','viddlerplayer-'+params.token);result.embedCode.appendChild(name);result.success=true;this.log('Building PS3 replacement code successfull');return result;}
this.log('Building PS3 replacement code unsuccessfull');if(typeof replaceOnError!='undefined'&&replaceOnError!=null&&replaceOnError==true){result.success=false;result.embedCode='<p>There was an error when trying to replace old embed code</p>';}
return result;},run:function(autoplay){try{autoplay=(autoplay)?autoplay:false;var forceHtml5=BrowserDetection.isHTML5PlayerMode();this.log("Browser found: "+BrowserDetection.browser);this.log("OS found: "+BrowserDetection.OS);if(forceHtml5){this.log("Running replacement script");var viddlerVideos=this.findViddlerVideos();while(viddlerVideos.length>0){singleVideo=viddlerVideos.shift();if(BrowserDetection.OS=='PS3'){var newEmbed=this.buildPS3EmbedCode(singleVideo,false);}
else{var newEmbed=this.buildHTML5EmbedCode(singleVideo,false);}
if(typeof newEmbed!='undefined'&&newEmbed!=null&&typeof newEmbed.success!='undefined'&&newEmbed.success!=null&&newEmbed.success==true){var parent=singleVideo.parentNode;parent.replaceChild(newEmbed.embedCode,singleVideo);this.log("Embed code for object"+(typeof singleVideo.id!='undefined'&&singleVideo.id!=null?" with id=#"+singleVideo.id:'')+" successfully replaced.");$("video",parent).attr('autoplay',autoplay);$("video",parent).bind("play",function(){playStarted();});$("video",parent).bind("ended",function(){playEnded();});$("video",parent).bind("timeupdate",function(){playTimeUpdate();});}
else{this.log("Replacing embed code for object"+(typeof singleVideo.id!='undefined'&&singleVideo.id!=null?" with id=#"+singleVideo.id:'')+" failed");}}}
else{this.log("Replacement not required");}}
catch(e){this.log(e.description);}}}
window.onload=function(){};
