
var searchObj = function(n,v,st,vtg,rd,cd,trk) {
  this.YUD = YAHOO.util.Dom;
  this.YUE = YAHOO.util.Event;
  this.name = n
  this.videoObj = v;
  this.searchText = st;
  this.numberOfVideosToGet = vtg || 120;
  this.resultsDivObj = (typeof rd === "string") ? this.YUD.get(rd) : rd;
  this.chunkingDiv = (typeof cd === "string") ? this.YUD.get(cd) : cd;
  this.act_trk = trk;
  this.ref = "searchObj";
  this.clipArray = [];
  var t = this;
  this.resultsDivObj;
  this.currentIteration = 0;
  this.currentResultsLength = 0;
  this.scrollWait = false;
  this.numberOfResults = 0;
  this.finishedSearch = false;
};
searchObj.prototype = {
  h: document.getElementsByTagName('head')[0],
  getData: function(u,i) {
    var id = [i,'_targetReq'].join('');
    var s = document.getElementById(id);
    if (s) {
      this.h.removeChild(s);
      delete s;
    }
    s = document.createElement('script');
    s.id = id;
    s.type = 'text/javascript';
    s.src = [u];
    this.h.appendChild(s);
  },
  init: function() {
    var t = this;
    var v = this.YUD.get("vs");
    this.YUE.on("vs","click",function() {
      v.value = "";
    });
    this.YUE.on(document.searchForm,"submit",function(){
      if(t.act_trk){
        sc_trackAction(this,"video_search_click",wxVideo.omniture_acct);
     }
      t.numberOfResults = 0;
      t.clipArray = [];
      t.resultsDivObj.innerHTML = "";
      t.YUD.addClass("searchGallery","searchingVideo"); // Turn on searching icon
      t.YUD.addClass("srchTxt","displayNone"); // Turn off search Text
      t.YUD.addClass("videoSearchPaginator1","displayNone"); // Turn off video pagination
      t.YUD.removeClass("videoSearch","displayNone"); // Open video search results
      t.currentIteration = 0;
      t.searchTerm = escape(v.value);
      t.getData(['http://api.brightcove.com/services/library?command=find_videos_by_text&text=',t.searchTerm,'&page_size=',t.numberOfVideosToGet,'&page_number=0&video_fields=id,name,referenceId,thumbnailURL,shortDescription,length,lastModifiedDate,tags,linkText,linkURL&token=RN-hky_2vRCPyeOOGfOuRYVDuJpl8Xfd13-MFBcU9DI.&callback=wxVideo.searchObj.gotResults'].join(''),'searchVideo');
      return false;
    });
  },
  gotResults: function(json) {
    this.YUD.get("videoSearchPaginator1").innerHTML = "";
    this.clipArray = [];
    this.resultsDivObj.innerHTML = "";
    this.YUD.removeClass("searchGallery","searchingVideo"); // Turn off searching icon
    if(json.items && json.items.length && json.items.length > 0 && !json.error){
      for(var a=0;a<json.items.length;a++){
        var c = json.items[a];
        var url = this.videoObj.videoNamespace.generateURL(c.name,c.referenceId);
        var co = new clipObj(this.videoObj,c.referenceId,"",url,c.name,c.thumbnailURL,c.thumbnailURL,c.shortDescription,c.length,c.lastModifiedDate,c.tags,"",c.linkText,c.linkURL,"");
        this.addClip(co);
      }
    }else{
      this.YUD.get("videoSearchResults").innerHTML = ["<div class=\"noresults\">No search results found for \"",unescape(this.searchTerm),"\"</div>"].join("");
      return;
    }
    this.chunkingDiv.innerHTML = "";
    this.cwo = new searchChunkingWidgetObj(this,this.chunkingDiv,this.resultsDivObj,this.clipArray,this.numberOfVideosToGet,"drawSearchResultsClip",12,this.ref,"initSearchClip");
    this.cwo.init();
    this.YUD.get("videoSearchResultText").innerHTML = unescape(this.searchTerm);
    var thruChunks = (12 > this.clipArray.length) ? this.clipArray.length : (12);
    this.YUD.get("searchTotal").innerHTML = this.clipArray.length;
    this.YUD.removeClass("srchTxt","displayNone");
    if(this.clipArray.length > 12){
      this.YUD.removeClass("videoSearchPaginator1","displayNone");
    }
  },
  addClip: function(c) {
    this.clipArray.push(c);
  },
  playClip: function(clipId) {
    for (var a=0;a<this.clipArray.length;a++) {
      if (this.clipArray[a].id == clipId) {
        this.videoObj.play(this.clipArray[a]);
        break;
      }
    }
  }
};
clipObj.prototype.drawSearchResultsClip = function(parentDiv,objName) {
  if(this.imgLoaded === false){
    var a = document.getElementById(["vcid",objName,this.id].join(""));
    a.style.backgroundImage  = "url(" + this.largeThumb + ")";
    this.imgLoaded = true;
  }
  /*
  var t = this;
  var a = document.createElement("div");
  YAHOO.util.Dom.addClass(a,"vidBlock");
  a.setAttribute("clipid",t.id);
  a.setAttribute("vmobj",objName);
  var b = document.createElement("div");
  b.setAttribute("class","vidBlockImg");
  var c = document.createElement("img");
  c.setAttribute("src",this.largeThumb);
  c.setAttribute("alt",this.title);
  c.setAttribute("width","120");
  c.setAttribute("height","77");
  b.appendChild(c);
  a.appendChild(b)
  var d = document.createElement("div");
  YAHOO.util.Dom.addClass(d,"vidBlockTxt");
  var e = document.createElement("span");
  YAHOO.util.Dom.addClass(e,"wxClickable");
  e.appendChild(document.createTextNode(this.title));
  d.appendChild(e);
  a.appendChild(d);
  parentDiv.appendChild(a);
  */
};

