var path = document.location.href; path = path.split('/'); path= 'http://'+path[2];

/**
 * Preload Map Imagery
 */
function preloadDirectory(e)
{
  if (document.images)
  {
    preload_image_object = new Image();
  
    // set image url
    image_url = new Array();
    image_url[0] = path+"/images/directory/business_areas/businessTraining_on.gif";
    image_url[1] = path+"/images/directory/business_areas/financialAdvice_on.gif";
    image_url[2] = path+"/images/directory/business_areas/growingEstablishedBusinessAdvice_on.gif";
    image_url[3] = path+"/images/directory/business_areas/lookingForWorkspace_on.gif";
    image_url[4] = path+"/images/directory/business_areas/networkingEvents_on.gif";
    image_url[5] = path+"/images/directory/business_areas/startupAdvice_on.gif";
    image_url[6] = path+"/images/directory/map/bg.gif";
    image_url[6] = path+"/images/directory/map/berkshire.png";
    image_url[7] = path+"/images/directory/map/berkshire_2.png";
    image_url[7] = path+"/images/directory/map/buckinghamshire_2.png";
    image_url[6] = path+"/images/directory/map/buckinghamshire.png";
    image_url[7] = path+"/images/directory/map/eastsussex_2.png";
    image_url[6] = path+"/images/directory/map/eastsussex.png";
    image_url[7] = path+"/images/directory/map/hampshire_2.png";
    image_url[6] = path+"/images/directory/map/hampshire.png";
    image_url[7] = path+"/images/directory/map/isleofwight_2.png";
    image_url[6] = path+"/images/directory/map/isleofwight.png";
    image_url[7] = path+"/images/directory/map/kent_2.png";
    image_url[6] = path+"/images/directory/map/kent.png";
    image_url[7] = path+"/images/directory/map/oxfordshire_2.png";
    image_url[6] = path+"/images/directory/map/oxfordshire.png";
    image_url[7] = path+"/images/directory/map/selectall_2.png";
    image_url[6] = path+"/images/directory/map/selectall.png";
    image_url[6] = path+"/images/directory/map/searchNowOn.gif";
    image_url[7] = path+"/images/directory/map/surrey_2.png";
    image_url[6] = path+"/images/directory/map/surrey.png";
    image_url[7] = path+"/images/directory/map/westsussex_2.png";
    image_url[6] = path+"/images/directory/map/westsussex.png";
    
    for(var i=0; i<=image_url.length; i++) 
    {
      preload_image_object.src = image_url[i];
    }
  }
}




function displayDirectoryBox(e)
{
  
}




/**
 * AREA CONTROLS
 */
function addAreaListeners(e)
{  
  if( !document.getElementsByTagName ) return;

  var allLinks = document.getElementsByTagName('a');
  var targetName;
  
  for( var i=0; i<allLinks.length; i++ )
  {
    var link = allLinks[i];
    if( link.className && ( ' '+link.className).indexOf(' dl_') != -1 )
    {
      Event.observe( $(link), 'mouseover', overArea, false );
      Event.observe( $(link), 'mouseout', outArea, false );
      Event.observe( $(link), 'click', selectArea, false );
      targetName = link.className.replace('dl_', '' );
      selectedAreas[targetName] = 0;
      $('hn_'+targetName).value = '';
    }
  }
  turnOffAllAreas();
  
  Event.observe( $('searchNow'), 'mouseover', function(){ this.src="/images/directory/map/searchNowOn.gif"; }, false );
  Event.observe( $('searchNow'), 'mouseout',  function(){ this.src="/images/directory/map/searchNow.gif"; }, false );
  
}




function selectArea(e)
{
  var target = findTarget(e);
  var targetName = target.className.replace('dl_', '' );
    
  if( selectedAreas[targetName]  != 1 )
  {
    $(target).setStyle( { background:'url(/images/directory/business-areas/' + targetName + '_on.gif) top left no-repeat', color:'#E2008B' } );
    selectedAreas[targetName] = 1;
    $('hn_'+targetName).value = targetName;
  }
  else
  {
    $(target).setStyle( { background:'url(/images/directory/business-areas/' + target.className.replace('dl_', '' ) + '.gif) top left no-repeat', color:'#78C143' } );
    selectedAreas[targetName] = 0;
    $('hn_'+targetName).value = '';
  }
  target.blur();
  
  return false;
}




