
YBOSS = function(){
  var config = {
    webItemHTML:'<a href="{clickurl}"><font class="titel">{title}</font></a><br>'+
				'<font class="beschreibung">{abstract}</font><br><a href="{clickurl}" target="nex" style="text-decoration: none;"><font class="link">{displayurl}</font>'+
				'<img src="http://www.fireball.de/newwindow.jpg" title="Seite in neuem Fenster" style="margin-left: 10px;" target="nets" /></a><br><br>',
    newsItemHTML:'<li lang="{language}"><a href="{clickurl}">{title}</a><p>{abstract} ({source})</p></li>',
    imageItemHTML:'<li><a href="{clickurl}">{title}</a><a href="{url}"><img src="{thumbnail}" width="{thumbnailwidth}" height="{thumbnailheight}"></a><p>{shortened}</p></li>'
  }
  var appID = 'q5iTpk7IkY5wRk4NKJ6y' + 
              '.FXAHZd9SS8axsVIjdu4Vg';
  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[0]);
      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/' + 
                   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 (o.ysearchresponse.totalhits==0){//keine Resultate
  databack.ergebnisse = o.ysearchresponse.totalhits;
	databack.webHTML += '';
	queries[0].callback(databack);
  	 	}//fertig keine Resultate
	else {
	
	
  
    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 = '';
	  databack.ergebnisse = o.ysearchresponse.totalhits;
	  
	  
      for(var i=0,j=o.ysearchresponse.count;i<j;i++){
        var item = o.ysearchresponse.resultset_web[i];
        databack.web.push(
          {
            abstract:item.abstract,
            title:item.title,
            url:item.clickurl,
            displayurl:item.dispurl
          }
        );
        var html = config.webItemHTML.replace('{clickurl}',item.clickurl);
        html = html.replace('{title}',item.title);
        html = html.replace('{abstract}',item.abstract);
		html = html.replace('{clickurl}',item.clickurl);
		html = html.replace('{displayurl}',item.dispurl);
		if ([i] ==3){
		html = html + "<div id='bilder1' style='margin-left: 0px; margin-top: 0px; margin-bottom: 15px; dislay: none;'></div>";
		}
        databack.webHTML += html;
      }
      databack.webHTML += '';
	  
    }
	}//fertig resultate

	
  

    if(queries[query].count === queries[query].all){
      queries[query].callback(databack);
    }
	

  }
  return{
    get:get,
    config:config,
    retrieved:retrieved,
    appID:appID
  }
}();
