window.search_node = null;
//var appname = navigator.appName.toLowerCase();
//var platform = navigator.platform.toLowerCase();

////////////////////////////////////////////////////////////////////////////////
//*** Big thanks for this code to Gavin Kistner, !@phrogz.net
//license: http://phrogz.net/JS/_ReuseLicense.txt

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture){
  if (!useCapture) useCapture=false;
  if (obj.addEventListener){
    obj.addEventListener(evt,fnc,useCapture);
    return true;
  } else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
  else{
    MyAttachEvent(obj,evt,fnc);
    obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
  }
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
  if (!obj.myEvents) obj.myEvents={};
  if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
  var evts = obj.myEvents[evt];
  evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
  if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
  var evts = obj.myEvents[evt];
  for (var i=0,len=evts.length;i<len;i++) evts[i]();
}
//   AttachEvent(Input, 'keyup', pressFilter, false);
////////////////////////////////////////////////////////////////////////////////

function referSearch(){
  var st = ge('search_text');
  if( st.value.length < 3 ) {  alert("Текст поиска слишком короткий"); return; }//Текст поиска слишком короткий");  return;  }
  document.location = "http://djdark.ru/search/search.html#0;"+escape( st.value );
}

function ge(id) 
{
  var rr = null;
  if (document.getElementById)
    var rr = document.getElementById(id);
  else if (document.all)
    var rr = document.all[id];
  else if (document.layers)
    var rr = document.layers[id];
  return rr;
}

function pressFilter(e) { 
  try {
    var k = 0;
    if (e) {
      k = e.keyCode;
      if (k == 13) { 
        referSearch(); 
      }
    }
  } catch ( e ) { 
    //need for key <ESC>, etc.
  }
}

function addCss(c) {
  var cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = 'http://djdark.ru/'+c;
  document.getElementsByTagName("head")[0].appendChild(cssNode);
}

function getParent(){
  var img = document.getElementsByTagName("img");
  var prnt = null;
  var index = '';
  for(i=0; i<img.length; i++){
    if( !img[i].parentNode || !img[i].parentNode.href )
      continue;

    index = img[i].parentNode.href.replace(/^http\:\/\/djdark\.ru/i, '');
    if(index.match(/(\/|\/index.html|\/index.php)/i)) {
      prnt = img[i].parentNode.parentNode;
      break;
    }
  }
  return prnt;
}

function unstopableGetParent(){
  var prnt = getParent();
  if(prnt == null){
    setTimeout('unstopableGetParent();', 1000);
    return;
  }
//  prnt.parentNode.insertBefore(window.search_node, prnt.nextSibling);
//  if ( console ) console.log(window.search_node);
  var hicon = document.getElementsByClassName('home-icon')[0];
//  if( console )
//  {
//    hicon.style.width = '450px';
//    hicon.style.display = 'block';
//    hicon.style.marginBottom = '10px';
//    hicon.children[0].style.float = 'right';
//    window.search_node.style.float = 'left';
//    window.search_node.style.left = '0px';
//    window.search_node.style.marginBottom = '10px';
    hicon.insertBefore(window.search_node, hicon.children[0]);
//  }
  ge('search_text').focus();
}

function addSearch() {
  var Oblique = document.createElement('div');
  Oblique.id = 'search';
  
  var Input = document.createElement('input');
   Input.type = 'text';
   Input.name = 'find';
   Input.id = 'search_text';
   Input.className = 'text';
   AttachEvent(Input, 'keyup', pressFilter, false);
  Oblique.appendChild(Input);
  
  var Button = document.createElement('input');
   Button.type = 'button';
   Button.id = 'search_button';
   Button.value = 'Поиск';
   Button.onclick = function(){referSearch();};
  Oblique.appendChild(Button);
  
  window.search_node = Oblique;
  unstopableGetParent();
}

window.init_search = function () {
  if(arguments.callee.done) return;
  arguments.callee.done = true;
  addCss('search/shortsch.css');
  addSearch();
};

(function () {  window.init_search();  })();

