jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery.fn.hint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get it once since it won't change
    var title = t.attr('title'); 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('blur');
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form:first()').submit(function(){
          if (t.val() == title) {
              t.val('');
              t.removeClass('blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
}

function browsetabclick(tab,nohash) {
	// Decide which tabs to show and which to hide.
	for ( var i in toptabs ) {
		//alert(toptabs[i]);
		if (toptabs[i] == tab) {
			$("#tab_"+tab).addClass("selected");
			$("#ti"+tab).show();
		} else {
			$("#ti"+toptabs[i]).hide();
			$("#tab_"+toptabs[i]).removeClass("selected");
		}
	}
	
	// Cookie stuff.
	if (nohash == false || typeof nohash == "undefined") {
		window.location.hash = "#"+tab;
		$.cookie('browsetabclick',tab,{path: '/',expires: 10,domain: cookiedomain});
	}
}



function resultstabclick(tab,nohash) {
	if (tab == "probonos") {
		$("#tiprobonos #jfirm_listings_long").show();
	} else {
		$("#tiprobonos #jfirm_listings_long").hide();
	}
	if (tab == "lawfirms") {
		$("#tilawfirms #jfirm_listings_long").show();
	} else {
		$("#tilawfirms #jfirm_listings_long").hide();
	}
	if (tab == "all") {
		//alert("ALL! "+tab);
		$("#restab_all").addClass("selected");
		for ( var i in resultstab ) {
			$("#restab_"+resultstab[i]).removeClass("selected");
			$("#ti"+resultstab[i]).show();
		}
	} else {
		//alert("NOT ALL! "+tab);
		$("#restab_all").removeClass("selected");
		for ( var i in resultstab ) {
			if (resultstab[i] == tab) {
				$("#restab_"+resultstab[i]).addClass("selected");
				$("#ti"+resultstab[i]).show();
			} else {
				$("#restab_"+resultstab[i]).removeClass("selected");
				$("#ti"+resultstab[i]).hide();
			}
		}
	}
	if (nohash == false || typeof nohash == "undefined") {
		window.location.hash = "#"+tab;
		$.cookie('resultstabclick',tab,{path: '/',expires: 10,domain: cookiedomain});
	}
}

jQuery.preloadImages = function() { for(var i = 0; i<arguments.length; i++) { jQuery("<img>").attr("src", arguments[i]); } }

function browse_reset_location() {
	//if (google.gears) {
		//alert("GEARS!");
	//	geo = google.gears.factory.create('beta.geolocation');
	//	geo.getCurrentPosition(browse_updateposition,browse_handleError);
	//} else {
		//alert("NO GEARS! "+google.loader.ClientLocation.latitude+ ", "+google.loader.ClientLocation.longitude);
		
		
		
	if (navigator.geolocation) {
		location = navigator.geolocation.getCurrentPosition(browse_updateposition,browse_handleError);
	} else {
		if (google.loader.ClientLocation) {
			mapcenterpoint = new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
			browsemap.setCenter(mapcenterpoint,browsemap.getZoom());
		} else {
			mapcenterpoint = mapstartcenterpoint;
			browsemap.setCenter(mapstartcenterpoint,browsemap.getZoom());
		}
	}
	//}
	return false;
}

function browse_updateposition(position) {
	//mapcenterpoint = new google.maps.LatLng(position.latitude,position.longitude);
	//browsemap.setCenter(mapcenterpoint,browsemap.getZoom());
	//alert(position.coords.latitude+', '+position.coords.longitude);
	mapcenterpoint = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
	browsemap.setCenter(mapcenterpoint,browsemap.getZoom());
	
}
function browse_handleError(positionError) {	
	if (google.loader.ClientLocation) {
		mapcenterpoint = new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
		browsemap.setCenter(mapcenterpoint,browsemap.getZoom());
	} else {
		mapcenterpoint = mapstartcenterpoint;
		browsemap.setCenter(mapstartcenterpoint,browsemap.getZoom());
	}
}

function setmaplisten() {
	GEvent.addListener(browsemap,"moveend",function() {
		$("#locstring").html('<img src="/images/ajax-loader.gif" alt="Loading..." title="Loading..." />');
		
		
		$.get("/locbrowse/"+browsemap.getCenter().lat()+"/"+browsemap.getCenter().lng(),function(data) {
			updatebrowseboxes(data);
		});
	});
}

function init_home_map() {
	if (doautomappop) {
		$.get("/locbrowse/"+mapcenterpoint.lat()+"/"+mapcenterpoint.lng(),function(data) {
			updatebrowseboxes(data);
		});
	}
	browsemap = new google.maps.Map2(document.getElementById("mapholder"));
	browsemap.setCenter(mapcenterpoint,mapzoomlevel);
	browsemap.addControl(new GSmallZoomControl());
	var founddeftab = false;
	var firsttab = false;
	for ( var i in toptabs ) {
		if (window.location.hash == "#"+toptabs[i]) {
			browsetabclick(toptabs[i],true);
			founddeftab = true;
		}
		if (firsttab == false) {
			firsttab = toptabs[i];
		}
	}
	if (!founddeftab) {
		var cookietab = $.cookie("browsetabclick");
		if (cookietab && jQuery.inArray(cookietab,toptabs) > -1) {
			browsetabclick(cookietab,true);
		} else {
			browsetabclick(firsttab,true);
		}
	}	
	if (doautomappop) {
		setmaplisten();
	} else {
		setTimeout(setmaplisten,2000);
	}
	
	var alink = $('<a href="#">Reset Map Location</a>');
	alink.click(function() {
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(browse_updateposition,browse_handleError);
		} else {
			if (google.loader.ClientLocation) {
				mapcenterpoint = new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
				browsemap.setCenter(mapcenterpoint,browsemap.getZoom());
			} else {
				mapcenterpoint = mapstartcenterpoint;
				browsemap.setCenter(mapstartcenterpoint,browsemap.getZoom());
			}
		}
		return false;
	});
	
	$("#mapresetlinkcontainer").append(alink);
	
	
}
function init_browse_tabs() {
	var founddeftab = false;
	if (resultstab.length > 0) {
		for ( var i in resultstab ) {
			if (window.location.hash == "#"+resultstab[i]) {
				resultstabclick(resultstab[i],true);
				founddeftab = true;
			}
		}
	}
	if (!founddeftab) {
		var cookietab = $.cookie("resultstabclick");
		if (cookietab && jQuery.inArray(cookietab,resultstab) > -1) {
			resultstabclick(cookietab,true);
		} else {
			resultstabclick("all",true);
		}
	}		
}

