/* contains functions used by the search page */

var map=null;
var dragListener=null;

function searchRegister(search_mode){
	var bounds = map.getBounds();
	var order = document.getElementById('o').value;
	var order_dir = document.getElementById('d').value;
	var page = document.getElementById('p').value;
	var keywords = document.getElementById('search-keywords').value;
	
	var query="keywords="+escape(keywords)+"&o="+escape(order)+"&d="+order_dir+"&p="+page+"&mode="+search_mode+"&";

	if(search_mode !== "initial"){
		var sw = bounds.getSouthWest();
		var ne = bounds.getNorthEast();
		query +="sw[lat]="+sw.lat()+"&sw[lng]="+sw.lng()+"&ne[lat]="+ne.lat()+"&ne[lng]="+ne.lng()+"&";
	}

	
	var locale = $("#locale");
	if(locale.val() !=="" || locale.val() !== locale.defaultValue){
		query += "locale="+locale.val()+"&";
	}
	
/*	var keywords = document.getElementById('keywords');
	if(keywords.value !== "" || keywords.value !== keywords.defaultValue)
		query += "&keywords="+keywords.value;
	*/
	$(".category-list input").each(function(i){
								if(this.checked){
									query += this.name+"&";
								}
							});

	var radius = document.getElementById('radius');
	var national = document.getElementById('national');

	if(!national.checked && parseInt(radius.value, 10)>0){
		query += "radius="+radius.value+"&";
	}
		
	
	setTimeout("xmlhttp('/ajx.search.php','"+query+"',eval)",0);
	return false;
}

//switch the marker icon
function getIcon(id){
	var icon = new GIcon();
	//icon.image = "/gfx/markers/"+id+".png";
	icon.image = "/gfx/markers/1.png";
	icon.iconSize = new GSize(16, 16);
	icon.iconAnchor = new GPoint(10,23);
	icon.infoWindowAnchor = new GPoint(10, 10);
	 
	return icon;
}

function toggle_map(link){
	var map_div = $('#map-container');
	if(map_div.is(":hidden")){
		map_div.fadeTo(0.01,0);
		map_div.slideDown("slow",function(){ map_div.fadeTo("slow",1,function(){ map.checkResize(); }); });
		link.id="map-hide";
		link.innerHTML="- Hide Map";
		var exp = new Date();
        exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));     // 30 days ahead
		document.cookie ='hide_map=false; expires='+exp+'; path=/';
	}else{
		map_div.fadeTo("slow",0.01,function(){ map_div.slideUp("slow"); });
		link.id="map-show";
		link.innerHTML="+ Show Map";
		var exp = new Date();
        exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));     // 30 days ahead
		document.cookie ='hide_map=true; expires='+exp+'; path=/';
	}
}

//show/hide company details in listing
var show_company = false;

function toggle_company() {
	$(".company-item").each(function(i) {
		$(this).find(".emails").css({
					display: 'none'
		});
		$(this).find(".description").css({
					display: 'none'
		});
		$(this).find(".employees").css({
					display: 'none'
		});
		$(this).find("a.org").click(function() {
			var org_div = $(this).parent().parent();
			if(org_div.find(".description").css('display') === 'none') { 
				org_div.find(".emails").css({
					display: 'block'
				});
				org_div.find(".description").css({
					display: 'block'
				});
			org_div.find(".employees").css({
					display: 'block'
				});
				org_div.addClass('show');
				show_company = org_div;
			}
			else	{
				org_div.find(".emails").css({
					display: 'none'
				});
				org_div.find(".description").css({
					display: 'none'
				});
			 org_div.find(".employees").css({
					display: 'none'
				});
				org_div.removeClass('show');
			}
			return false;
		});
	});
	
}

// A ImageControl is a GControl that displays custom image buttons
function ImageControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
ImageControl.prototype = new GControl();

ImageControl.prototype.initialize = function(map) {
	var container = document.createElement("div");

	var zoomInDiv = document.createElement("div");
	this.setInButtonStyle_(zoomInDiv);
	container.appendChild(zoomInDiv);
	zoomInDiv.appendChild(document.createTextNode("Zoom In"));
	GEvent.addDomListener(zoomInDiv, "click", function() {
		map.zoomIn();
	});

	var zoomOutDiv = document.createElement("div");
	this.setOutButtonStyle_(zoomOutDiv);
	container.appendChild(zoomOutDiv);
	zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
	GEvent.addDomListener(zoomOutDiv, "click", function() {
		map.zoomOut();
	});


	map.getContainer().appendChild(container);
	return container;
};

ImageControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
};

ImageControl.prototype.setInButtonStyle_ = function(button) {
	button.style.width = "22px";
	button.style.height = "20px";
	button.style.textIndent = "-2000em";
	button.style.background = "transparent url(/gfx/zoom-in.png) left top no-repeat";
	button.style.cursor = "pointer";
};

ImageControl.prototype.setOutButtonStyle_ = function(button) {
	button.style.width = "22px";
	button.style.height = "20px";
	button.style.textIndent = "-2000em";
	button.style.background = "transparent url(/gfx/zoom-out.png) left top no-repeat";
	button.style.cursor = "pointer";
};

function load(){
	document.getElementById('map').style.display='block';
	if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"),{size:new GSize(659,400)});
	cpoint = new GLatLng(54.622978,-2);
	map.setCenter(cpoint, 5);
	
	map.enableDoubleClickZoom();
	map.addControl(new ImageControl());
	// map.addControl(new GSmallZoomControl());
	map.addControl(new GOverviewMapControl());
	
	 searchRegister('initial');
	//setup some event listeners
	dragListener = GEvent.addListener(map, "dragend", searchRegister);
	GEvent.addListener(map, "zoomend", function(oldLevel,newLevel){
														/* only if we are zooming out */
														if(newLevel<=oldLevel){
															searchRegister();
														}
													});

	}
}

$(document).ready(function(){ 
	
	// deselect 'national' checkbox if a postcode is entered
	$('input#locale').keypress(function() {
		$('input#national').attr('checked', false);
	});
	
	// if no postcode entered, reselect 'national' checkbox
	$('input#locale').blur(function() {
		if ($(this).val() === '') {
			$('input#national').attr('checked', true);
		}
	});
	
	$('#radius-slider').bind('slide', set_radius_value);
	$('#radius-slider').slider({ handle:'.slider-handle',
											minValue:1,
											maxValue:99,
											startValue:$('#radius').val()
										});
														 
	toggle_company();
	load();
});

function set_radius_value(e,oe, ui){
	$('#radius').val(parseInt(ui.value, 10));
	return true;
}

$(window).bind('unload',function(){ GUnload(); });

