function setupfacebook(){
	//alert('Setting Up Facebook');
	window.fbAsyncInit = function() {
		FB.init({
				appId: '557c472051768a17ac1dee6209b7c914', 
				status: true, 
				cookie: true, 
				xfbml: true});

		/* All the events registered */
		FB.Event.subscribe('auth.login', function(response) {
			// do something with response
			login();
		});
		
		FB.Event.subscribe('auth.logout', function(response) {
			// do something with response
			logout();
		});

		FB.getLoginStatus(function(response) {
			if (response.session) {
				// logged in and connected user, someone you know
				login();
			}
		});
	};
	(function() {
		var e = document.createElement('script');
		e.type = 'text/javascript';
		e.src = document.location.protocol +
			'//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());
}

function login(){
	FB.api('/me', function(response) {
		addfbuserinfo();
		try{
			document.getElementById('fblogtriggers').innerHTML = '<a href="javascript:triggerlogout()"><img src="http://www.cplanet.com/globalgraphics/general/fblogout.png" border="0" /></a>';
		}catch(err){
			//alert('No Facebook Login Div Located');	
		}
	});
}
function logout(){
	document.getElementById('fblogtriggers').innerHTML = '<a href="javascript:triggerlogin()"><img src="http://www.cplanet.com/globalgraphics/general/fblogin.png" border="0" /></a>';
	clearfbuserinfo();
}

function setsocialmedia(fbxid){
	if(fbxid){
		addafcommentbox(fbxid)
	}
	setupfacebook();
}

function addafcommentbox(fbxid){
	
	var fbcommentsbox = '<fb:comments xid="'+fbxid+'" numposts="25" width="700" id="fbcomments"></fb:comments>';
	//alert(fbcommentsbox);
	document.getElementById('fbcommentdiv').innerHTML = fbcommentsbox;	
}

function triggerlogin(){
	//alert('Should open login box');
	FB.login(function(response) {
	  if (response.session) {
		  addfbuserinfo();
	  } else {
		//alert('user cancelled login');
	  }
	});
}

function addfbuserinfo(){
	try{
		FB.api('/me', function(response) {
		 var query = FB.Data.query('select name, hometown_location, sex, pic_square, email from user where uid={0}', response.id);
		 	query.wait(function(rows) {				 
				setlocalfb(rows[0].pic_square, rows[0].name);
			});
		});
	  }catch(err){
		//No Action  
	  }	
}

function clearfbuserinfo(){
	try{
		clearlocalfb();
	}catch(err){
		//No Action  
	}	
}

function triggerlogout(){
	//alert('Should open login box');
	FB.logout(function(response) {
	  if (response.session) {
		alert('user still logged in');
	  } else {
		logout();
	  }
	});
}

function fbstreamPublish(fbtype, name, description, fbtextlink, fbimagesrc, fbimagelink, hrefLink, hrefTitle, userPrompt, cpmessage){

	//alert(fbtype +'\n'+ name +'\n'+ description +'\n'+ fbtextlink +'\n'+ fbimagesrc +'\n'+ fbimagelink +'\n'+ hrefLink +'\n'+ hrefTitle);

	switch(fbtype){
		
		default:
			var attachment = { 
				'name': (name), 
				'description': (description), 
				'href': (fbtextlink),
				'media': [{ 'type': 'image', 
					'src': (fbimagesrc), 
					'href': (fbimagelink)}] };
		break;
		
	}
	 
		
	FB.ui(
	{
		method: 'stream.publish',
		message: (cpmessage),
		attachment: (attachment),
		action_links: [
			{ text: hrefTitle, href: hrefLink }
		],
		user_prompt_message: userPrompt
	},
	function(response) {
		
	});

}

function fbpicshare(name, description, imagesrc){
	
	//alert(name +'\n'+ description +'\n'+ imagesrc);
	if(!name){
		var newname = "Image from Cthere Site: " + 	document.getElementById("HiddenDestinationName").value;
	}else{
		var newname = name + " - from Cthere Site: " + 	document.getElementById("HiddenDestinationName").value;
	}
	
	var hrefLink = "http://www.cthere.com";
	var hrefTitle = "Create a Cthere Site"
		
	var fbtextlink = "http://www.cplanet.com/cthere.aspx?destid=" + document.getElementById("HiddenDestinationID").value;
	var fbimagelink = imagesrc;
	var userPrompt = "Create a Cthere Site"
	
	fbstreamPublish('picture', newname, description, fbtextlink, imagesrc, fbimagelink, hrefLink, hrefTitle, userPrompt)
	
}

function fbgalshare(name, description, fbgallink){
	
	//alert(name +'\n'+ description +'\n'+ fbgallink);
	
	if(!name){
		var newname = "Gallery from Cthere Site: " + 	document.getElementById("HiddenDestinationName").value;
	}else{
		var newname = name + " - from Cthere Site: " + 	document.getElementById("HiddenDestinationName").value;
	}
	
	if(!document.getElementById("HiddenDestinationLogo").value){
		var imagesrc = "http://www.cplanet.com/globalgraphics/general/cplanet_logo.jpg";
	}else{
		var imagesrc = document.getElementById("HiddenDestinationLogo").value;
	}
	
	var imagelink = fbgallink;
	
	var hrefLink = "http://www.cthere.com";
	var hrefTitle = "Create a Cthere Site"
		
	var fbtextlink = "http://www.cplanet.com/cthere.aspx?destid=" + document.getElementById("HiddenDestinationID").value;
	var userPrompt = "Create a Cthere Site";
	
	fbstreamPublish('gallery', newname, description, fbtextlink, imagesrc, imagelink, hrefLink, hrefTitle, userPrompt);
	
}


//stream publish method
function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
	FB.ui(
	{
		method: 'stream.publish',
		message: '',
		attachment: {
			name: name,
			caption: '',
			description: (description),
			href: hrefLink
		},
		action_links: [
			{ text: hrefTitle, href: hrefLink }
		],
		user_prompt_message: userPrompt
	},
	function(response) {

	});

}
function showStream(){
	FB.api('/me', function(response) {
		//console.log(response.id);
		streamPublish(response.name, 'Cplanet.com helps you explore the world.', 'Testing Facebook Connect', 'http://www.cplanet.com', "What planet are you on?");
	});
}

function share(){
	var shareurl = window.location.href
	var share = {
		method: 'stream.share',
		u: shareurl
	};
	
	//alert(shareurl);
	
	FB.ui(share, function(response) { console.log(response); });
}

function graphStreamPublish(){
	var body = 'Reading New Graph api & Javascript Base FBConnect Tutorial from Thinkdiff.net';
	
	var attachment = { 
		'name': 'i\'m bursting with joy', 
		'description': 'a funny looking cat', 
		'media': [{ 'type': 'image', 
			'src': 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg', 
			'href': 'http://bit.ly/187gO1'}] }; 

	FB.api('/me/feed', 'post', { message: body, attachment: attachment}, function(response){
		if (!response || response.error) {
			alert('Error occured');
		} else {
			alert('Post ID: ' + response.id);
		}
	});
}

function fqlQuery(){
	FB.api('/me', function(response) {
		 var query = FB.Data.query('select name, hometown_location, sex, pic_square, email from user where uid={0}', response.id);
		 query.wait(function(rows) {
		   
		   var fbuserinfo =
			 'Your name: ' + rows[0].name + "\n" +
			 '<img src="' + rows[0].pic_square + '" alt="" />' + "\n" +
			 'You are : ' + rows[0].sex + "\n" +
			 'You are from : ' + rows[0].hometown_location + "\n" +
			 'Your Email Address is : ' + rows[0].email + "\n";
			 
			 alert(fbuserinfo);
		 });
	});
}

function setStatus(){
	status1 = document.getElementById('status').value;
	FB.api(
	  {
		method: 'status.set',
		status: status1
	  },
	  function(response) {
		if (response == 0){
			alert('Your facebook status not updated. Give Status Update Permission.');
		}
		else{
			alert('Your facebook status updated');
		}
	  }
	);
}
