﻿    /*
    *  The SearchControl manages searchers and draws a UI for them.  However,
    *  searchers can be used by themselves without the SearchControl.  This is
    *  called using a "Raw Searcher".  When doing this, you must handle and draw
    *  the search results manually.
    */
    
    google.load('search', '1');
    
    var WebSearch;
    
    function addPaginationLinks() {
      // The cursor object has all things to do with pagination
      var cursor = WebSearch.cursor;
      var curPage = cursor.currentPageIndex; // check what page the app is on
      var pagesDiv = document.createElement('div');
      for (var i = 0; i < cursor.pages.length; i++) {
        var page = cursor.pages[i];
        if (curPage == i) { // if we are on the curPage, then don't make a link
          var label = document.createTextNode(' ' + page.label + ' ');
          pagesDiv.appendChild(label);
        } else {
          // If we aren't on the current page, then we want a link to this page.
          // So we create a link that calls the gotoPage() method on the searcher.
          var link = document.createElement('a');
          link.href = 'javascript:WebSearch.gotoPage('+i+');';
          link.innerHTML = page.label;
          link.style.marginRight = '2px';
          pagesDiv.appendChild(link);
        }
      }
    
      var contentDiv = document.getElementById('content');
      contentDiv.appendChild(pagesDiv);
    }
    
    function searchComplete() {
      // Check that we got results
      if (WebSearch.results && WebSearch.results.length > 0) {
        // Grab our content div, clear it.
        var contentDiv = document.getElementById('content');
	var bread=getElementsByClassName('breadcrambs show',contentDiv);
        contentDiv.innerHTML = '';
	if (bread!=null) contentDiv.appendChild(bread[0]);
		
		var innerDiv=document.createElement('div');
		var header_text=document.createElement('h2');
		var header_span=document.createElement('span');
		header_span.innerHTML='Результаты поиска:';
		header_text.appendChild(header_span);
		innerDiv.appendChild(header_text);
    
	contentDiv.appendChild(innerDiv);
        // Loop through our results, printing them to the page.
        var results = WebSearch.results;
        for (var i = 0; i < results.length; i++) {
          // For each result write it's title and image to the screen
          var result = results[i];
              
		  // We use titleNoFormatting so that no HTML tags are left in the title
          var title = document.createElement('span');
		  title.className='plashka show';
		  var title_text=document.createElement('strong');
          title_text.innerHTML = result.titleNoFormatting;
		  title.appendChild(title_text); //ADD
		  
		var newContent=document.createElement('p');    
          newContent.innerHTML=result.content; //ADD
		    
		var readmore_span=document.createElement('span');//ADD
		readmore_span.className='read_more show';
		var readmore_a=document.createElement('a');
		readmore_a.href=result.url;
		readmore_a.innerHTML='подробнее';
		
		readmore_span.appendChild(readmore_a);
		
		contentDiv.appendChild(title);
		contentDiv.appendChild(newContent);
		contentDiv.appendChild(readmore_span);		
        }
    
        // Now add the paging links so the user can see more results.
        addPaginationLinks(WebSearch);
      }
    }
    
    function OnLoad() {
      // Our WebSearchinstance.
      WebSearch= new google.search.WebSearch();
    
      // Restrict to extra large images only
      WebSearch.setSiteRestriction('tritec.ru');
    
      // Here we set a callback so that anytime a search is executed, it will call
      // the searchComplete function and pass it our WebSearchsearcher.
      // When a search completes, our WebSearchobject is automatically
      // populated with the results.
      WebSearch.setSearchCompleteCallback(this, searchComplete, null);
    
      // Find me a beautiful car.
      //WebSearch.execute("Subaru STI");
    }

    function GetSearch(){
	var searchtext=document.getElementById('searchInput').value;
	WebSearch.execute(searchtext)
    }

    function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
    google.setOnLoadCallback(OnLoad);