//This function implements the toElement method on string to return a Element from a String 
//Object. Helpful to get a element from a string HTML.
String.implement({ 
	toElement: (function() { 
		var div = new Element('div'); 
		return function() { 
			return div.set('html', this); 
		} 
	})() 
});


    google.load('search', '1.0');
    function OnLoad() 
    {
      window.addEvent('domready',function() {  

        //This is a scenario to add gsc-results div tag if missing due to navigation completely hidden.
        try
        {
			if ( !$('gsc-results') && $('bg_style') ) 
			{
				  var googleText = "<div id='share-storylist' class='share-storylist'><div class='share-hover' id='share-hover'></div></div> <div id='gsc-results' class='gsc-results' style='display:none;'><div class='gsc-cursor-box bottomHR' id='cursor_header'><span class='brandColor heading'>Search Results</span><span id='cursor_t'></span></div><div id='banner-display'><img alt='Search Banner Tag' src='/resources/shared/images/whitepixel.gif' id='banner_image'></div><div id='results'></div><div align='right' class='gsc-cursor-box topHR' id='cursor'></div><div style='display: none;' id='banner_isi_info'>sample text</div></div>".toElement();
				  googleText.inject($('bg_style'));
			}                    
        }
        catch ( e )
        {
        
        }

      	new RawSearchControl();
      
      });
    }
    
    
    function IncludeJavaScript(jsFile)
    {
      document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); 
    }
     
    IncludeJavaScript('/resources/shared/scripts/google_banner.js'); 
    
    /**
     * The RawSearchControl demonstrates how to use Searcher Objects
     * outside of the standard GSearchControl. This includes calling
     * searcher .execute() methods, reacting to search completions,
     * and if you had previously disabled html generation, how to generate
     * an html representation of the result.
     */
    function RawSearchControl() {
      // latch on to key portions of the document
      this.searcherform = $("searcher");
      this.results = $("results");
      this.cursor = $("cursor");
      this.searchform = $("searchform");
      this.cursor_t = $("cursor_t");

      // News Related   
      if(document.getElementById("searcherNews")) 
      {
          this.searcherformNews = document.getElementById("searcherNews");
          this.resultsNews = document.getElementById("resultsNews");
          this.cursorNews = document.getElementById("cursorNews");
          this.searchformNews = document.getElementById("searchformNews");
      }
      // News Related
      

      // create map of searchers as well as note the active searcher
      this.activeSearcher = "wyeth.com";
      this.searchers = new Array();

      // create and wire up an instance of GwebSearch and one of
      // GlocalSearch. Note, we disable html generation. We are doing
      // this so that we can demonstrate how to manually create it if
      // needed. Note that we register to handle search completion notifications
      // when searches complete, they are called in the context of this instance
      // of RawSearchControl and they are passed the searcher that just completed

      // wire up a raw GwebSearch searcher
      var searcher = new google.search.WebSearch();
      searcher.setNoHtmlGeneration();

      searcher.setSiteRestriction(
      {
        cseId : "003349770745872249173:j6c2s2uqni4",
        siteUrl : "http://www.pfizerpro.com"
      }
      );
          
      searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
      searcher.setLinkTarget(GSearch.LINK_TARGET_TOP);
      searcher.setSearchCompleteCallback(this,
                                         RawSearchControl.prototype.searchComplete,
                                         [searcher]
                                         );
      this.searchers["wyeth.com"] = searcher;


      // do the same for All Wyeth Sites
      var searcher = new google.search.WebSearch();
      searcher.setNoHtmlGeneration();
          searcher.setSiteRestriction(
      {
        cseId : "003349770745872249173:j6c2s2uqni4"
      }
      );
      searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
      searcher.setSearchCompleteCallback(this,
                                         RawSearchControl.prototype.searchComplete,
                                         [searcher]
                                         );
      this.searchers["all"] = searcher;
      
      // Searcher for the news Site search
      var searcher = new google.search.WebSearch();
      searcher.setNoHtmlGeneration();

      searcher.setSiteRestriction(
      {
        cseId : "003349770745872249173:j6c2s2uqni4",
        siteUrl : "http://www.wyeth.com/news"
      }
      );
          
      searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
      searcher.setLinkTarget(GSearch.LINK_TARGET_TOP);

      searcher.setSearchCompleteCallback(this,
                                         RawSearchControl.prototype.searchComplete,
                                         [searcher]
                                         );
      this.searchers["news"] = searcher;      

      // now, create a search form and wire up a submit and clear handler
      this.searchForm = new google.search.SearchForm(true, this.searchform);
      this.searchForm.setOnSubmitCallback(this,RawSearchControl.prototype.onSubmit);
      this.searchForm.setOnClearCallback(this,RawSearchControl.prototype.onClear);
      
      // News Related
      if(document.getElementById("searcherNews"))
      {
          this.searchFormNews = new google.search.SearchForm(true, this.searchformNews);
          this.searchFormNews.setOnSubmitCallback(this,RawSearchControl.prototype.onSubmitNews);
          this.searchFormNews.setOnClearCallback(this,RawSearchControl.prototype.onClear);
      }
      
      // News Related
    }

    /**
     * figure out which searcher is active by looking at the radio
     * button array
     */
    RawSearchControl.prototype.computeActiveSearcher = function() {
      for (var i=0; i<this.searcherform["searcherType"].length; i++) {
        if (this.searcherform["searcherType"][i].checked) {
          this.activeSearcher = this.searcherform["searcherType"][i].value;
          return;
        }
      }
    }

    /**
     * figure out which searcher is active by looking at the radio
     * button array
     */
    RawSearchControl.prototype.computeActiveSearcherNews = function() {
         this.activeSearcher = "news";
    }
    /**
     * onSubmit - called when the search form is "submitted" meaning that
     * someone pressed the search button or hit enter. The form is passed
     * as an argument
     */
    RawSearchControl.prototype.onSubmit = function(form) {
      //this.computeActiveSearcher();
      if (form.input.value) {
        // if there is an expression in the form, call the active searchers
        // .execute method
        //this.searchers[this.activeSearcher].execute(form.input.value);
        this.searchers[this.activeSearcher].execute(form.input.value);
      }

      // always indicate that we handled the submit event
      return false;
    }
    
        /**
     * onSubmit - called when the search form is "submitted" meaning that
     * someone pressed the search button or hit enter. The form is passed
     * as an argument
     */
    RawSearchControl.prototype.onSubmitNews = function(form) {
      this.computeActiveSearcherNews();
      if (form.input.value) {
        // if there is an expression in the form, call the active searchers
        // .execute method
        this.searchers[this.activeSearcher].execute(form.input.value);
      }

      // always indicate that we handled the submit event
      return false;
    }

    /**
     * onClear - called when someone clicks on the clear button (the little x)
     */
    RawSearchControl.prototype.onClear = function(form) {
      this.clearResults();
    }

    /**
     * searchComplete - called when a search completed. Note the searcher
     * that is completing is passes as an arg because thats what we arranged
     * when we called setSearchCompleteCallback
     */
    RawSearchControl.prototype.searchComplete = function(searcher) {

      // always clear old from the page
      
      
 
      
      
      document.getElementById('gsc-results').style.display="inline";
      this.clearResults();
      document.getElementById('content_area').style.display="none";
      document.getElementById('banner-display').style.display="none";
      //This is to push the Logged in User Greeting above the Search results
      if ( $$('.logged_in').length >= 1) {
      	$('gsc-results').inject($$('.logged_in')[0],'bottom')
      }
      
      // if the searcher has results then process them
      if (searcher.results && searcher.results.length > 0) {


        // now manually generate the html that we disabled
        // initially and display it
        var div = createDiv("", "header");
        if(this.results == null) {
                this.results = document.getElementById("results");
        }
        this.results.appendChild(div);
        for (var i=0; i<searcher.results.length; i++) {
          var result = searcher.results[i];
          searcher.createResultHtml(result);
          if (result.html) {
            div = result.html.cloneNode(true);
          } else {
            div = createDiv("** failure to create html **");
          }
          
          //if(isPresentinArray(document.getElementsByName('search'),url_keyWords))
          //    addTextNode(document.getElementsByName('search'),div);
            
          this.results.appendChild(div);
        }
       
       
       // Append ISI for banner
       //     
       
       
       
       
        //make decision to show banner
         
     showSearchBanner();
     

        // now, see if we have a cursor, and if so, create the
        // cursor control
        if (searcher.cursor) {
          var cursorNode = createDiv(null, "gsc-cursor");
          var cursorNode_t = createDiv(null, "gsc-cursor");
                    
          for (var i=0; i<searcher.cursor.pages.length; i++) {
            var className = "gsc-cursor-page";
            if (i == searcher.cursor.currentPageIndex) {
              className = className + " gsc-cursor-current-page";
            }
            var pageNode = createDiv(searcher.cursor.pages[i].label, className);
            pageNode.onclick = methodClosure(this, this.gotoPage,
                                             [searcher, i]);
                                             
             
            var copy_P = createDiv(searcher.cursor.pages[i].label, className);
            copy_P.onclick = methodClosure(this, this.gotoPage,
                                             [searcher, i]); 
           
            
            cursorNode.appendChild(pageNode);
            cursorNode_t.appendChild(copy_P);
            //var headerElement = document.createElement('<h1 style="display:inline;border-bottom:none">Search Results</h1>');
            //cursorNode_t.appendChild(headerElement);
          }
          this.cursor.appendChild(cursorNode);
         

          this.cursor_t.appendChild(cursorNode_t);
          
          var more = createLink(searcher.cursor.moreResultsUrl,
                                GSearch.strings["more-results"] + "&nbsp;&raquo;",
                                GSearch.LINK_TARGET_SELF,
                                "gsc-trailing-more-results");
         // this.cursor.appendChild(more);
         //Add the Search Results Header
         cursorArray = $$('#cursor_t')
         if(cursorArray) {
          //cursorArray[0].innerHTML = '<h1 style="display:inline;border-bottom:none">Search Results</h1>'+cursorArray[0].innerHTML;
          //cursorArray[0].innerHTML = '<span class="brandColor heading">Search Results</span>'+cursorArray[0].innerHTML;

         }
        
          
        }
      } else {
        //No Results found. Show a friendly message 
        document.getElementById("results").innerHTML = "<h3> No results found, please try a different query.</h3>";
      }
      
     //enableHover('#gsc-results');
     //appendISILinks( );
     appendISILinksBySearchingKeywords();
      
    }

    RawSearchControl.prototype.gotoPage = function(searcher, page) {
      searcher.gotoPage(page);
    }

    /**
     * clearResults - clear out any old search results
     */
    RawSearchControl.prototype.clearResults = function() {
      removeChildren(this.results);
      removeChildren(this.cursor);
      removeChildren(this.cursor_t);
    }

    /**
     * Static DOM Helper Functions
     */
    function removeChildren(parent) {
      if(parent) {
          while (parent.firstChild) {
            parent.removeChild(parent.firstChild);
          }
      }
    }
    function createDiv(opt_text, opt_className) {
      var el = document.createElement("div");
      if (opt_text) {
        el.innerHTML = opt_text;
      }
      if (opt_className) { el.className = opt_className; }
      return el;
    }

    function methodClosure(object, method, opt_argArray) {
      return function() {
        return method.apply(object, opt_argArray);
      }
    }

    function createLink(href, opt_text, opt_target, opt_className, opt_divwrap) {
      var el = document.createElement("a");
      el.href = href;
      if (opt_text) {
        el.innerHTML = opt_text;
      }
      if (opt_className) {
        el.className = opt_className;
      }
      if (opt_target) {
        el.target = opt_target;
      }
      if (opt_divwrap) {
        var div = this.createDiv(null, opt_className);
        div.appendChild(el);
        el = div;
      }
      return el;
    }
    
    
    function addTextNode(search_term, div_node)
    {
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id','testname');
     newdiv.innerHTML = '<p style="font-size : smaller;"><a href="#">Indication and Important Safety Information</a></p>';
     div_node.childNodes[0].appendChild(newdiv);
    }

   

    // register to be called at OnLoad when the page loads
    google.setOnLoadCallback(OnLoad,
        true);


IncludeJavaScript('/resources/foresee/foresee.js'); 
