jQuery(document).ready(function($) {

	$("h1, h2, h3, h4, h5, h6, .sharing_label").addClass("gothic");


/* 	PRELIMINARY STALKEROONI STUFF */
		var mapOptions = {
          center: new google.maps.LatLng(40.745176,-73.983049),
          zoom: 11,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var zoomLevel = 2;
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
        google.maps.event.addListener(map, 'zoom_changed', function() { 
			if (map.getZoom() == 0)
				zoomLevel = 1;
			else
				zoomLevel = map.getZoom();
			console.log(map.getZoom());
		});
		var foursquareTokenSplit = window.location.hash.search("=");
		var foursquareToken = window.location.hash.substr(foursquareTokenSplit + 1);
		
	

	$("#the-stalkerooni-names").submit(function(){
	/* Check for existence of usernames, send tweets to plot function with form index */
		if ($('input[name="tw-handle1"]').val()){
			var twHandle1 = $('input[name="tw-handle1"]').val();
			plotTweets(twHandle1, 0);
		}
		if ($('input[name="tw-handle2"]').val()){
			var twHandle2 = $('input[name="tw-handle2"]').val();
			plotTweets(twHandle2, 1);
		}
		if ($('input[name="tw-handle3"]').val()){
			var twHandle3 = $('input[name="tw-handle3"]').val();
			plotTweets(twHandle3, 2);
		}
	/* Plot tweets */
		function plotTweets(username, index){
			$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=' + username + '&count=200&callback=?', function(data){
			var items = [];
			parsedItems = [];
			$.each(data, function(key, val) {
				if (val.coordinates) {
					parsedItems.push(val.coordinates);
				}
			});
			function mapMarkers(map, locations){
				for (i=0; i < locations.length; i++) {
					var thisLocation = locations[i];
					var locLatLong = new google.maps.LatLng(thisLocation.coordinates[1], thisLocation.coordinates[0]);
					var colors = ['#ff0000','#00ff00','#ffa500'];
					var theMarker = new google.maps.Marker({
									position: locLatLong,
									map: map,
									zIndex: 2
					});
					var theTweet = new google.maps.InfoWindow({
									position: locLatLong,
									content: "sounds like a plan",
									zIndex: 2
					});
				}
			}; 
	        mapMarkers(map, parsedItems);
	        console.log(map.getZoom());
			})
		};

	})
	
/* Facebook calls	 */
	$(".stalkerooni.fb-login-button").click(function(){
		var myScope = "{scope: 'email, user_status, read_stream, user_checkins'}";
		FB.getLoginStatus(function(response) {
	  	if (response.status === 'connected') {
		    FB.login(function(response){
		    	FB.api('/me', function(response) {
		    		console.log(response.name + ' is logged in.');
		    	});
		    }, myScope);
		    $("#fb-options").css("visibility", "visible"); }
	  	else if (response.status === 'not_authorized') {
		    // the user is logged in to Facebook, 
		    //but not connected to the app 
		    FB.login(function(response){
		    	FB.api('/me', function(response) {
		    		console.log('Good to see you, ' + response.name + '.');
		    	});
		    }, myScope);}
	   	else {
		    // the user isn't even logged in to Facebook.
		    alert("not logged into Facebook"); }
		});
	});	
		
	$("#show-stats").click(function(){
		FB.api('/me/statuses', function(response){
				console.log(response);			
			for (i = 0; i < response.data.length; i++){
				$("#facebookstuff").append("<div class='stat-node'><div class='mini-title'>" + response.data[i].from.name + "</div><div class='mini-content'>" + response.data[i].message + "</div></div>");			
			}
		});
	
	});	

	$("#show-checkins").click(function(){
		FB.api('/me/checkins', function(response){
				console.log(response);			
			for (i = 0; i < response.data.length; i++){
				$("#facebookstuff").append("<div>" + response.data[i].place.name + "</div>");
				$("#facebookstuff").append("<div class=\"tabme\">" + response.data[i].place.location.latitude + "," + response.data[i].place.location.longitude + "</div>");
				var checkinLocation = new google.maps.LatLng(response.data[i].place.location.latitude, response.data[i].place.location.longitude);
				var theTime = new Date(response.data[i].created_time);
				var theContent = "<div>" + response.data[i].place.name + "</div><div>" + theTime.toDateString() + " " + theTime.toTimeString() + "</div>";
				var infowindow = new google.maps.InfoWindow({
					content: theContent
				});
				(function myPlots(content){
					var image = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/facebook.png", //the image URL
								new google.maps.Size(20,18), //the sprite size
								new google.maps.Point(0,250) //the sprite offset (x,y)
								);
					var shadow = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/fbshadow.png", //the image URL
								new google.maps.Size(30,18), //the sprite size
								new google.maps.Point(0,0) //the sprite offset (x,y)
								);								
					var theMarker = new google.maps.Marker({
										position: checkinLocation,
										map: map,
										animation: google.maps.Animation.DROP,
										icon: image,
										zIndex: 2
					});
					var theShadow = new google.maps.Marker({
										position: checkinLocation,
										map: map,
										animation: google.maps.Animation.DROP,
										icon: shadow,
										zIndex: 1
					});
					google.maps.event.addListener(theMarker, 'click', function() {
						infowindow.setContent(content);
						infowindow.open(map,theMarker);
					});		
				}(theContent));				
			};
		});
	});	

	$("#show-home").click(function(){
		function parsenewsfeed (json) {
			for (i = 0; i < json.data.length; i++){
				$("#facebookstuff").append("<div>" + json.data[i].from.name + "</div>");
				if (json.data[i].message)
					$("#facebookstuff").append("<div class=\"tabme\">message - " + json.data[i].message + "</div>");
				else if (json.data[i].story)
					$("#facebookstuff").append("<div class=\"tabme\">story - " + json.data[i].story + "</div>");
				else if (json.data[i].caption)
					$("#facebookstuff").append("<div class=\"tabme\">caption - " + json.data[i].caption + "</div>");
				else if (json.data[i].picture)
					$("#facebookstuff").append("<div class=\"tabme\"><img src=" + json.data[i].picture + "\"></div>");
				else
					$("#facebookstuff").append("<div class=\"missing\">no selector!!!</div>");
			}
		};
		FB.api('/me/home', function(response){
			parsenewsfeed(response);
			var nextPage = response.paging.next;
			$.ajax({
				url: nextPage, 
				dataType: "jsonp",
				success: function(nextData){
					parsenewsfeed(nextData);
					}
				});
			});
	});

	
