
var g_curcity = "";
var g_curtag = "";
var g_curperson = "";




$(document).ready(function(){
	
	// All Buttons - Hover
	$('.button').hover(
	  	function () {
	    	$(this).addClass("button-on");
	  	},
	  	function () {
	    	$(this).removeClass("button-on");
	  	}
	);
}); // document ready




function FindEverybodyAtWebVisions() {
	g_curtag = "webvisions";	
	$.getJSON("/bin/ajax.php", {f:'shizzeepsstaticbytag', tag:g_curtag}, DisplayEverybodyAtWebVisions);
} // FindEverybodyAtWebVisions



function FindSomebodyAtWebVisions(who) {
	g_curtag = "webvisions";
	g_curperson = who;
	var parms = {};
	parms.f = 'shizzeepsstaticbytag';
	parms.tag = g_curtag;
	$.getJSON("/bin/ajax.php", parms, DisplaySomebody);
	return false;
} // FindSomebodyAtWebVisions





// city must be 3-digit lowercase
function FindShizzeepsPDX() {			
	g_curcity = 'pdx';	
	$.getJSON("/bin/ajax.php", {f:'shizzeepsstatic', city:g_curcity}, DisplayDataStatic);			
} // FindShizzeepsPDX()



function FindShizzeepsWebVisions() {
	g_curtag = "webvisions";	
	$.getJSON("/bin/ajax.php", {f:'shizzeepsstaticbytag', tag:g_curtag}, DisplayDataStatic);
}//FindShizzeepsWebVisions



function FindShizzeepsWebVisionsPlace() {
	g_curtag = "webvisions";	
	$.getJSON("/bin/ajax.php", {f:'shizzeepsstaticbytag', tag:g_curtag}, WhoIsHereWebVisionsPlace);
}//FindShizzeepsWebVisionsPlace


 			

function WhoIsHereWebVisionsPlace(json){

	if (json == "") {
		$('#explanation').html("No one has shouted lately.");
		return;
	}
	
	var i, j, place, id, out, shouts, shoutcount, shout;
	var places = json.results.places;
	var count = places.length;
	out = "";
	
	for (i=0; i<count; i++) {
		place = places[i];
		id = place.places_key;
		if (id == g_wvPlaceId) {
			// this is the place this page wants to display
			$('#wv_title #placename').html(place.places_name);
			shouts = place.shouts.results.shouts;
			shoutcount = shouts.length;
			$('#pplcount').html('('+shoutcount+')');
			for (j=0; j<shoutcount; j++) {
				shout = shouts[j];
				out += '<div><img align="center" src="' + shout.people_images.people_image_48 + '" />';
				out += shout.people_name + '</div>';
			}
		}
	}
	
	$('#wv_people').html(out);
	
}//WhoIsHereWebVisionsPlace



function DisplayEverybodyAtWebVisions(json){

	if (json == "") {
		$('#explanation').html("No one has shouted from here lately.");
		return;
	}
	
	var i, j, place, id, out, shouts, shout;
	var places = json.results.places;
	var count = places.length;
	var shoutcount = peoplecount = 0;
	out = "";
	
	for (i=0; i<count; i++) {
		place = places[i];
		id = place.places_key;

		shouts = place.shouts.results.shouts;
		shoutcount = shouts.length;
		peoplecount += shoutcount;
		for (j=0; j<shoutcount; j++) {
			shout = shouts[j];
			out += '<div class="grid corners">';
			out += '<img id="' + shout.people_name + '" class="somebody link" align="center" src="' + shout.people_images.people_image_48 + '" />';
			out += '<br />' + shout.people_name + '<br />' + shout.profiles_name;
			out += '<a href="/webvisions/place/?id=' + id + '">';
			out += '<br />' + shout.places_name;
			out += '</a>';
			out += '</div>';
		}
	}
	
		out += '<br style="clear:both" />';
	$('#pplcount').html('('+peoplecount+')');
	$('#wv_people').html(out);
	
	
	// make the avatars find that person
	$('.somebody').click(function(){
		 window.location = '/webvisions/somebody/?who=' + $(this).attr('id');
	});
	
	
}//DisplayEverybodyAtWebVisions


// Displays all the people who match the search term
function DisplaySomebody(json){

	if (json == "") {
		$('#explanation').html("That person has not shouted lately.");
		return;
	}
	
	var i, j, k, place, id, out, shouts, shoutcount, shout;
	var places = json.results.places;
	var count = places.length;
	var peoplecount = 0;
	out = "";
	
	var searchterm = g_curperson;
	
	out = '<h5 class="sheepblue">You searched for "' + searchterm + '."</h5>';
	
	for (i=0; i<count; i++) {
		place = places[i];
		id = place.places_key;

		shouts = place.shouts.results.shouts;
		shoutcount = shouts.length;
		for (j=0; j<shoutcount; j++) {
			shout = shouts[j];
			name1 = shout.people_name.toLowerCase();
			name2 = shout.profiles_name.toLowerCase();
			if (name1.indexOf(searchterm) >= 0 || name2.indexOf(searchterm) >= 0) {
				peoplecount++;
				out += '<div class="grid corners">';
				out += '<img align="center" src="' + shout.people_images.people_image_48 + '" />';
				out += '<br />' + shout.people_name + '<br />' + shout.profiles_name;
				out += '<a href="/webvisions/place/?id=' + id + '">';
				out += '<br />' + shout.places_name;
				out += '</a>';
				if (shout.shouts_messages) {;
					out += '<ul>';
					for (k=0; k<shout.shouts_messages.length; k++) {
						out += '<li class="somebodyshouts">'
						out += shout.shouts_messages[k].message;
						out += '</li>';
					}
					out += '</ul>';
				}
				out += '</div>';
			}
		}		
	}
	
	
	if (peoplecount == 0) {
		out += '<h5 class="sheepblue">Sorry, that person is not at WebVisions.</h5>';
	}
	
	out += '<br style="clear:both" />';
	$('#dataout').html(out);
	
	// stripe the shouts
	$('li:even').addClass('stripe1');
	$('li:odd').addClass('stripe2');
	
}//DisplaySomebody








