﻿///// LIVE    
var LiveSearch = new SearchEngine({name:"Bing", desc:"Bing", imageUrl:"Images/new/bing.gif"});
LiveSearch.searchData = {};
LiveSearch.liveCallbacks = {};
LiveSearch.searchFunc = function doGoogleSearch(term, id, offset, opts)
{
  var search = LiveSearch.searchData[id] || {id: id, total:0, required: searchResultsToShow, offset:offset, term:term, options:opts};
  LiveSearch.searchData = {};
  LiveSearch.searchData[id] = search;
  var url = [];
  LiveSearch.liveCallbacks[id] = function(response) { LiveSearch.ProcessSearchResults(response,id);}
  url.push("http://api.bing.net/json.aspx?Version=2.0");
  url.push("&AppId=5DE6D123674B9E08A96B4E4D1B9ED8F19B53AD34");
  url.push("&Query="+encodeURIComponent(term));  
  url.push("&JsonType=callback");
  url.push("&JsonCallback=LiveSearch.liveCallbacks["+id+"]");  
  url.push("&r="+new Date().valueOf());
  
  if (search.options.country && this.countries && this.countries[search.options.country])
      url+=("&Market="+this.countries[search.options.country]);
  if (search.options.adult_ok)
     url.push("&Adult=Off");
  else if (search.options.safeContent)
     url.push("&Adult=Strict");
  else
     url.push("&Adult=Moderate");
     
  if (search.options.searchType=="web")
  {  
    url.push("&Sources=Web");
    url.push("&Web.Count=8");
    url.push("&Web.Offset="+offset);    
  }
  else if (search.options.searchType=="image")
  {
    url.push("&Sources=Image");
    url.push("&Image.Count=8");
    url.push("&Image.Offset="+offset);    
  }
  addScript(url.join(''),"LiveSearch_"+id);
}


LiveSearch.ProcessSearchResults = function(response,id)
{
  var search = LiveSearch.searchData[id];
  if (!search)
    return;
  var errors = response.SearchResponse.Errors;
  var results = null;
  if (search.options.searchType=="image")
  {
    results = response.SearchResponse.Image;
  } else if (search.options.searchType=="web")
  {
    results = response.SearchResponse.Web;
  }
  
  if (errors != null || results.Results == null)
  {
      // There are errors in the response. Display error details.
      notifyEngineResultsEstimation("Bing", id, 0);
      notifyEngineResults("Bing", id, [], search.offset, false);
    LiveSearch.searchData[id] = null;
    return;
  }

  
  
  var offset = results.Offset;
  var total = results.Total;
  var resultCount = results.Results.length;
  
  var hasMore = offset+resultCount < total;
  var resultIdx = offset;
  var lastResultIdx = offset + resultCount;
  var ret = [];
  if (resultIdx==0)
  {    
    notifyEngineResultsEstimation("Bing",id,total, total);
  }
  for(var i=0,j=results.Results.length;i<j;i++)
  {
    var item = results.Results[i];
    if (search.options.searchType=="web")
    {
      ret.push(
      {
        rank : i,
        title: item.Title || "",
        description: item.Description || "",
        url: item.Url || "",
        visibleUrl: item.DisplayUrl || ""
      });
    }
    else if (search.options.searchType=="image")
    {
      ret.push(
      {
        Url: item.DisplayUrl || "",
        ClickUrl : item.MediaUrl || "",
        FileSize : item.FileSize,
        Height : item.Height,
        Width : item.Width,
        Summary: item.Title || "",
        Title : item.Title || "",
        ThumbnailUrl: item.Thumbnail.Url || "",
        ThumbnailHeight: item.Thumbnail.Height,
        ThumbnailWidth:item.Thumbnail.Width    
      });
    }
  }
  notifyEngineResults("Bing",id,ret,resultIdx, hasMore);
  search.total += ret.length;
  if (search.total <= search.required && hasMore)
  {      
    LiveSearch.searchFunc(search.term, search.id, search.offset+search.total, search.options);
  }
  else
    LiveSearch.searchData[id] = null;
}

LiveSearch.CreateSearch = function(term, id, options)
{
  var srch = new Search(this, term, id, options);
  srch.InternalGetPage = function (pageNumber, pageReadyCallback, notifyEstimatesCallback)
  {
    var additionalOffset = 0;
    if (this.Pages[pageNumber])
    {
      additionalOffset = this.Pages[pageNumber].length;
    }
    
    LiveSearch.searchFunc(this.term, this.id, additionalOffset+(pageNumber-1)*searchResultsToShow, options);
  }
  return srch;
}

LiveSearch.Register();
