// JavaScript Document
/*************************************************
TweeterClubs
************************************************/

var GCURRENT_URL = get_current_url();
var HASHTAG_LENGTH = 140;
var IS_ADMIN = 0;
var IS_MEMBER = 0;

$(document).ready(function() {
	if (First_id != 0)
		$('#show_more_tweets_div').show();
	else
		$('#show_more_tweets_div').hide();
		
	if (GCLUB_NAME != "") {
		HASHTAG_LENGTH = 140 - (GCHAT_TAG.length + 2 + 3);
		
		$('li.right_sub_list_item').filter(function(){	
			return $(this).text() == GCLUB_NAME;
		}).addClass('right_list_item_selected');
		
		$('#all_clubs').removeClass('right_list_item_selected');
		
		$('#club_info_div').slideDown();
		
		if (User_name != "") {
			is_member();
			
		
		}
		else {
			$('#tweetbox_div').hide();
			$('#searchbox_div').show();
		}
	};
	
	//Initilizing all light boxes
	terms_of_use_lb();
	learn_more_lb();
	contact_lb();
	create_club_lb();
	update_club_lb();
	
	$('li.right_list_item, li.right_sub_list_item, li.deleted_club_item').hover(
		function() {
			$(this).addClass('right_list_item_hover');
		},
		function() {
			$(this).removeClass('right_list_item_hover');
		}
	);
	
	$('#translate').hover(
		function() {
			$(this).addClass('translate_hover');		
		},
		function() {
			$(this).removeClass('translate_hover');
		}
	);
	
	$('#tweet_item_list > li.list_item').hover(
		function(){
			$(this).find('ul.message_actions').fadeIn('fast');
		}, function(){
			$(this).find('ul.message_actions').fadeOut('fast');
		}
	);
	
	$('#join_club_div').hover(
		function() {
			$(this).addClass('underlined');
		},
		function() {
			$(this).removeClass('underlined');
		}
	);
	
	$('#submit_search_form, #submit_tweet_box').hover(
		function() {
			$(this).addClass('button_hover');
		},
		function() {
			$(this).removeClass('button_hover');
		}
	);
	
	$('#btn_create').hover(
		function() {
			$(this).addClass('btn_create_hover');
		},
		function() {
			$(this).removeClass('btn_create_hover');
		}
	);
	
	$('.club_info_membership_list_item_link').hover(
		function() {
			$(this).addClass('right_list_item_hover');
		},
		function() {
			$(this).removeClass('right_list_item_hover');
		}
	);
	
	$('#club_info_header').click(function() {
		$('#club_info_list').slideToggle("fast", function() {
			if($('#club_info_header').hasClass('section_header_div_expanded')) {
				$('#club_info_header').removeClass('section_header_div_expanded');
				$('#club_info_header').addClass('section_header_div_collapsed');
			}
			else {
				$('#club_info_header').removeClass('section_header_div_collapsed');
				$('#club_info_header').addClass('section_header_div_expanded');
			}
		})
	});
	
	$('#translate').click(function() {
		$(this).addClass('translated_header');
		translate();
/*		if ($(this).hasClass('translate_header')) {
			translate();
			$(this).removeClass('translate_header').addClass('translated_item').text('Translated');
		}
*/	});
	
	$('#leave_club').click(function() {
		var answer = confirm("You are about to delete your membership in club " + GCLUB_NAME);
		if (answer) {
			$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_membership",
				{
					user_name: User_name,
					club_name: GCLUB_NAME
				},
				function(json) {
					if (json.error == undefined) {
						//update_membership_clubs();
						//build_club_list();
						//$('#rss_div').hide();
						window.location = get_tc_home_url();
					}
					else {
						alert ("Due to technical difficulties, your membership could not be deleted.");
					}
				},
				"json"
			);
		}
		
	});
	
	$('li.deleted_club_item').click(function() {
		$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_membership",
			{
				user_name: User_name,
				club_name: $(this).attr("id")
			},
			function(json) {
				if (json.error == undefined) {
					update_membership_clubs();
					//build_club_list();
				}
				else {
					alert ("Due to technical difficulties, your membership could not be deleted.");
				}
			},
			"json"
		);
	});
	
	$('#delete_club').click(function() {
		var answer = confirm("You are about to delete your club " + GCLUB_NAME + ". This club may have members.");
		if (answer) {
			$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_club",
				{
					club_name: GCLUB_NAME
				},
				function(json) {
					if (json.error == undefined) {
						//update_my_clubs();
						//build_club_list();
						window.location = get_tc_home_url() + "index.php/tweeterclubs/render_main_authenticated";
					}
					else {
						alert ("Due to technical difficulties, your club could not be deleted.");
					}
				},
				"json"
			);
		}
		
	});
	
	$('#join_club_div').click(function() {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_join_club",
			{
				user_name: User_name,
				club_name: GCLUB_NAME
			},
			function(json) {
				if (json.error == undefined) {
					$('#join_club_div').slideUp();
				}
				else {
					alert("We are sorry, but we could not join you to the club. Please try again later.");
				}
				update_membership_clubs();
				$('#leave_club_list').slideDown();
			}
		);
	/*	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_membership_clubs",
			{
				user_name: User_name
			},
			function(json) {
				var json_length = json.length;
				var membership_club_list = "";
				for (i = 0; i < json_length; i++) {
					if (json[i].status == "active") 
						if(json[i].name == GCLUB_NAME)
							membership_club_list += '<li class="right_sub_list_item right_list_item_selected" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
						else
							membership_club_list += '<li class="right_sub_list_item" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
					else if (json[i].status == "deleted")
						membership_club_list += '<li class="deleted_club_item" id="' + json[i].name + '">' + json[i].name + '<br /><span id="remove_from_list">Deleted Club. Click to remove from list.</span></li>';
				}
				$('#member_of_clubs_list').html(membership_club_list);
				$('li.right_sub_list_item').bind({
					click: function(){
						build_tweet_list($(this).text(), $(this).attr("id"));
					}
				});
				$('li.right_sub_list_item').hover(
					function() {
						$(this).addClass('right_list_item_hover');
					},
					function() {
						$(this).removeClass('right_list_item_hover');
					}
				);
			}
		);*/
	});
	
	$('#show_newer_tweets_div').click(function() {
		$('#show_newer_tweets_div').fadeOut('fast');
		New_tweet_counter = 0;
		$('.hidden_list_item').each(function() {
			$(this).removeClass('hidden_list_item');
		});
	});
	
	$('#show_more_tweets_div').click(function() {
		get_more_tweets();
	});
	
	$('.block_user').click(function() {
		block_user($(this).attr("id"));
	});
	
	$('.delete_tweet').click(function() {
		delete_tweet($(this).attr("id"));
/*		var answer = confirm("Are you sure you want to delete this message? This can not be undone!");
		if (answer) {
			$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_tweet",
				{
					club_name: GCLUB_NAME,
					message_id: $(this).attr("id")
				},
				function() {
					build_tweet_list(GCLUB_NAME, GCHAT_TAG);
				}
			);
		}
*/	});

	$('.reply_to_tweet').click(function() {
		reply_to_tweet($(this).attr("id"));
	});
	
	$('.retweet_tweet').click(function() {
		retweet_tweet($(this).attr("id"));
	});
	
	$('#my_clubs').click(function() {
		$('#my_clubs_list').slideToggle("fast", function() {
			if($('#my_clubs').hasClass('right_list_item_header_expanded')) {
				$('#my_clubs').removeClass('right_list_item_header_expanded');
				$('#my_clubs').addClass('right_list_item_header_collapsed');
			}
			else {
				$('#my_clubs').removeClass('right_list_item_header_collapsed');
				$('#my_clubs').addClass('right_list_item_header_expanded');
			}
		})
	});
	
	$('#member_of_clubs').click(function() {
		$('#member_of_clubs_list').slideToggle("fast", function() {
			if($('#member_of_clubs').hasClass('right_list_item_header_expanded')) {
				$('#member_of_clubs').removeClass('right_list_item_header_expanded');
				$('#member_of_clubs').addClass('right_list_item_header_collapsed');
			}
			else {
				$('#member_of_clubs').removeClass('right_list_item_header_collapsed');
				$('#member_of_clubs').addClass('right_list_item_header_expanded');
			}
		})
	});
	
	$('#club_central_header').click(function() {
		$('#club_central_list').slideToggle("fast", function() {
			if($('#club_central_header').hasClass('section_header_div_expanded')) {
				$('#club_central_header').removeClass('section_header_div_expanded');
				$('#club_central_header').addClass('section_header_div_collapsed');
			}
			else {
				$('#club_central_header').removeClass('section_header_div_collapsed');
				$('#club_central_header').addClass('section_header_div_expanded');
			}		
		});
	});
	
	$('#admin_central_header').click(function() {
		$('#admin_central_list').slideToggle("fast", function() {
			if($('#admin_central_header').hasClass('section_header_div_expanded')) {
				$('#admin_central_header').removeClass('section_header_div_expanded');
				$('#admin_central_header').addClass('section_header_div_collapsed');
			}
			else {
				$('#admin_central_header').removeClass('section_header_div_collapsed');
				$('#admin_central_header').addClass('section_header_div_expanded');
			}		
		});
	});
	
	$('li.right_sub_list_item').click(function() {
		//console.info("home url: " + get_tc_home_url());
		//console.info("text: " + $(this).text());
		window.location = get_tc_home_url() + $(this).text() + "/index.php/tweeterclubs/render_main_authenticated"; 
	});
	
