var B = new Array();//holds business names for autocomplete
var direct_link = false;

//Alpha is for markers holds 54 results as is. Can switch to numbers easily by 
//switching alpha[labelNumber] for just labelNumber in resetMap function.
var alpha = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN", "OO", "PP", "QQ", "RR", "SS", "TT", "UU", "VV", "WW", "XX", "YY", "ZZ");

var direct_group = null;

//Creates the markers on the google map using labeled marker plugin. The marker styles were created on page load with the category results. 
function createMarker(point, name, label, address, phone, web, img, type, desc, name_link) {
  var marker = new LabeledMarker(point, {icon: customIcons[type], labelText: label, title: name, labelOffset: new GSize(-3, -32)});

	if (address==null) {
		address = "";
	}

	if (phone==null) {
		phone = "";
	}

	if (desc==null) {
		desc = "";
	}

  var html = "<table><tr>";
  html = html + "<td>";
  
  if(img.length > 0) {
  html = html + "<img src="+img+" width='53' height='53' style=\"margin-right: 3px; margin-top: 2px;\"/>";
  }
  
  html = html + "</td>";
  
  html = html + "<td><strong>" + name_link + "</strong> <br/>";

	if (address.length > 0) {
		html = html + address;
		html = html + " &nbsp; ";
	}
  
	if (phone.length > 0) {
 		html = html + phone;
	}

	if ((address.length + phone.length) > 0) {
		html = html + "<br />";
	}
    
	if (desc.length > 0) { 
	  html = html + "<div style=\"width: 250px; padding-top: 5px;\">";
	  html = html + desc;
	  html = html + "</div>";
	}

  html = html + "</td></tr></table>";
  
  markerGroups[type].push(marker);
  markerHTML[type].push(html);
  //listener to display the marker information box.
  GEvent.addListener(marker, 'click', function() {
  	marker.openInfoWindowHtml(html);
  });
  return marker;
}


function formatPhone(phone, format) {
	var num = phone.replace(/[^\d]/g,'');

	switch(format) {
		case '0': //Format (xxx)-xxx-xxxx
			return phone = "(" + num.substring(0,3) + ")-" + num.substring(3, 6) + "-" + num.substring(6);
			break;
		case '1': //Format xxx-xxx-xxxx
			return phone = num.substring(0,3) + "-" + num.substring(3, 6) + "-" + num.substring(6);
			break;
		default: //Format xxxxxxxxxx
			return phone = num;
			break;
	}
}


var load_business_pending = new Array();

function removeItems(array, item) {
	var i = 0;
	
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		} else {
			i++;
		}
	}

	return array;
}

function resetMap() {
	//console.log("resetting the map");
	map.clearOverlays();
	var center = new GLatLng(37.68714862992902, -97.33689308166504);
	var zoom = 15;
	map.setCenter(center, zoom);
	$('.category input').attr('checked', false);
	B = new Array();
	
	//$.getJSON("mod/getCategories.php", function(group) {
	
		group = data_categories;
		
		$.each(group, function(g,id) {
			var parms = "group="+id.group_id;
			var element = "#G"+id.group_id;
			$(element).css("display", "none");
			
			load_business_pending[load_business_pending.length] = id.group_id;
			
			business = data_listings[id.group_id];
			
			//$.getJSON("mod/getBusinesses.php", parms, function(business) {
				if(!business){
				} else
				var labelNumber = 0;
				
				$.each(business, function(b,biz) {
					var point = new GLatLng(biz.lat, biz.lon);
					var name = biz.name;
					var name_link = biz.name_lk
					B.push(name);
					//var label = biz.id;
					var label = "";
					//var address = biz.addr+ "<br/>" + biz.city+ ", " + biz.state;
					var address = biz.addr;
					//var phone = formatPhone(biz.ph, "0");
					var phone = biz.ph;
					var web = biz.web;
					var desc = biz.descr;
					//var hours = biz.hours;

					if(biz.photo.length > 0){
						var img = 'user/image/'+ biz.photo;
					} else {
						var img= '';
					}

					var type = biz.grp;
					var marker = createMarker(point, name, label, address, phone, web, img, type, desc, name_link);
					
					map.addOverlay(marker);
					
					marker.hide();
					
					//$('#keyword').autocomplete(B);
					
					labelNumber++;
					
					
					load_business_pending = removeItems(load_business_pending, biz.grp);
					//console.log(load_business_pending);
				});
			//});
		});
	//});
}

function idMatch(businessID) {
	//console.log("I am an ID match");
	
	direct_link = true;
	
	parms = "id="+businessID;
	
	$("#searchResults").load("mod/findBusinessByID_single.php", parms, function() {
		//alert('1');
						
		cmd = "idMatch_finish()";
		popup_timer = setTimeout(cmd, 500);
	
	});
	//$.getJSON("mod/findBusinessByID.php", parms, function(bus) {
	//	$.each(bus, function(id,biz) {
	//		bizMatch(biz.business_name);
	//	});
	//});
}

