var countryInterval = "";

//For Login Box
function showDropMenu1() {
    obj = document.getElementById('dropMenu1');
    obj.style.display='block';
    obj = document.getElementById('tblDropMenu1');
    obj.style.display='block';
}
//For Resto Submit
function showDropSubmit() {
    obj = document.getElementById('dropSubmit');
    obj.style.display='block';
    obj = document.getElementById('tblDropSubmit');
    obj.style.display='block';
}
//For Resto Submit
function hideDropSubmit() {
    obj = document.getElementById('dropSubmit');
    obj.style.display='none';
    obj = document.getElementById('tblDropSubmit');
    obj.style.display='none';
}
//For related Keyword
function relatedPlace(id) {
	if(id==1)
	{
		document.getElementById("hideRelated").style.display = "none";
		document.getElementById("showRelated").style.display = "block";
	}
	else
	{
		document.getElementById("hideRelated").style.display = "block";
		document.getElementById("showRelated").style.display = "none";
	}
}
//For Country Box
function showMenuCountry(type,interval)
{
	//alert(type);
	if(type == "show")
	{
		document.getElementById("divCountryBox").style.display = "block";
		//countryInterval = setTimeout('showMenuCountry("hide",'+interval+')',interval);
	}
	else
	{
		document.getElementById("divCountryBox").style.display = "none";
		//clearInterval(countryInterval);
	}
}

function JobSearch() {
	var JobForm = document.frmJobs;
	if (!JobForm.q.value) {
	  alert ('Please Fill The Search Box');
	  JobForm.q.focus();
	  return false;
	}
	
	if (JobForm.q.value.length<=0) {
	  alert ('Please Fill The Search Box');
	  JobForm.q.focus();
	  return false;
	}
	
	if (JobForm.by[0].checked==false && JobForm.by[1].checked==false && JobForm.by[2].checked==false) {
	  alert ('Please Select Jobs Title, Location, or Company');
	  return false;
	}

	/*if (JobForm.by[0].checked==true) {
		JobForm.action ="<?=$sdGloBaseUrl?>/jobs/singapore/search/title/";
		JobForm.submit();
		return true;
	} else if (JobForm.by[1].checked==true) {
		JobForm.action ="<?=$sdGloBaseUrl?>/jobs/singapore/search/location/";
		JobForm.submit();
		return true;
	} else if (JobForm.by[2].checked==true) {
		JobForm.action ="<?=$sdGloBaseUrl?>/jobs/singapore/search/company/";
		JobForm.submit();
		return true;
	}*/
	JobForm.action = '/jobs/singapore/search/';
	JobForm.submit();
	return true;
}

///////Drop//////

var DDSPEED = 2;
var DDTIMER = 15;
var ss;

function menu(name, left, right)
{
	this.name = name;
  this.left = left;
  this.right = right;	
}