/*	$("a.club_listing_link, li.right_sub_list_item").click(function(){
		build_tweet_list($(this).text(), $(this).attr("id"));
		var club_name = $(this).text();
		$('#my_clubs_list, #member_of_clubs_list').children().removeClass('right_list_item_selected');
		//console.dir($('li.right_sub_list_item').attr("id",chat_tag));
		$('li.right_sub_list_item').filter(function(){	
			return $(this).text() == club_name;
		}).addClass('right_list_item_selected');
	});
*/	
	$('#all_clubs').click(function() {
		if (User_name != "")
			window.location = get_tc_home_url() + "index.php/tweeterclubs/render_main_authenticated";
		else
			window.location = get_tc_home_url();
/*		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_clubs",
			function(json) {
				//alert("Sucess!");
				var json_length = json.length;
				var club_list = '<div class="listing_header">Club Listing</div>'; 
				club_list += '<ol id="club_item_list" class="listing_list">';
				
				for (i = 0; i < json_length; i++) {
					(json[i].club_icon_url == "NULL") ? club_icon_url="" : club_icon_url=json[i].club_icon_url;
					(json[i].club_description == "NULL") ? club_description="" : club_description=json[i].club_description;
					club_list += '<li class="list_item">';
					club_list += '<span class="item_body">'
					club_list += '<span class="item_image">';
					club_list += '<img src="' + club_icon_url + '" width="48px" height="48px" />';
					club_list += "</span>";
					club_list += '<span class="item_text">';
					club_list += '<a href="#" id="' + json[i].chat_tag + '" class="club_listing_link">' + json[i].name+ "</a><br />" + club_description;
					club_list += "</span>";
					club_list += "</span>";
					club_list += "</li>"
				}
				club_list += "</ol>";
				$('#club_listing').html(club_list);
				$('#my_clubs_list, #member_of_clubs_list, #leave_club_list, #delete_club_list').children().removeClass('right_list_item_selected');
				$('#all_clubs').addClass('right_list_item_selected');
				$('#tweet_listing').hide();
				$('#frm_tweetbox').clearForm();
				$('#tweetbox_div').hide();
				$('#join_club_div').slideUp();
				$('#admin_central_div').slideUp();
				$('#club_info_div').slideUp();
				$('#searchbox_div').show();
				$('#club_listing').show();
				update_membership_clubs();
				GCHAT_TAG = "";
				GCLUB_NAME = "";
				$('.club_listing_link').bind({
					click: function(){
						build_tweet_list($(this).text(), $(this).attr("id"));
					}
				});
			}
		);
*/	});
	
	$('#submit_tweet_box').click(function() {
		$("#frm_tweetbox").validate({
			submitHandler: function(form) {
				$.post(
					GCURRENT_URL + "index.php/tweeterclubs/ajax_add_tweet",
					{
						tweet_text: $('#tweet_text').val(),
						hidden_profile_image_url: $('#hidden_profile_image_url').val(),
						hidden_screen_name: $('#hidden_screen_name').val(),
						hidden_chat_tag: $('#hidden_chat_tag').val(),
						hidden_club_name: $('#hidden_club_name').val()
					},
					function(data) {
						if(data.success == undefined)
							alert(data.error);
						else {
							$('#frm_tweetbox').clearForm();
							$('#show_newer_tweets_div').hide();
							build_tweet_list($('#hidden_club_name').val(), $('#hidden_chat_tag').val());
							update_number_tweets();
							update_membership_clubs();
						}
					},
					"json"
				);				
				$('#join_club_div').slideUp();
			}
		});
	});
	
