﻿var gs_time = null;

function gs_start() {
  var gs_select = byId("gs_select");
  if (gs_select) { 
    gs_select.style.display = 'none'; 
  }
  var gs_dropdown = byId("gs_dropdown");
  if (gs_dropdown) { 
    gs_dropdown.style.display = 'block'; 
    gs_dropdown.onmouseover = function() {
      gs_toggle_opcje('block');
    };
    gs_dropdown.onmouseout = function(){
      gs_toggle_opcje('none');
    };
  }
  var opt_uzytkownicy = byId("opt_uzytkownicy");
  if (opt_uzytkownicy) { 
    opt_uzytkownicy.onclick = function() {
      gs_set_action('/szukaj/profile');
      gs_podmien_txt('Użytkownicy');
      gs_podmien_label('Szukaj w Naszej-Klasie');
      gs_toggle_opcje('none_now');
      return false;
    }
  }
  var opt_internet = byId("opt_internet");
  if (opt_internet) {
    opt_internet.onclick = function() {
      gs_set_action('/szukaj/internet');
      gs_podmien_txt('Internet');
      gs_podmien_label('Szukaj w Google');
      gs_toggle_opcje('none_now');
      return false;
    }
  }
  var gs_submit_btn = byId("gs_submit_btn");
  if (gs_submit_btn) {
    gs_submit_btn.onclick = function() {
      return gs_query(this.form);
    }
    gs_submit_btn.onfocus = function(){
      return gs_toggle_opcje('none_now');
    }
  }
}

function gs_tab( e ) {
  var e = e || window.event;
  if (e.keyCode == 9) {
    gs_toggle_opcje('block');
    var mini_search = byId('mini_search_query');
    if (mini_search) {
      mini_search.focus();
    }
  }
  return true;
}

function is_set_by_us( value )
{
  return ((value=='Szukaj w Google') || (value=='Szukaj w Naszej-Klasie'));
}

function gs_search_query_val() {
  var mini_search_query = byId("mini_search_query");
  if (mini_search_query) {
    //mini_search_query.focus(); for future use.
    var gs_selected = byId('gs_selected');
    gs_podmien_label('Szukaj w Naszej-Klasie');
    mini_search_query.onfocus = function(){
      if (is_set_by_us(this.value)) { 
        this.value='';
      }
      return gs_toggle_opcje('none_now');
    };
    mini_search_query.onkeydown = function( event ) {
      return gs_tab( event );
    };
    mini_search_query.onblur = function(){
      var search_value = this.value.trim();
      if (search_value=='') { 
        if (gs_selected.innerHTML == 'Internet') {
          gs_podmien_label('Szukaj w Google');
        } else {
          gs_podmien_label('Szukaj w Naszej-Klasie');
        }
      }
    };
  }
}

function gs_set_action(akcja)
{
  var mini_search = byId('google_mini_search'); 
  if (mini_search) {
    mini_search.action = akcja;
  }
}


function gs_toggle_opcje(akcja)
{ 
  if (gs_time) clearTimeout(gs_time);
  if (akcja=='none') { 
    gs_time = setTimeout(function() {
          var gs_actions = byId('gs_akcje')
          if (gs_actions) { 
            gs_actions.style.display = 'none';
          }
        }, 500);
    return;
  };
  var gs_akcje = byId('gs_akcje');
  if (!gs_akcje) {
    return;
  }
  if (akcja=='none_now') {
    gs_akcje.style.display='none';
  }
  else {
    gs_akcje.style.display='block';
  }
}

function gs_podmien_txt(tekst) {
  var gs_selected = byId('gs_selected');
  if (gs_selected) {
      gs_selected.innerHTML = tekst;
  }
  var gs_zaawansowane = byId('gs_zaawansowane');
  if (!gs_zaawansowane) {
    return;
  }
  if (tekst == 'Użytkownicy') {
    gs_zaawansowane.style.display='block';
  }
  else {
    gs_zaawansowane.style.display='none';
  }
}

