// Copyright 2008 Janusz Skonieczny.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * Document initializaction 
 */
function DocumentInit(){
  //MakeTextAreaResizable();
  InitConsole()
  InitSearch();
  ModernBrowserCheck();
  $('.autogrow').autogrow();  
  console.debug("document.ready init completed");  
}
$(document).ready(DocumentInit); 

/**
 * Alters all textarea elements and makes them resizable.
 * - there is an issue, make resizable after they are shown.
 */
function MakeTextAreaResizable(){
$.each($("textarea"), function(i,item){
      //console.debug(item.size);
      $(item).resizable({ 
        handles: "se", 
        animate: true, 
        animateDuration: "slow", 
        animateEasing: "swing",
        ghost: true     
      });
      $(item).size = "100%"
  });          
}

/**
 * Post a form
 */
function InitConsole(){
  if (window.console && window.console.debug){
    console.debug("Console already is")
    return;
  }
  window.console = {};
  window.console.debug = function(str){};
  // sorry no logging today :(
}

 var searchControl;
/**
 * Search 
 */
function InitSearch() {
  // Our ImageSearch instance.
  var webSearch = new google.search.WebSearch();

  // Restrict to extra large images only
  webSearch.setSiteRestriction('skonieczny.pl');

  // Create a search control
  searchControl = new google.search.SearchControl();
  
  var options = new google.search.SearcherOptions();
  options.setRoot(document.getElementById("search-results"))
  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
  searchControl.addSearcher(webSearch, options);

  var drawOptions = new google.search.DrawOptions();
  drawOptions.setInput(document.getElementById("search"))
  searchControl.draw(document.getElementById("searchform"), drawOptions);
  //searchControl.execute('skonieczny');
  $('#search').keyup(function(e){
        if (e.keyCode == 13) {
            $('#main').empty();
            searchControl.execute($('#search').attr("value"));
        }        
      });
} 
 
function ModernBrowserCheck(){
  if(!jQuery.support.boxModel){
    $("#browser-upgrade").show("slow");
    }
 }