/*	$('#submit_search_form').click(function() {
		$('#frm_search').validate({
			submitHandler: function(form) {
				$.getJSON(
					GCURRENT_URL + "index.php/tweeterclubs/search_tc",
					{
						search_criteria: $('#txt_search').val()
					},
					function(json) {
						if(json.error == undefined && json.results != 0) {
							var json_length = json.length;
							var club_list = '<div class="listing_header">Club Listing</div>'; 
							club_list += '<ol id="club_item_list" class="listing_list">';
							
							for (i = 0; i < json_length; i++) {
								if (json[i].status == "active") {
									if (User_name == "")
										link_val = get_tc_home_url() + json[i].name;
									else
										link_val = get_tc_home_url() + json[i].name + "/index.php/tweeterclubs/render_main_authenticated";
									(json[i].club_icon_url == "NULL") ? club_icon_url="" : club_icon_url=json[i].club_icon_url;
									(json[i].club_description == "NULL") ? club_description="" : club_description=json[i].club_description;
									club_list += '<li class="list_item">';
									club_list += '<span class="item_body">'
									club_list += '<span class="item_image">';
									club_list += '<img src="' + club_icon_url + '" width="48px" height="48px" />';
									club_list += "</span>";
									club_list += '<span class="item_text">';
									club_list += '<a href="' + link_val + '" id="' + json[i].chat_tag + '" class="club_listing_link">' + json[i].name + "</a><br />" + club_description;
									club_list += "</span>";
									club_list += "</span>";
									club_list += "</li>"
								}
							}
							club_list += "</ol>";
							$('#club_listing').html(club_list);
							$('#all_clubs').removeClass('right_list_item_selected');
							$('#tweet_listing').hide();
							$('#tweetbox_div').hide();
							$('#searchbox_div').show();
							$('#frm_search').clearForm();
							$('#club_listing').show();
							$('#club_info_div').slideUp();
*//*							$('.club_listing_link').bind({
								click: function(){
									build_tweet_list($(this).text(), $(this).attr("id"));
								}
							});
*//*						}
						else {
							var json_length = json.length;
							var club_list = '<div class="listing_header">No Results Found</div>'; 
							$('#club_listing').html(club_list);
							$('#all_clubs').removeClass('right_list_item_selected');
							$('#tweet_listing').hide();
							$('#searchbox_div').show();
							$('#club_listing').show();
							$('#club_info_div').slideUp();
						}
					}
				)
			}
		});
	});
*/	
	$('#submit_create_form').click(function() {
		$("#frm_create_club").validate({
			rules: {
				club_name:{
					required: true,
					alphanumeric: true
				},
				chat_tag: {
					required: true,
					hashtag: true
				},
				club_description:"required",
				club_icon_url: {
					url: true
				}
				,
				club_image_url: {
					url: true
				}
			},
			messages: {
			//	club_description:"Please enter a club description"
			},
			submitHandler: function(form) {
				$.post(
					GCURRENT_URL + "index.php/tweeterclubs/ajax_create_club",
					{
						club_name: $('#club_name').val(), 
						chat_tag: $('#chat_tag').val(), 
						admin_user: $('#admin_user').val(),
						club_icon_url: $('#club_icon_url').val(),
						club_image_url: $('#club_image_url').val(),
						club_description: $('#club_description').val()
					},
					function(data) {
						if (data.success == undefined)
							alert("The club name " + $('#club_name').val() + " is already in use.");
						else {
							var club_name = $('#club_name').val();
							$('.overlay').add('#create_club_form_lb').fadeOut('fast', function() {
								$('body').css({'overflow': 'auto'});	
								var admin_user_safe = $('#admin_user').val();
								
								$('#frm_create_club').clearForm();
								$('#admin_user').val(admin_user_safe);
								$('label.error').hide();
							});
							window.location = get_tc_home_url() + club_name + "/index.php/tweeterclubs/render_main_authenticated";
/*							$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_clubs",
								function(json) {
									//alert("Sucess!");
									var json_length = json.length;
									var club_list = '<div class="listing_header">Club Listing</div>'; 
									club_list += '<ol id="club_item_list" class="listing_list">';
									
									for (i = 0; i < json_length; i++) {
										(json[i].club_icon_url == "NULL") ? club_icon_url="" : club_icon_url=json[i].club_icon_url;
										(json[i].club_description == "NULL") ? club_description="" : club_description=json[i].club_description;
										club_list += '<li class="list_item">';
										club_list += '<span class="item_body">'
										club_list += '<span class="item_image">';
										club_list += '<img src="' + club_icon_url + '" width="48px" height="48px" />';
										club_list += "</span>";
										club_list += '<span class="item_text">';
										club_list += '<a href="#" id="' + json[i].chat_tag + '" class="club_listing_link">' + json[i].name+ "</a><br />" + club_description;
										club_list += "</span>";
										club_list += "</span>";
										club_list += "</li>"
									}
									club_list += "</ol>";
									$('#club_listing').html(club_list);
									$('#all_clubs').addClass('right_list_item_selected');
									$('#tweet_listing').hide();
									$('#tweetbox_div').hide();
									$('#searchbox_div').show();
									$('#club_listing').show();
									$('.club_listing_link').bind({
										click: function(){
											build_tweet_list($(this).text(), $(this).attr("id"));
										}
									});
								}
							);
							update_my_clubs();*/
						}
					},
					"json"
				);
			}
		});
	});
	
	$('#submit_update_form').click(function() {
		$("#frm_update_club").validate({
			rules: {
				update_chat_tag: {
					required: true,
					hashtag: true
				},
				update_club_description:"required",
				update_club_icon_url: {
					url: true
				}
				,
				update_club_image_url: {
					url: true
				}
			},
			submitHandler: function(form) {
				$.post(
					GCURRENT_URL + "index.php/tweeterclubs/ajax_update_club",
					{
						club_name: $('#update_club_name').val(), 
						chat_tag: $('#update_chat_tag').val(), 
						club_icon_url: $('#update_club_icon_url').val(),
						club_image_url: $('#update_club_image_url').val(),
						club_description: $('#update_club_description').val()
					},
					function(data) {
						var chat_tag = $('#update_chat_tag').val();
						if (data.success == undefined && (data.error).substring(0,8) != "HTTP 404")
							alert("The club name " + $('#update_club_name').val() + " could not be updated.");
						else {
							$('.overlay').add('#update_club_form_lb').fadeOut('fast', function() {
								$('body').css({'overflow': 'auto'});
								build_tweet_list(GCLUB_NAME, chat_tag);	
								var admin_user_safe = $('#admin_user').val();
								$('#frm_update_club').clearForm();
								$('#admin_user').val(admin_user_safe);
								$('label.error').hide();
							});
							window.location = get_tc_home_url() + GCLUB_NAME;
						}
					},
					"json"
				);
/*				$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_my_clubs",
					{
						user_name: User_name
					},
					function(json) {
						var json_length = json.length;
						var my_club_list = "";
						for (i = 0; i < json_length; i++) {
							if (json[i].status == "active") {
								if (json[i].name == GCLUB_NAME) 
									my_club_list += '<li class="right_sub_list_item right_list_item_selected" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
								else 
									my_club_list += '<li class="right_sub_list_item" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
							}
						}
						$('#my_clubs_list').html(my_club_list);
						$('li.right_sub_list_item').bind({
							click: function(){
								build_tweet_list($(this).text(), $(this).attr("id"));
							}
						});
						$('li.right_sub_list_item').hover(
							function() {
								$(this).addClass('right_list_item_hover');
							},
							function() {
								$(this).removeClass('right_list_item_hover');
							}
						);
					}
				);
*/			}
		});
	});
	
});

function update_number_tweets() {
	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_verify_credentials", function(json){
			if (json.error == undefined) {
				$('#number_tweets').text(json['success'].number_tweets + " tweets");
			}
		});
	}
}

function update_membership_clubs() {
	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_membership_clubs",
			{
				user_name: User_name
			},
			function(json) {
				var json_length = json.length;
				var membership_club_list = "";
				for (i = 0; i < json_length; i++) {
					if (json[i].status == "active") {
						if (json[i].name == GCLUB_NAME)
							membership_club_list += '<li class="right_sub_list_item right_list_item_selected" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
						else
							membership_club_list += '<li class="right_sub_list_item" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
					}
					else if (json[i].status == "deleted") {
						if (json[i].name == GCLUB_NAME)
							membership_club_list += '<li class="deleted_club_item" id="' + json[i].name + '">' + json[i].name + '<br /><span id="remove_from_list">Deleted Club. Click to remove from list.</span></li>';
						else
							membership_club_list += '<li class="deleted_club_item" id="' + json[i].name + '">' + json[i].name + '<br /><span id="remove_from_list">Deleted Club. Click to remove from list.</span></li>';
					}
				}
				$('#member_of_clubs_list').html(membership_club_list);
				$('li.right_sub_list_item').bind({
					click: function(){
						build_tweet_list($(this).text(), $(this).attr("id"));
					}
				});
				$('li.right_sub_list_item').hover(
					function() {
						$(this).addClass('right_list_item_hover');
					},
					function() {
						$(this).removeClass('right_list_item_hover');
					}
				);
				$('li.deleted_club_item').bind({
					click: function() {
						delete_club_item($(this).attr("id"));
					}
				});
				$('li.right_sub_list_item').bind({
					click: function() {
						window.location = get_tc_home_url() + $(this).text() + "/index.php/tweeterclubs/render_main_authenticated";
					} 
				});
			}
		);
	}
}

