//***  CONFIGURATION *******//

servCount   =6;  //6
servDefault = 'news video announce jobs photo forum';



searchCount = 4;

searchURL   = 'http://www.boom.ge/search.php';
settingsURL = 'http://www.boom.ge/settings';

allURL    = Array(
        'http://www.boom.ge/search.php',
        'http://links.boom.ge/search.php',
        'http://announce.boom.ge/search.php',
        'http://news.boom.ge/geo/'
      );

boomColors = Array('#D10303','#00940A','#3A679E','#FF7E00','#FE5B00','#757575',"#E71085","#6892F3","#FAC361","#124B91","#6CA409","#000000");

//**************************//

ListVisible = false;
var Tout;

if(readCookie('js_menu') == '')
   menuSet = servDefault.split(' ');
else
   menuSet = readCookie('js_menu').split(' ');

//if(readCookie('color') in boomColors)
//    ChangeColors(readCookie('color'));


function writeCookie(cookieName, cookieContent, cookieExpireTime){
  var cookiePath = '/';
  if(cookieExpireTime>0){
    var expDate=new Date()
    expDate.setTime(expDate.getTime()+cookieExpireTime*1000*60*60)
    var expires=expDate.toGMTString()
    document.cookie=cookieName+"="+escape(cookieContent)+";path="+escape(cookiePath)+";expires="+expires+";domain=boom.ge";
  }
  else{
    document.cookie=cookieName+"="+escape(cookieContent)+";path="+escape(cookiePath)+";domain='boom.ge'";
  } 
}

function readCookie(cookieName){
  var ourCookie=document.cookie;
  if(!ourCookie || ourCookie=="")return ""
  ourCookie=ourCookie.split(";")
  var i=0;
  var Cookie;
  while(i<ourCookie.length){
    Cookie=ourCookie[i].split("=")[0];
    if(Cookie.charAt(0)==" ")
    Cookie=Cookie.substring(1);
    if(Cookie==cookieName){
      return unescape(ourCookie[i].split("=")[1])
    }
    i++;
  }
  return '';
}

function deleteCookie(cookieName){
  var cookiePath = '/';
  document.cookie=cookieName+"="+readCookie(cookieName)+";path="+escape(cookiePath)+";expires=Thu, 01-Jan-1970 00:00:01 GMT;";
}

function DrawService(id, key, position){
 key = menuSet[id-1]; 
 if(document.cookie)
   document.write('<span id="servItem'+ position + '" class="dark_gray">' + getMenuImage(id-1,key) + '</span>');
 else
   BuildButtonNonEdit(id);
}


function HideList(position){
   var MenuId = 'srv'+position;
   if(Tout!="") clearTimeout(Tout); 
   Tout=setTimeout(function(){SetVisibility(MenuId,'none')},500);
 //SetVisibility(MenuId,'none');
}


function ShowList(position){
   var MenuId = 'srv'+position;
   if(document.getElementById(MenuId).style.display=="none"){ 
      ListVisible = true;
      changeHTML(MenuId,Buildsrv(position));
      for(i=1;i<=servCount;i++){
         window.status.value= i;
         if(i!=position){
           SetVisibility('srv'+i,'none');
         } 
      }   
      SetVisibility(MenuId,'block');
   }
   else{ 
     //SetVisibility(MenuId,'none'); 
   }
}


function BuildButtonNonEdit(id,key,position){
  document.write('<span id="servItem'+ position + '" class="dark_gray">' + getMenuImage(id,key) + '</span>');
}

function changeHTML(elementId,code){
  var element=document.getElementById(elementId);
  element.innerHTML = code;
}

function getMenuImage(id, key){
  if(key){
    for(i=0;i<=MenuAllData.length;i++){
      if(MenuAllData[i]){
        if(key == MenuAllData[i][6]){
          id = i;
        }
      }
    }
  }
  return '<a class="serv_button" href="'+MenuAllData[id][0]+'"><img src="'+MenuAllData[id][1]+'" width="'+MenuAllData[id][2]+'" height="'+MenuAllData[id][3]+'" alt="'+MenuAllData[id][4]+'" title="'+MenuAllData[id][4]+'" align="center" border="0">&nbsp;'+MenuAllData[id][5]+'</a>';
} 



