/*
* @copyright Copyright 2010 Adolf Würth GmbH & Co. KG
* @link  http://www.wuerth.de
* @version  0.1
* @lastmodified    2010-03-22
*/

function getSession(){
  if(typeof(wu_session) != 'undefined' && wu_session != ''){
    return wu_session;
  } else {
    return 'NOSESSION';
  }
}

function getOrg(){
  if(typeof(wu_org) != 'undefined' && wu_org != ''){
    return wu_org;
  } else {
    return '0001';
  }
}

function getLanguage(){
  if(typeof(wu_language) != 'undefined' && wu_language != ''){
    return wu_language;
  } else {
    return 'DE';
  }
}

function getSuggest(){
  if(typeof(wu_suggest) != 'undefined' && wu_language != ''){
    return wu_suggest;
  } else {
    return 'socketClient.php';
  }
}


//global var for having the control over the autocompleter all the time => e.g. recall without typing
var ajax = '';
function autocomplete2() {
	ajax = new Ajax.Autocompleter("terms","hint2",getSuggest()+ '?parms=' + getSession() + ";" + getLanguage() + ";NOLUC;" + getOrg(),{minChars: 3, indicator: 'indi', indicatorRemove: 'indiRemove', afterUpdateElement: getSelectionId2, callback: getSearchAreaCallBack});
//	ajax = new Ajax.Autocompleter("terms","hint2",getSuggest()+ '?wu_session=' + getSession() + '&wu_language=' + getLanguage() + '&wu_org=' + getOrg(),{minChars: 3, indicator: 'indi', indicatorRemove: 'indiRemove', afterUpdateElement: getSelectionId2, callback: getSearchAreaCallBack});
}

/*Wolles Version*/
function getSelectionId2(text, li) {
  var lid = li.id;
  var terms = document.getElementById('terms');
  var terms2 = document.getElementById('terms2');
  if(terms != null && terms2 != null){
    if(lid.length <= 0){
      terms.value = lid;
      terms2.value = lid;
      return;
    }
    var inOf = lid.indexOf("|");
    if(inOf == "-1"){
      //kein Pipezeichen
      terms.value = lid;
      terms2.value = lid;
    } else {
      //Pipezeichen
      var spl = lid.split("|");
 
      //go to URLS if given back
      if(spl[0] != ''){
        var urlInSplit = spl[0].indexOf("http"); //Aufruf Servicemodul
        if(urlInSplit > "-1") {
          var sep = '?';
          if(spl[0].indexOf('?') > -1) sep = '&';
          var sepWu = spl[0].indexOf("wueko.wuerth");
          if(sepWu > "-1" && sep == '&') sep = ';';
          terms.value = '';

          var goToUrl = spl[0];
          if(sepWu > "-1"){
            if(substr_count(goToUrl, '[JS_SESSION]') > 0) goToUrl = str_replace('[JS_SESSION]', getSession(), goToUrl);
            if(substr_count(goToUrl, '[JS_ORG]') > 0) goToUrl = str_replace('[JS_ORG]', getOrg(), goToUrl);
            if(substr_count(goToUrl, '[JS_LANGUAGE]') > 0) goToUrl = str_replace('[JS_LANGUAGE]', getLanguage(), goToUrl);
            window.location = goToUrl;
            return;
          }

          
          window.location = spl[0] + sep + 'terms=&sc=' + getSearchArea() + '&wu_session=' + getSession() + '&wu_language=' + getLanguage() + '&wu_org=' + getOrg();
          return;
        }
      }
      //otherwise
      if(spl[3] == "product"){
        //product search
        terms.value = spl[1];
        terms2.value = spl[0];
        setFakeAndSearchArea(spl[2], 'optionsCategory', 'currentCategory', 'sc', '', '25');
      } else if(spl[3] == "web"){
        //website search
        if(spl[0] != ''){
          //should not jump into this part...but who knows :)
          var qm = '?';
          if(spl[0].indexOf('?') > -1) qm = '&';
          var isWu = spl[0].indexOf("wueko.wuerth");
          if(isWu > "-1" && qm == '&') qm = ';';
          terms.value = '';
          window.location = spl[0] + qm + 'terms=&sc=' + getSearchArea() + '&wu_session=' + getSession() + '&wu_language=' + getLanguage() + '&wu_org=' + getOrg();
          return;
        } else {
          terms.value = spl[1];
        }
      } else if(spl[3] == "menue"){
        terms.value = utf8_decode(spl[1]);
	  }
    }
    document.suche2.submit();
    terms2.value = '';
  }
}

function recallSearch(){
  //global var ajax => from suggest autocompleter
  var searchInput = document.getElementById('terms');
  if(ajax != null && ajax != '' && searchInput != null && searchInput.value.length > 2){
    ajax.activate();
  }
}

function hideSearch(){
	if(ajax != null && ajax != ''){
		ajax.hide();
	}
}

//callback function for ajax.autocompleter => add value of category selection to the autocompleter before calling it
function getSearchAreaCallBack(element, entry){
	var sc = '';
	var sel = document.getElementById('sc');
	if(sel != null){
		for(var i = 0; i < sel.options.length; i++){
      if(sel.options[i].selected == true){
        sc = sel.options[i].value;
        break;
      }
    }
	}
  return entry + '&sc=' + sc + '&wu_session=' + getSession() + '&wu_language=' + getLanguage() + '&wu_org=' + getOrg();
}

//set focus on input search field...and fire suggest search again
function handleSearch(preSearchValue){
	var el = document.getElementById('terms');
	if(el != null){
		handlePreDescriptionText('terms','onsubmit',preSearchValue);
		//set cursor to the end of text in input field
		el.focus();
		el.value = el.value;
		//recall suggest => global var ajax from autocompleter
		recallSearch();
	}
}

function suggestTabs(divId, productId, webId, product, web){
  var divArea = document.getElementById(divId);
  if(divArea != null){
    var prodArea = document.getElementById(productId);
    var webArea = document.getElementById(webId);
    if(prodArea != null && webArea != null){
      if(product == 'show'){
        prodArea.style.backgroundColor = '#cc0000';
      }
      if(product == 'hide'){
        prodArea.style.backgroundColor = '#605D5C';
      }
      if(web == 'show'){
        webArea.style.backgroundColor = '#cc0000';
      }
      if(web == 'hide'){
        webArea.style.backgroundColor = '#605D5C';
      }
    }
    var liArray = divArea.getElementsByTagName('li');
    var expl = null;
    for (var i = 0; i < liArray.length; i++) {
      expl = liArray[i].id.split('|');
      if(expl[3] != null && expl[3] != undefined){
        if(expl[3] == 'product'){
          if(product == 'hide'){
            liArray[i].style.display = 'none';
          }
          if(product == 'show'){
            liArray[i].style.display = 'block';
          }
        }
        if(expl[3] == 'web'){
          if(web == 'hide'){
            liArray[i].style.display = 'none';
          }
          if(web == 'show'){
            liArray[i].style.display = 'block';
          }
        }
      }
    }
  }
}

function utf8_decode ( str_data ) {
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;

    str_data += '';

    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

Event.observe(document.onresize ? document : window, 'resize', hideSearch);

document.observe('dom:loaded', function (event) {
	autocomplete2();
});