function update_my_clubs() {
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_my_clubs",
		{
			user_name: User_name
		},
		function(json) {
			var json_length = json.length;
			var my_club_list = "";
			for (i = 0; i < json_length; i++) {
				if (json[i].status == "active")
					my_club_list += '<li class="right_sub_list_item" id="' + json[i].chat_tag + '">' + json[i].name + '</li>';
			}
			$('#my_clubs_list').html(my_club_list);
			$('li.right_sub_list_item').bind({
				click: function(){
					build_tweet_list($(this).text(), $(this).attr("id"));
				}
			});
			$('li.right_sub_list_item').hover(
				function() {
					$(this).addClass('right_list_item_hover');
				},
				function() {
					$(this).removeClass('right_list_item_hover');
				}
			);
		}
	);
}

function build_club_list() {
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_clubs",
		function(json) {
			//alert("Sucess!");
			var json_length = json.length;
			var club_list = '<div class="listing_header">Club Listing</div>'; 
			club_list += '<ol id="club_item_list" class="listing_list">';
			
			for (i = 0; i < json_length; i++) {
				(json[i].club_icon_url == "NULL" || json[i].club_icon_url == "") ? club_icon_url= get_tc_home_url() + "media/images/tclubs_icon.gif" : club_icon_url=json[i].club_icon_url;
				(json[i].club_description == "NULL") ? club_description="" : club_description=json[i].club_description;
				club_list += '<li class="list_item">';
				club_list += '<span class="item_body">'
				club_list += '<span class="item_image">';
				club_list += '<img src="' + club_icon_url + '" width="48px" height="48px" />';
				club_list += "</span>";
				club_list += '<span class="item_text">';
				club_list += '<a href="#" id="' + json[i].chat_tag + '" class="club_listing_link">' + json[i].name+ "</a><br />" + club_description;
				club_list += "</span>";
				club_list += "</span>";
				club_list += "</li>"
			}
			club_list += "</ol>";
			$('#club_listing').html(club_list);
			$('#my_clubs_list, #member_of_clubs_list, #leave_club_list, #delete_club_list').children().removeClass('right_list_item_selected');
			$('#all_clubs').addClass('right_list_item_selected');
			$('#tweet_listing').hide();
			$('#frm_tweetbox').clearForm();
			$('#tweetbox_div').hide();
			$('#join_club_div').slideUp();
			$('#admin_central_div').slideUp();
			$('#club_info_div').slideUp();
			$('#searchbox_div').show();
			$('#club_listing').show();
			GCHAT_TAG = "";
			GCLUB_NAME = "";
			$('.club_listing_link').bind({
				click: function(){
					build_tweet_list($(this).text(), $(this).attr("id"));
				}
			});
		}
	);
}

function build_tweet_list(club_name, chat_tag) {
	HASHTAG_LENGTH = 140 - (chat_tag.length + 2 + 3); //+2 -> for space and #; +3 -> space and #c
	GCHAT_TAG = chat_tag;
	GCLUB_NAME = club_name;
	if (User_name != "")
		is_member_tweet();
	else
		get_tweet_list();
	
/*	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_is_member", 
		{
			user_name: User_name,
			club_name: club_name
		}, function(json){
			if (json[0].error == undefined) {
				IS_MEMBER = 1;
			}
			else {
				IS_MEMBER = 0;
			}
			is_member_complete = true;
		});
		
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_whois_admin", 
		{
			club_name: club_name
		}, function(json){
			console.info(User_name + " " +json[0].name)
			if (json[0].name == User_name) {
				IS_ADMIN = 1;
			}
			else {
				IS_ADMIN = 0;
			}
		});
	}
*/	
	
	
}

function get_tweet_list() {
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_tweets", "club_name=" + GCLUB_NAME, function(json){
		//alert("Sucess!");
		var json_length = json.length;
		var tweet_list = '<div class="listing_header">Real-time results for <span class="bold">' + GCLUB_NAME + '</span></div>';
		tweet_list += '<ol id="tweet_item_list" class="listing_list">';
		
		for (i = 0; i < json_length; i++) {
			tweet_list += '<li class="list_item">';
			tweet_list += '<span class="item_body">';
			tweet_list += '<span class="item_image">';
			tweet_list += '<img src="' + json[i].profile_image_url + '" width="48px" height="48px" />';
			tweet_list += "</span>";
			tweet_list += '<span class="item_text">';
			tweet_list += '<a href="http://www.twitter.com/' + json[i].from_user + '" target="_blank" class="tweet_from_user">' + json[i].from_user + "</a> " + json[i].text + ' ';
			tweet_list += '<span class="tweet_create_date">' + json[i].created_at;
			tweet_list += '<ul class="message_actions">';
			if (json[i].from_user == User_name) {
				tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li>';
			}
			else if (IS_ADMIN == 1 && json[i].from_user != User_name) {
				if (json[i].user_status != "blocked")
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="block_user action_item_link">Block User</a></li> | ';
				else
					tweet_list += '<li class="action_item">User Blocked</li> | ';
				tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li> | ';
				tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
				//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
			}
			else {
				tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
				//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
			}
			tweet_list += '</ul>'
			tweet_list += "</span>";
			tweet_list += "</span>";
			tweet_list += "</span>";
			tweet_list += "</li>";
		}
		tweet_list += "</ol>";
		if (json_length > 0) {
			Last_id = json[0]['id'];
			First_id = json[json_length-1]['id'];
		}
		else {
			Last_id = 0;
		}
		//console.info("HASHTAG_LENGTH: " + HASHTAG_LENGTH);
		$('.counter').remove();
		$('#tweet_text').charCounter(HASHTAG_LENGTH, {
			container: "<div></div>",
			classname: "counter",
			format: "%1",
			pulse: false,
			delay: 10
		});
		$('.counter').appendTo("#tweet_box_footer");
		$('#hidden_chat_tag').val(GCHAT_TAG);
		$('#hidden_club_name').val(GCLUB_NAME);
		$('#club_info_header').text(GCLUB_NAME);
		$('#club_info_chattag').text("Chat Tag: " + GCHAT_TAG);
		$('#tweet_listing').empty();
		$('#tweet_listing').html(tweet_list);		
		$('#all_clubs').removeClass('right_list_item_selected');
		$('#my_clubs_list, #member_of_clubs_list, #leave_club_list, #delete_club_list').children().removeClass('right_list_item_selected');
		$('li.right_sub_list_item').filter(function(){	
			return $(this).text() == GCLUB_NAME;
		}).addClass('right_list_item_selected');
		$('#club_listing').hide();
		$('#club_info_div').slideDown();
		if ($.cookie("oauth") != null) {
			if (IS_MEMBER == 2) {
				$('#searchbox_div').show();
				$('#tweetbox_div').hide();
				$('#join_club_div').hide();
				$('#blocked_user_div').slideDown();
				$('.message_actions').hide();
			}
			else {
				$('#searchbox_div').hide();
				$('#tweetbox_div').show();
				if (IS_ADMIN == 0 && IS_MEMBER == 1) 
					$('#leave_club_list').slideDown();
				else 
					$('#leave_club_list').slideUp();
				
				if (IS_MEMBER == 0) 
					$('#join_club_div').slideDown();
				else 
					$('#join_club_div').hide();
				
				if (IS_ADMIN == 1) {
					$('#admin_central_div').slideDown();
					$('#delete_club_list').slideDown();
				}
				else {
					$('#admin_central_div').hide();
					$('#delete_club_list').slideUp();
				}
			}
		}
		$('#tweet_item_list > li.list_item').unbind('mouseenter');
		$('#tweet_item_list > li.list_item').unbind('mouseleave');
		$('.block_user').unbind('click');
		$('.delete_tweet').unbind('click');
		$('.reply_to_tweet').unbind('click');
		$('.retweet_tweet').unbind('click');
		
		if (IS_MEMBER != 2) {
			$('#tweet_item_list > li.list_item').bind({
				mouseenter: function(){
					$(this).find('ul.message_actions').fadeIn('fast');
				},
				mouseleave: function(){
					$(this).find('ul.message_actions').fadeOut('fast');
				}
			});
			$('.block_user').bind({
				click: function(){
					block_user($(this).attr("id"));
				}
			});
			$('.delete_tweet').bind({
				click: function(){
					delete_tweet($(this).attr("id"));
				}
			});
			$('.reply_to_tweet').bind({
				click: function(){
					reply_to_tweet($(this).attr("id"));
				}
			});
			$('.retweet_tweet').bind({
				click: function(){
					retweet_tweet($(this).attr("id"));
				}
			});
		}
		check_more_tweets();
		$('#tweet_listing').show();
	});
}