function moving_menu(elm_id)
{
  this.root_id = elm_id;
  this.num_menu = 0;
  this.scroll_timer;
  this.curr_scroll_pos = 0;
  this.dest_scroll_pos = 0;
  this.scrolling_table = document.getElementById(this.root_id + '_table');
  this.scroll_width = parseInt(document.getElementById(this.root_id + '_scroll').style.width);
  this.menus = new Array();
  this.right_menu = 0;
  this.total_width = 0;
  this.divider_width = 2;
  this.scroll_timer = false;
  this.move_direction = 0;
  
  this.init = function()
  {
    var elmt = document.getElementById(this.root_id + '_div');
    this.num_menu = elmt.getElementsByTagName('dl').length;
    var tds = this.scrolling_table.getElementsByTagName('th');
    var left = this.scrolling_table.style.left;
    if (left == '') left = 0;
    var init_right = parseInt(left) + this.scroll_width;
    for (var i = 0; i < tds.length; i++)
    {
			tds[i].onmouseover = function() {ddMenu(this.id, 1)};
      tds[i].onmouseout = function() {ddMenu(this.id, -1)};
      var width = parseInt(tds[i].width) + this.divider_width;
      var left = this.total_width;
      var right = this.total_width + width - 1;
			var name = tds[i].id;
      if (init_right >= left && init_right <= right) this.right_menu = i;
      this.menus.push(new menu(name, left, right));
      this.total_width += width;
    }
    this.updateNavigation();		
  };
	
	this.getActivePos = function (active_name){
		var pos = 0;
		for (var i = 0; i < this.menus.length; i++){
			menu_name = this.menus[i].name;
			pos = this.menus[i].right;
			if(active_name == menu_name){				
				//alert(active_name+" "+ menu_name)
				break;
			}
    }
		return pos;
	}
	
	this.initScroll = function(destination){
		document.getElementById(destination+'-ddheader').style.backgroundImage='url(/img/jobs/nav_bar/bg-bar_active.gif)';
		active_width = document.getElementById(destination+'-ddheader').width;
		new_pos = this.getActivePos(destination+'-ddheader');
		new_pos = (new_pos - (active_width) - this.divider_width) * -1;
		//alert(new_pos);
		if(new_pos<-260){
			new_pos = new_pos + 260;
			this.move_direction = -1;
			if (!this.scroll_timer)
			{
				var obj = this;
				this.scroll_timer = setInterval(function()
					{
						obj.scroll(50);
						if(obj.curr_scroll_pos < new_pos){
							obj.stopScroll();
						}
					}, 1
				);
			}
		}
	}
  
  this.updateNavigation = function()
  {
    var obj = this;
    if (this.scroll_width - this.curr_scroll_pos < this.total_width)
    {
      var right_scroll = document.getElementById(this.root_id + '_right');
      right_scroll.src = 'http://' + location.hostname + '/img/jobs/nav_bar/bar-right-pointing.gif';
      right_scroll.style.cursor = 'pointer';
      right_scroll.onmousedown = function()
      {
        obj.startScroll(-1);
      };
      right_scroll.onmouseup = function()
      {
        obj.stopScroll();
      };
    }
    else
    {
      var right_scroll = document.getElementById(this.root_id + '_right');
      right_scroll.src = 'http://' + location.hostname + '/img/jobs/nav_bar/bar-right-pointing-dead.gif';
      right_scroll.style.cursor = 'default';
      right_scroll.onmousedown = null;
      right_scroll.onmouseup = null;
    }
    
    if (this.curr_scroll_pos < 0)
    {
      var left_scroll = document.getElementById(this.root_id + '_left');
      left_scroll.src = 'http://' + location.hostname + '/img/jobs/nav_bar/bar-left-pointing.gif';
      left_scroll.style.cursor = 'pointer';
      left_scroll.onmousedown = function()
      {
        obj.startScroll(1);
      };
      left_scroll.onmouseup = function()
      {
        obj.stopScroll();
      };
    }
    else
    {
      var left_scroll = document.getElementById(this.root_id + '_left');
      left_scroll.src = 'http://' + location.hostname + '/img/jobs/nav_bar/bar-left-pointing-dead.gif';
      left_scroll.style.cursor = 'default';
      left_scroll.onmousedown = null;
      left_scroll.onmouseup = null;
    }
  };
  
  this.startScroll = function(direction)
  {
    this.move_direction = direction;
    if (!this.scroll_timer)
    {
      var obj = this;
      this.scroll_timer = setInterval(function()
        {
          obj.scroll();
        }, 10
      );
    }
  };
  
  this.stopScroll = function()
  {
    if (this.scroll_timer)
    {
      clearInterval(this.scroll_timer);
      this.scroll_timer = false;
      this.updateNavigation();
    }
  };
  
  this.scroll = function(interval)
  {
		if(interval)
			var move_distance = interval * this.move_direction;
		else
	    var move_distance = this.getMoveDistance() * this.move_direction;
    this.curr_scroll_pos += move_distance;
    if (this.move_direction < 0)
    {
      if (this.scroll_width - this.curr_scroll_pos > this.total_width) this.curr_scroll_pos = this.scroll_width - this.total_width;
      if (this.scroll_width - this.curr_scroll_pos > this.menus[this.right_menu].right && this.right_menu < this.menus.length - 1) ++this.right_menu;
      if (this.scroll_width - this.curr_scroll_pos == this.total_width) this.stopScroll();
    }
    else if (this.move_direction > 0)
    {
      if (this.curr_scroll_pos > 0) this.curr_scroll_pos = 0;
      if (this.scroll_width - this.curr_scroll_pos < this.menus[this.right_menu].left) --this.right_menu;
      if (this.curr_scroll_pos == 0) this.stopScroll();
    }
    this.scrolling_table.style.left = this.curr_scroll_pos + 'px';
  };
  
  this.getMoveDistance = function()
  {
    return 5;
  };
  
  this.init();
}

// main function to handle the mouse events //
function ddMenu(elm_id,d){
  var id = elm_id.split('-', 2)[0];
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  if (!c) return;
  var t = document.getElementById('moving_menu_table');
  var left_t = t.style.left;
  if (left_t == '') left_t = 0;
  else left_t = parseInt(left_t);
  c.style.left = (0 + h.offsetLeft + left_t) + 'px';
  clearInterval(c.timer);
  clearTimeout(ss);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    ss = setTimeout(function() {
      c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
      }, 300
    );
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

function goToParent(parentLink) {
	document.location = parentLink;	
}

