var FBConnect = {

	/*
	* If this value is true alert boxes will be raised when various
	* types of configuration errors are detected.
	*/
	debugVerbose : (window.location.href.indexOf('fbc_verbose_debug') != -1),

	initialized : false,

	init : function(api_key, plugin_path, streamPublish, home_url, wp_user, app_config) {

		if (!api_key) {
			FBConnect.error("api_key is not set");
		}

		if (!plugin_path) {
			FBConnect.error("plugin path not provided");
		}

		// Check for properly configured template - note this test fails in IE!
		if (this.debugVerbose) {
			var html_tag = document.getElementsByTagName('html').item(0);
			if (html_tag.getAttribute('xmlns:fb') === null) {
				FBConnect.error('xmlns:fb not defined on html tag - check your templates');
			}
		}

		FBConnect.home_url = home_url || "/";
		FBConnect.plugin_path = plugin_path;
		FBConnect.wp_user = wp_user;
	
		FB.init(api_key, plugin_path + "xd_receiver.php", app_config);
	
		FBConnect.initialized = true;
	},

	appconfig_reload : {
		reloadIfSessionStateChanged: true
	},
	
	appconfig_none : {},
	
	appconfig_ajaxy : {
		ifUserConnected : fbc_onlogin_noauto,
		ifUserNotConnected : fbc_onlogout_noauto
	},
	
	logout : function() {
		FB.ensureInit(function() {
			FB.Connect.logout();
		});
	},
	
	redirect_home : function() {
		window.location = FBConnect.home_url;
	},

	/*
	wordpress specific functions
	*/
	setup_feedform : function() {

		var comment_form = ge('commentform');
		if (!comment_form) {
			FBConnect.error('unable to locate id=commentform');
			return;
		}

		var new_subbutton = '';
		var subbutton = comment_form.getElementsByTagName('input');
		for (var i=0; i<subbutton.length; i++) {
			if (subbutton[i].type=='submit') {
				new_subbutton = subbutton[i];
			}
		}
		if (new_subbutton.id == 'submit' || new_subbutton.name == 'submit' || (new_subbutton.id == 'submit' && new_subbutton.name == 'submit')) {
			new_subbutton.setAttribute('id', 'fbc_submit_hack');
			new_subbutton.setAttribute('name', 'fbc_submit_hack');
		}

		var share = '';
		var share = ge('share');
		share.onclick = function() {
			if (this.className === 'yes') {
				this.className = 'no';
			} else {
				this.className = 'yes';
			}
			return false;
		}

		comment_form.onsubmit = function() {
			return FBConnect.show_comment_feedform();
		}

	},

	show_comment_feedform : function() {

		var default_post_img = 'http://www.georgehess.com/blog/wp-content/themes/default/images/default_post_img.jpg';
		var post_content = '';
		var post_content = ge('content');
		var post_img = '';
		var post_img = post_content.getElementsByTagName('img');
		if (post_img.length > 0) {
			post_img = post_img[0].src;
		} else {
			post_img = default_post_img;
		}

		var template_data = {
			'post-url': window.location.href,
			'post-img': post_img,
			'post-title': FBConnect.article_title,
			'post-desc': FBConnect.article_desc,
			'blog-name': FBConnect.blog_name,
			'blog-url': FBConnect.home_url
		};

		var comment_text = '';

		var commentform = ge('commentform');
		if (commentform) {
			// if this isn't present something is seriously wrong
			var comment_box = commentform.comment;
			if (comment_box) {
				comment_text = comment_box.value;
			} else {
				FBConnect.error('unable to locate comment textarea');
				return true;
			}
		} else {
			FBConnect.error('unable to locate comment form, expected id=commentform');
			return true;
		}

		var share = '';
		var share = ge('share');
		if (comment_text.length === 0 || share.className === 'no') {
			// allow normal submit to complete
			return true;
		}

		var attachment = {'name':template_data['post-title'],'href':template_data['post-url'],'description':template_data['post-desc'],'media':[{'type':'image','src':template_data['post-img'],'href':template_data['post-url']}]};
		FB.ensureInit(function () {
			FB.Connect.streamPublish(comment_text, attachment, null, null, null, function(){
				commentform.submit();
			});
		});

		// submit handled by showFeedDialog
		return false;

	},

	error : function(msg) {
		if (FBConnect.debugVerbose) {
			var emsg = 'Error: ' + msg;
			alert(emsg);
		}
	}

};

// end FBConnect

function fbc_onlogout_noauto() {
	fbc_set_visibility_by_class('fbc_hide_on_login', '');
	fbc_set_visibility_by_class('fbc_hide_on_logout', 'none');
}

function fbc_onlogin_noauto() {
	fbc_set_visibility_by_class('fbc_hide_on_login', 'none');
	fbc_set_visibility_by_class('fbc_hide_on_logout', '');
	FBConnect.setup_feedform();
}

function fbc_set_visibility_by_class(cls, vis) {
	var res = document.getElementsByClassName(cls);
	for(var i = 0; i < res.length; ++i) {
		res[i].style.visibility = vis;
	}
}

function ge(elem) {
	return document.getElementById(elem);
}