var enh_c_h=[];
var current_map_el='';	
function $(id){
	return document.getElementById(id);
}

function check_searchform(){
	pass = true;
	if($('keyword').value == '' && $('name').value == '' ){
		disp('keywordError');
		disp('nameError');
		pass = false;
	}else{
		hide('keywordError');
		hide('nameError');
	}
	return pass;
}

function expand(id,max,obj,obj2){
	h=obj.offsetHeight;
	if (h<max){
		step = Math.round((max-h)/5);
		step = (step<1) ? 1 :step ;
		obj2.style.margin='1px';
		obj.style.height=h+step+'px';
		obj2.style.margin='0';
	}
	else if (h>=max){
		clearInterval(delay);
		obj2.style.margin='1px';
		obj.style.height=max+'px';
		obj2.style.margin='0';
	}	
}

function collapse(id,min,m,steps){
	obj=$(id);
	h=obj.offsetHeight;
	num=id.substr(4,id.length);
	obj2=$('lnk_'+num);
	min=(min==0)?1:min;

	if (h>min){
		step = Math.round((h-min)/steps);
		step = (step<2) ? 1 :step ;
		obj2.style.margin='';
		obj.style.height=h-step+'px';
		obj2.style.margin='0';

	}
	else{
		clearInterval(delay);
		$('map_'+num).style.display='none';
		if(min==1)obj.style.display='none';		
		obj2.style.margin='1px';
		obj.style.height=(m==0)? 1 : min;
		obj2.style.margin='0';

	}	
}

function prep_accordion(){
	h_arr=[];
	for(i=0;i<10;i++){
		idname='enh_'+i;
		if(obj=$(idname)){
			a=obj.offsetHeight;
			h_arr.push(a);			
			obj.style.height='1px';		
			hide(idname);
		}
	}
}
function PointFromPostcode(postcode, callbackFunction) {
  localSearch.setSearchCompleteCallback(null,
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}
function getmap(id,postcode){
	current_map_el=id;
	PointFromPostcode(postcode, function (point) {
		showmap(point.lat(),point.lng());
      });
}

function showmap(lat,lon){
	obj=$(current_map_el);
	num=current_map_el.substr(4,current_map_el.length);
	mapDiv=$('map_'+num);
	ch=obj.offsetHeight;

	if(obj.style.display=='none'){
		obj.style.display='block';
		obj.style.height='1px';
	}

	if (!(ch==200)){
		window.enh_c_h[num]=ch;
		mapDiv.style.display='block';
		if(ch>200){
			delay=setInterval('collapse("'+current_map_el+'",200,1,5)',10);
		}
		else{
			mapDiv.style.display='block';
			if (GBrowserIsCompatible()) {
		     	var map = new GMap2(mapDiv);  	
			    map.setCenter(new GLatLng(lat,lon), 13);
			    map.addControl(new GSmallMapControl());
			    map.addControl(new GMapTypeControl());
		
			    var bounds = map.getBounds();
		 		var southWest = bounds.getSouthWest();
				var northEast = bounds.getNorthEast();
				var lngSpan = northEast.lng() - southWest.lng();
				var latSpan = northEast.lat() - southWest.lat();
				var point = new GLatLng(lat,lon);
				map.addOverlay(new GMarker(point));    
			}
			obj2=$('lnk_'+num);
			
			delay=setInterval('expand("'+current_map_el+'",200,obj,obj2)',10);
		}		
	}else{
		num=current_map_el.substr(4,current_map_el.length);
		if(ch==200 && ch!=window.h_arr[num]){
			if(window.enh_c_h[num]!=0) mapDiv.style.display='none';
			delay=setInterval('collapse(\''+current_map_el+'\','+window.enh_c_h[num]+',1,3)',10);
		}
	}
}

function action(id){
	obj=$(id);
	if(obj.style.display=='none') {
		num=id.substr(4,id.length);
		max=h_arr[num];
		obj2=$('lnk_'+num);
		obj.style.display='block';
		obj.style.height='1px';
		delay=setInterval('expand("'+id+'",'+max+',obj,obj2)',10);
	}else{
		delay=setInterval('collapse("'+id+'",0,0,5)',10);
	}
}

function show(id){
 if($(id).style.display=='none')
 	disp(id);
 else hide(id);
}
function disp(id){
	$(id).style.display='block';
}

function hide(id){
	$(id).style.display='none';
}

function popup(mylink, windowname,x,y){
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width='+x+'px,height='+y+'px,toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');
return false;
}

function profile(mylink, windowname,x,y){
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open('http://thisisbusiness.info/profiles/'+mylink+'/'+mylink+'.html', windowname, 'width='+x+'px,height='+y+'px,toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');
return false;
}

function show_add(a){
	if(a=='1'){$('preview').style.background='transparent url(http://www.thisisbusiness.info/images/add1zoom.png) no-repeat 0 0;';}
	else {$('preview').style.background='transparent url(http://www.thisisbusiness.info/images/add2zoom.png) no-repeat 0 0;';}
	
}
function hide_add(){
$('preview').style.background='transparent url(http://www.thisisbusiness.info/images/addpage.png) no-repeat 0 0;';
}