function profileaccordionclick (tab) {
	if ($('#ac_'+tab).hasClass('acopen')) {
		$('#ac_'+tab).removeClass('acopen');
		$('#ac_'+tab).addClass('acclose');
		$('#ti'+tab).hide();
	} else {
		$('#ac_'+tab).addClass('acopen');
		$('#ac_'+tab).removeClass('acclose');
		$('#ti'+tab).show();
		
		if (tab == "location" && !mapinitialized) {
			if (GBrowserIsCompatible()) {
				this.geoXML = new GGeoXml(kml);
				this.counter = 0;
				var InitMapper = GEvent.bind(this.geoXML,"load",this,function() {
					if (this.counter == 0) {
						this.profilemap = new google.maps.Map2(document.getElementById("profilemap"));
						var zoomlevel = profilemap.getBoundsZoomLevel(geoXML.getDefaultBounds());
						zoomlevel--;
						this.profilemap.setCenter(geoXML.getDefaultCenter(),zoomlevel);
						this.profilemap.addControl(new GLargeMapControl());
						this.profilemap.addControl(new GHierarchicalMapTypeControl());
						this.profilemap.addControl(new GOverviewMapControl());
						this.profilemap.addOverlay(geoXML);
						this.counter++;
					} else {
						GEvent.removeListener(InitMapper);
					}
				});
				mapinitialized = true;
			}
		} else if (tab == "twitter" && !twittermapinitialized) {
			if (GBrowserIsCompatible()) {
				this.geoXMLT = new GGeoXml(twitterkml);
				this.counterT = 0;
				var InitMapperT = GEvent.bind(this.geoXMLT,"load",this,function() {
					if (this.counterT == 0) {
						this.profilemapT = new google.maps.Map2(document.getElementById("twitter_map"));
						var zoomlevelT = profilemapT.getBoundsZoomLevel(geoXMLT.getDefaultBounds());
						this.profilemapT.setCenter(geoXMLT.getDefaultCenter(),zoomlevelT);
						this.profilemapT.addControl(new GLargeMapControl());
						this.profilemapT.addControl(new GHierarchicalMapTypeControl());
						this.profilemapT.addControl(new GOverviewMapControl());
						this.profilemapT.addOverlay(geoXMLT);
						this.counterT++;
					} else {
						GEvent.removeListener(InitMapperT);
					}
				});
				twittermapinitialized = true;
			}
			/* drawChart_justia(); */	
		}

	}

	// Decide if we need to show or hide the summary tab.
/*
	if (in_array(tab, noSummaryTabs)) {
		$('#head').hide();
	} else {
		$('#head').show();
	}
*/
	
}

