var old_click;
function fbLogout() {
	FB.logout();
	document.getElementById("fblogin").innerHTML = '<div style="margin:24px 0 0 0;"><a href="#empty" onclick="basicConnect();"><img class="click" name="fb_connect" src="images/fb_connect_button.png" alt="Facebook Connect" onmouseover="rollOver(\'fb_connect\',\'images/fb_connect_button_invert.png\')" onmouseout="rollOver(\'fb_connect\',\'images/fb_connect_button.png\')"/></a></div>';
	document.getElementById('has_photos').innerHTML = '<div id="has_photos" style="text-align:center;"><span></span></div>';
	document.getElementById("friends").innerHTML ='<div style="padding:3px 0 2px 3px;"><a class="white_b10" href="#empty" onclick="basicConnect();"><span>Connect to see if your friends have cats in the mosaic</span></a></div>';
}
function fbLoginNoPerms() {
	FB.login(function(response) {
		if (response.authResponse) {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			appApprovedUpload(uid);
		} else {
			//user cancelled login or did not grant authorization
		}
	}, {scope:'email,user_photos'});
}
function fbUploadRequestPerms() {
	FB.login(function(response) {
		if (response.authResponse) {
			FB.api('/me/permissions?&access_token='+response.authResponse.accessToken, function (perms) {
				if(perms.data[0].user_photos && perms.data[0].email){
					fbGetAlbum();
					document.getElementById("fb_req_perms").style.display = "none";
					document.getElementById("fb_albums_loading").style.display = "block";
				}else{
					document.getElementById("fb_req_perms").style.display = "block";
					document.getElementById("fb_albums_loading").style.display = "none";
				}
			});
		} else {
			//user cancelled login or did not grant authorization
			document.getElementById("fb_req_perms").style.display = "block";
			document.getElementById("fb_albums_loading").style.display = "none"
		}
	}, {scope:'email,user_photos'});
}
function fbLoginNoBasic() {
	FB.login(function(response) {
		if (response.authResponse) {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			appApprovedUpload(uid);
		} else {
			//user cancelled login or did not grant authorization
		}
	}, {scope:'email'});
}
function fbLoginNoPermsSplash() {
	FB.login(function(response) {
		if (response.authResponse) {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			var isSafari = (/Safari/.test(navigator.userAgent) || /MSIE 7\.0/i.test(window.navigator.userAgent));
			//var isSafari = (/Safari/.test(navigator.userAgent));
			if(isSafari){
				window.top.location = 'http://www.facebook.com/pages/PictureMosaics/191200890912034?sk=app_204242762924518'
			} else {
				window.location = 'upload.php';
			}
		} else {
			//user cancelled login or did not grant authorization
		}
	}, {scope:'email,user_photos'});
}
function fbLoginNoBasicPerms() {
	FB.login(function(response) {
		if (response.authResponse) {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			appApproved(uid);
		} else {
			//user cancelled login or did not grant authorization
		}
	}, {scope:'email'});
}
function fbGetAlbum() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			FB.api('/me/albums', function(fbResponse) {
				if(fbResponse){
					var sqlString = '';
					var responseAlbums = fbResponse.data;
					var htmlString = '<select onChange="fbGetPhotos(this.options[this.selectedIndex].value); clearOldClick();">';
					for(var i=0; i < responseAlbums.length; i++) {
						thisAlbum = responseAlbums[i];
						if(typeof thisAlbum.count !== "undefined") {
							htmlString += '<option value="' + thisAlbum.id + '">' + thisAlbum.name + '</option>';
						}
					}
					htmlString += '</select>';
					document.getElementById("fbAlbum").innerHTML = htmlString;
					document.getElementById("fb_req_perms").style.display = "none";
					fbGetPhotos(responseAlbums[0].id);
				}else{
					//user has no available albums
				}
			});
		}else{
			//invalid user status
		}
	});
}
function fbGetPhotos(albumID) {
	FB.api('/' + albumID + '/photos', { return_ssl_resources: 1 }, function(fbResponse) {
		var htmlString = '<table class="userPics"><tr>';
		var responsePhoto;
		var width = 0;
		var height = 56;
		var counter = 0;
		var margin_top = 0;
		var margin_left = 0;
		for (var i=0;i<fbResponse.data.length;i++) {
			responsePhoto = fbResponse.data[i];
			width = parseInt(responsePhoto.width);
			height = parseInt(responsePhoto.height);
			if(height == 0){
				height = 56;
			}
			if(width == 0){
				width = 75;
			}
			if(width >= height){
				height = parseInt((height / width) * 75);
				width = 75;
				margin_top = parseInt((81 - height)/2);
			}
			if(height > width){
				width = parseInt((width / height) * 75);
				height = 75;
				margin_top = parseInt((81 - height)/2);
			}
			htmlString += '<td><div class="fb_photo_box" id="fbbox'+i+'" onclick="clickManage(\'fbbox'+i+'\',\''+responsePhoto.images[0].source+'\');" onmouseover="this.style.border=\'1px solid #0065b2\'" onmouseout="this.style.border=\'1px solid #a1a1a1\'"><img style="width:'+width+'px; height:'+height+'px; margin-top:'+margin_top+'px;" src="'+responsePhoto.images[3].source+'" width="'+width+'px" height="'+height+'px" alt="Picture '+i+'"/></div></td>';
			//htmlString = htmlString+'<img class="click center_img" style="width:'+width+'px; height:'+height+'px;" src="'+response[i].src_small+'" width="'+width+'px" height="'+height+'px" alt="Picture '+i+'"/>';
			if(counter == 4){
				htmlString += '</tr><tr>';
				counter = 0;
			} else {
				counter++;
			}
		}
		htmlString += '</tr></table>';
		//alert(htmlString);
		//document.getElementById("fb_container").style.visibility="visible";
		//document.getElementById("fb_container").style.display="block";
		document.getElementById("fbPhoto").innerHTML = htmlString;
		document.getElementById("fb_albums_loading").style.display = "none";
	});
}
function clickManage(new_click,image_path) {
	if (old_click == null) {
		//document.getElementById(new_click).style.border="1px solid #0065b2";
		document.getElementById(new_click).style.background="#d2ecff";
	} else {
		//document.getElementById(new_click).style.border="1px solid #0065b2";
		document.getElementById(new_click).style.background="#d2ecff";
		//document.getElementById(old_click).style.border="1px solid #a1a1a1";
		document.getElementById(old_click).style.background="#ffffff";
	}
	document.form1.fb_file.value=image_path;
	document.form1.fb_upload.value="1";
	document.form1.myinputfilepath.value=browse_for;
	old_click=new_click;
}
function clearOldClick() {
	old_click=null;
	document.form1.fb_upload.value="0";
}
function checkStatus() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			if(!user_visible){
				appApprovedUpload(uid);
			}else{
				appUpload();
			}
		} else if (response.status === 'not_authorized') {
			// the user is logged in to Facebook, but not connected to the app
			fbLoginNoPerms();
		} else {
			// the user isn't even logged in to Facebook.
			if(source == 'fb'){
				alert("Please Login to facebook first");
			} else{
				fbLoginNoPerms();
			}
		}
	});
}
function checkUpload() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			FB.api('/me/permissions', function (response) {
    			alert("User permissions response: " + response);
			} );
		} else if (response.status === 'not_authorized') {
			// the user is logged in to Facebook, but not connected to the app
			uploadPerms();
		} else {
			// the user isn't even logged in to Facebook.
			if(source == 'fb'){
				alert("Please Login to facebook first");
			} else{
				uploadPerms();
			}
		}
	});
}
function checkBasic() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			if(!user_visible){
				appApprovedUpload(uid);
			}else{
				appUpload();
			}
		} else if (response.status === 'not_authorized') {
			// the user is logged in to Facebook, but not connected to the app
			fbLoginNoBasic();
		} else {
			// the user isn't even logged in to Facebook.
			if(source == 'fb'){
				alert("Please Login to facebook first");
			} else{
				fbLoginNoBasic();
			}
		}
	});
}
function checkStatusSplash() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			FB.api( { method : 'fql.query',
			  	  query : 'SELECT email,user_photos FROM permissions WHERE uid=' + uid
			},
			function(response) {
				if(response[0].user_photos) {
					window.location = 'upload.php';
				} else {
					fbLoginNoPermsSplash();
				}
			});
		} else if (response.status === 'not_authorized') {
			// the user is logged in to Facebook, but not connected to the app
			fbLoginNoPermsSplash();
		} else {
			// the user isn't even logged in to Facebook.
			if(source == 'fb'){
				alert("Please Login to facebook first");
			} else{
				fbLoginNoPermsSplash();
			}
		}
	});
}
function basicConnect() {
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
			appApproved(uid);
		} else if (response.status === 'not_authorized') {
			// the user is logged in to Facebook, but not connected to the app
			fbLoginNoBasicPerms();
		} else {
			// the user isn't even logged in to Facebook.
			if(source == 'fb'){
				alert("Please Login to facebook first");
			} else{
				fbLoginNoBasicPerms();
			}
		}
	});
}
function setUserInfo(id,user_email,first,last) {
	fb_id = id;
	email = user_email;
	fname = first;
	lname = last;
}
function setFriendList(friend_list) {
	friends = friend_list;
}