function get_newer_tweets() {
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_newer_tweets", 
		"club_name=" + GCLUB_NAME + "&last_id=" + Last_id, 
		function(json){
		//alert("Sucess!");
			if (json == null)
				var json_length = 0;
			else 
				var json_length = json.length;
			//var tweet_list = '<div class="listing_header">Real-time results for <span class="bold">' + GCLUB_NAME + '</span></div>';
			var tweet_list = '';
			//tweet_list += '<ol id="tweet_item_list" class="listing_list">';
			
			for (i = 0; i < json_length; i++) {
				tweet_list += '<li class="list_item hidden_list_item">';
				tweet_list += '<span class="item_body">';
				tweet_list += '<span class="item_image">';
				tweet_list += '<img src="' + json[i].profile_image_url + '" width="48px" height="48px" />';
				tweet_list += "</span>";
				tweet_list += '<span class="item_text">';
				tweet_list += '<a href="http://www.twitter.com/' + json[i].from_user + '" target="_blank" class="tweet_from_user">' + json[i].from_user + "</a> " + json[i].text + ' ';
				tweet_list += '<span class="tweet_create_date">' + json[i].created_at;
				tweet_list += '<ul class="message_actions">';
				if (json[i].from_user == User_name) {
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li>';
				}
				else if (IS_ADMIN == 1 && json[i].from_user != User_name) {
					if (json[i].user_status != "blocked")
						tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="block_user action_item_link">Block User</a></li> | ';
					else
						tweet_list += '<li class="action_item">User Blocked</li> | ';
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li> | ';
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
					//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
				}
				else {
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
					//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
				}
				tweet_list += '</ul>'
				tweet_list += "</span>";
				tweet_list += "</span>";
				tweet_list += "</span>";
				tweet_list += "</li>";
			}
			//tweet_list += "</ol>";
			
			//console.info("HASHTAG_LENGTH: " + HASHTAG_LENGTH);
/*			$('.counter').remove();
			$('#tweet_text').charCounter(HASHTAG_LENGTH, {
				container: "<div></div>",
				classname: "counter",
				format: "%1",
				pulse: false,
				delay: 10
			});
			$('.counter').appendTo("#tweet_box_footer");
			$('#hidden_chat_tag').val(GCHAT_TAG);
			$('#hidden_club_name').val(GCLUB_NAME);
			$('#club_info_header').text(GCLUB_NAME);
			$('#club_info_chattag').text("Chat Tag: " + GCHAT_TAG);							
			$('#all_clubs').removeClass('right_list_item_selected');
			$('#my_clubs_list, #member_of_clubs_list, #leave_club_list, #delete_club_list').children().removeClass('right_list_item_selected');
			$('li.right_sub_list_item').filter(function(){	
				return $(this).text() == GCLUB_NAME;
			}).addClass('right_list_item_selected');
			$('#club_listing').hide();
			$('#club_info_div').slideDown();
			if ($.cookie("oauth") != null) {
				$('#searchbox_div').hide();
				$('#tweetbox_div').show();
				if (IS_ADMIN == 0 && IS_MEMBER == 1) 
					$('#leave_club_list').slideDown();
				else
					$('#leave_club_list').slideUp();
					
				if (IS_MEMBER == 0)
					$('#join_club_div').slideDown();
				else
					$('#join_club_div').hide();
	
				if (IS_ADMIN == 1) {
					$('#admin_central_div').slideDown();
					$('#delete_club_list').slideDown();
				}
				else{
					$('#admin_central_div').hide();
					$('#delete_club_list').slideUp();
				}
			}
*/			if (tweet_list != "")
				$('#tweet_item_list').prepend(tweet_list);
			if (json_length > 0) {
				Last_id = json[0]['id'];
				New_tweet_counter = New_tweet_counter + json_length;
			}
			if (New_tweet_counter > 0)
				$('#show_newer_tweets_div').text(New_tweet_counter + " more tweets since you last searched.").fadeIn('fast');
			
			if (IS_MEMBER == 2)
				$('.message_actions').hide();
				
			if (tweet_list != "") {
				$('#tweet_item_list > li.list_item').unbind('mouseenter');
				$('#tweet_item_list > li.list_item').unbind('mouseleave');
				$('.block_user').unbind('click');
				$('.delete_tweet').unbind('click');
				$('.reply_to_tweet').unbind('click');
				$('.retweet_tweet').unbind('click');
				
				if (IS_MEMBER != 2) {
					$('#tweet_item_list > li.list_item').bind({
						mouseenter: function(){
							$(this).find('ul.message_actions').fadeIn('fast');
						},
						mouseleave: function(){
							$(this).find('ul.message_actions').fadeOut('fast');
						}
					});
					$('.block_user').bind({
						click: function(){
							block_user($(this).attr("id"));
						}
					});
					$('.delete_tweet').bind({
						click: function(){
							delete_tweet($(this).attr("id"));
						}
					});
					$('.reply_to_tweet').bind({
						click: function(){
							reply_to_tweet($(this).attr("id"));
						}
					});
					$('.retweet_tweet').bind({
						click: function(){
							retweet_tweet($(this).attr("id"));
						}
					});
				}
			}

//			$('#tweet_listing').show();
		}
	);
}

