///////Drop//////

var DDSPEED = 2;
var DDTIMER = 15;
var ss;
var dd_last_mouseover = null, dd_last_click = null;
var global_left_dropdown_arrow,global_right_dropdown_arrow;

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 = $(this.root_id + '_table');
  this.scroll_width = parseInt($(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 = $(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.curr_scroll_pos = parseInt(this.scrolling_table.style.left);
    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(last_pos, destination){
		if(last_pos) document.getElementById(last_pos+'-ddheader').style.backgroundImage='url(/img/restaurant/nav_bar/bg-bar.gif)';
		var tmp = $(destination + '-ddheader');
		mouseOutDropdown(tmp);
		tmp.style.backgroundImage='url(/img/restaurant/nav_bar/bg-bar_active.gif)';
		if(dd_last_click != null) dd_last_click.style.backgroundImage='url(/img/estaurant/nav_bar/bg-bar.gif)';
		dd_last_click = tmp;
		/*
		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.initScroll_NotMap = function(destination){
		document.getElementById(destination+'-ddheader').style.backgroundImage='url(/img/restaurant/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<-245){
			new_pos = new_pos + 245;
			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 = $(this.root_id + '_right');
      right_scroll.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-right-pointing.gif';
      right_scroll.style.cursor = 'pointer';
      right_scroll.onmousedown = function()
      {
        obj.startScroll(-1);
      };
      right_scroll.onmouseup = function()
      {
        obj.stopScroll();
      };
			right_scroll.onmouseover = function()
      {
        setHoverArrow('right')
      };
			right_scroll.onmouseout = function()
			{
				obj.stopScroll();
				setOutArrow('right');
			};
    }
    else
    {
      var right_scroll = $(this.root_id + '_right');
      right_scroll.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-right-pointing-dead.gif';
      right_scroll.style.cursor = 'default';
      right_scroll.onmousedown = null;
      right_scroll.onmouseup = null;
			global_right_dropdown_arrow = right_scroll.src;
    }
    
    if (this.curr_scroll_pos < 0)
    {
      var left_scroll = $(this.root_id + '_left');
      left_scroll.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-left-pointing.gif';
      left_scroll.style.cursor = 'pointer';
      left_scroll.onmousedown = function()
      {
        obj.startScroll(1);
      };
      left_scroll.onmouseup = function()
      {
        obj.stopScroll();
      };
			left_scroll.onmouseover = function()
      {
        setHoverArrow('left')
      };
			left_scroll.onmouseout = function()
			{
				obj.stopScroll();
				setOutArrow('left');
			};
    }
    else
    {
      var left_scroll = $(this.root_id + '_left');
      left_scroll.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-left-pointing-dead.gif';
      left_scroll.style.cursor = 'default';
      left_scroll.onmousedown = null;
      left_scroll.onmouseup = null;
			global_left_dropdown_arrow = left_scroll.src;
    }
  };
  
  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 = $(id + '-ddheader');
  var c = $(id + '-ddcontent');
  if (!c) return;
  var t = $('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 - 20) + 'px';
  clearInterval(c.timer);
  clearTimeout(ss);
  if(d == 1){
		mouseOverDropdown(h);
    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);mouseOutDropdown(h);},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 = $(id + '-ddheader');
  var c = $(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);
  }
}


// ismail
function ddSubMenu(elm_id, sub_id, d, pos){
  var id = elm_id.split('-', 2)[0];
	var p = $(id + '-' + sub_id + '-parentsub');
  var c = $(id + '-' + sub_id + '-subddcontent');
	var t = $(id + '-ddcontent');
  if (!c) return;
  c.style.left = (parseInt(t.style.left) + p.offsetWidth) + 'px';
	c.style.top = pos * p.offsetHeight + 'px';
  clearInterval(c.timer);
  if(d == 1){
    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';
    }
		c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    p.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// ismail
// cancel the collapse if a user rolls over the dropdown //
function cancelHideSub(id, sub_id){
  var h = $(id + '-' + sub_id + '-parentsub');
  var c = $(id + '-' + sub_id + '-subddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

function goToParent(parentLink) {
	document.location = parentLink;	
}

//ismail
function mouseOverDropdown(elm)
{
	if (dd_last_mouseover != null)
	{
		if (elm != dd_last_mouseover) mouseOutDropdown(dd_last_mouseover);
		else return false;
	}
	if (dd_last_click == elm) return false;
	var imgs = elm.getElementsByTagName('img');
	var img = imgs[0];
	//img.src = 'http://' + location.hostname + '/img/map/downarrow-active.gif';
	img.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/drop-down-hover.gif';
	
	var old_ie = is_safari = is_ie = false;
	if (navigator.appVersion.indexOf('MSIE') != -1)
	{
		is_ie = true;
		var arVersion = navigator.appVersion.split("MSIE");
		if (parseFloat(arVersion[1]) == 6) old_ie = true;
	}
	else if (navigator.appVersion.indexOf('Safari') != -1 || window.opera) is_safari = true;
	var tmp = elm;
	var first_child = tmp.childNodes[0];
	//$(id+'-ddheader').style.backgroundImage='url(/img/business_finder/nav_bar/bg-bar_active.gif)';
	tmp.style.backgroundImage='url(/img/restaurant/nav_bar/bg.gif)';
	var img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_top.gif';
	img.height = '3';
	img.width = parseInt(tmp.offsetWidth) - 1;
		//if (old_ie || is_safari) img.style.cssText = 'position: relative; top: -2px; left: 1px; border: 0px';
	if (old_ie || is_safari) img.style.cssText = 'position: relative; top: -2px; left: 1px; border: 0px';
	else img.style.cssText = 'position: relative; top: -3px; *top: -5px; left: 1px; border: 0px';
	tmp.insertBefore(img, first_child);
	img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_top-right.gif';
	img.height = 3;
	img.width = 2; 
	if (old_ie || is_safari) img.style.cssText = 'position: relative; top: -5px; left: ' + (parseInt(tmp.offsetWidth)) + 'px';
	else img.style.cssText = 'position: relative; top: -6px; *top: -7px; left: ' + (parseInt(tmp.offsetWidth)) + 'px';
	tmp.insertBefore(img, first_child);
	img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_top-left.gif';
	img.height = 3;
	img.width = 2; 
	if (old_ie || is_safari) img.style.cssText = 'position: relative; top: -5px; left: -3px';
	else img.style.cssText = 'position: relative; top: -6px; *top: -7px; left: -3px';
	tmp.insertBefore(img, first_child);
	img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_bottom.gif';
	img.height = '3';
	img.width = parseInt(tmp.offsetWidth) - 1; 
	if (is_safari) img.style.cssText = 'position: relative; top: 6px; left: 1px';
	else img.style.cssText = 'position: relative; top: 5px; *top: 6px !important; left: 1px';
	tmp.appendChild(img);
	img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_bottom-right.gif';
	img.height = 3;
	img.width = 2; 
	if (is_safari) img.style.cssText = 'position: relative; top: 3px; left: ' + (parseInt(tmp.offsetWidth)) + 'px';
	else img.style.cssText = 'position: relative; top: 2px; *top: 3px; left: ' + (parseInt(tmp.offsetWidth)) + 'px';
	tmp.appendChild(img);
	img = document.createElement('img');
	img.src = 'http://' + location.hostname + '/img/business_finder/nav_bar/shadow_bottom-left.gif';
	img.height = 3;
	img.width = 2; 
	if (is_safari) img.style.cssText = 'position: relative; top: 3px; left: -3px';
	else img.style.cssText = 'position: relative; top: 2px; *top: 3px; left: -3px';
	tmp.appendChild(img);
	
	if (is_ie && tmp.nextSibling)
	{
		var right_border = tmp.nextSibling.childNodes[0];
		right_border.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/border_right.gif';
	}
	else if (!is_ie && tmp.nextSibling.nextSibling)
	{
		var right_border = tmp.nextSibling.nextSibling.childNodes[0];
		right_border.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/border_right.gif';
	}
	
	dd_last_mouseover = elm;
}

function mouseOutDropdown(elm)
{
	if (dd_last_mouseover == null) return false;
	var is_ie = false;
	if (navigator.appVersion.indexOf('MSIE') != -1) is_ie = true;
	var tmp = elm;
	var imgs = tmp.getElementsByTagName('img');
	if (imgs.length == 1) return false;
	tmp.style.backgroundImage='url(http://' + location.hostname + '/img/restaurant/nav_bar/bg-bar.gif)';
	tmp.removeChild(imgs[6]);
	tmp.removeChild(imgs[5]);
	tmp.removeChild(imgs[4]);
	imgs[3].src = 'http://' + location.hostname + '/img/restaurant/nav_bar/downarrow-bar.gif';
	tmp.removeChild(imgs[2]);
	tmp.removeChild(imgs[1]);
	tmp.removeChild(imgs[0]);
	
	if (is_ie && tmp.nextSibling)
	{
		var right_border = tmp.nextSibling.childNodes[0];
		right_border.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-divider.gif';
	}
	else if (!is_ie && tmp.nextSibling.nextSibling)
	{
		var right_border = tmp.nextSibling.nextSibling.childNodes[0];
		right_border.src = 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-divider.gif';
	}
	
	dd_last_mouseover = null;
}

//lavinia
function setHoverArrow(type)
{
	if(type=="left")
	{
		global_left_dropdown_arrow = $('moving_menu_left').src;
		if($('moving_menu_left').src != 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-left-pointing-dead.gif') 
			$('moving_menu_left').src = 'http://' + location.hostname + '/img/restaurant/nav_bar/left_arrow_hover.gif';
	}
	else
	{
		global_right_dropdown_arrow = $('moving_menu_right').src;
		if($('moving_menu_right').src != 'http://' + location.hostname + '/img/restaurant/nav_bar/bar-right-pointing-dead.gif') 
			$('moving_menu_right').src = 'http://' + location.hostname + '/img/restaurant/nav_bar/rightarrow_hover.gif';
	}
}

//lavinia
function setOutArrow(type)
{
	if(type=="left")
		$('moving_menu_left').src = global_left_dropdown_arrow;
	else
		$('moving_menu_right').src = global_right_dropdown_arrow;
}

// ismail
function setActiveDropdown_resto(elm,cat,catId, type)
{
	clearActiveDropdown_property();
	var dd = elm.parentNode.parentNode.parentNode;
	var id = dd.id.split('-', 2)[0];
	global_dropdown = id;
	
	var dd_menu = new moving_menu('moving_menu');
	dd_menu.initScroll(last_dropdown_position, id);
	last_dropdown_position = id;
	displayGreyText(cat,catId, type);
}

// ismail
function clearActiveDropdown_property()
{
	if (global_dropdown)
	{
		global_dropdown = undefined;
	}
}

function displayGreyText(cat, catId, type, businessId){
	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x)
		{		
			x.onreadystatechange = function()
			{
				if (x.readyState == 4 && x.status == 200)
				{			
					document.getElementById("ajaxChangeTextHeader").innerHTML = x.responseText;
				} else {
					// loaind
					document.getElementById("ajaxChangeTextHeader").innerHTML = "<div style='width:250px;float:left;' align='center' class='TextBold'>LOADING</div>";
				}
			};
			
			var param = document.location.href;				
			var url = "http://"+location.hostname+"/restaurants/singapore/reviews/lib/sdNavBar/function_header_changes_ajax.php?cat="+cat+"&catId="+catId+"&type="+type+"&businessId="+businessId;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}