function profiletabclick(tab,nohash) {
	for ( var i in profiletabs ) {
		//alert(toptabs[i]);
		if (profiletabs[i] == tab) {
			$("#tab_"+tab).addClass("selected");
			$('#ac_'+tab).addClass('acopen');
			$('#ac_'+tab).removeClass('acclose');
			$("#ti"+tab).show();
		} else {
			$("#ti"+profiletabs[i]).hide();
			$('#ac_'+profiletabs[i]).addClass('acclose');
			$('#ac_'+profiletabs[i]).removeClass('acopen');
			$("#tab_"+profiletabs[i]).removeClass("selected");
		}
	}
	
	if (tab == "location" && !mapinitialized) {
		if (GBrowserIsCompatible()) {
			this.geoXML = new GGeoXml(kml);
			this.counter = 0;
			var InitMapper = GEvent.bind(this.geoXML,"load",this,function() {
				if (this.counter == 0) {
					this.profilemap = new google.maps.Map2(document.getElementById("profilemap"));
					var zoomlevel = profilemap.getBoundsZoomLevel(geoXML.getDefaultBounds());
					zoomlevel--;
					this.profilemap.setCenter(geoXML.getDefaultCenter(),zoomlevel);
					this.profilemap.addControl(new GLargeMapControl());
					this.profilemap.addControl(new GHierarchicalMapTypeControl());
					this.profilemap.addControl(new GOverviewMapControl());
					this.profilemap.addOverlay(geoXML);
					this.counter++;
				} else {
					GEvent.removeListener(InitMapper);
				}
			});
			mapinitialized = true;
		}
	} else if (tab == "twitter" && !twittermapinitialized) {
		if (GBrowserIsCompatible()) {
			this.geoXMLT = new GGeoXml(twitterkml);
			this.counterT = 0;
			var InitMapperT = GEvent.bind(this.geoXMLT,"load",this,function() {
				if (this.counterT == 0) {
					this.twittermap = new google.maps.Map2(document.getElementById("twitter_map"));
					var zoomlevelT = twittermap.getBoundsZoomLevel(geoXMLT.getDefaultBounds());
					this.twittermap.setCenter(geoXMLT.getDefaultCenter(),zoomlevelT);
					this.twittermap.addControl(new GLargeMapControl());
					this.twittermap.addControl(new GHierarchicalMapTypeControl());
					this.twittermap.addControl(new GOverviewMapControl());
					this.twittermap.addOverlay(geoXMLT);
					this.counterT++;
				} else {
					GEvent.removeListener(InitMapperT);
				}
			});
			twittermapinitialized = true;

		}
		//drawChart_justia();
	}
	
	// Decide if we need to show or hide the summary tab.
	if (in_array(tab, noSummaryTabs)) {
		$('#head').hide();
	} else {
		$('#head').show();
	}
	
	if (nohash == false || typeof nohash == "undefined") {
		window.location.hash = "#"+tab;
		//$.cookie('profiletabclick',tab,{path: '/',expires: 10,domain: cookiedomain});
	}
}

function init_profile_map() {
	var founddeftab = false;
	//google.load("maps", "2");
	for ( var i in profiletabs) {
		if (window.location.hash == "#"+profiletabs[i]) {
			profiletabclick(profiletabs[i],true);
			founddeftab = true;
		}
	}
	if (!founddeftab) {
		profiletabclick("overview",true);
	}
}



function updatebrowseboxes(data) {
	var jsonData = eval('('+data+')');
	$("#tipractice").html(jsonData.html);
	$("#locstring").html(jsonData.string);
	$("#locationbox").val(jsonData.string);
}

function jbadge_banner_hide() {
	$('#banner').slideUp();
	$.cookie('banner_dismissed', true, { path: '/' });
}

if (Drupal.jsEnabled) {
	$('img.twitpic').hover(
		function() {
			$(this).width(150);
			$(this).height(150);
		},
		function() {
			$(this).width(25);
			$(this).height(25);
		}
	);
}

function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}

function show_twitter_stats_justia(){
	$('#twitter_stats_all').hide();
	$('#twitter_stats_justia').show();
	drawChart_justia();

}

function show_twitter_stats_all(){
	$('#twitter_stats_justia').hide();
	$('#twitter_stats_all').show();
	drawChart_all();
}