var TruveoVideoSearch=function(appid){var xmlhttpUrl="http://xml.truveo.com/libs/xmlhttp";var serviceUrl="http://xml.truveo.com/apiv3";var urlParams=null;var onUpdateHandlerArray=new Array();var onLoadHandlerArray=new Array();var onErrorHandlerArray=new Array();var errorMessages={"501":"Error connecting to XMLHTTP AJAX service","502":"Invalid AJAX method call: The wrong number of arguments was provided to this method.","503":"Method not available: You must be logged in to use this method."};this.appid=appid;this.showAdult=0;this.showRelatedItems=0;this.start=0;this.results=10;this.tagResults=10;this.channelResults=10;this.categoryResults=10;this.userResults=10;this.mode="ajax";this.method="";this.query="";this.querySuggestion="";this.previousQuery="";this.VideoSet=null;this.TagSet=null;this.ChannelSet=null;this.CategorySet=null;this.UserSet=null;this.WatchlistSet=null;this.Result=null;this.getVideos=function(query,start){if(arguments.length<1){var query=""}if(arguments.length<2){var start=0}var urlParams={method:"truveo.videos.getVideos",query:query,results:this.results,start:start,showRelatedItems:this.showRelatedItems,tagResults:this.tagResults,channelResults:this.channelResults,categoryResults:this.categoryResults,userResults:this.userResults,showAdult:this.showAdult};this.submitRestQuery(urlParams,this.mode);return(true)};this.attachEvent=function(eventName,handler){switch(eventName){case"onupdate":var index=onUpdateHandlerArray.length;onUpdateHandlerArray[index]=handler;break;case"onerror":var index=onErrorHandlerArray.length;onErrorHandlerArray[index]=handler;break;case"onload":var index=onLoadHandlerArray.length;onLoadHandlerArray[index]=handler;break}return(true)};this.onupdate=function(methodName){for(var i=0;i<onUpdateHandlerArray.length;i++){eval(onUpdateHandlerArray[i])}};this.onerror=function(errorCode,errorMessage){for(var i=0;i<onErrorHandlerArray.length;i++){eval(onErrorHandlerArray[i])}};this.onload=function(reloadStateFlag){if(arguments.length<1){reloadStateFlag=false}if(reloadStateFlag==true){this.loadAPIState()}for(var i=0;i<onLoadHandlerArray.length;i++){eval(onLoadHandlerArray[i])}};this.update=function(xmlText){var xotree=new XML.ObjTree();xotree.force_array=["Video","Tag","Channel","Category","User","Watchlist"];var tree=xotree.parseXML(xmlText);if(tree&&("Response" in tree)){var response=tree.Response;if("Error" in response){this.onerror(response.Error["-Code"],response.Error["#text"])}else{if(("method" in response)&&(response.method.indexOf("checkToken")>=0)&&response.Result&&response.Result.code){if(response.Result.code==1){this.onload()}else{this.onload()}}else{if(("method" in response)&&(response.method.indexOf("getToken")>=0)&&response.Result&&response.Result.token){this.onload(true)}else{var methodName="";if("method" in response){this.method=response.method;methodName=response.method.slice(response.method.lastIndexOf(".")+1)}if("query" in response){this.previousQuery=this.query;this.query=response.query}if("querySuggestion" in response){this.querySuggestion=response.querySuggestion}if("VideoSet" in response){this.VideoSet=response.VideoSet;if("firstResultPosition" in response.VideoSet){this.start=response.VideoSet.firstResultPosition}}if("TagSet" in response){this.TagSet=response.TagSet}if("ChannelSet" in response){this.ChannelSet=response.ChannelSet}if("CategorySet" in response){this.CategorySet=response.CategorySet}if("UserSet" in response){this.UserSet=response.UserSet}if("WatchlistSet" in response){this.WatchlistSet=response.WatchlistSet}if("Result" in response){this.Result=response.Result}this.onupdate(methodName)}}}}};this.onXMLHTTPLoad=function(xmlhttp){this.update(xmlhttp.responseText)};this.onXMLHTTPError=function(message){this.onerror(501,errorMessages["501"]+"; "+message)};this.submitRestQuery=function(urlParams,mode){if(mode=="rest"){var restUrl=window.location.protocol+"//"+window.location.host+window.location.pathname+"?";for(name in urlParams){if((name!="token")&&(name!="showAdult")){restUrl+=name+"="+encodeURIComponent(urlParams[name])+"&"}}window.location.href=restUrl.slice(0,-1)}else{var xmlhttp=new XMLHTTP();xmlhttp.setXMLHTTPURL(xmlhttpUrl);xmlhttp.registerCallback(this);xmlhttp.open("GET",this.getRestUrl(urlParams),true);xmlhttp.send(null)}};this.getRestUrl=function(urlParameters){var urlString=serviceUrl+"?appid="+encodeURIComponent(this.appid);for(name in urlParameters){urlString+="&"+name+"="+encodeURIComponent(urlParameters[name])}return(urlString)};this.getUrlParameters=function(url){var args=new Object();if(url.lastIndexOf("?")==-1){return(args)}else{var urlQueryString=url.slice(url.lastIndexOf("?")+1)}if(urlQueryString.length==0){return(args)}var pairs=urlQueryString.split("&");for(var i=0;i<pairs.length;i++){var pos=pairs[i].indexOf("=");if(pos==-1){var argname=pairs[i];var value=""}else{var argname=pairs[i].substring(0,pos);var value=pairs[i].substring(pos+1)}args[argname]=decodeURIComponent(value)}return(args)};this.removeUrlParameters=function(url,paramArray){if(url.lastIndexOf("?")==-1){var baseUrl=url}else{var baseUrl=url.slice(0,url.lastIndexOf("?"))}var urlParameters=this.getUrlParameters(url);for(var i=0;i<paramArray.length;i++){if(paramArray[i] in urlParameters){urlParameters[paramArray[i]]=null}}var j=0;var paramString="";for(name in urlParameters){if(urlParameters[name]!=null){if(j==0){paramString+=name+"="+encodeURIComponent(urlParameters[name])}else{paramString+="&"+name+"="+encodeURIComponent(urlParameters[name])}}j++}if(paramString==""){return(baseUrl)}else{return(baseUrl+"?"+paramString)}};this.initialize=function(){urlParams=this.getUrlParameters(window.location.href);if("token" in urlParams){this.onload(true)}else{if("auth" in urlParams){var newUrlParams={method:"truveo.users.getToken",auth:urlParams.auth};this.submitRestQuery(newUrlParams)}else{if("loginCancel" in urlParams){this.onload(true)}else{if("logout" in urlParams){this.onload()}else{if("errorCode" in urlParams){this.onerror(urlParams.errorCode,urlParams.errorMessage)}else{this.onload()}}}}}}};XMLHTTP=function(){var E=1500;var H=null;this.status=null;this.statusText=null;this.responseText=null;this.responseXML=null;this.synchronous=false;this.readyState=0;this.callbackContext=null;this.onreadystatechange=function(){};this.onXMLHTTPError=function(){};this.onXMLHTTPLoad=function(){};this.registerCallback=function(Y){this.callbackContext=Y};this.setXMLHTTPURL=function(Y){H=Y};this.abort=function(){J=true;T()};this.getAllResponseHeaders=function(){var Y="";for(property in S){Y+=property+": "+S[property]+"\r\n"}return Y};this.getResponseHeader=function(Y){for(property in S){if(property.toLowerCase()==Y.toLowerCase()){return S[property]}}return null};this.overrideMimeType=function(Y){I=Y};this.open=function(c,Z,a,b,Y){if(!U(c,Z)){return }N=(c)?c:"";K=(Z)?Z:"";R=(b)?b:"";G=(Y)?Y:"";B(1)};this.openRequest=function(c,Z,a,b,Y){return this.open(c,Z,a,b,Y)};this.send=function(Y){if(J){return }var Z=M(Y);X(Z)};this.setRequestHeader=function(Y,Z){if(J){return }for(property in D){if(property.toLowerCase()==Y.toLowerCase()){D[property]=Z;return }}D[Y]=Z};var N="";var K="";var R="";var G="";var D={"HTTP-Referer":document.location,"Content-Type":"application/x-www-form-urlencoded"};var S={};var I="";var O=this;var C="";var P=[];var J=false;var F=function(Z,Y){if(arguments.length<2){Y=O}if((Y.callbackContext!=null)&&Y.callbackContext.onXMLHTTPError){Y.callbackContext.onXMLHTTPError(Z)}else{O.onXMLHTTPError(Z)}O.abort();return false};var M=function(b){if(!b){b=""}var Z="";for(property in D){Z+=encodeURIComponent(property)+"="+encodeURIComponent(D[property])+"&"}var g=N+"$"+C+"$"+R+"$"+G+"$"+Z+"$"+A(b)+"$"+K;var Y=g;var f=E,a=[];var e=Math.floor(Y.length/f),c=0;while(Y.length>0){var d=H+"?multipart$"+C+"$"+c+++"$"+e+"$"+Y.substr(0,f);a.push(d);Y=Y.substr(f)}if(a.length==1){Y=H+"?"+g}else{Y=a}return Y};var U=function(Z,Y){if(!Z){return F("Please, specify the query method (GET, POST or HEAD)")}if(!Y){return F("Please, specify the URL")}if(Z.toLowerCase()!="get"&&Z.toLowerCase()!="post"&&Z.toLowerCase()!="head"){return F("Please, specify either a GET, POST or a HEAD method")}if(Y.toLowerCase().substr(0,7)!="http://"){return F("Only HTTP protocol is supported (http://)")}return true};var X=function(a){if("object"==typeof a){for(var Z=0;Z<a.length;Z++){X(a[Z])}return }var Y=document.createElement("script");Y.src=a;Y.type="text/javascript";if(navigator.userAgent.indexOf("Safari")){Y.charset="utf-8"}Y=document.getElementsByTagName("head")[0].appendChild(Y);P.push(Y);return Y};var A=function(Y){Y=escape(Y);Y=Y.replace("+","%2B");return Y};var T=function(){for(var Y=0;Y<P.length;Y++){if(P[Y].parentNode){P[Y].parentNode.removeChild(P[Y])}}};var L=function(){C="v"+Math.random().toString().substr(2);window[C]=O};var B=function(Z,Y){if(arguments.length<2){Y=O}O.readyState=Z;if((Y.callbackContext!=null)&&Y.callbackContext.onreadystatechange){Y.callbackContext.onreadystatechange()}else{O.onreadystatechange()}if(Z==4){if((Y.callbackContext!=null)&&Y.callbackContext.onXMLHTTPLoad){Y.callbackContext.onXMLHTTPLoad(Y)}else{O.onXMLHTTPLoad()}}};var Q=function(){var Z=O.getResponseHeader("Content-type")+I;if(!(Z.indexOf("html")>-1||Z.indexOf("xml")>-1)){return }if(document.implementation&&document.implementation.createDocument&&navigator.userAgent.indexOf("Opera")==-1){var a=new DOMParser();var Y=a.parseFromString(O.responseText,"text/xml");O.responseXML=Y}else{if(window.ActiveXObject){var Y=new ActiveXObject("MSXML2.DOMDocument.3.0");if(Y.loadXML(O.responseText)){O.responseXML=Y}}else{var Y=document.body.appendChild(document.createElement("div"));Y.style.display="none";Y.innerHTML=O.responseText;W(Y,true);O.responseXML=Y.childNodes[0];document.body.removeChild(Y)}}};var W=function(a,Y){var Z=a.childNodes.length;if(Z==0){return }do{var b=a.childNodes[--Z];if(b.nodeType==3&&!V(b.nodeValue)){a.removeChild(b)}if(b.nodeType==1&&Y){W(b,true)}}while(Z>0)};var V=function(Y){Y=Y.replace("\r","");Y=Y.replace("\n","");Y=Y.replace(" ","");return(Y.length==0)?false:true};this._parse=function(Y){if(J){return }if(Y.multipart){return }if(!Y.success){return F(Y.description,this)}S=Y.responseHeaders;this.status=Y.status;this.statusText=Y.statusText;this.responseText=Y.responseText;Q();T();B(4,this)};L()};if(typeof (XML)=="undefined"){XML=function(){}}XML.ObjTree=function(){return this};XML.ObjTree.VERSION="0.23";XML.ObjTree.prototype.xmlDecl='<?xml version="1.0" encoding="UTF-8" ?>\n';XML.ObjTree.prototype.attr_prefix="-";XML.ObjTree.prototype.parseXML=function(C){var B;if(window.DOMParser){var A=new DOMParser();var D=A.parseFromString(C,"application/xml");if(!D){return }B=D.documentElement}else{if(window.ActiveXObject){A=new ActiveXObject("Microsoft.XMLDOM");A.async=false;A.loadXML(C);B=A.documentElement}}if(!B){return }return this.parseDOM(B)};XML.ObjTree.prototype.parseDOM=function(A){if(!A){return }this.__force_array={};if(this.force_array){for(var D=0;D<this.force_array.length;D++){this.__force_array[this.force_array[D]]=1}}var C=this.parseElement(A);if(this.__force_array[A.nodeName]){C=[C]}if(A.nodeType!=11){var B={};B[A.nodeName]=C;C=B}return C};XML.ObjTree.prototype.parseElement=function(E){if(E.nodeType==7){return }if(E.nodeType==3||E.nodeType==4){var F=E.nodeValue.match(/[^\x00-\x20]/);if(F==null){return }return E.nodeValue}var B;var D={};if(E.attributes&&E.attributes.length){B={};for(var G=0;G<E.attributes.length;G++){var I=E.attributes[G].nodeName;if(typeof (I)!="string"){continue}var C=E.attributes[G].nodeValue;if(!C){continue}I=this.attr_prefix+I;if(typeof (D[I])=="undefined"){D[I]=0}D[I]++;this.addNode(B,I,D[I],C)}}if(E.childNodes&&(E.childNodes.length>=0)){var H=true;if(B){H=false}for(var G=0;G<E.childNodes.length&&H;G++){var A=E.childNodes[G].nodeType;if(A==3||A==4){continue}H=false}if(H){if(!B){B=new String()}for(var G=0;G<E.childNodes.length;G++){B+=E.childNodes[G].nodeValue}}else{if(!B){B={}}for(var G=0;G<E.childNodes.length;G++){var I=E.childNodes[G].nodeName;if(typeof (I)!="string"){continue}var C=this.parseElement(E.childNodes[G]);if(!C){continue}if(typeof (D[I])=="undefined"){D[I]=0}D[I]++;this.addNode(B,I,D[I],C)}}}return B};XML.ObjTree.prototype.addNode=function(C,A,B,D){if(this.__force_array[A]){if(B==1){C[A]=[]}C[A][C[A].length]=D}else{if(B==1){C[A]=D}else{if(B==2){C[A]=[C[A],D]}else{C[A][C[A].length]=D}}}};XML.ObjTree.prototype.writeXML=function(A){var B=this.hash_to_xml(null,A);return this.xmlDecl+B};XML.ObjTree.prototype.hash_to_xml=function(C,B){var F=[];var A=[];for(var E in B){if(!B.hasOwnProperty(E)){continue}var H=B[E];if(E.charAt(0)!=this.attr_prefix){if(typeof (H)=="undefined"||H==null){F[F.length]="<"+E+" />"}else{if(typeof (H)=="object"&&H.constructor==Array){F[F.length]=this.array_to_xml(E,H)}else{if(typeof (H)=="object"){F[F.length]=this.hash_to_xml(E,H)}else{F[F.length]=this.scalar_to_xml(E,H)}}}}else{A[A.length]=" "+(E.substring(1))+'="'+(this.xml_escape(H))+'"'}}var G=A.join("");var D=F.join("");if(typeof (C)=="undefined"||C==null){}else{if(F.length>0){if(D.match(/\n/)){D="<"+C+G+">\n"+D+"</"+C+">\n"}else{D="<"+C+G+">"+D+"</"+C+">\n"}}else{D="<"+C+G+" />\n"}}return D};XML.ObjTree.prototype.array_to_xml=function(B,E){var A=[];for(var C=0;C<E.length;C++){var D=E[C];if(typeof (D)=="undefined"||D==null){A[A.length]="<"+B+" />"}else{if(typeof (D)=="object"&&D.constructor==Array){A[A.length]=this.array_to_xml(B,D)}else{if(typeof (D)=="object"){A[A.length]=this.hash_to_xml(B,D)}else{A[A.length]=this.scalar_to_xml(B,D)}}}}return A.join("")};XML.ObjTree.prototype.scalar_to_xml=function(A,B){if(A=="#text"){return this.xml_escape(B)}else{return"<"+A+">"+this.xml_escape(B)+"</"+A+">\n"}};XML.ObjTree.prototype.xml_escape=function(A){return A.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")};