clipObj.prototype.initSearchClip = function(searchObj,objName,indx){
  var YUD = YAHOO.util.Dom;
  var YUE = YAHOO.util.Event;
  var t = this;
  var so = searchObj;
  var startingObjNum = so.cwo.currentChunk * so.cwo.chunkAmount;
  var endingObjNum = (so.cwo.currentChunk == so.cwo.numberOfChunks - 1) ? so.cwo.dataArray.length - 1 : startingObjNum + so.cwo.chunkAmount - 1;
  var videoSearchContainer = document.createElement("div");
  var videoGalleryContent = document.createElement("div");
  YUD.addClass(videoGalleryContent,"videoGalleryContent wxClickable");
  videoGalleryContent.setAttribute("vmobj",objName);
  videoGalleryContent.setAttribute("clipid",t.id);
  var videoContent = document.createElement("div");
  YAHOO.util.Dom.addClass(videoContent,"video-block");var videoImage=document.createElement("div");YAHOO.util.Dom.addClass(videoImage,"video-image");videoImage.setAttribute("id",["vcid",objName,this.id].join(""));
  var videoDesc = document.createElement("div");
  YUD.addClass(videoDesc,"videoDesc");
  var videoDescLink = document.createElement("div");
  YUD.addClass(videoDescLink,"wxClickable");
  videoDescLink.appendChild(document.createTextNode(this.title));
  videoDesc.appendChild(videoDescLink);
  videoContent.appendChild(videoImage);
  videoContent.appendChild(videoDesc);
  videoGalleryContent.appendChild(videoContent);
  return videoGalleryContent;
};

var searchChunkingWidgetObj = function(so,d,t,e,s,m,a,i,n) {
  var YUD = YAHOO.util.Dom;
  this.searchObj = so;
  this.containerObj = (typeof d === "string") ? YUD.get(d) : d;
  this.targetObj = (typeof t === "string") ? YUD.get(t) : t;
  this.dataArray = e;
  this.numberToDisplay = s;
  this.targetMethodName = m;
  this.chunkAmount = (typeof a === "string") ? parseInt(a) : a;
  this.widgetId = i;
  this.targetInitMethodName = n || function(){};
  this.imageBase = "http://i.imwx.com/web/video/th-loading-140x79.gif";
  this.LarrowSelected = "videoPArrowL wxClickable";
  this.LarrowNotSelected = "videoPArrowLN";
  this.RarrowSelected = "videoPArrowR wxClickable";
  this.RarrowNotSelected = "videoPArrowRN";
  this.chunkSelected = "videoPDotSelect";
  this.chunkNotSelected = "videoPDot";
  var arrayLength = this.dataArray.length > this.numberToDisplay ? this.numberToDisplay : this.dataArray.length;
  this.numberOfChunks = (parseInt(arrayLength / this.chunkAmount)) + ((arrayLength % this.chunkAmount > 0) ? 1 : 0);
  this.currentChunk = 0;
  this.chunkChunkObjArray = [];
  this.leftArrowObj = null;
  this.rightArrowObj = null;
  this.widgetWidth = YUD.getStyle(this.searchObj.resultsDivObj,"width").match(/[0-9]+/)[0];
  this.currentWidgetWidth = "";
  this.smol = YUD.get('showMoreOrLessSearch');
  this.pg = YUD.get('videoSearchPaginator1');
  this.anm = null; //animation object for module
};

