//deze var geeft aan of er minimaal 1 service connected is
var connected = false;

//deze var geeft aan of de positie op de kaart is gezet
var mapped = false;

// is het een testomgeving? dan posten we niets naar facebook e.d.
var testing = false;

// laatste netwerk met OK?
var socialType = false;
var socialAvatar = false;
var socialName = false;
var pageState = "login";
var altType = "none";

//in socialServices wordt de status en belangrijke vars van een service opgeslagen
var socialServices = Array();
socialServices[socialServices.length] = {type: "facebook", connected: 0, user_id: 0};
socialServices[socialServices.length] = {type: "hyves",connected: 0, user_id: 0};
socialServices[socialServices.length] = {type: "twitter", connected: 0, user_id: 0};

//open een popup met deze functie
function pop(url) {
	window.open(url,"pop", "width=500,height=450");
}

// on marker change (drag on map), the position is stored in the system
// - no return actions
function updateVoornemen(markerPostition) {
	
	console_log("Updating voornemen: "+voornemenID);
	console_log(markerPosition.Ba); // lat
	console_log(markerPosition.Ca); // lon

	$.getJSON("/wp-content/plugins/social-sharer/social-save-resolution.php", 
	{
		geoact: 'update',
		resolution_id: voornemenID,
		lat: markerPosition.Ba,
		lon: markerPosition.Ca
	},
	function(data) {
		console_log(data);
	});
}

// save a new resolution, will be called when the map is shown for the first time.
// - when resolution is saved, the provided callback function is called.
function insertVoornemen(callback) {
	console_log(voornemenID);

	$.getJSON("/wp-content/plugins/social-sharer/social-save-resolution.php", 
	{
		geoact: 'insert',
		resolution_id: voornemenID,
		lat: '',
		lon: ''
	},
	function(data) {
		console_log(data);
		callback();
	});
}


//callback functie van authenticaties
function Authenticated(type) {
	socialType = type;
	console_log(pageState);
	if(pageState == "login") {
		$("#stap01-disconnected").hide();
		showLoading();
		checkConnections();
	} else {
		if(socialType == "none") {
			socialType = altType;
			altType = "none";
		}
		socialCheckBox(socialType);
		socialActive(socialType);
	}
}

function socialCheckBox(type) {
	
	if (typeof _gaq != 'undefined'){
		_gaq.push(['_trackEvent', 'home', 'connect-via-'+type]);
	}
	
	console_log("check: "+type);
	$("input[name=cb-"+type+"]").attr('checked', true);
}
function socialInactive(type) {
	console_log("inactive: "+type);
	$("#connect-"+type+" img").fadeTo('slow',0.5);
	$("#connect-"+type+" img").removeClass('active');
}
function socialActive(type) {
	//console_log("active: "+type);
	$("#connect-"+type+" img").fadeTo('slow',1);
	$("#connect-"+type+" img").addClass('active');
}
function changeCheckBox(element) {
	if($(element).is(':checked') != true) {
		$(element).attr('checked', true);
	} else {
		$(element).attr('checked', false);
	}
	/*
	tempID = $(element).attr('id');

	console_log('tid: '+tempID+", checked? "+$(element).is(':checked'));
	if($(element).is(':checked') != true) {
		$(element).attr('checked', false);
	console_log('after - tid: '+tempID+", checked? "+$(element).is(':checked'));
		return true;
	} else {
		if(tempID == "cb-hyves") { sType = "hyves"; }
		if(tempID == "cb-facebook") { sType = "facebook"; }
		if(tempID == "cb-twitter") { sType = "twitter"; }
		if(!$("#connect-"+sType+" img").hasClass('active')) {
			$(element).attr('checked', false);
			pop('/wp-content/plugins/social-sharer/mahieu/authenticate.php?type='+sType);
		}
	console_log('after - tid: '+tempID+", checked? "+$(element).is(':checked'));
		return false;
	}
	*/
}

function changeCheckButton(element) {
	tempID = $(element).attr('id');
	if(tempID == "connect-hyves") { sType = "hyves"; }
	if(tempID == "connect-facebook") { sType = "facebook"; }
	if(tempID == "connect-twitter") { sType = "twitter"; }
	cbID = "input[name=cb-"+sType+"]";
	console_log('tid: '+tempID+", checked? "+$(cbID).is(':checked'));
	if($(cbID).is(':checked')) {
		$(cbID).attr('checked', false);
		return true;
	} else {
		if(!$("#connect-"+sType+" img").hasClass('active')) {
			$(cbID).attr('checked', false);
			altType = sType;
			pop('/wp-content/plugins/social-sharer/mahieu/authenticate.php?type='+sType);
		} else {
			$(cbID).attr('checked', true);
		}
		return false;
	}
}



