var saved_last_pos = '',curr_dd_page =1;
function createLocationBoxDD(content)
{
	if (!$('get_location_box_dd'))
	{	
 	 var parent_div = $('imgDiv');
	 var box = document.createElement("div");
	 box.align = 'left';
	 box.id = 'get_location_box_dd';	 
	 box.style.cssText = "position: absolute; top:0px; z-index:1000;";
	 box.innerHTML = content;
	 parent_div.appendChild(box);
	}
}
 
function getLocationBoxDD(type)
{
	if ($) {
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x)
		{		
			x.onreadystatechange = function()
			{ 
				if (x.readyState == 4 && x.status == 200)
				{			
					closeLoadingLocationBoxDD();
					createLocationBoxDD(x.responseText,type);
				} else
				{					
					showLoadingLocationBoxDD(type);
				}
			};								
			var last_pos = getLastPos();
			var original_pos = getOriginalPos();			
			var str_same = '';
			if (last_pos.x == original_pos.x && last_pos.y == original_pos.y) str_same ='&same=1';
			saved_last_pos = last_pos;
			last_pos = last_pos.x+","+last_pos.y;
			var level = MapAPI.mapClient.levelIndex;
			var url = "http://"+location.hostname+"/routing/get_location_code.php?last_pos="+last_pos+"&level="+level+"&type="+type+str_same;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}

function showLoadingLocationBoxDD(type)
{
	if ($('loading_box_dd'))
	{
		$('loading_box_dd').style.display = 'block';
	} else
	{
		var loading_box = document.createElement('div');
		loading_box.id = 'loading_box_dd';
		loading_box.style.cssText = "position:absolute; top:0px; right:0px; width:375px; z-index:1000; height:80px; overflow:hidden;  background-color:#FFFFFF; border:1px solid #CCCCCC;";
		var img = document.createElement('img');
		img.src = 'http://'+location.hostname+'/img/map/loading.gif';
		img.style.cssText = "position:absolute; top:-120px; left:10px;";
		loading_box.appendChild(img);
		
		var close_btn = document.createElement('div');
		close_btn.style.cssText = "position:absolute; top:5px; right:-350px; width:375px; z-index:1000; height:80px;";
		close_btn.innerHTML = '<a href="javascript:void(0);" onclick="javascript:closeLoadingLocationBoxDD();">[x]</a>';
		loading_box.appendChild(close_btn);
		
		var parent_div = $('imgDiv');
		parent_div.appendChild(loading_box);
	}
}

function closeLoadingLocationBoxDD()
{
	if ($('loading_box_dd'))
	{
		$('loading_box_dd').style.display = 'none';
	}
}

function getOriginalPos()
{
	return ($('original_pos').innerHTML).replace(" ","");
}

function openLocationBox(type)
{
	if (saved_last_pos == getLastPos())
	{	
		showLocationBox();		
	} else
	{
		if ($('get_location_box')) removeElement('get_location_box');
		getLocationBox(type);		
	}
}

function openLocationBoxDD(type)
{
	if (saved_last_pos == getLastPos())
	{	
		showLocationBoxDD();		
	} else
	{
		if ($('get_location_box_dd')) removeElement('get_location_box_dd');
		getLocationBoxDD(type);		
	}
}


function removeElement(id)	{
	var Node = $(id);	
	Node.parentNode.removeChild(Node);
}

function closeLocationBoxDD()
{
	if ($('get_location_box_dd'))
	{
		$('get_location_box_dd').style.display = 'none';
	}
}

function showLocationBox()
{
	if ($('get_location_box'))
	{
		$('get_location_box').style.display = 'block';
	}
}

function getLastPos()
{
	var vertex = $("last_pos").innerHTML;
	vertex = vertex.split(",");
	return new Vertex(vertex[0],vertex[1]);
}
	
	
	function jumpToPage(p)
	{
		if (p != 1)	document.frmRouting.action += p + '/';
		document.frmRouting.submit();
		return false;
	}
	
	function showTitle(status)
	{
		if (status == 1)
		{
			$('address_info').style.display = 'block';
			$('show_indicator').style.display = 'none';
			$('hide_indicator').style.display = 'block';
		} else
		{
			$('address_info').style.display = 'none';
			$('show_indicator').style.display = 'block';
			$('hide_indicator').style.display = 'none';
		}
	}
	
	function showTopDestination(status)
	{
		if (status == 1)
		{
			$('top_destination').style.display = 'block';
			$('hide_top_destination').style.display = 'none';
			$('show_top_destination').style.display = 'block';
		} else
		{
			$('top_destination').style.display = 'none';
			$('hide_top_destination').style.display = 'block';
			$('show_top_destination').style.display = 'none';
		}
	}
	
	function setCenter(x,y)
	{
		MapAPI.mapClient.setCenterVertex(new Vertex(x,y));
		MapAPI.mapClient.DrawMap();
	}

function go_to_page(p,url,id)
{
	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)
				{			
					$('dd_detail').innerHTML = x.responseText;
					if (id)
					{
					  var elm = $('trNav' + id);
					  if (elm != null) 
					  {
						elm.bgColor = '#ffffaa';
						if ($('map_icon_'+id))
							$('map_icon_'+id).src ='/img/dd_search/digit/'+id+'_red.gif';
						$('img_num_'+id).src = '/img/dd_search/digit/'+id+'_red.gif';
					  }  
					}
				} else
				{
					if (!id)
					{
						showLoading('dd_detail',100,80);
					}
				}
			};											
			var url = "http://"+location.hostname+"/routing/dd_detail.php?p="+p+"&url_alias="+url;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}