function overArea(e)
{  
  var target = findTarget(e);
  var targetName = target.className.replace('dl_', '' );
  
  $(target).setStyle( { background:'url(/images/directory/business-areas/' + targetName + '_on.gif) top left no-repeat', color:'#E2008B' } );
    
  var allLinks = document.getElementsByTagName('a');
  for( var i=0; i<allLinks.length; i++ )
  {
    var link = allLinks[i];
    if( link.className && ( ' '+link.className).indexOf(' dl_') != -1 && link.className != target.className )
    {
      if( selectedAreas[ link.className.replace('dl_', '' ) ] != 1 )
      {
        link.style.background = 'url(/images/directory/business-areas/'+link.className.replace('dl_', '' )+'.gif) top left no-repeat';
        link.style.color = '#78C143';
      }
      else
      {
        link.style.background = 'url(/images/directory/business-areas/'+link.className.replace('dl_', '' )+'_on.gif) top left no-repeat';
        link.style.color = '#E2008B';
      }
    }
  }
  
}

function outArea(e)
{
  var target = findTarget(e);
  var targetName = target.className.replace('dl_', '' );
  
  if( selectedAreas[targetName] != 1 )
  {
    target.style.background = 'url(/images/directory/business-areas/' + target.className.replace('dl_', '' ) + '.gif) top left no-repeat';
    target.style.color = '#78C143';
  }  
}


function turnOffAllAreas()
{
  var allLinks = document.getElementsByTagName('a');
  for( var i=0; i<allLinks.length; i++ )
  {
    var link = allLinks[i];
    if( link.className && ( ' '+link.className).indexOf(' dl_') != -1 )
    {
      link.style.background = 'url(/images/directory/business-areas/' + link.className.replace('dl_', '' ) + '.gif) top left no-repeat';
      link.style.color = '#78C143';
    }
  }
}





/**
 * MAP CONTROLS
 */
function addMapListeners(e)
{
  if( !document.getElementsByTagName ) return;
  
  var allLocs = document.getElementsByTagName('area');
  var locName, loc, locParent;
    
  for( var i=0; i<allLocs.length; i++ )
  {
    loc = allLocs[i];
    locParent = loc.parentNode;
    if( locParent && ( ' '+locParent.name).indexOf(' mp_') != -1 )
    {
      Event.observe( $(loc), 'click', selectLoc, false );
      Event.observe( $(loc), 'mouseover', overLoc, false );
      Event.observe( $(loc), 'mouseout', outLoc, false );
        
      locName = locParent.name.replace('mp_', '' );
      selectedLocs[locName] = 0;
    }
  }
}









function overLoc(e)
{
  var target = findTarget(e);  
  var targetParent = target.parentNode;
  var targetName = targetParent.name.replace('mp_', '' );
    
  if( selectedLocs[targetName]  != 1 )
  {
    document.getElementById(targetName).src = '/images/directory/map/' + targetName + '.png';
  }
  else
  {
    document.getElementById(targetName).src = '/images/directory/map/' + targetName + '.png';
  }
}




function selectLoc(e)
{
  var target = findTarget(e);
  var targetParent = target.parentNode;
  var targetName = targetParent.name.replace('mp_', '' );

  if( selectedLocs[targetName]  != 1 )
  {
    if( targetName == 'selectall' )
    {
      selectall(1);
      document.getElementById('hn_'+targetName).value = targetName;
    }
    else
    {
      document.getElementById(targetName).src = '/images/directory/map/' + targetName + '.png';
      selectedLocs[targetName] = 1;
      document.getElementById('hn_'+targetName).value = targetName;
    }
  }
  else
  {
    if( targetName == 'selectall' )
    {
      selectall(0);
      document.getElementById('hn_'+targetName).value = '';
    }
    else
    {
      selectedLocs['selectall'] = 0;
      document.getElementById('hn_selectall').value = '';
      document.getElementById('selectall').src = '/images/directory/map/selectall.gif';
      document.getElementById(targetName).src = '/images/spacer.gif';
      selectedLocs[targetName] = 0;
      document.getElementById('hn_'+targetName).value = '';
    }
  }
  target.blur();
  
  return false;  
}