function get_more_tweets() {
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_more_tweets", 
		"club_name=" + GCLUB_NAME + "&last_id=" + First_id, 
		function(json){
		//alert("Sucess!");
			if (json == null)
				var json_length = 0;
			else 
				var json_length = json.length;
			//var tweet_list = '<div class="listing_header">Real-time results for <span class="bold">' + GCLUB_NAME + '</span></div>';
			var tweet_list = '';
			//tweet_list += '<ol id="tweet_item_list" class="listing_list">';
			
			for (i = 0; i < json_length; i++) {
				tweet_list += '<li class="list_item">';
				tweet_list += '<span class="item_body">';
				tweet_list += '<span class="item_image">';
				tweet_list += '<img src="' + json[i].profile_image_url + '" width="48px" height="48px" />';
				tweet_list += "</span>";
				tweet_list += '<span class="item_text">';
				tweet_list += '<a href="http://www.twitter.com/' + json[i].from_user + '" target="_blank" class="tweet_from_user">' + json[i].from_user + "</a> " + json[i].text + ' ';
				tweet_list += '<span class="tweet_create_date">' + json[i].created_at;
				tweet_list += '<ul class="message_actions">';
				if (json[i].from_user == User_name) {
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li>';
				}
				else if (IS_ADMIN == 1 && json[i].from_user != User_name) {
					if (json[i].user_status != "blocked")
						tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="block_user action_item_link">Block User</a></li> | ';
					else
						tweet_list += '<li class="action_item">User Blocked</li> | ';
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="delete_tweet action_item_link">Delete</a></li> | ';
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
					//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
				}
				else {
					tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['from_user'] + '" class="reply_to_tweet action_item_link">Reply</a></li>';
					//tweet_list += '<li class="action_item"><a href="#" id="' + json[i]['id'] + '" class="retweet_tweet action_item_link">Retweet</a></li>';
				}
				tweet_list += '</ul>'
				tweet_list += "</span>";
				tweet_list += "</span>";
				tweet_list += "</span>";
				tweet_list += "</li>";
			}
			if (tweet_list != "")
				$('#tweet_item_list').append(tweet_list);
			if (json_length > 0) {
				First_id = json[json_length-1]['id'];
			}
			check_more_tweets();
			
			if (IS_MEMBER == 2)
				$('.message_actions').hide();
			
			if (tweet_list != "") {
				$('#tweet_item_list > li.list_item').unbind('mouseenter');
				$('#tweet_item_list > li.list_item').unbind('mouseleave');
				$('.block_user').unbind('click');
				$('.delete_tweet').unbind('click');
				$('.reply_to_tweet').unbind('click');
				$('.retweet_tweet').unbind('click');
				
				if (IS_MEMBER != 2) {
					$('#tweet_item_list > li.list_item').bind({
						mouseenter: function(){
							$(this).find('ul.message_actions').fadeIn('fast');
						},
						mouseleave: function(){
							$(this).find('ul.message_actions').fadeOut('fast');
						}
					});
					$('.block_user').bind({
						click: function(){
							block_user($(this).attr("id"));
						}
					});
					$('.delete_tweet').bind({
						click: function(){
							delete_tweet($(this).attr("id"));
						}
					});
					$('.reply_to_tweet').bind({
						click: function(){
							reply_to_tweet($(this).attr("id"));
						}
					});
					$('.retweet_tweet').bind({
						click: function(){
							retweet_tweet($(this).attr("id"));
						}
					});
				}
			}
		}
	);
}

function check_more_tweets() {
	//console.info("First_id " + First_id);
	$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_get_more_tweets",
		"club_name=" + GCLUB_NAME + "&last_id=" + First_id, 
		function(json){
			if (json == null)
				Check_more_tweets = 0;
			else
				Check_more_tweets = json.length;
			//console.info("Check_more_tweets " + Check_more_tweets);
			if (Check_more_tweets > 0)
				$('#show_more_tweets_div').show();
			else
				$('#show_more_tweets_div').hide();
		}
	);
}

function is_member(){
//	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_is_member", {
			user_name: User_name,
			club_name: GCLUB_NAME
		}, function(json){
			if (json[0].error == undefined && json[0].status != "blocked") {
				IS_MEMBER = 1;
			}
			else if(json[0].error == undefined && json[0].status == "blocked") {
				IS_MEMBER = 2;
			}
			else {
				IS_MEMBER = 0;
			}
			is_admin();
		});
//	}
}

function is_member_tweet(){
//	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_is_member", {
			user_name: User_name,
			club_name: GCLUB_NAME
		}, function(json){
			if (json[0].error == undefined && json[0].status != "blocked") {
				IS_MEMBER = 1;
			}
			else if(json[0].error == undefined && json[0].status == "blocked") {
				IS_MEMBER = 2;
			}
			else {
				IS_MEMBER = 0;
			}
			is_admin_tweet();
		});
//	}
}

function is_admin() {
//	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_whois_admin", {
			club_name: GCLUB_NAME
		}, function(json){
			//console.info(User_name + " " + json[0].name)
			if (json[0].name == User_name) {
				IS_ADMIN = 1;
			}
			else {
				IS_ADMIN = 0;
			}
			//get_tweet_list();
			member_admin_config();
		});
//	}
}

function is_admin_tweet() {
//	if (User_name != "") {
		$.getJSON(GCURRENT_URL + "index.php/tweeterclubs/ajax_whois_admin", {
			club_name: GCLUB_NAME
		}, function(json){
			//console.info(User_name + " " + json[0].name)
			if (json[0].name == User_name) {
				IS_ADMIN = 1;
			}
			else {
				IS_ADMIN = 0;
			}
			get_tweet_list();
		});
//	}
}

function member_admin_config() {
	$('.counter').remove();
	$('#tweet_text').charCounter(HASHTAG_LENGTH, {
		container: "<div></div>",
		classname: "counter",
		format: "%1",
		pulse: false,
		delay: 10
	});
	$('.counter').appendTo("#tweet_box_footer");
	$('#hidden_chat_tag').val(GCHAT_TAG);
	$('#hidden_club_name').val(GCLUB_NAME);
	
	$('#searchbox_div').hide();
	$('#tweetbox_div').show();
	if (IS_MEMBER == 2) {
		$('#searchbox_div').show();
		$('#tweetbox_div').hide();
		$('#join_club_div').hide();
		$('#blocked_user_div').slideDown();
		$('#tweet_item_list > li.list_item').unbind('mouseenter');
		$('#tweet_item_list > li.list_item').unbind('mouseleave');
		$('.message_actions').hide();
	}
	else {
		if (IS_ADMIN == 0 && IS_MEMBER == 1) 
			$('#leave_club_list').slideDown();
		else 
			$('#leave_club_list').slideUp();
		
		if (IS_MEMBER == 0) 
			$('#join_club_div').slideDown();
		else 
			$('#join_club_div').hide();
		
		if (IS_ADMIN == 1) {
			$('#admin_central_div').slideDown();
			$('#delete_club_list').slideDown();
		}
		else {
			$('#admin_central_div').hide();
			$('#delete_club_list').slideUp();
		}
	}
}

function delete_club_item(club_name) {
	$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_membership",
		{
			user_name: User_name,
			club_name: club_name
		},
		function(json) {
			if (json.error == undefined) {
				update_membership_clubs();
				build_club_list();
			}
			else {
				alert ("Due to technical difficulties, your membership could not be deleted.");
			}
		},
		"json"
	);
}