function showLoading(parent_id,top,left)
{
	if (!$('loading_bar'))
	{
		var parent = $(parent_id);
		parent.innerHTML = '';
		var div = document.createElement('div');	
		div.id = "loading_bar";
		div.style.position = 'relative';
		var img = document.createElement('img');	
		img.src = 'http://'+location.hostname+'/img/map/loading2.gif';
		img.style.cssText = "position:absolute; top:"+top+"px; left:"+left+"px;";
		div.appendChild(img);
		parent.appendChild(div);
	}
}
		
function routingNodeMouseOver(id)
{
  var p = Math.ceil(id / 6);
  if (p != curr_dd_page)
  {
	  go_to_page(p,url_alias,id);
	  curr_dd_page = p;
  } else
  {
	  var elm = $('trNav' + id);
	  if (elm != null) 
	  {
		elm.bgColor = '#ffffaa';
		if ($('map_icon_'+id))
			$('map_icon_'+id).src ='/img/dd_search/digit/'+id+'_red.gif';
		$('img_num_'+id).src = '/img/dd_search/digit/'+id+'_red.gif';
	  }  
  }
}

function routingNodeMouseOut(id)
{
  var elm = $('trNav' + id);
  if (elm != null) 
  {
	elm.bgColor = '#ffffff';
	if ($('map_icon_'+id))
		$('map_icon_'+id).src ='/img/dd_search/digit/'+id+'.gif';
	$('img_num_'+id).src = '/img/dd_search/digit/'+id+'.gif';
  }
}


 
function submit_dd(type)
{
	var f = document.search_form;
	var from = f.new_link_id_1.value;	
	var str_from = f.dd_from.value;	
	var to =  f.new_link_id_2.value;
	var str_to = f.dd_to.value;
	
	if (from == 0 && str_from != '' || to == 0 && str_to != '')
	{
		if(str_from==""){
			alert('Please input start address');	
			f.dd_from.focus();
		}else if(str_to==""){
			alert('Please input end address');
			f.dd_to.focus();
		}else{
			checkValidAddress(str_from,str_to,from,to);
		}		
		
	} else	{
		if ( str_from == '')
		{
			alert('Please input start address');	
			f.dd_from.focus();
		} else if (str_to== '')
		{
			alert('Please input end address');
			f.dd_to.focus();
		}else if (from == to)
		{
			alert('Start address and input address can not be same');
			f.dd_from.focus();
		} else 
		{
			var pref,method_drive = f.method_drive;
			for(var i=0;i<4;i++)
			{
				if (method_drive[i].checked == true)
				{
					if (method_drive[i].value == "no_weight") pref =  "shortest_distance";
					else if (method_drive[i].value == "short_travel") pref = "major_roads";
					else if (method_drive[i].value == "nearby_exp") pref = "nearest_expressway";
					else pref = method_drive[i].value;
				}
			}
			if (pref == "certain"){
				alert('Please select your preferred expressway.');
			}else{
				var url = "http://"+location.hostname+"/routing/automobile_guide/"+from+"_to_"+to+"/"+pref.toLowerCase()+"/";
				location.href = url;
			}
		}
		
		
	}

}

