function removeHTMLTags(strInputCode) {
	/*
	 * This line is optional, it replaces escaped brackets with real ones, i.e. <
	 * is replaced with < and > is replaced with >
	 */
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function(strMatch, p1) {
		return (p1 == "lt") ? "<" : ">";
	});

	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;
}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
		string = string.replace(token, newtoken);
	}
	return string;
}

var waitALittle = function() {
	window.scrollTo(0, 0);
	jQuery
			.blockUI({
				css : {
					backgroundColor : '#f5f5f5',
					color : '#000',
					padding : '10px'
				},
				message : "<div style='text-align:center; font-size: 13px; font-weight: bold;;'>"
						+ lang_list[25] + "</div>"
			});
}

String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function clearMenus() {
	jQuery('#submenu-friends, .catdropm').hide();
	hideBuryMenus();
	jQuery('#friends-alink').removeClass('current');
}
function hideBuryMenus() {
	for ( var i = 0; i < 14; i++) {
		jQuery('#div' + i).css('display', 'none');
		jQuery('#tooltoggle' + i).css('display', 'none');
	}
}
function handleHover() {
	jQuery("html")
			.bind(
					"click",
					function(event) {
						if (!jQuery(event.target).is('.catdrop')
								&& (!jQuery(event.target).attr("id") || jQuery(
										event.target).attr("id").substring(0,
										10) != 'tooltoggle')) {
							clearMenus();
						}
					});
	jQuery('.catdrop').click(function() {
		var m = jQuery(this).parents('li.h-drop').children('.catdropm');
		m.toggle();
		jQuery('.catdropm:visible').not(m).hide();
		return false;
	});
}

function show_cant_access() {
	jQuery.blockUI({
		css : {
			backgroundColor : "#f5f5f5",
			color : "#000",
			padding : "5px"
		},
		message : "<h2>" + lang_list['header_action_retriction'] + "</h2>"
	});
	setTimeout(jQuery.unblockUI, 2000);
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function bookmark_event(count, id_event) {
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/bookmarks/bookmark",
		data : "id_event=" + id_event,
		success : function(msg) {
			switch (msg) {
			case 'not-logged-in': {
				show_cant_access();
				break;
			}
			default: {
				if (msg == "add") {
					jQuery('#bookmark' + count).removeClass('bookmark');
					jQuery('#bookmark' + count).addClass('bookmarked');
				} else if (msg == "delete") {
					jQuery('#bookmark' + count).removeClass('bookmarked');
					jQuery('#bookmark' + count).addClass('bookmark');
				}
			}
			}
		}
	});
}

function event_attendance(count, id_event) {
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/event/attendance",
		data : "id_event=" + id_event,
		success : function(msg) {
			switch (msg) {
			case 'not-logged-in': {
				show_cant_access();
				break;
			}
			default: {
				if (msg == "add") {
					jQuery('#attendance' + count).removeClass(
							'attendance-not-confirmed');
					jQuery('#attendance' + count).addClass(
							'attendance-confirmed');

					jQuery('#bookmark' + count).removeClass('bookmark');
					jQuery('#bookmark' + count).addClass('bookmarked');
				} else if (msg == "delete") {
					jQuery('#attendance' + count).removeClass(
							'attendance-confirmed');
					jQuery('#attendance' + count).addClass(
							'attendance-not-confirmed');
				}
			}
			}
		}
	});
}

jQuery(document).ready(function() {
	jQuery("span.fb_share_no_count").each(function() {
		jQuery(this).removeClass("fb_share_no_count");
		jQuery(".fb_share_count_inner", this).html("0");
	});
	
	jQuery("#submenu-drop-ini-li").mouseover(function() {
		jQuery("#submenu-drop-communities").hide();
		jQuery("#submenu-drop-event").hide();
		jQuery("#submenu-drop-content").hide();
	});	

		
	jQuery("#submenu-drop-communities-li").mouseover(function() {
		jQuery("#submenu-drop-communities").show();
		
		jQuery("#submenu-drop-event").hide();
		jQuery("#submenu-drop-content").hide();		
	});
	
	jQuery("#submenu-drop-event-li").mouseover(function() {
		jQuery("#submenu-drop-event").show();
		
		jQuery("#submenu-drop-communities").hide();
		jQuery("#submenu-drop-content").hide();		
	});
	
	jQuery("#submenu-drop-content-li").mouseover(function() {
		jQuery("#submenu-drop-content").show();
		
		jQuery("#submenu-drop-communities").hide();
		jQuery("#submenu-drop-event").hide();		
	});	
	
});

function signin() {
	jQuery("#signin-container").show();
}

function closeSignin() {
	jQuery("#signin-container").hide();
}

function submit_frm_ext_contact_list_left() {
	if ((jQuery("#email_box_left").val() != "")
			&& (jQuery("#passwd_box_left").val() != "")) {
		waitALittle();
		jQuery("#frm_ext_contact_list").submit();
	}
}