function DisplayDataStatic(json) {

	if (json == "") {
		$('#explanation').html("No one has shouted lately.");
		return;
	}

	var out, place, dets, id, who, megaphone, notepad, expl, city, st, errmsg, title;
	
	//var status = $('#status').html();
	var br = '<br />', sep = ' | ';
	var limit = json.request.limit;
	var places = json.results.places;
	var count = places.length;
	var when = FormatShizzowDate(json.request.time);
		
	if (g_curcity == 'pdx') {
		expl = 'Top Places in Portland, OR ';
	} else if (g_curcity == 'aus') {
		expl = 'Top Places in Austin, TX ';
	}
	
	if (g_curtag == 'webvisions') {
		expl = 'Top Places at WebVisions ';
	}
	
	expl += ' Containing Shizzeeps Right Now';
	$('#explanation').html(expl);
	$('#dataout').html('');
	
	for (i=0; i<count; i++) {
	
		place = places[i];
		id = place.places_key;

		
		out = 
			'<div class="corners place" id="' + id + '">' +
			'<div class="corners population">' + place.population + '</div>' +
			'<a href="http://www.shizzow.com/places/' + id + '" target="_blank">' + place.places_name + '</a>' +
			'</div>';	
						
		$('#dataout').append(out);
		
		if (place.is_favorite == 1){
			$('#'+id).addClass('fav');
		}
		

		// Get the place's details from the orig. call
		$('#'+id).append('<div id="' + id + '_dets" class="dets"></div>');
				
		dets = '<div class="addr1" id="' + id + '_addr">' + place.address1 + br;
		if (place.address2 != '') {
			 dets = dets + place.address2 + br;
		} 
		dets = dets + place.city + ', ' + place.state_iso;
		//dets += '</div><div class="addr2">';
		if (place.phone > 0) {
			dets += br + place.phone;
		}
		if (place.website) {
			dets += br + '<a href="' + place.website + '" target="_blank">Website</a>';
		} 
		dets += '</div>';		
		
		$('#'+id+'_dets').append(dets);	
		
				
		// Get the Shizzeps who are at this place
		if (g_curtag == 'webvisions') {
			w = '<div class="w balloon corners">';
			w += '<a href="place/?id=' + id + '">';
			w += '<img src="http://s3.amazonaws.com/twitter_production/profile_images/87141711/wv09_normal.jpg" alt="WebVisions" />';
			w += '</a></div>';
			$('#'+id).append(w);
		} else {
			WhoIsHereStatic(place.shouts, id);
		}
		
	}
	
	
		
}//DisplayDataStatic()






function WhoIsHereStatic(json, id) {
		
		var i, j, person, peoplediv, count, shouts, shout, msg, pdets;
		var br = '<br />', sep = ' | ';
		
		count = json.results.count;
		shouts = json.results.shouts;
		peoplediv = '<div id="whosat' + id + '" class="corners whoshere"></div>';
		$('#'+id).prepend(peoplediv);
		
		for (i=0; i<count; i++) {
			shout = shouts[i];
			person = '<div class="person" id="' + shout.people_name + '"><a target="_blank" href="http://shizzow.com/people/' + shout.people_name + '">' + shout.people_name + '</a></div>';
			$('#whosat'+id).append(person);
			
			pdets = '<div class="pdets balloon corners" id="shout_' + shout.shouts_history_id + '">'; 
			pdets = pdets + '<a target="_blank" href="http://shizzow.com/people/' + shout.people_name + '">';
			pdets = pdets + shout.profiles_name + '</a><div class="when">' + shout.shout_time + '</div>';
			if (shout.shouts_messages) {
				for (j=0; j<shout.shouts_messages.length; j++) {
					msg = shout.shouts_messages[j];
					pdets += msg.message + br;
				}
			}
			pdets += '</div>';
			$('#'+shout.people_name).append(pdets);
		}	
		
		$('.pdets').hide();
		$('.whoshere').mouseover(function(){
			$(this).children('*').children('.pdets').show();
		});
	
		$('.whoshere').mouseout(function(){
			$(this).children('*').children('.pdets').hide();
		});
		
	


}// WhoIsHereStatic()




function FormatShizzowDate (dtime) {

	// dt looks like this: 2009-02-26T10:02:29-0800
	
	var dt, tm, tz, sep;
	
	dt = dtime.substr(0,10);
	tm = dtime.substr(11,8);
	tz = dtime.substr(19,6);
		
	return dt + ' at ' + tm + ', GMT ' + tz;

}


function ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