function setEmpty(type){
	var f = document.search_form;
	if(type=="from"){		
		f.new_link_id_1.value = 0;	
	}else{
		f.new_link_id_2.value = 0;
	}
}

function checkValidAddress(address,address2,link_id_from,link_id_to)
{
	var f = document.search_form;
	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)
				{	
				
					var pref,method_drive = f.method_drive;
					for(var i=0;i<4;i++)
					{
						if (method_drive[i].checked == true)
						{
							if (method_drive[i].value == "no_weight") pref =  "shortest_distance";
							else if (method_drive[i].value == "short_travel") pref = "major_roads";
							else if (method_drive[i].value == "nearby_exp") pref = "nearest_expressway";
							else pref = method_drive[i].value;
						}
					}
					
					result = x.responseText;	
					arr_result = result.split("_");	
					from = arr_result[0];
					to = arr_result[1];
					if(result!=""){
						if (pref == "certain"){
							alert('Please select your preferred expressway.');
						}else{
							if(from>0 && to>0)	{
								var url = "http://"+location.hostname+"/routing/automobile_guide/"+from+"_to_"+to+"/"+pref.toLowerCase()+"/";
								location.href = url;
							}else{
								if(from==""){
									alert('Sorry, your start address can not be found.');
								}else{
									alert('Sorry, your end address can not be found.');
								}
							}
						}
					}else{
						alert('Please enter your address correctly.');
					}
			
					
					return true;
				} else
				{
					//showLoadingLocationBox(type);
				}
			};						
			address = encodeURIComponent(address); 
			address = address.replace(/%20/gi,"+");
			
			address2 = encodeURIComponent(address2); 
			address2 = address2.replace(/%20/gi,"+");
			var url = "http://"+location.hostname+"/routing/get_valid_address.php?address="+address+"&address2="+address2+"&link_id_from="+link_id_from+"&link_id_to="+link_id_to;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}

function swap_location()
{
	var f = document.search_form;
	var from = f.new_link_id_1.value;	
	f.new_link_id_1.value = f.new_link_id_2.value;
	f.new_link_id_2.value = from;
	
	from = f.dd_from.value;
	f.dd_from.value = f.dd_to.value;
	f.dd_to.value = from;

}

function selectThis(obj)
{
	obj.style.backgroundColor='#3366CC';
	obj.style.color= '#FFFFFF';
}

function deSelectThis(obj)
{
	obj.style.backgroundColor='';
	obj.style.color= '#000000';
}

function str_replace(search, replace, subject) { 
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length; 
    while (j = 0, i--) {
		if (s[i])
		{
        	while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
    };
     
    return sa ? s : s[0];
}

// lavinia
function close_tweet()
{
	if ($('tweet_form')) $('tweet_form').style.display = 'none';
}
// lavinia
function show_tweet()
{	
	if ($('tweet_form'))
	{
		$('tweet_form').innerHTML = global_tweet_form;	
		$('tweet_form').style.display = 'block';
	} else
	{
		var tweet_form = document.createElement('div');
		tweet_form.id = 'tweet_form';
		tweet_form.style.cssText = 'position:absolute;left:110px;top:0px;z-index:20;';
		var parent = $('imgDiv');
		parent.appendChild(tweet_form);
		
		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)
					{			
						global_tweet_form = x.responseText;
						$('tweet_form').innerHTML = x.responseText;
					} else
					{
						$('tweet_form').innerHTML = "<b>Loading...</b>";
					}
				};
				var url = "http://"+location.hostname+"/asia_travel/mappage/ajax/show_tweet_form.php";
				x.open("GET", url, true);
				x.send(null);					
			}			
		return false;	
	}
}

function checkMouseLeave (element, evt) {
	var evt = window.event || evt;
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}