var search_activated = '';
var stop_keydn = false;
function search_width() {
  $('#search').css({width: ($('#service').width() - $('#breabcrumbs').width()) + "px"});
  $('#searchField').css({width: ($('#service').width() - $('#breabcrumbs').width() - 39) + "px" });
}
function search_tree() {
  $('#breabcrumbs a').bind('click', function() {
    if ($('div.container[@rel=home]').length)
    {
      while ($('div.container:last').attr('rel') != 'home') page_close();
      if ($(this).is('.top')) {
        if (/[^ \t]{2,}/.test(document.getElementById('searchField').value)) {
          _search_close();
          search_close();
        }
        $('#mainList li').removeClass('active');
        if ($('#categorySelect').length) {
          $('#categorySelect').load(modal_link($(this).attr('href')), function() {
            set_win_title();
            index_domodal_();
          });
        } else {
          $('#in_container').load(modal_link2($(this).attr('href')), function() {
            set_win_title();
            index_domodal();
          });
        }
        set_href($(this).attr('href'));
        $('#searchRoot').attr('value', $(this).attr('href'));
        $('#breabcrumbs').html('');
      } else if ($(this).attr('rel')) {
        if (/[^ \t]{2,}/.test(document.getElementById('searchField').value)) {
          _search_close();
          search_close();
        }
        $('#mainList li').removeClass('active');
        $('#mainList li[@rel=' + $(this).attr('rel') + ']').addClass('active');
        if ($('#categorySelect').length) {
          $('#categorySelect').load(modal_link($(this).attr('href')), function() {
            set_win_title();
            subcat_domodal();
            $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
            search_tree();
          });
        } else {
          $('#in_container').load(modal_link2($(this).attr('href')), function() {
            set_win_title();
            index_domodal();
            subcat_domodal();
            $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
            search_tree();
          });
        }
        set_href($(this).attr('href'));
        $('#searchRoot').attr('value', $(this).attr('href'));
        $(this).parent().addClass('current').next('span').remove();
      } else {
        if (/[^ \t]{2,}/.test(document.getElementById('searchField').value)) {
          $('#close').trigger('click');
        }
        if (!$('#itemList').length) {
          set_href($(this).attr('href'));
          $('#searchRoot').attr('value', $(this).attr('href'));
          $('#in_container').html(empty_list).load(modal_link($(this).attr('href')), function() {
            set_win_title();
            items_domodal();
            list_func();
            $('#in_container .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
            $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
            search_tree();
          })
        }
      }
      search_width();
      return false;
    }
  });
  search_width();
}
$(document).bind('ready', function() {
  var search = document.getElementById('search');
  var searchField = document.getElementById('searchField');
  var searchValue = document.getElementById('value');
  search_tree();
  if(/[^ \t]{2,}/.test(searchField.value)){
    searchValue.style.display = 'none';
    //$('#close').css({visibility: 'visible'}); //.bind('click', function(){document.location.href = $('#searchRoot').attr('value');});
    search_submit();
    //search_activated = searchValue.value;
  }
  else{
    searchField.value = '';
    searchField.blur();
    
  };
  searchField.onfocus = function() {
      searchValue.style.display = 'none';		
  }	    
  searchField.onblur = function() {
    if(/.+/.test(searchField.value)){
      searchValue.style.display = 'none';		
    }
    else{
      searchValue.style.display = 'block';
    };
  }	  
	searchValue.onclick = function() {
		searchValue.style.display = 'none';
		searchField.focus();
  }
  $(searchField).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key==13)
    {
      if ((search_activated == this.value)) return false;
      stop_keydn = true;
      $('#spinner').css({visibility: 'hidden'});   
      if (window.searchtm) window.clearTimeout(window.searchtm);
      search_submit();
      return false;
    } 
    else 
    {
      stop_keydn = false;
    }
    return true;
  }).bind('keyup', function() {
    if (stop_keydn) return false;
    if ((search_activated == this.value)) { 
      if (window.searchtm) window.clearTimeout(window.searchtm);
      $('#pencil').css({visibility: 'hidden'});
      if (/[^ \t]{2,}/.test(this.value)) {
        $('#close').css({visibility: 'visible'});
      }
      return false;
    }
    $('#spinner').css({visibility: 'hidden'});   
    if (window.searchtm) window.clearTimeout(window.searchtm);
    $('#close').css({visibility: 'hidden'});
    if (!/.+/.test(this.value)) {
      $('#pencil').css({visibility: 'hidden'});
    } else {
      $('#pencil').css({visibility: 'visible'});
    }
    window.searchtm = window.setTimeout(search_submit, $('#SearchTM').attr('value'));
  })
  $('#close').bind('click', function() {
    while ($('div.container:last').attr('rel') != 'home') page_close();
    if ($('#breabcrumbs a').length > 2) {
      $('#dv_items').load(modal_link3($('#searchRoot').attr('value')), function() {
        set_win_title();
        list_func();
        do_show();
      });
    } else _search_close();
    search_close();
    return false;
  })
});
function search_close()
{
  search_activated = '';
  $('#searchField').attr('value', '').each(function() {this.blur();});
  $('#value').show();
  $('#pencil').css({visibility: 'hidden'});
  $('#close').css({visibility: 'hidden'});
}
function _search_close() 
{
  if ($('div.in_container').length > 1) {
    $('div.in_container:last').remove();
    $('div.in_container:last').show();
  }
}
function search_submit()
{
  if (!/[^ \t]{2,}/.test(document.getElementById('searchField').value)) {
    if (!/.+/.test(document.getElementById('searchField').value)) $('#close').trigger('click'); 
  } else {
    search_activated = document.getElementById('searchField').value;
    $('#pencil').css({visibility: 'hidden'});
    $('#spinner').css({visibility: 'visible'});
    while ($('div.container:last').attr('rel') != 'home') page_close();
    if ($('#breabcrumbs a').length > 2 && $('#dv_items').length) {
      $('#dv_items').load(modal_link3($('#searchRoot').attr('value') + 'search/' + encodeURIComponent($('#searchField').attr('value').replace(/\//g, 'S_L_H')) + '\.html'), function() {
        $('#spinner').css({visibility: 'hidden'});
        $('#close').css({visibility: 'visible'});
        list_func();
        adv_list_func();
        do_show();
      });
    } else if ($('div.in_container').length > 1) {
      $('div.in_container:last').html(empty_list).load(modal_link($('#searchRoot').attr('value') + 'search/' + encodeURIComponent($('#searchField').attr('value').replace(/\//g, 'S_L_H')) + '\.html'), function() {
        $('#spinner').css({visibility: 'hidden'});
        $('#close').css({visibility: 'visible'});
        items_domodal();
        list_func();
        adv_list_func();
        $('div.in_container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    } else {
      $('div.in_container:last').hide().each(function() {
        var el = document.createElement('div');
        $(el).attr('id', 'in_container').attr('class', 'in_container').insertAfter($(this)).html(empty_list).load(modal_link($('#searchRoot').attr('value') + 'search/' + encodeURIComponent($('#searchField').attr('value').replace(/\//g, 'S_L_H')) + '\.html'), function() {
          $('#spinner').css({visibility: 'hidden'});
          $('#close').css({visibility: 'visible'});
          items_domodal();
          list_func();
          adv_list_func();
          $('div.in_container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
        });
      }); 
    }
  }
}
function adv_list_func()
{
  $('#s1').bind('click', function() {
    $('#searchRoot').attr('value', $('#searchRoot').attr('value').replace(/[^\/]+\/$/, ''));
    $('#service #breabcrumbs span.current').prev().addClass('current').next().remove();
    var last_href = $('#searchRoot').attr('value');
    set_href(last_href);
    if ($('div.in_container').length > 1) {
      $('div.in_container:first').load(modal_link2(last_href), function() {
        set_win_title();
        index_domodal();
        subcat_domodal();
      });
    } else {
      $('div.in_container:last').each(function() {
        var el = document.createElement('div');
        $(el).attr('id', 'in_container').attr('class', 'in_container').insertBefore($(this)).hide().html(empty_list).load(modal_link2(last_href), function() {
          set_win_title();
          index_domodal();
          subcat_domodal();
        });
      });
    }
    search_tree();
    $('#close').css({visibility: 'hidden'});
    $('#spinner').css({visibility: 'visible'});
    $('div.in_container:last').html(empty_list).load(modal_link($('#searchRoot').attr('value') + 'search/' + encodeURIComponent($('#searchField').attr('value').replace(/\//g, 'S_L_H')) + '\.html'), function() {
      $('#spinner').css({visibility: 'hidden'});
      $('#close').css({visibility: 'visible'});
      items_domodal();
      list_func();
      adv_list_func();
      $('div.in_container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    });
    return false;
  });
  $('#s2').bind('click', function() {
    $('#searchRoot').attr('value', '/');
    $('#service #breabcrumbs').html('');
    var last_href = '/';
    set_href(last_href);
    if ($('div.in_container').length > 1) {
      $('div.in_container:first').load(modal_link2(last_href), function() {
        set_win_title();
        index_domodal();
      });
    } else {
      $('div.in_container:last').each(function() {
        var el = document.createElement('div');
        $(el).attr('id', 'in_container').attr('class', 'in_container').insertBefore($(this)).hide().html(empty_list).load(modal_link2(last_href), function() {
          set_win_title();
          index_domodal();
        });
      });
    }
    search_tree();
    $('#close').css({visibility: 'hidden'});
    $('#spinner').css({visibility: 'visible'});
    $('div.in_container:last').html(empty_list).load(modal_link($('#searchRoot').attr('value') + 'search/' + encodeURIComponent($('#searchField').attr('value').replace(/\//g, 'S_L_H')) + '\.html'), function() {
      $('#spinner').css({visibility: 'hidden'});
      $('#close').css({visibility: 'visible'});
      items_domodal();
      list_func();
      adv_list_func();
      $('div.in_container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    });
    return false;
  });
  $('#s3').bind('click', function() {
    $('#header .logo a').trigger('click');
    return false;
  });
}