function selectall( state )
{
  if( BrowserDetect.browser.toLowerCase() == 'firefox' )
  {
    var allLocs = document.getElementsByTagName('area');

    for( var i=0; i<allLocs.length; i++ )
    {
      var target = allLocs[i];
      var targetParent = target.parentNode;
      var targetName = targetParent.name.replace('mp_', '' );
      if( state == 1 )
      {
        if( targetName == 'selectall' )
        {
          document.getElementById(targetName).src = '/images/directory/map/selectall.png';
        }
        else
        {
          document.getElementById(targetName).src = '/images/directory/map/' + targetName + '.png';
          document.getElementById('hn_'+targetName).value = targetName;
        }
        selectedLocs[targetName] = 1;
      }
      else
      {
        if( targetName == 'selectall' )
        {
          document.getElementById(targetName).src = '/images/directory/map/selectall.png';
        }
        else
        {
          document.getElementById(targetName).src = '/images/spacer.gif';
          document.getElementById('hn_'+targetName).value = '';
        }
        selectedLocs[targetName] = 0;
      }
    }
  }
  else
  {
    var allLocs = document.getElementsByTagName('img');
    
    for( var i=0; i<allLocs.length; i++ )
    {
      var target = allLocs[i];
      if( target.className && ( ' '+target.className).indexOf('map') != -1 )
      {
        var targetName = target.className.replace('map', '' ).toLowerCase();
        
        if( state ==1 )
        {
          if( targetName == 'selectall' )
          {
            target.src = '/images/directory/map/selectall.png';
          }
          else
          {
            target.src = '/images/directory/map/' + targetName + '.png';
            document.getElementById('hn_'+targetName).value = targetName;
          }
          selectedLocs[targetName] = 1;
        }
        else
        {
          if( targetName == 'selectall' )
          {
            target.src = '/images/directory/map/selectall.png';
          }
          else
          {
            target.src = '/images/spacer.gif';
            document.getElementById('hn_'+targetName).value = '';
          }
          selectedLocs[targetName] = 0;
        }
      }
    }
  }
}

function outLoc(e)
{
  var target = findTarget(e);  
  var targetParent = target.parentNode;
  var targetName = targetParent.name.replace('mp_', '' );
    
  if( selectedLocs[targetName]  != 1 )
  {
    if( targetName == 'selectall' )
    {
      document.getElementById(targetName).src = '/images/directory/map/selectall.gif';
    }
    else
    {
      document.getElementById(targetName).src = '/images/spacer.gif';
    }
  }
  else
  {
    document.getElementById(targetName).src = '/images/directory/map/' + targetName + '.png';
  }
}



/**
 * LOAD
 */
var selectedAreas = new Array();
var selectedLocs = new Array();
var highlight = 0;

document.observe("dom:loaded", preloadDirectory );
document.observe("dom:loaded", addAreaListeners );
document.observe("dom:loaded", addMapListeners );

Event.observe( window, 'load', displayDirectoryBox, false );


/**
 * DISPLAY
 */