function gs_podmien_label(tekst) {
  var mini_search = byId('mini_search_query');
  if (mini_search) {
    if (is_set_by_us(mini_search.value) || (mini_search.value=='')) { 
      mini_search.value = tekst;
    } 
  }
}

function google_afs_request_done(google_ads)
{
  /*
   * Verify that there are actually ads to display.
   */
  var googleAdsLinkUrl = 'http://services.google.com/feedback/online_hws_feedback?client=' + google_afs_client;
  var googleAdsTitle = '<a href="' + googleAdsLinkUrl + '">Ads by Google</a>';

  var google_num_ads = google_ads.length;
  if (google_num_ads <= 0) {
    return;
  }

  var wideAds = '';     // wide ad unit html text
  var wideAdsBottom = '';     // wide ad unit html text
  var narrowAds = '';   // narrow ad unit html text

  for(i = 0; i < google_num_ads; i++) {
    if (google_ads[i].type=="text/wide") {
      if (i%2) {
        wideAds += '<li><h3><a href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a></h3>' +
                   google_ads[i].line2 + '<br>' +
                   '<a href="' + google_ads[i].url + '"><cite>' + google_ads[i].visible_url + '</cite></a></li>';
      } else {
        wideAdsBottom += '<li><h3><a href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a></h3>' +
                         google_ads[i].line2 + '<br>' +
                         '<a href="' + google_ads[i].url + '"><cite>' + google_ads[i].visible_url + '</cite></a></li>';
      }
    } else {
      narrowAds += '<li><h3><a href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a></h3>' +
                 google_ads[i].line2 + '<br>' +
                 google_ads[i].line3 + '<br>' +
                 '<a href="' + google_ads[i].url + '"><cite>' + google_ads[i].visible_url + '</cite></a></li>';
    }
  }

  if (narrowAds != '') {
    narrowAds = '<div><h2>' + googleAdsTitle + '</h2><ul>' + narrowAds + '</ul><div class="clear"></div></div>';
  }

  if (wideAds != '') {
    wideAds = '<div><h2>' + googleAdsTitle + '</h2><ul>' + wideAds + '</ul></div>';
  }

  if (wideAdsBottom != '') {
    wideAdsBottom = '<div><h2>' + googleAdsTitle + '</h2><ul>' + wideAdsBottom + '</ul></div>';
  }

  // Write HTML for wide and narrow ads to the proper <div> elements
  var wide_obj = byId("wide_ad_unit");
  if (wide_obj) {
    wide_obj.innerHTML = wideAds;
  }
  var wide_bottom_obj = byId("wide_ad_unit_bottom");
  if (wide_bottom_obj) {
    wide_bottom_obj.innerHTML = wideAdsBottom;
  }
  var narrow_obj = byId("narrow_ad_unit");
  if (narrow_obj) {
    narrow_obj.innerHTML = narrowAds;
  }
}

function gs_get_param(name)
{
  var match = new RegExp(name + "=(.+)[&]","i").exec(location.search);
  if (match==null) {
    match = new RegExp(name + "=(.+)","i").exec(location.search);
  }
  if (match==null) {
    return null;
  }
  match = match + "";
  result = match.split(",");
  return result[1];
}

function gs_query(form) {
  if (!form) {
    return;
  }
  if (form.q.value !== '' && !is_set_by_us(form.q.value)) {
    var url = form.action.split('/');
    var search_handler = url[url.length-1];
    var search_category = '';
    switch (search_handler) {
      case 'szukaj':
      case 'internet':
        search_category = 'Siec';
        break;
      case 'profile':
        search_category = 'Profile';
        break;
      default:
        search_category = 'Inne';
    }
    if(typeof(pageTracker) != 'undefined') {
      pageTracker._trackPageview("/search_stats?search_query=" + form.q.value + '&search_category=' + search_category); 
    }
    var query = form.action + '?q=' + encodeURIComponent(form.q.value);
    window.location.href = query;
  }
  return false;
}

var gs_old_onload = window.onload ? window.onload : function() {};
window.onload = function() {
  gs_old_onload();
  gs_start();
  gs_search_query_val();
}