/* Foursquare calls */
	$(".fs-login-button").click(function(){
		if(foursquareToken != ""){
			$("#fs-options").css("visibility", "visible"); }
		else
			window.location = "https://foursquare.com/oauth2/authenticate?client_id=WL5T2LTTBYUVOCQCF3DBMSDD4NCUUBPMOHT1XSKSYCQWNRRU&response_type=token&redirect_uri=http://re.adyma.de/stalkerooni";
	});
	
	$("#fs-checkins").click(function(){
		$.ajax({
			url: "https://api.foursquare.com/v2/users/self/checkins?oauth_token=" + foursquareToken + "&v=20120104&limit=250", 
			dataType: "jsonp",
			success: function(fsJSON){
				console.log(fsJSON.response.checkins);
				console.log(fsJSON.response.checkins.items.length);
				for (i = 0; i < fsJSON.response.checkins.items.length; i++){
					var jsonShorty = fsJSON.response.checkins.items[i];
					$("#facebookstuff").append("<div>" + jsonShorty.venue.name + "</div>"); //the place name
					$("#facebookstuff").append("<div class=\"tabme\">" + jsonShorty.venue.location.lat + "," + jsonShorty.venue.location.lng + "</div>"); // the lat, long
					var checkinLocation = new google.maps.LatLng(jsonShorty.venue.location.lat, jsonShorty.venue.location.lng);
					var theTime = new Date(jsonShorty.createdAt * 1000);
					var theContent = "<div>" + jsonShorty.venue.name + "</div><div>Checked in on " + theTime.toLocaleString() + ".</div>";
					var infowindow = new google.maps.InfoWindow({
						content: theContent
					});
					(function myFoursquarePlots(content){
						var image = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/icon-36x36.png", //the image URL
									new google.maps.Size(20,20), //the sprite size
									new google.maps.Point(0,0), //the sprite offset (x,y)
									new google.maps.Point(10,20), //  anchor
									new google.maps.Size(20,20) // set scaled size
									);
						var shadow = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/fbshadow.png", //the image URL
									new google.maps.Size(30,18), //the sprite size
									new google.maps.Point(0,0) //the sprite offset (x,y)
									);								
						var theMarker = new google.maps.Marker({
											position: checkinLocation,
											map: map,
											animation: google.maps.Animation.DROP,
											icon: image,
											zIndex: 2
						});
						var theShadow = new google.maps.Marker({
											position: checkinLocation,
											map: map,
											animation: google.maps.Animation.DROP,
											icon: shadow,
											zIndex: 1
						});
						google.maps.event.addListener(theMarker, 'click', function() {
							infowindow.setContent(content);
							infowindow.open(map,theMarker);
						});		
					}(theContent));				
				};
			}
		});
	});


	$("#fs-venuehistory").click(function(){
		$.ajax({
			url: "https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=" + foursquareToken + "&v=20120104", 
			dataType: "jsonp",
			success: function(fsJSON){
				console.log(fsJSON.response.venues);
				console.log(fsJSON.response.venues.items.length);
				for (i = 0; i < fsJSON.response.venues.items.length; i++){
					$("#facebookstuff").append("<div>" + fsJSON.response.venues.items[i].venue.name + "</div>");
					$("#facebookstuff").append("<div class=\"tabme\">" + fsJSON.response.venues.items[i].venue.location.lat + "," + fsJSON.response.venues.items[i].venue.location.lng + "</div>");
					var checkinLocation = new google.maps.LatLng(fsJSON.response.venues.items[i].venue.location.lat, fsJSON.response.venues.items[i].venue.location.lng);
					var theTimes = fsJSON.response.venues.items[i].beenHere;
					var theContent = "<div>" + fsJSON.response.venues.items[i].venue.name + "</div><div>Been here " + theTimes + " times.</div>";
					var infowindow = new google.maps.InfoWindow({
						content: theContent
					});
					(function myFoursquarePlots(content){
						var image = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/icon-36x36.png", //the image URL
									new google.maps.Size(20,20), //the sprite size
									new google.maps.Point(0,0), //the sprite offset (x,y)
									new google.maps.Point(10,20), //  anchor
									new google.maps.Size(20,20) // set scaled size
									);
						var shadow = new google.maps.MarkerImage("http://re.adyma.de/wp-content/themes/readymade2011/images/fbshadow.png", //the image URL
									new google.maps.Size(30,18), //the sprite size
									new google.maps.Point(0,0) //the sprite offset (x,y)
									);								
						var theMarker = new google.maps.Marker({
											position: checkinLocation,
											map: map,
											animation: google.maps.Animation.DROP,
											icon: image,
											zIndex: 2
						});
						var theShadow = new google.maps.Marker({
											position: checkinLocation,
											map: map,
											animation: google.maps.Animation.DROP,
											icon: shadow,
											zIndex: 1
						});
						google.maps.event.addListener(theMarker, 'click', function() {
							infowindow.setContent(content);
							infowindow.open(map,theMarker);
						});		
					}(theContent));				
				};
			}
		});
	});
/*
	$("").click(function(){
	
	});
	$("").click(function(){
	
	});
*/

 




/* closes (document)ready */
 });