function displayMap()
{
  document.write('<div class="map">');
  document.write('<img src="/images/spacer.gif" alt="Oxfordshire" class="mapOxfordshire" usemap="#mp_oxfordshire" id="oxfordshire" />');
  document.write('<img src="/images/spacer.gif" alt="BuckinghamShire" class="mapBuckinghamshire" usemap="#mp_buckinghamshire" id="buckinghamshire" />');
  document.write('<img src="/images/spacer.gif" alt="Berkshire" class="mapBerkshire" usemap="#mp_berkshire" id="berkshire" />');
  document.write('<img src="/images/spacer.gif" alt="Hampshire" class="mapHampshire" usemap="#mp_hampshire" id="hampshire" />');
  document.write('<img src="/images/spacer.gif" alt="Isle of Wight" class="mapIsleofwight" usemap="#mp_isleofwight" id="isleofwight" />');
  document.write('<img src="/images/spacer.gif" alt="Surrey" class="mapSurrey" usemap="#mp_surrey" id="surrey" />');
  document.write('<img src="/images/spacer.gif" alt="West Sussex" class="mapWestsussex" usemap="#mp_westsussex" id="westsussex" />');
  document.write('<img src="/images/spacer.gif" alt="East Sussex" class="mapEastsussex" usemap="#mp_eastsussex" id="eastsussex" />');
  document.write('<img src="/images/spacer.gif" alt="Kent" class="mapKent" usemap="#mp_kent" id="kent" />');
  document.write('<img src="/images/directory/map/selectall.gif" alt="Select All" class="mapSelectall" usemap="#mp_selectall" id="selectall" />');
  document.write('<map name="mp_oxfordshire"><area shape="poly" coords="57,87,46,83,34,80,20,81,10,81,2,74,2,63,1,54,1,45,6,36,9,27,14,20,18,12,26,8,31,3,37,10,30,13,33,24,48,20,50,38,44,49,52,55,62,54,66,62,62,70,65,76,69,82,62,90,57,87" href="#" alt="Oxfordshire" />');
  document.write('<map name="mp_buckinghamshire"><area shape="poly" coords="44,83,44,90,40,88,40,80,33,81,23,81,20,70,25,67,25,61,21,56,12,56,6,51,7,46,14,46,8,26,5,20,13,15,22,22,27,19,28,15,24,9,28,9,34,3,43,3,46,11,41,16,42,28,41,37,50,44,44,47,37,43,35,45,45,57,50,57,52,64,54,66,53,71,58,83,52,85,46,81" href="#" alt="Buckinghamshire" /></map>');
  document.write('<map name="mp_berkshire"><area shape="poly" coords="6,18,2,4,13,5,22,4,38,9,53,13,60,6,69,3,71,9,80,9,79,3,88,10,79,19,77,24,69,27,66,31,59,25,38,22,37,27,27,24,13,26,9,30,2,21,7,18" href="#" alt="Berkshire" /></map>');
  document.write('<map name="mp_hampshire" id="mp_hampshire"><area shape="poly" coords="53,72,45,78,37,83,33,86,21,83,13,73,12,65,9,61,5,55,11,53,22,59,28,56,28,51,26,39,22,26,29,23,32,21,37,10,44,7,68,6,84,6,97,18,91,21,91,30,97,38,91,41,88,43,83,67,83,78,63,80" href="#" alt="Hampshire" /></map>');
  document.write('<map name="mp_isleofwight" id="mp_isleofwight"><area shape="poly" coords="7,11,17,3,27,3,35,4,35,11,32,18,23,18,12,14" href="#" alt="Isle of Wight" /></map>');
  document.write('<map name="mp_surrey" id="mp_surrey"><area shape="poly" coords="8,48,4,34,11,32,10,22,17,16,28,4,42,13,58,25,68,22,72,28,74,45,57,41,43,47,27,50,13,52" href="#" alt="Surrey" /></map>');
  document.write('<map name="mp_westsussex" id="mp_westsussex"><area shape="poly" coords="56,46,37,50,24,51,15,57,5,52,4,39,5,28,10,19,17,16,29,14,41,12,52,9,60,5,66,5,78,5,75,16,74,25,68,25,65,37" href="#" alt="West Sussex" /></map>');
  document.write('<map name="mp_eastsussex" id="mp_eastsussex"><area shape="poly" coords="27,45,15,45,3,42,4,34,7,33,13,21,20,13,18,5,42,4,54,13,74,15,83,21,78,28,64,35,50,42,40,47" href="#" alt="East Sussex" /></map>');
  document.write('<map name="mp_kent" id="mp_kent"><area shape="poly" coords="71,71,58,64,41,61,30,55,22,50,8,48,4,48,4,28,8,22,10,13,17,5,27,8,33,4,54,4,66,12,74,17,107,12,110,29,108,42,90,53,80,56,78,70" href="#" alt="kent" /></map>');
  document.write('<map name="mp_selectall" id="mp_selectall"><area shape="rect" coords="0,0,109,14" href="#" alt="Select All" /></map>');
  document.write('<input type="hidden" name="loc[0]" id="hn_oxfordshire" value="" />');
  document.write('<input type="hidden" name="loc[1]" id="hn_buckinghamshire" value="" />');
  document.write('<input type="hidden" name="loc[2]" id="hn_berkshire" value="" />');
  document.write('<input type="hidden" name="loc[3]" id="hn_hampshire" value="" />');
  document.write('<input type="hidden" name="loc[4]" id="hn_isleofwight" value="" />');
  document.write('<input type="hidden" name="loc[5]" id="hn_surrey" value="" />');
  document.write('<input type="hidden" name="loc[6]" id="hn_westsussex" value="" />');
  document.write('<input type="hidden" name="loc[7]" id="hn_eastsussex" value="" />');
  document.write('<input type="hidden" name="loc[8]" id="hn_kent" value="" />');
  document.write('<input type="hidden" name="loc[9]" id="hn_selectall" value="" />');
  document.write('</div>');
}


function displayAreas()
{
  document.write('<ul>');
  document.write('<li><a class="dl_startupAdvice">Startup Advice</a></li>');
  document.write('<li><a class="dl_growingEstablishedBusinessAdvice">Growing/Established<br/>Business Advice</a></li>');
  document.write('<li><a class="dl_businessTraining">Business Training</a></li>');
  document.write('<li><a class="dl_networkingEvents">Networks and Events</a></li>');
  document.write('<li><a class="dl_lookingForWorkspace">Looking for Workspace</a></li>');
  document.write('<li><a class="dl_financialAdvice">Financial Advice</a></li>');
  document.write('</ul>');
  document.writeln('<input type="hidden" name="area[0]" id="hn_startupAdvice" value="" />');
  document.writeln('<input type="hidden" name="area[1]" id="hn_growingEstablishedBusinessAdvice" value="" />');
  document.writeln('<input type="hidden" name="area[2]" id="hn_businessTraining" value="" />');
  document.writeln('<input type="hidden" name="area[3]" id="hn_networkingEvents" value="" />');
  document.writeln('<input type="hidden" name="area[4]" id="hn_lookingForWorkspace" value="" />');
  document.writeln('<input type="hidden" name="area[5]" id="hn_financialAdvice" value="" />');
}