searchChunkingWidgetObj.prototype = {
  init: function(){
    var YUE = YAHOO.util.Event;
    var YUD = YAHOO.util.Dom;
    var t = this;
    YUE.on(this.smol,"click",this.showMoreOrLess,null,this);

    var dotContainer = document.createElement("DIV");
    YUD.addClass(dotContainer,"dotContainerClass");

    for (var a=0;a<this.numberOfChunks;a++) {
      var dot = document.createElement("DIV");
      dot.setAttribute("id",[this.widgetId,"_chunk",a].join(''));
      var state = (a===0)? this.chunkSelected:this.chunkNotSelected;
      YUD.addClass(dot,state);
      this.chunkChunkObjArray.push(dot);
      dotContainer.appendChild(dot);
      dot.onclick=function(){
        if(t.searchObj.videoObj.videoNamespace.actionTrack.pagination){
          sc_trackAction(this,"video_pagination_click",wxVideo.omniture_acct); 
        }
        var e = this.id.match(/chunk(\d+)/);
        var f = parseInt(e[1]);
        t.gotoChunk(f);
      };
      dot.onmouseover=function(){
        YAHOO.util.Dom.replaceClass(this.id,"videoPDot","videoPDotMo");
      };
      dot.onmouseout=function(){
        YAHOO.util.Dom.replaceClass(this.id,"videoPDotMo","videoPDot");
      }
    }

    var arrowsContainer = document.createElement("DIV");
    YAHOO.util.Dom.addClass(arrowsContainer,"arrowsContainerClass");

    var Larrow = document.createElement("DIV");
    YUD.addClass(Larrow,"videoPArrowLN");
    Larrow.setAttribute("id",[this.widgetId,"_left"].join(''));
    arrowsContainer.appendChild(Larrow);
    this.leftArrowObj = Larrow;
    Larrow.onclick = function(){
      if(t.searchObj.videoObj.videoNamespace.actionTrack.pagination){
        sc_trackAction(this,"video_pagination_click",wxVideo.omniture_acct); 
      }
      t.prevChunk();
    };

    var Rarrow = document.createElement("DIV");
    Rarrow.setAttribute("id",[this.widgetId,"_right"].join(''));
    var d = (this.numberOfChunks <= 1) ? this.RarrowNotSelected : this.RarrowSelected;
    YUD.addClass(Rarrow,d);

    arrowsContainer.appendChild(Rarrow);
    this.rightArrowObj = Rarrow;
    Rarrow.onclick=function(){
      if(t.searchObj.videoObj.videoNamespace.actionTrack.pagination){
        sc_trackAction(this,"video_pagination_click",wxVideo.omniture_acct); 
      }
      t.nextChunk();
    };

    this.containerObj.appendChild(arrowsContainer);
    this.containerObj.appendChild(dotContainer);

    this.drawChunks(this.targetObj,this.widgetId);
    setTimeout(function(){t.showChunk(0);},50);
    return this;
  },
  turnChunkOn: function(c){
    this.chunkChunkObjArray[c].className = this.chunkSelected;
  },
  turnChunkOff: function(c){
    this.chunkChunkObjArray[c].className = this.chunkNotSelected;
  },
  turnLeftOn: function(){
    this.leftArrowObj.className = this.LarrowSelected;
  },
  turnLeftOff: function(){
    this.leftArrowObj.className = this.LarrowNotSelected;
  },
  turnRightOn: function(){
    this.rightArrowObj.className = this.RarrowSelected;
  },
  turnRightOff: function(){
    this.rightArrowObj.className = this.RarrowNotSelected;
  },
  drawChunks:function(parentDiv,objName){
    this.targetObj.innerHTML = "";
    var thisSearchObj = this.searchObj;
    var videoSearchContainer = null;
    for (var a=0;a<this.dataArray.length;a++) {
      this.dataArray[a].imgLoaded = false;
      var singleRenderedClip = this.dataArray[a][this.targetInitMethodName](thisSearchObj,objName,a);
      if( a % 12 == 0){
        videoSearchContainer = document.createElement("div");
        YAHOO.util.Dom.addClass(videoSearchContainer,"videoSearchContainer");
        parentDiv.appendChild(videoSearchContainer);
      }
      videoSearchContainer.appendChild(singleRenderedClip);
    }
  },
  showChunk: function(c){
    var YUD = YAHOO.util.Dom;
    var totalItems = (120 > this.dataArray.length) ? this.dataArray.length : 120;
    var thruChunks = ((this.currentChunk+1) * 12 > totalItems) ? totalItems : ((this.currentChunk+1) * 12)
    YUD.get("searchNumber").innerHTML = (1 + (this.currentChunk)*12) + " - " + (thruChunks);
    YUD.get("searchTotal").innerHTML = totalItems;
    var startingObjNum = this.currentChunk * this.chunkAmount;
    var endingObjNum = (this.currentChunk === this.numberOfChunks - 1) ? this.dataArray.length - 1 : startingObjNum + this.chunkAmount - 1;
    //this.targetObj.innerHTML = "";
    for (var a=startingObjNum;a<=endingObjNum;a++) {
      this.dataArray[a][this.targetMethodName](this.targetObj,this.widgetId);
    }
    var tcc = this.currentChunk;
    var to = this.targetObj;
    var tw = (this.currentChunk + 1) * this.widgetWidth;
    if(tw > parseInt(YAHOO.util.Dom.getStyle(to,"width"))){
      YAHOO.util.Dom.setStyle(to,"width",tw + "px");
    }
    if(this.anm == null){
      this.anm = new YAHOO.util.Anim(to,{left:{to:(tcc*-this.widgetWidth)}},.2);
    }else{
      this.anm.attributes.left.to =tcc*-this.widgetWidth;
    }
    this.anm.onComplete.subscribe(function(){
      YAHOO.util.Dom.setStyle(to,"width",tw + "px");
    });
    this.anm.animate();
  },
  showMoreOrLess: function(){
    var YUD = YAHOO.util.Dom;
    var videoGallery = YUD.get(this.targetObj).parentNode;
    if(YUD.hasClass(this.smol,"videoPShowA")){
      YUD.replaceClass(this.smol,"videoPShowA","videoPShowL");
      YUD.addClass(this.pg,"displayNone");
      this.currentWidgetWidth = YAHOO.util.Dom.getStyle(this.targetObj,"width");
      YUD.setStyle(this.targetObj,"width",[this.widgetWidth,"px"].join(""));
      YUD.setStyle(this.targetObj,"left","0px");
      for(var a=0;a<this.dataArray.length;a++){
        this.dataArray[a][this.targetMethodName](this.targetObj,this.widgetId);
      }
      YUD.replaceClass(videoGallery,"videoGallery","videoGalleryShowAll");
      //YUD.setStyle(videoGallery,"height","auto");
    }
    else{
      YUD.replaceClass(this.smol,"videoPShowL","videoPShowA");
      YUD.removeClass(this.pg,"displayNone");
      YUD.setStyle(this.targetObj,"width",this.currentWidgetWidth);
      YUD.replaceClass(videoGallery,"videoGalleryShowAll","videoGallery");
      //YUD.setStyle(videoGallery,"height","108px");
      this.showChunk(this.currentChunk);
    }
  },
  nextChunk: function(){
    if (this.currentChunk < this.numberOfChunks - 1) {
      this.turnChunkOff(this.currentChunk);
      this.currentChunk++;
      this.turnChunkOn(this.currentChunk);
      this.updateArrows();
      this.showChunk(this.currentChunk);
    }
  },
  updateArrows: function(){
    if (this.currentChunk === 0) {
      this.turnLeftOff();
    } else {
      this.turnLeftOn();
    }
    if (this.currentChunk === this.numberOfChunks - 1) {
      this.turnRightOff();
    } else {
      this.turnRightOn();
    }
  },
  prevChunk: function(){
    if (this.currentChunk > 0) {
      this.turnChunkOff(this.currentChunk);
      this.currentChunk--;
      this.turnChunkOn(this.currentChunk);
      this.updateArrows();
      this.showChunk(this.currentChunk);
    }
  },
  gotoChunk: function(c){
    if (this.chunkChunkObjArray[c] && this.currentChunk !== c) {
      this.turnChunkOff(this.currentChunk);
      this.currentChunk = c;
      this.turnChunkOn(this.currentChunk);
      this.updateArrows();
      this.showChunk(this.currentChunk);
    }
  }
};


