var timeout_id = 0;

function getServiceContent(ul, link_content, check_count, blank_target)
{
  var div = new Element('div', { 'class': 'service_container' });
  var link = new Element('a', { 'href': 'javascript:;' });
  
  link.update(link_content);
  
  if (blank_target) {
  	link.writeAttribute('target', '_blank');
  }
  
  ul.addClassName('service_content');
  ul.hide();
  
  var content_links = ul.select('li a');
  
  if (check_count && content_links.length == 1)
  {
    link.href = content_links[0].href;
    div.update(link);
    
    return div;
  }
  
  link.observe('mouseover', function () {
  	clearTimeout(timeout_id);
  	$$('.service_container').invoke('removeClassName', 'current');
  	$$('.service_content').invoke('hide');
  	
    ul.toggle();
    
    if (ul.visible())
    {
      div.addClassName('current');
    }
    else
    {
  	  $$('.service_container').invoke('removeClassName', 'current');
    }
  });
  link.observe('mouseout', function () {
    timeout_id = setTimeout(function () {
      ul.hide();
      $$('.service_container').invoke('removeClassName', 'current');
    }, 200, this);
  });
  link.observe('focus', function () {
    ul.show();
    div.addClassName('current');
  });
  link.observe('blur', function () {
    ul.hide();
    div.removeClassName('current');
  });
  link.observe('click', function () {
  	clearTimeout(timeout_id);
    
    if (ul.visible())
    {
  	  $$('.service_content').invoke('hide');
  	  $$('.service_container').invoke('removeClassName', 'current');
    }
    else
    {
      ul.show();
      div.addClassName('current');
    }
  });
  
  ul.observe('mouseover', function () {
  	clearTimeout(timeout_id);
  });
  ul.observe('mouseout', function () {
    timeout_id = setTimeout(function () {
      ul.hide();
      $$('.service_container').invoke('removeClassName', 'current');
    }, 200, this);
  });
  
  div.update(link);
  
  return div;
}

function getCloseContent(ul, link_content)
{
  var div = new Element('div', { 'class': 'close' });
  var link = new Element('a', { 'href': 'javascript:;' });
  
  link.update(link_content);
  
  link.observe('click', function () {
    ul.hide();
    $$('.service_container').invoke('removeClassName', 'current');
  });
  
  div.update(link);
  
  return div;
}

function initServiceLinks()
{
  var relative_url_path = '/';
 
  if (location.href.indexOf('.dev.endertech') != -1)
  {
    relative_url_path = '/tvtango/';
  }
  
 
  $$('.downloads ul:not(.applied)').each(function (ul) {
  	var img = new Element('img', { 'src': relative_url_path + 'images/button_download.png', 'alt': 'Download' });
  	
    new Insertion.Before(ul, getServiceContent(ul, img));
    new Insertion.Top(ul, getCloseContent(ul, 'Close'));
    ul.addClassName('applied');
  });
  $$('.dvds ul:not(.applied)').each(function (ul) {
  	var img = new Element('img', { 'src': relative_url_path + 'images/button_dvd.png', 'alt': 'DVD' });
  	
    new Insertion.Before(ul, getServiceContent(ul, img, true, true));
    //new Insertion.Top(ul, getCloseContent(ul, 'Close'));
    ul.addClassName('applied');
  });
  $$('.dvd_releases ul:not(.applied)').each(function (ul) {
	
  	var img = new Element('img', { 'src': relative_url_path + 'images/find_dvd.png', 'alt': 'DVD' });
  	
    new Insertion.Before(ul, getServiceContent(ul, img, true));
    new Insertion.Top(ul, getCloseContent(ul, 'Close'));
    ul.addClassName('applied');
    // in case hiding of flash of unstyled content (FOUC) is applied in template
    $(ul.parentNode).show();
  });
}

Event.observe(window, 'load', initServiceLinks);