/* contains functions used by the search page */

var map=null;
var dragListener=null;

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();
    													}
    												});

	}
	
/*	var node = document.createElement('div');
	node.id="map-top";
	map.appendChild(node);
	node = document.createElement('div');
	node.id="map-bottom";
	map.appendChild(node);
	node = document.createElement('div');
	node.id="map-left";
	map.appendChild(node);
	node = document.createElement('div');
	node.id="map-right";
	map.appendChild(node);
*/
}

searchRegister = function (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="+mode+"&";

	if(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)>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";
		document.cookie ='hide_map=false; expires=Fri, 3 Aug 2030 20:47:11 UTC; path=/';
	}else{
	   map_div.fadeTo("slow",0.01,function(){ map_div.slideUp("slow"); });
		link.id="map-show";
		link.innerHTML="Show Map";
		document.cookie ='hide_map=true; expires=Fri, 3 Aug 2030 20:47:11 UTC; 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') { 
				  // 		        if(show_company) {
				  // 		          show_company.down(".emails").setStyle({
				  //                     display: 'none'
				  //                 });
				  // show_company.down(".description").setStyle({
				  //                     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();
});

// var panRightDiv = document.createElement("div");
// 	this.setRightButtonStyle_(panRightDiv);
// 	container.appendChild(panRightDiv);
// 	panRightDiv.appendChild(document.createTextNode("Pan Right"));
// 	GEvent.addDomListener(panRightDiv, "click", function() {
// 		map.panDirection(-1, 0);
// });

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";
}

// ImageControl.prototype.setRightButtonStyle_ = function(button) {
// 	button.style.width = "18px";
// 	button.style.height = "25px";
// 	button.style.textIndent = "-2000em";
// 	button.style.background = "transparent url(/gfx/map-right.png) left top no-repeat";
// 	button.style.cursor = "pointer";
// }
// 
// ImageControl.prototype.setLeftButtonStyle_ = function(button) {
// 	button.style.width = "18px";
// 	button.style.height = "25px";
// 	button.style.textIndent = "-2000em";
// 	button.style.background = "transparent url(/gfx/map-left.png) left top no-repeat";
// 	button.style.cursor = "pointer";
// }



$(document).ready(function(){ $('#radius-slider').slider({ handle:'.slider-handle',
																				minValue:1,
																				maxValue:99,
																				startValue:$('#radius').val(),
																				slide:function(e,ui){
																							$('#radius').val(parseInt(ui.value));
																						}});
																 
						toggle_company();
						load();
						
						});
$(window).bind('unload',function(){ GUnload(); });