function idMatch_finish() {

	$('.search_finder').click()

	$('.business_photo').hover(

		function(){
			//$(this).addClass('business_photo_show');
			div_id = $(this).attr("rel");

			$("#" + div_id).show();

		},

		function(){
			div_id = $(this).attr("rel");

			$("#" + div_id).hide();

			//$(this).removeClass('business_photo_show') 
		}
	);
}

function bizMatch(businessName) {
	parms = "name="+businessName;
	$.getJSON("mod/findBusinessByName.php", parms, function(bus) {
		$.each(bus, function(bb,sBiz) {
	
			var sPoint = new GLatLng(sBiz.business_lat, sBiz.business_long);

			if (direct_link) {
				  iconOptions.width = 35;
				  iconOptions.height = 35;
				  iconOptions.primaryColor = "#006666";
				  iconOptions.cornerColor = "#FFFFFF";
				iconOptions.strokeColor = "#000000";
				coloredIcon = MapIconMaker.createMarkerIcon(iconOptions);
				
				//var sMarker = createMarker(point, name, label, address, phone, web, img, searchType, desc);
				var sMarker = new GMarker(sPoint, {icon: coloredIcon});
			
			} else {
				var sMarker = new GMarker(sPoint);
				
			}

			
			var sId = sBiz.business_id;
			var sName = sBiz.business_name;
			var sName_link = sBiz.business_name_link;
			var sLabel = sBiz.business_id;
			var sAddress = sBiz.business_address + "<br/>" + sBiz.business_city + ", " + sBiz.business_state;
			var sAddress_short = sBiz.business_address;
			//var sPhone = formatPhone(sBiz.business_phone, "0");
			var sPhone = sBiz.business_phone;
			var sDesc = sBiz.business_description;
			var sWeb = sBiz.business_website;
			var sType = sBiz.business_group;
				
			if(sBiz.business_photo.length > 0){
				var sImg = 'user/image/'+ sBiz.business_photo;
			} else {
				var sImg= '';
			}
			
			if (sAddress_short==null) {
				sAddress_short = "";
			}
			
			if (sPhone==null) {
				sPhone = "";
			}
			
			if (sDesc==null) {
				sDesc = "";
			}

			var shtml = "<table><tr>";

			shtml = shtml + "<td>";

			if(sImg.length > 0) {
			  shtml = shtml + "<img src=\"" + sImg + "\" width=\"53\" height=\"53\" style=\"margin-right: 3px; margin-top: 2px;\"/>";
			}

			shtml = shtml + "</td>";
			shtml = shtml + "<td><strong>" + sName_link + "</strong> <br/>";

			if (sAddress_short.length > 0) {
				shtml = shtml + sAddress_short;
				shtml = shtml + " &nbsp; ";
			}

			if (sPhone.length > 0) {
				shtml = shtml + sPhone;
			}

			if ((sAddress_short.length + sPhone.length) > 0) {
				shtml = shtml + "<br />";
			}
  
  			if (sDesc.length > 0) { 
  			  shtml = shtml + "<div style=\"width: 250px; padding-top: 5px;\">";
  			  shtml = shtml + sDesc;
  			  shtml = shtml + "</div>";
  			}

			shtml = shtml + "</td></tr></table>";

			map.addOverlay(sMarker);
			map.zoomIn(sPoint, true);
			sMarker.openInfoWindowHtml(shtml);


		
			if (direct_link) {
				direct_link = false;
			}



			$('#keyword').click(function() {
				//console.log("clicked");
				$('#keyword').val("");
				sMarker.closeInfoWindow();
				sMarker.hide();
			});
			
		});
	});
}

function group_direct(group_id) {
	//console.log("group_direct");
	//console.log(load_business_pending.length);

	if (load_business_pending.length == 0) {
		//console.log('group_direct: running');
	
		$("#" + group_id).attr("checked", "checked");
		$("#" + group_id).trigger('change');
	
	} else {
		//console.log('group_direct: waiting');
		
		cmd = "group_direct(" + direct_group + ")";
		group_timer = setTimeout(cmd, 500);
	
	}

}

