/*
  YBOSS by Christian Heilmann
  Version: 1.0
  Homepage: http://icant.co.uk/sandbox/yboss/
  Copyright (c) 2008, Christian Heilmann
  Code licensed under the BSD License:
  http://wait-till-i.com/license.txt
*/
YBOSS = function(){
  var config = {
    webItemHTML:'<li><a href="{clickurl}">{title}</a><p>{abstract}</p></li>'
  }
  var appID = '4muP0fTV34EwDebJKZAXcJ8rM' + 
              '.HrcyjGnNiihCuezlmEa_aNBJv.vcXaln98qow-';
  var queries = [];
  function get(o){
    if(typeof o !== 'undefined' && 
       typeof o.searches === 'string' && 
       typeof o.query === 'string' &&
       typeof o.callback === 'function'){
      var searches = makeAPIurl(o);
      o.query = encodeURIComponent(o.query);
      queries[o.query] = {
        all:o.searches.split(',').length,
        count:0,
        databack:{},
        callback:o.callback
      };
      add(searches);
    }
  }
  function add(urls){
    for(var i=0;i<urls.length;i++){
      var s = document.createElement('script');
      s.setAttribute('src',urls[i]);
      s.setAttribute('type','text/javascript');
      document.getElementsByTagName('head')[0].appendChild(s);
    }
  }
  function makeAPIurl(o){
      var searchtypes = [];
      var APIurl = 'http://boss.yahooapis.com/ysearch/web/v1/' + 
                   clean(o.query) +'?format=json&callback=YBOSS.retrieved&' +
                   'appid=' + appID;
      if(typeof o.count === 'number' && 
         parseInt(o.count) === o.count && o.count > 0){
        APIurl += '&count=' + o.count;
      }
      if(typeof o.start === 'number' && 
         parseInt(o.start) === o.start && o.start > 0){
        APIurl += '&start=' + o.start;
      }
      if(typeof o.sites === 'string'){
        APIurl += '&sites=' + clean(o.sites);
      }
      if(typeof o.filter === 'string'){
        APIurl += '&filter=' + clean(o.filter);
      }
      if(typeof o.lang === 'string'){
        APIurl += '&lang=' + clean(o.lang);
      }
      if(typeof o.region === 'string'){
        APIurl += '&region=' + clean(o.region);
      }
      if(typeof o.type === 'string'){
        APIurl += '&type=' + clean(o.type);
      }
      if(typeof o.dimensions === 'string'){
        APIurl += '&dimensions=' + clean(o.dimensions);
      }
      if(typeof o.refererurl === 'string'){
        APIurl += '&refererurl=' + clean(o.refererurl);
      }
      if(typeof o.url === 'string'){
        APIurl += '&url=' + clean(o.url);
      }
      if(typeof o.age === 'string'){
        APIurl += '&age=' + clean(o.age);
      }
      if(typeof o.filter === 'string'){
        APIurl += '&filter=' + clean(o.filter);
      }
      if(typeof o.sites === 'string'){
        APIurl += '&sites=' + clean(o.sites);
      }
      if(o.searches.indexOf('search')!==-1){
        searchtypes.push(APIurl);
      }
      if(o.searches.indexOf('images')!==-1){
        searchtypes.push(APIurl.replace('web','images'));
      }
      if(o.searches.indexOf('news')!==-1){
        searchtypes.push(APIurl.replace('web','news'));
      }
      return searchtypes;
  }
  function clean(s){
    return encodeURIComponent(s);
  }
  function retrieved(o){
    if(typeof o.ysearchresponse.nextpage !== 'undefined'){
      var next = o.ysearchresponse.nextpage.split('?')[0];
      var query = next.replace(/.*?\//g,'');
    } 
    if(typeof o.ysearchresponse.prevpage !== 'undefined'){
      var last = o.ysearchresponse.prevpage.split('?')[0];
      var query = last.replace(/.*?\//g,'');
    } 
    queries[query].count++;
    queries[query].databack.query = query;
    databack = queries[query].databack;
    if(typeof o.ysearchresponse.resultset_web !== 'undefined'){
      databack.web = [];
      databack.webHTML = '<ul>';
      for(var i=0,j=o.ysearchresponse.resultset_web.length;i<j;i++){
        var item = o.ysearchresponse.resultset_web[i];
      
      //add load here ///////////////////////////////////////////////////////////
               mynext = i + 40
               allout[mynext][0] = i;// overall rank
               allout[mynext][1] = -1;// google rank
               allout[mynext][2] = i;// yahoo rank
               allout[mynext][3] = -1;// bing rank
               allout[mynext][4] = ".";//source google
               allout[mynext][5] = "Y";//source yahoo
               allout[mynext][6] = ".";//source bing
               allout[mynext][7] = mynext;//reference index
               allout[mynext][8] = 0;//sorted order
               if (item.url==undefined) {
                  allout[mynext][9] = "blank"
               }else{
                  allout[mynext][9] = item.url
               }
               if (item.title==undefined) {
                  temp= "blank"
               }else{
                  temp= item.title
                  }

               temp = temp.replace(/(<([^>]+)>)/ig,"");
               allout[mynext][10] = temp ;

                if (item.abstract==undefined) {
                  temp= "blank"
               }else{
                  temp= item.abstract
                  }
               temp = temp.replace(/(<([^>]+)>)/ig,"");
               allout[mynext][11] = temp
               //allout[mynext][12] = item.url


      }
    }
    
    if(queries[query].count === queries[query].all){
      queries[query].callback(databack);
    }
  }
  return{
    get:get,
    config:config,
    retrieved:retrieved,
    appID:appID
  }
}();