function block_user(from_user) {
	var answer = confirm("This will permanently block this user from your club. This can not be undone!");
	if (answer) {
		$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_block_user",
			{
				user_name: from_user,
				club_name: GCLUB_NAME
			},
			function(json) {
				if (json.error == undefined)
					build_tweet_list(GCLUB_NAME, GCHAT_TAG);
				else
					alert("Our apologies, but the user " + user_name + " could not be banned at this time. Please try again later.");
			},
			"json"
		);
	}
}

function delete_tweet(message_id) {
	var answer = confirm("Are you sure you want to delete this message? This can not be undone!");
	if (answer) {
		$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_delete_tweet",
			{
				club_name: GCLUB_NAME,
				message_id: message_id
			},
			function(json) {
				if (json.error == undefined)
					build_tweet_list(GCLUB_NAME, GCHAT_TAG);
			},
			"json"
		);
	}
}

function reply_to_tweet(user_name) {
	$('#tweet_text').val("@" + user_name + " ").focus();
}

function retweet_tweet(message_id) {
	//console.info("message_id " + message_id);
	$.post(GCURRENT_URL + "index.php/tweeterclubs/ajax_retweet",
		{
			message_id: message_id
		},
		function(json) {
			if (json.success == undefined)
				alert("Message could not be retweeted.");
			else
				alert("Message successfully retweeted.");
		},
		"json"
	);
}

