/* Event.observe(window, "load", function(){
 get_selection('region','region','');
    });
*/    
var region = -1;
var new_x_place = '';
var new_x_country = '';
 

function hover(elem)
{

   par = $(elem).parentNode; 
    $A(par.getElementsByTagName('li')).each(function(s) {$(s).removeClassName('hover')});
    elem.className = 'hover';
}


function remove_selection(selectbox)
{
    if($(selectbox))
    {
        while($(selectbox).hasChildNodes())
            $(selectbox).removeChild($(selectbox).lastChild)
    }
}

function get_selection(selectbox,childselectbox,parentid) {
    
    //alert('direct-selection.php?parent='+selectbox+'&child='+childselectbox+'&id='+parentid+'&region_id='+region)   
    if (selectbox == 'region')
    {
        region = parentid;
        remove_selection($('place'));
        remove_selection($('country'));
    }
    else
        remove_selection($('place'));
    
    var li_sel ='';

       
    new Ajax.Request('direct-selection.php?parent='+selectbox+'&child='+childselectbox+'&id='+parentid+'&region_id='+region, {
      method: 'get',
      onSuccess: function(transport) {
        var response = transport.responseText || "no response text";
        if(eval(response)) {
            var my_options = $A(eval(response));
            var count = 0;
            $A(my_options).each(function(s, index) {
              var tmp = $A(s);
              //var li = document.createElement('li');
              //var txt = document.createTextNode(tmp[1]);
              
              /*
              if (childselectbox == 'region')
                  li.setAttribute('onClick','hover(this); get_selection(\'region\',\'country\','+tmp[0]+');');
              else if (childselectbox == 'country')
                  li.setAttribute('onClick','hover(this); get_selection(\'country\',\'place\','+tmp[0]+');');
              else if (childselectbox == 'place')
                  li.setAttribute('onClick','location.href=\'front_content.php?idart=3&lat='+tmp[2]+'&lng='+tmp[3]+'&zoom='+tmp[4]+'\'');
                  
               li.appendChild(txt);
              */
              
              if (childselectbox == 'region')
                  li_sel += '<li onClick="hover(this); get_selection(\'region\',\'country\','+tmp[0]+','+tmp[0]+')">';
              else if (childselectbox == 'country')
                      li_sel += '<li onClick="hover(this); get_selection(\'country\',\'place\','+tmp[0]+','+region+')">';        
                       
              else if (childselectbox == 'place')
              {
                  if (tmp[5] != '')
                      li_sel += '<li onClick="hover(this); location.href=\''+tmp[5]+'\'">';
                  else 
                      li_sel += '<li onClick="hover(this); location.href=\''+ host +'/front_content.php?idart=3&lat='+tmp[2]+'&lng='+tmp[3]+'&zoom='+tmp[4]+'\'">'; 
              }   

               li_sel += tmp[1] + '</li>';     
               
             // $(childselectbox).appendChild(li);
                  
            });  
            
            $(childselectbox).innerHTML = li_sel;
            w = $('region').getWidth() + $('country').getWidth() + $('place').getWidth() + 60;
            
            if (w > $('direct-access-wide').getWidth())
            {
                $('goback').setStyle({ 'display': 'block'});
            }
            
             
            $('direct-access-wide').setStyle('width:'+w+'px');
            
            if (childselectbox == 'place')
            {
                x_place = $('region').getWidth()*-1 + $('country').getWidth()*-1;
                new Effect.Move('direct-access-wide', { x: x_place, y: 0, mode: 'absolute' });
            }
            
            if (childselectbox == 'country')
            { 
               x_reg = $('region').getWidth()*-1;
              new Effect.Move('direct-access-wide', { x: x_reg , y: 0, mode: 'absolute' });
              //$('direct-access-wide').scrollLeft = x;
            }
            

        }
        else { alert ("there was a problem in the AJAX call to retrieve servers") };
      }

    });
}

document.observe('dom:loaded',function(){
   get_selection('region','region','');
  
   if ($('goback'))
   {
       $('goback').observe('mouseover', function(event) {
            new Effect.Move('direct-access-wide', { x: 0 , y: 0, mode: 'absolute' });
        });
   } 
    
});

preloadImg = function(images)
{
    _img = new Array();
    for(i=0;i<images.length;i++)
    {
        _img[i] = new Image();
        _img[i].source = host + images[i];
    }

}


function StripTags(str){
 return str.replace(/<\/?[^>]+>/gi, '');
}