function SetVisibility(block,style){
  if(document.getElementById){
    document.getElementById(block).style.display = style;
  }  
}



function Buildsrv(position){
  var CurrentMenuKey = menuSet[position-1]; 
  if(document.getElementById){
    slideHTML = '<div style=" position:absolute; z-index:900'+position+';width:106px;" class="serv_list"><table cellpadding="0" cellspacing="0" border="0" width="120">';
    for(i=0;i<=MenuAllData.length;i++){
      if(MenuAllData[i]){
        elementClass = 'active';
        var inJsMenu = false;
        var menuInOutClass = 'jsmenuOut';
        for(c=0;c<=menuSet.length;c++){
          if(menuSet[c] == MenuAllData[i][6]){
            inJsMenu = true;
          }
        }
        if(CurrentMenuKey == MenuAllData[i][6]){elementClass = 'current';}
        if(inJsMenu){ menuInOutClass = 'jsmenuIn';}
        slideHTML += '<tr class="' + elementClass + '"><td class="out" onmouseover="this.className=\'over\'" onmouseout="this.className=\'out\'"  onclick="return changeService('+position+',\''+MenuAllData[i][6]+'\');return false" align="left"><span class="'+menuInOutClass+'">';
        slideHTML += MenuAllData[i][5];
        slideHTML += '</span></td></tr>';
      }
    }
    slideHTML += '</table></div>'; 
    return slideHTML; 
  }
}


function changeService(pos,key) {
  ListVisible = false;
  deleteCookie('js_menu');
  CValue = "";
  for(i=0;i<servCount;i++){ 
     if(i!=pos-1)
        CValue  = CValue  + ' ' + menuSet[i];
     else  
        CValue  = CValue  + ' ' + key; 
  } 
  CValue = CValue.substring(1);  
  writeCookie('js_menu',CValue, 77777); 
  menuSet[pos-1]=key; 
  changeHTML('servItem'+pos,getMenuImage('',key)); 
  SetVisibility('srv'+pos,'none');
}


function HideLayers(){
  if(ListVisible){ 
    for(i=1;i<=servCount;i++)
      SetVisibility('srv'+i,'none');
  }
  ListVisible = false;
  HideColors();
  HideSuggest();
}


function HideColors(){
    $('colors').innerHTML = "";
}


function $(id){
  return document.getElementById(id);
}


lc  = 0;
rId = -1;

function suggest(e){ 
  var t = $('searchBox'); 
  var s = $('suggest');
  var v = t.value;
  
  var k = GeoKBD.event.getKeyCode(e); 
  
  if(k==13){
     return;
  }
  
  if(v.length>1){
     if(lc!=v.length && k!=38 && k!=40 ){
        var a = new Ajax.Updater('suggest','/suggest.php?query='+encodeURIComponent(v),{
                        method: 'get', 
                        evalScripts: true,
                        onComplete:  function(originalRequest){
                              if(originalRequest.responseText!=''){
                                 rId = -1;
                                 s.style.display = 'block';
                              }   
                        }     
                    });
     }               
  }
  else{
    s.style.display = 'none';
  }
  
  if(s.style.display == 'block'){       
        if(k==40){      //arrow down
           walkChildNodes(s,'d');
        }
        else if(k==38){ //arrow up
          walkChildNodes(s,'u');
        }
  }
  lc=v.length;
}

function applySuggest(text){
  $('searchBox').value = text;
  document.forms['search'].submit();  
  return false;
}

function HideSuggest(){
  try{
     $('suggest').style.display = "none";
     $('suggest').innerHTML = '';
  }
  catch(e){}   
}