jQuery(document).ready(function() {
	// show welcome message (first visit on "home" after signup)
	jQuery("#hide_1").click(function() {
		jQuery("#welcome_message").hide();
	});

	// send invite by email individually
	input_help_value = lang_list['left_side_invite_friends_input_help_value'];
	jQuery("#email-invite-friend").css("color", "gray");
	jQuery("#email-invite-friend").val(input_help_value);

	jQuery("#email-invite-friend").focus(function() {
		if (jQuery("#email-invite-friend").val() == input_help_value) {
			jQuery("#email-invite-friend").val("");
			jQuery(this).css("color", "#000000");
		}
	})

	jQuery("#email-invite-friend").blur(function() {
		if (jQuery("#email-invite-friend").val() == '') {
			jQuery(this).css("color", "gray");
			jQuery("#email-invite-friend").val(input_help_value);
		}
	})

	// email field hint (import contacts from Gmail, Hotmail, etc)
	email_box_left = lang_list['import_contacts_email2'];
	jQuery("#email_box_left").css("color", "gray");
	jQuery("#email_box_left").val(email_box_left);

	jQuery("#email_box_left").focus(function() {
		if (jQuery("#email_box_left").val() == email_box_left) {
			jQuery("#email_box_left").val("");
			jQuery(this).css("color", "#000000");
		}
	})

	jQuery("#email_box_left").blur(function() {
		if (jQuery("#email_box_left").val() == '') {
			jQuery(this).css("color", "gray");
			jQuery("#email_box_left").val(email_box_left);
		}
	})

	// password field hint (import contacts from Gmail, Hotmail, etc)
	passwd_box_left_aux = lang_list['import_contacts_passwd2'];
	jQuery("#passwd_box_left_aux").css("color", "gray");
	jQuery("#passwd_box_left_aux").val(passwd_box_left_aux);

	jQuery("#passwd_box_left_aux").focus(function() {
		jQuery("#passwd_box_left_aux").hide();
		jQuery("#passwd_box_left").show();
		jQuery("#passwd_box_left").focus();
	})

	jQuery("#passwd_box_left").blur(function() {
		if (jQuery("#passwd_box_left").val() == '') {
			jQuery("#passwd_box_left_aux").show();
			jQuery("#passwd_box_left").hide();
		}
	})

	// signup username validation &
	// update user data username validation
	jQuery('#username').alphanumeric();
	jQuery("#first_name").focus();

	if (jQuery('#username').val() != '') {
		jQuery('#username-text').html(jQuery('#username').val());
	}

	jQuery('#username').keyup(function() {
		var username = jQuery('#username').val();
		jQuery('#username-text').html(username);
	});

	jQuery('#username').keyup(function() {
		var username = jQuery('#username').val();
		if (username != "") {
			jQuery("#loading-username-check").show();
			jQuery.ajax({
				url : BASE_URL + 'signup/validate_username',
				type : "POST",
				data : "username=" + username,
				success : function(data) {
					jQuery("#username-error").hide();
					jQuery("#username-error-post").hide();
					if (data == '0') {
						jQuery("#username-error").show();
					}
					jQuery("#loading-username-check").hide();
				}
			});
		}
	});
});

delete_suggested_invite = function(index, id_user, place) {
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/invite/delete_suggested_invite",
		data : "id_user=" + id_user,
		success : function(msg) {
			var next = parseInt(index) + 2;
			jQuery("#friend-suggestion-" + index).hide("slow");
			if (place == 'home') {
				jQuery("#friend-suggestion-" + next).show("slow");
			}
		}
	});
}

add_friend = function(id_user_main, id_user_invited) {
	waitALittle();
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/friend_add/add_as_friend",
		data : "id_user_main=" + id_user_main + "&id_user_invited="
				+ id_user_invited,
		success : function(msg) {
			jQuery("#sys_message_text").html(msg);
			jQuery("#sys_message_container").show("slow");
			jQuery.unblockUI();
		}
	});
}

add_friend_suggested = function(index, id_user_main, id_user_invited, place) {
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/friend_add/add_as_friend",
		data : "id_user_main=" + id_user_main + "&id_user_invited="
				+ id_user_invited,
		success : function(msg) {
			var next = parseInt(index) + 2;
			jQuery("#friend-suggestion-" + index).hide("slow");
			if (place == 'home') {
				jQuery("#friend-suggestion-" + next).show("slow");
			}
		}
	});
}

answer_add_friend = function(index, id_invite, status, id_user_main,
		id_user_invited) {
	jQuery.ajax({
		type : "POST",
		url : BASE_URL + "account/friend_add/accept_as_friend",
		data : "id_invite=" + id_invite + "&status=" + status
				+ "&id_user_main=" + id_user_main + "&id_user_invited="
				+ id_user_invited,
		success : function(msg) {
			jQuery("#pendent-invite-" + index).hide("slow");
		}
	});
}


jQuery(function() {
	jQuery('img[rel=tipsy]').tipsy({gravity: 's', html: true});
	jQuery('a[rel=tipsy]').tipsy({gravity: 's', html: true});
});
