﻿var href = function(current) {  //get the href of the clicked link (the id if the div to display)
// start of fix
if(current.length == 0) { return "se_asia"; }
if(current.length > 1) { var active = [current[0]]; current = active; }
// end of fix
if(current.length == 1) {
  if(current[0].nodeName == "LI") {
    var link = $(current).children("h4").children("a").attr("href");
  }
} else if(current.nodeName == "A") {
  var link = $(current).attr("href");
}

  var place = link.indexOf("#");
  link = link.substring(place+1,link.length);
  return link;  
}

var displayinfo = function(link) {  //display the div of the sent id (link)
  $("#regions div.sub-regions-js").css({ "display":"none" });
  $("#"+link).css({ "display":"block" });
}

var setup = function() { // change the styles of the elements to show/hide
  var divs = $("#regions div.sub-regions");
  $(divs).removeClass("sub-regions");
  $(divs).addClass("sub-regions-js");
}

var regions = function() {
  setup();
  var version = $("#regions > div");
  if(version.length == 0) { //if locations page
  
    var active = $("#regions li.active");
    var newid = href(active);
    displayinfo(newid);
    
  } else { //if contacts page
    
    var newid = version[0].id;
    displayinfo(newid);
  }
  
  var links = $("#regions ul > li > h4 > a");
  
  $(links).click(function() { 
    
    var thisid = href(this);
    displayinfo(thisid);
    $("#regions li.active").removeClass("active");
    $(this).parent("h4").parent("li").addClass("active");
    return false;
  });
}