function hl(o){
  o.style.backgroundColor = '#f5f5f5'; 
}

function uhl(o){
  o.style.backgroundColor = '#fff';	
}

function nullit(){
  try{	
     uhl($('suggest').childNodes[rId]);	
  }
  catch(e){}   
  rId = -1; 
  
  /*
  group = $('suggest').childNodes;
  for(i = 0; i < gsize; i++){
  	 if(group[i].nodeType == 1){ 
	    if(group[i].tagName == 'DIV'){
	    	uhl(group[i]);
	    }
	 }   	
  }*/
}


function walkChildNodes(obj,d){  
	var i, group,gsize;
	
	group = obj.childNodes;
	gsize = group.length;
	
    if(d=='d'){
	   rId = (rId<gsize-1)? rId+1 : 0 ;   
	   idx = (rId==0) ? (gsize-1) : (rId-1);
	}
	else{
	   rId = (rId>0) ? (rId-1) : (gsize-1) ;
	   idx = (rId==gsize-1) ? 0 : (rId+1);
	}
	
	for(i = 0; i < gsize; i++){
		if(group[i].nodeType == 1){ 
			if(group[i].tagName == 'DIV'){	
		       if(i==rId){
				   hl(group[i]);
				   $('searchBox').value = group[i].attributes['qword'].value;
	               //uhl(group[idx]);			   
				   //break;
			   }
			   else	{
			     uhl(group[i]);
			   }	
		    }	
		}
	}
}


function SetHome(obj){
  obj.style.behavior='url(#default#homepage)'; 
  obj.setHomePage('http://www.boom.ge'); 
  return false;
}


//*****   SEARCH   *****///

function SetSearch(obj){
  for(i=1; i<=searchCount ; i++)
     document.getElementById('search'+i).className =(obj==i)?'searchActive':'searchInactive'; 
  searchURL = allURL[obj-1];   
  if(document.forms['search'].query.value!=""){
    SendRequest();
    document.forms['search'].submit();
  }
  return false;
}

function SendRequest(){
  document.forms['search'].action = searchURL;
}

//*********************//
function SaveGeo(){
 deleteCookie("geo");
 if(document.getElementById('geo').checked)
   writeCookie('geo',"1", 77777);
 else
   writeCookie('geo',"0", 77777);
   
 /* 
  elm = document.getElementById('searchBox');
  rng = elm.createTextRange();
  rng.collapse(false);
  rng.select();
  elm.click();
  elm.focus();*/
}

//*********************//

function selectMenuItem(objSelect,param){
  var sURL = objSelect.options[objSelect.selectedIndex].value;
  window.location.href = "?" + param + "=" + sURL;
  return false;
}

//*********************
function ShowColors(){
   var colors ="";
   for(i=0;i<boomColors.length;i++){
       colors += "<div style='width:16px;height:16px;margin:2px;background:"+boomColors[i]+"'>";
       colors += "<img src='http://www.boom.ge/images/dot.gif' style=\"width:12px;height:12px;border:2px solid #ccc;cursor:hand;\" onclick=\"ChangeColors('"+i+"');\"></div>";
   }
   document.getElementById('colors').innerHTML = colors; 
}



function ChangeColors(colorId){ 
  writeCookie('color',colorId, 77777);
  try{  
    document.styleSheets[0].rules[0].style.borderColor = boomColors[colorId];
    document.styleSheets[0].rules[1].style.background  = boomColors[colorId];
    document.styleSheets[0].rules[2].style.color       = boomColors[colorId];
  }
  catch(e){
    window.location.reload();
  }   
}


function setColor(){ 
  try{  
    var colorId = readCookie('color');
    document.styleSheets[0].rules[0].style.borderColor = boomColors[colorId];
    document.styleSheets[0].rules[1].style.background  = boomColors[colorId];
    document.styleSheets[0].rules[2].style.color       = boomColors[colorId];
  }
  catch(e){
     var a ='';
  }   
}

 setColor();