$(document).ready(function() {

	if (GBrowserIsCompatible()) {
			var center = new GLatLng(37.68599392939966, -97.3357343673706);
			var zoom = 15;
			map = new GMap2(document.getElementById("mapit"));
			map.setCenter(center, zoom);
			map.enableContinuousZoom();
			map.addControl(new GLargeMapControl3D());
			map.addControl(new GMapTypeControl());
			map.enableDoubleClickZoom();
			map.enableScrollWheelZoom();
			geocoder = new GClientGeocoder();
	}

	resetMap();

	if ($.browser.msie) {
	  $('.category input').click(function() {
		this.blur();
		this.focus();
	  });
	}


	$('.category input').change(function(event) {
		type = $(this).val();

		checked = $(this).attr('checked');

		//console.log(type + 'change' + checked);

		element = "#G"+type;
		
		//console.log($(element).html().length);
		
		if ($(element).html().length==0) {
			$(element).load("listener_map.php?group=" + type, function() {
				
					$('.map_finder,.map_finder2').click(function() {
						v = $(this).attr('rel').split(",");
						group_val = v[0];
						marker_val = v[1];
						//console.log("Group of image was: "+group_val+" Biz of image was: "+marker_val);
						var mfMarker = markerGroups[group_val][marker_val];
						mfPoint = mfMarker.getPoint();
						map.zoomIn(mfPoint, true);
						mfMarker.openInfoWindowHtml(markerHTML[group_val][marker_val]);
						self.scrollTo(0,375);
					});

					$('.business_photo').hover(

						function(){
							//$(this).addClass('business_photo_show');
							div_id = $(this).attr("rel");

							$("#" + div_id).show();

						},

						function(){
							div_id = $(this).attr("rel");

							$("#" + div_id).hide();

							//$(this).removeClass('business_photo_show') 
						}
					);

			});
		}

		for (var i = 0; i < markerGroups[type].length; i++) {
		  var marker = markerGroups[type][i];
		  
		  if(checked) {
			marker.show();
			$(element).css("display", "block");
		  } else {
			marker.hide();
			$(element).css("display", "none");
		  }
		}

	});

	$('.map_finder,.map_finder2').click(function() {
		v = $(this).attr('rel').split(",");
		group_val = v[0];
		marker_val = v[1];
		//console.log("Group of image was: "+group_val+" Biz of image was: "+marker_val);
		var mfMarker = markerGroups[group_val][marker_val];
		mfPoint = mfMarker.getPoint();
		map.zoomIn(mfPoint, true);
		mfMarker.openInfoWindowHtml(markerHTML[group_val][marker_val]);
		self.scrollTo(0,375);
	});

	$('#keyword').focus(function() {
		if ($("#keyword").attr("value")=="Search") {
			$('#keyword').attr("value", "");
		}
	});
	
	$("#keyword").blur(function() {
		if ($("#keyword").attr("value").trim()=="") {
			$("#keyword").attr("value", "Search");
		}	
	});
	
	

	$('#bizDesc').submit(function() {
		var key = $("#keyword").attr("value");
		
		key = jQuery.trim(key);
		
		if (key != "Search") {

			jQuery.each(B, function (){
				if(this == key) {
					bizMatch(this);
				}
			});

			resetMap();

			parms = "keyword="+key;
			$('#searchResults').load("mod/findBusinessByKeyword.php", parms);
		
		} else {
			
			$("#keyword").attr("value", "Enter Search Terms");
			
			timer = setTimeout(function() {
				$("#keyword").attr("value", "Search");
				
			}, 4500);
		
		}
	});

	$('.business_photo').hover(
	
		function(){
			//$(this).addClass('business_photo_show');
			div_id = $(this).attr("rel");
			
			$("#" + div_id).show();
			
		},
		
		function(){
			div_id = $(this).attr("rel");
			
			$("#" + div_id).hide();

			//$(this).removeClass('business_photo_show') 
		}
	);
	
	if (direct_group > 0) {
		cmd = "group_direct(direct_group)";
		group_timer = setTimeout(cmd, 500);
	}

});//end document ready





var style3_tabs = Array();

style3_tabs[0] = "map_maps";
style3_tabs[1] = "map_events";
style3_tabs[2] = "map_qline";
style3_tabs[3] = "map_downloads";

var style3_tab_active = "map_maps";

function style3_tab_go(tab_name) {
	for (i=0; i < style3_tabs.length; i++) {
		if (style3_tabs[i]==tab_name) {
			show_tab = "#style3_tabs_" + tab_name;
			$(show_tab).css("background-image", "url(images/style3_tabs_" + tab_name + "_active.gif)");

			show_content = "#style3_content_" + tab_name;
			
			$(show_content).show();
			
			style3_tab_active = tab_name;
			
			//if (tab_name=="video") {
			//	video_init();
			
			//}
			
			//if (tab_name=="photos") {
			//	photos_init();

			//	$("#style3_right_sidebar").hide();
								
			//} else {
			//	$("#style3_right_sidebar").show();
			
			//}
			
		
		} else {
			hide_tab = "#style3_tabs_" + style3_tabs[i];
			$(hide_tab).css("background-image", "url(images/style3_tabs_" + style3_tabs[i] + "_inactive.gif)");

			hide_content = "#style3_content_" + style3_tabs[i];
			$(hide_content).hide();	
		
		}	
	}
}

function style3_tab_over(tab_name) {
	tab_id = "#style3_tabs_" + tab_name;
	$(tab_id).css("background-image", "url(images/style3_tabs_" + tab_name + "_active.gif)");
}

function style3_tab_out(tab_name) {
	//console.log(tab_name + ":" + style3_tab_active);

	if (tab_name !== style3_tab_active) {
		tab_id = "#style3_tabs_" + tab_name;
		$(tab_id).css("background-image", "url(images/style3_tabs_" + tab_name + "_inactive.gif)");
	}
}


function style3_map_init() {
	addr = window.location + "";
	
	if (addr.search("#") > 0) {
		addr_parts = addr.split("#");

		anchor = addr_parts[1];
		anchor = anchor.toLowerCase();
		anchor = "map_" + anchor;
		
		for (j=0; j < style3_tabs.length; j++) {
			if (anchor==style3_tabs[j]) {
				style3_tab_go(anchor);
			}
		}

	} 
	
}


