// JavaScript Document
  dojo.require("dojox.data.GoogleSearchStore");
  dojo.require("dijit.form.Button");
  dojo.require("dojo.fx");
  dojo.require("dojox.fx.easing");

  
  function doSearch() {
    var queryString = dojo.byId("searchText").value;
    
    var store = new dojox.data.GoogleWebSearchStore();
    var list = dojo.byId("searchOutput");
	var output = dojo.byId("searchOutputImage");
    
    //Clean up any previous searches
    while(list.firstChild){
      list.removeChild(list.firstChild);
    }
	while(output.firstChild){
      output.removeChild(output.firstChild);
    }
    
    store.fetch({
      query:{text: queryString},
    count: 20,
	onError: function(error, request){
      console.log(arguments);
      list.innerHTML = "Sorry, an error occurred:" + error;
    console.log(error);
    },

      onComplete: function(items, request) {
        //Print out the search results as an unordered 
    //list
        var delay = 0;
        dojo.forEach(items, function(item){
          var li = document.createElement("li");
          li.innerHTML = 
            "<a target='_new' href=\"" +
            store.getValue(item, "url")  + 
            "\">" +
            store.getValue(item, "title") +
            "</a>";	
          dojo.style(li, "opacity", "0");
          list.appendChild(li);
          
          //Fade in the results.
          delay += 200;
          dojo.fadeIn({node:li}).play(delay);          
        });
      }
    });
  }
  
//image search

  function doSearchImage() {
    var queryString = dojo.byId("searchText").value;

    var store = new dojox.data.GoogleImageSearchStore();
    var output = dojo.byId("searchOutputImage");
	var list = dojo.byId("searchOutput");

    //Clean up any previous searches
    while(output.firstChild){
      output.removeChild(output.firstChild);
    }
	while(list.firstChild){
      list.removeChild(list.firstChild);
    }
  
    //searchBtn.setAttribute("disabled",false);

    store.fetch({
      query:{text: queryString},
      count: 32,
    onError: function(error, request){
      console.log(arguments);
      output.innerHTML = "Sorry, an error occurred:" + error;
    console.log(error);
    },
      onComplete: function(items, request) {
        //Print out the search results as one
        //or more images
        var delay = 200;
        dojo.forEach(items, function(item, index){
          var anchor = document.createElement("a");
          anchor.innerHTML =
            "<img src=\"" +
            store.getValue(item, "tbUrl")  +
            "\" alt=\"" +
            store.getValue(item, "title") +
            "\"/>";
          anchor.href = store.getValue(item,
                                   "unescapedUrl");
          anchor.target="_new";
      
          var img = anchor.firstChild;
          dojo.style(img, "opacity", "0");
          output.appendChild(anchor);

          //Slide in the results.
          dojo.connect(img, "onload", function(){
        var coords = dojo.coords(anchor, true);
        var anim = dojo.fx.slideTo({
        node: img,
        left: 0,
        top: 0,
        duration: 3000,
        easing: dojox.fx.easing.elasticOut
      });
      //Put the image off the top right corner.
      dojo.style(img, {
        //left: "-" + (coords.x -150 + (index % 2 == 0 ? 500 : 0))+"px",
        top: "-" + coords.y+"px"
      });
      anim.play(delay * index);
      
      setTimeout(function(){
        dojo.style(img, "opacity", 1);
      }, delay * index);
      
      if(index == items.length -1){        
        //searchBtn.setAttribute("disabled",false);
      }
          });
        });
      }
    });
  }