//controleer welke services beschikbaar zijn
function checkConnections()
{
	//console_log("pageState"+pageState);
	var networkCount = 0;
	connected = false;
	
	for(var i = 0; i < socialServices.length; i++)
	{
		//verify of service actief is
		//console_log("Checking: "+socialServices[i].type)
		$.getJSON("/wp-content/plugins/social-sharer/mahieu/verify.php", 
		{ 
			type: socialServices[i].type
		},
		function(data) {
			//console_log(data);
			if(data.connected == 1)
			{
				connected = true;
				
				for(var j = 0; j < socialServices.length; j++){
					if(socialServices[j].type == data.type)
					{
						socialServices[j].connected = 1;
						socialServices[j].user_id = data.user_id;
						if(pageState == "loadMap") {
							socialActive(socialServices[j].type);
							socialCheckBox(socialServices[j].type);
						}
					}
				}
			} else {
				if(pageState == "loadMap") {
					socialInactive(data.type);
				}
			}

			networkCount++;
			if(networkCount == socialServices.length)
			{
				if(pageState != "loadMap" && pageState != "mapLoaded") {
					checkEmail();
				}
				if(pageState == "loadMap") {
					pageState = "mapLoaded"
					showMap();
				}
			}
		});
	}

}

//adds a wordpress user
function addUser(network,network_userID,email,avatar,name,callback)
{	
	if(network != "none" && network_userID)
	{
		console_log("post add-user: network:"+network+", userid: "+network_userID+", email: "+email+", avatar:" +avatar+", name: "+name);
		$.post("/wp-content/plugins/social-sharer/social-add-user.php", 
		{
			network: network,
			network_userID: network_userID,
			email: email,
			avatar: avatar,
			name: name
		},
		function(data) {
			console_log("Adduser data");
			console_log(data.user_id);
			temp_userid = data.user_id;
			callback();
		},
		"json");
	}
	
	return false;
	
}

// frontend to adduser providing input from form
function addEmail() {
	var socialemail = $("#social_user_email").val();
	addUser(socialType,temp_userid,socialemail,socialAvatar,socialName,loadMap);
}

function saveLocation() {
	// script to save the geolocation of the marker.
	// oncomplete:
	mapped = true;
	showLoading();
	showShare();
}

// function to loop through all checked (and authenticated) social services
// and call "share" with the necessary parameters
function deelVoornemen()
{
	if (typeof _gaq != 'undefined'){
		_gaq.push(['_trackEvent', 'home', 'deel-nu']);
	}
//	testing = false;
	console_log(testing);
	
	if(!testing) {
		var networkCount = 0;
		for(var i = 0; i < socialServices.length; i++)
		{
			console_log(socialServices[i]);
			if(socialServices[i].connected) {
				element = cbID = "input[name=cb-"+socialServices[i].type+"]";
				if($(element).is(':checked') == true) {
					networkCount++;
				}
			}
		}
		
		$("#voornemens #stap02-map").stop().animate({
				marginTop:-500
			}, {
				duration: 500,
				specialEasing: {
				marginTop:'easeOutBack'
			}, 
			complete: function() {
				$(this).hide();
				showLoading();
			}
		});
				
		
		var postCount = 0;
		for(var i = 0; i < socialServices.length; i++)
		{
			//console_log("social:" +socialServices[i].connected)
			element = cbID = "input[name=cb-"+socialServices[i].type+"]";
			if($(element).is(':checked') == true) {
				$.post("/wp-content/plugins/social-sharer/mahieu/share.php", 
				{ 
					type: socialServices[i].type,
					resolution_id: voornemenID,
					message: voornemenText+" #voelmegoed #"+voornemenID+" "+baseURL+"/"+voornemenLink,
					image: baseURL+'/wp-content/plugins/buddypress/bp-themes/actimel/_inc/images/resolutions/'+voornemenID+'.png',
					link: baseURL+"/"+voornemenLink
				},
				function(data) {
					postCount++;
					if(postCount == networkCount) {
						// callback: after all posted ajax-calls have returned.
						showThanks();
						
					}
				});
			}
		}
	} else {
		showThanks();
	}
}

function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13)
	{
		//do something
		geoCodePlace($('#inp_place').val());
	return false;
	}
	else
	return true;
} 