var create_club_lb = function() {
	if ($(window).height() > window.innerHeight) {
		var posYDivident = window.innerHeight;
	}
	else {
		var posYDivident = Math.round(($(window).height() - $('#create_club_form_lb').outerHeight()) / 2);
	}  
	var posYDivisor = $(window).height();
	var posXDivident = Math.round(($(window).width() - $('#create_club_form_lb').outerWidth())/2); 
	var posXDivisor = $(window).width();  
	
	$('#create_club_form_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	$('#signin_to_create_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	
	$('#btn_create').click(function() {
		if($.cookie("oauth") == null || User_name == "") {
			//$('body').css('overflow', 'hidden');
			$('.overlay').css({'top': $(document).scrollTop()});
			
			if($(window).height() > window.innerHeight) {
			  var posYDivident = window.innerHeight;
			}
			else {
			  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#signin_to_create_lb').outerHeight())/2));
			}
			var posYDivisor = $(window).height();
			
			$('#signin_to_create_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
			
			$('.overlay').add('#signin_to_create_lb').fadeIn('normal');
			
			$(window).scroll(function() {
				$('.overlay').add('#signin_to_create_lb').fadeOut('fast', function() {
					$('body').css('overflow', 'auto');														  
				});
			});
			$('.overlay').click(function(c) {
				c.preventDefault();
				$('.overlay').add('#signin_to_create_lb').fadeOut('fast', function() {
					$('body').css({'overflow': 'auto'});	
				});
			});
			$('.lb_cancel').click(function(c) {
				c.preventDefault();
				$('.overlay').add('#signin_to_create_lb').fadeOut('fast', function() {
					$('body').css({'overflow': 'auto'});	
				});
			});
		}
		else {
			//$('body').css('overflow', 'hidden');
			$('.overlay').css({'top': $(document).scrollTop()});
			
			if($(window).height() > window.innerHeight) {
			  var posYDivident = window.innerHeight;
			}
			else {
			  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#create_club_form_lb').outerHeight())/2));
			}
			var posYDivisor = $(window).height();
			
			$('#create_club_form_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
			
			$('.overlay').add('#create_club_form_lb').fadeIn('normal');
			
			if ($('#admin_user').val() == "")
				$('#admin_user').val(User_name);
			
			$(window).scroll(function() {
				$('.overlay').add('#create_club_form_lb').fadeOut('fast', function() {
					$('body').css('overflow', 'auto');
					var admin_user_safe = $('#admin_user').val();
					$('#frm_create_club').clearForm();		
					$('#admin_user').val(admin_user_safe);	
					$('label.error').hide();											  
				});
			});
			
			$('.overlay').click(function(c) {
				c.preventDefault();
				$('.overlay').add('#create_club_form_lb').fadeOut('fast', function() {
					$('body').css({'overflow': 'auto'});	
					var admin_user_safe = $('#admin_user').val();
					$('#frm_create_club').clearForm();
					$('#admin_user').val(admin_user_safe);
					$('label.error').hide();
				});
			});
			
			$('.lb_cancel').click(function(c) {
				c.preventDefault();
				$('.overlay').add('#create_club_form_lb').fadeOut('fast', function() {
					$('body').css({'overflow': 'auto'});
					var admin_user_safe = $('#admin_user').val();
					$('#frm_create_club').clearForm();	
					$('#admin_user').val(admin_user_safe);
					$('label.error').hide();
				});
			});
		}		
	});
}

var update_club_lb = function() {
	if ($(window).height() > window.innerHeight) {
		var posYDivident = window.innerHeight;
	}
	else {
		var posYDivident = Math.round(($(window).height() - $('#update_club_form_lb').outerHeight()) / 2);
	}  
	var posYDivisor = $(window).height();
	var posXDivident = Math.round(($(window).width() - $('#update_club_form_lb').outerWidth())/2); 
	var posXDivisor = $(window).width();  
	
	$('#update_club_form_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	
	$('#update_club').click(function() {
		//$('body').css('overflow', 'hidden');
		$('.overlay').css({'top': $(document).scrollTop()});
		$.getJSON(GCURRENT_URL + 'index.php/tweeterclubs/ajax_read_club',
			{
				club_name: GCLUB_NAME
			},
			function(json) {
				$('#update_chat_tag').val(json[0].chat_tag);
				$('#update_club_description').val(json[0].club_description);
				$('#update_club_icon_url').val(json[0].club_icon_url);
				$('#update_club_image_url').val(json[0].club_image_url);
				$('#update_club_name').val(GCLUB_NAME);
				$('#update_admin_user').val(User_name);
			}
		);
		
		if($(window).height() > window.innerHeight) {
		  var posYDivident = window.innerHeight;
		}
		else {
		  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#update_club_form_lb').outerHeight())/2));
		}
		var posYDivisor = $(window).height();
		
		$('#update_club_form_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
		
		$('.overlay').add('#update_club_form_lb').fadeIn('normal');
		
		$(window).scroll(function() {
			$('.overlay').add('#update_club_form_lb').fadeOut('fast', function() {
				$('body').css('overflow', 'auto');
				var admin_user_safe = $('#admin_user').val();
				$('#frm_update_club').clearForm();		
				$('#admin_user').val(admin_user_safe);	
				$('label.error').hide();											  
			});
		});
		
		$('.overlay').click(function(c) {
			c.preventDefault();
			$('.overlay').add('#update_club_form_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
				var admin_user_safe = $('#admin_user').val();
				$('#frm_update_club').clearForm();
				$('#admin_user').val(admin_user_safe);
				$('label.error').hide();
			});
		});
		
		$('.lb_cancel').click(function(c) {
			c.preventDefault();
			$('.overlay').add('#update_club_form_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});
				var admin_user_safe = $('#admin_user').val();
				$('#frm_update_club').clearForm();	
				$('#admin_user').val(admin_user_safe);
				$('label.error').hide();
			});
		});	
	});
}

var terms_of_use_lb = function() {
	if($(window).height() > window.innerHeight) {
	  var posYDivident = window.innerHeight;
	}
	else {
	  var posYDivident = Math.round(($(window).height() -$('#terms_of_use_lb').outerHeight())/2);
	}
	var posYDivisor = $(window).height();
	var posXDivident = Math.round(($(window).width() - $('#terms_of_use_lb').outerWidth())/2); 
	var posXDivisor = $(window).width();
	
	$('#terms_of_use_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	$('#terms_of_use').click(function() {
		//$('body').css('overflow', 'hidden');
		$('.overlay').css({'top': $(document).scrollTop()});
		$('.overlay').css('opacity', .6);
		
		if($(window).height() > window.innerHeight) {
		  var posYDivident = window.innerHeight;
		}
		else {
		  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#terms_of_use_lb').outerHeight())/2));
		}
		var posYDivisor = $(window).height();
		
		$('#terms_of_use_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
		
		$('.overlay').add('#terms_of_use_lb').fadeIn('normal');
		
		$(".close_lb").click(function(c) {
			c.preventDefault();
			$('.overlay').add('#terms_of_use_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$('.overlay').click(function(c) {
			c.preventDefault();
			$('.overlay').add('#terms_of_use_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$(window).scroll(function() {
			$('.overlay').add('#terms_of_use_lb').fadeOut('fast', function() {
				$('body').css('overflow', 'auto');														  
			});
		});
	});
}

var learn_more_lb = function() {
	if($(window).height() > window.innerHeight) {
	  var posYDivident = window.innerHeight;
	}
	else {
	  var posYDivident = Math.round(($(window).height() -$('#learn_more_lb').outerHeight())/2);
	}
	var posYDivisor = $(window).height();
	var posXDivident = Math.round(($(window).width() - $('#learn_more_lb').outerWidth())/2); 
	var posXDivisor = $(window).width();
	
	$('#learn_more_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	$('#learn_more').click(function() {
		//$('body').css('overflow', 'hidden');
		$('.overlay').css({'top': $(document).scrollTop()});
		$('.overlay').css('opacity', .6);
		
		if($(window).height() > window.innerHeight) {
		  var posYDivident = window.innerHeight;
		}
		else {
		  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#learn_more_lb').outerHeight())/2));
		}
		var posYDivisor = $(window).height();
		
		$('#learn_more_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
		
		$('.overlay').add('#learn_more_lb').fadeIn('normal');
		
		$(".close_lb").click(function(c) {
			c.preventDefault();
			$('.overlay').add('#learn_more_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$('.overlay').click(function(c) {
			c.preventDefault();
			$('.overlay').add('#learn_more_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$(window).scroll(function() {
			$('.overlay').add('#learn_more_lb').fadeOut('fast', function() {
				$('body').css('overflow', 'auto');														  
			});
		});
	});
};

var contact_lb = function() {
	if($(window).height() > window.innerHeight) {
	  var posYDivident = window.innerHeight;
	}
	else {
	  var posYDivident = Math.round(($(window).height() -$('#contact_lb').outerHeight())/2);
	}
	var posYDivisor = $(window).height();
	var posXDivident = Math.round(($(window).width() - $('#contact_lb').outerWidth())/2); 
	var posXDivisor = $(window).width();
	
	$('#contact_lb').show().css({'top': Math.round((posYDivident/posYDivisor)*100) + '%', 'left': Math.round((posXDivident/posXDivisor)*100) + '%', 'margin-top': 0, 'margin-left': 0}).hide();
	
	$('#contact_us').click(function() {
		//$('body').css('overflow', 'hidden');
		$('.overlay').css({'top': $(document).scrollTop()});
		$('.overlay').css('opacity', .6);
		
		if($(window).height() > window.innerHeight) {
		  var posYDivident = window.innerHeight;
		}
		else {
		  var posYDivident = Math.round($(document).scrollTop() + (($(window).height() - $('#contact_lb').outerHeight())/2));
		}
		var posYDivisor = $(window).height();
		
		$('#contact_lb').css('top', Math.round((posYDivident/posYDivisor)*100) + '%');
		$('#contact_iframe').each(function() {
			this.contentWindow.location.reload(true);
		});
		
		$('.overlay').add('#contact_lb').fadeIn('normal');
		
		$(".close_lb").click(function(c) {
			c.preventDefault();
			$('.overlay').add('#contact_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$('.overlay').click(function(c) {
			c.preventDefault();
			$('.overlay').add('#contact_lb').fadeOut('fast', function() {
				$('body').css({'overflow': 'auto'});	
			});
		});
		
		$(window).scroll(function() {
			$('.overlay').add('#contact_lb').fadeOut('fast', function() {
				$('body').css('overflow', 'auto');														  
			});
		});
	});
};

$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

function get_current_url() {
	var window_url = window.location.href;
	var slash_pos = window_url.indexOf('/', 7);
	if (window_url.charAt(slash_pos+1) != 'i') {
		slash_pos = window_url.indexOf('/', slash_pos+1);
	}
	var current_url = window_url.substring(0,slash_pos+1);
	return current_url;
}

function get_tc_home_url () {
	var window_url = window.location.href;
	var slash_pos = window_url.indexOf('/', 7);
	return window_url.substring(0,slash_pos+1);
}

var google_translate = function(text,tweet,content,token) 
{
	google.language.translate(text, '', user_lang, function(result) {
	  	if (!result.error) {
			//console.info('there was a translation : value is '+text+'; result is '+result.translation);
			if (text != result.translation) {
				//return result.translation;
  				var old_text = $('.item_text')[tweet].childNodes[content].textContent;
	            var hash_pattern = /([^(#|@)]+)|((#|@)[\w]+)/g;
	  	    	var a_tweet_text = old_text.match(hash_pattern);
	  	    	a_tweet_text[token] = result.translation;
  				var new_tweet = a_tweet_text.join(' ');
  				//$('.item_text')[tweet].childNodes[content].replaceWholeText(' '+unescape(new_tweet)+' ');
  				$('.item_text')[tweet].childNodes[content].replaceWholeText(' '+new_tweet.replace(/&amp;/g,"&").replace(/&#39;/g,"'").replace(/&quot;/g,'"')+' ');
			}
		}
		else {
			//return text;
		}
	});
	
}

var translate = function() 
{
	(user_lang == '') ? user_lang='en' : user_lang=user_lang;
	$('.item_text').each(function(i,tweet) {
	    var contents = tweet.childNodes;
	    //console.info(contents);
	    //console.info(tweet);
	    $.each(contents, function(j, content) {
	        if (content.nodeType == 3) {
	            //console.info(content.nodeType);
	            var tmp_tweet = content.textContent;
	            //console.info('tmp_tweet : '+tmp_tweet);
	            var hash_pattern = /([^(#|@)]+)|((#|@)[\w]+)/g;
	  	    	var a_tweet_text = tmp_tweet.match(hash_pattern);
	  	    	//console.info(a_tweet_text);
	  	    	$.each(a_tweet_text, function(k,value) {
	                if (!(value.charAt(0) == '#' || value.charAt(0) == '@' || value == " " || value == " RT "))
	                    google_translate(value,i,j,k);
	  	    	});
	  	    }
	    });    
	});
}