function init() {
	$('.login-btn').click(function() {
		if($('#external_login_url').length == 1) {
			SpongeSSO.popup_external_login_window($('#external_login_url').text());
			SpongeSSO.popup_modal_dialog();
			return false;
		} else {
			popup({type:"login"});
		}
	});
	
	$('.signup-btn').click(function() {popup({type:"signup"})});
	$('#signup-link').click(function() {popup({type:"signup"})});
	$('#forgot-password-link').click(function() {$('#login').dialog('close');popup({type:"recover-password"})});
	
	$('#login').dialog({bgiframe: true,modal: true,autoOpen: false,draggable: false,width: 503,height: 500,dialogClass: 'login-container'});
	$('#signup').dialog({bgiframe: true,modal: true,autoOpen: false,draggable: false,width: 434,height: 390,dialogClass: 'signup-container'});
	$('#profile').dialog({bgiframe: true,modal: true,autoOpen: false,draggable: false,width: 434,height: 490,dialogClass: 'profile-container',beforeclose: function(){ if(user_logged_in == false) {auth_revoke_fb();} }});
	$('#report').dialog({bgiframe: true,modal: true,autoOpen: false,draggable: false,width: 503,height: 500,dialogClass: 'report-container'});
	$('#recover-password').dialog({bgiframe: true,modal: true,autoOpen: false,draggable: false,width: 503,height: 200,dialogClass: 'recover-password-container'});
	$('#sso-link-users').dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		draggable: false,
		width: 503,
		height: 200,
		dialogClass: 'sso-link-users-container',
		beforeclose: function() {
			clear_sso_info();
		}
	});
	$('#sso-link-users-auth-code').dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		draggable: false,
		width: 503,
		height: 200,
		dialogClass: 'sso-link-users-auth-code-container',
		beforeclose: function() {
			clear_sso_info();
		}
	});
	$('#sso-external-login').dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		draggable: false,
		width: 503,
		height: 200,
		dialogClass: 'sso-external-login-container',
		beforeclose: function() {
			SpongeSSO.clear_polling_timeout();
		}
	});
	
	$("#signup-radio").click(function(){$("#login-submit").hide();$("#signup-submit").show();});
	$("#login-radio").click(function(){$("#signup-submit").hide();$("#login-submit").show();});
	
	$("#submit-fb-login").click(function(){ajax_fb_login();});
	$("#user-edit-save").click(function() {submit_edit_profile();});
	$("#close_login_open_signup").click(function(){close_login_open_signup();});
	

	
	$('#sidebar-ask-title').keyup(function(e) {
		$('.q-char .counter').html((140 -$(this).val().length));
	});
	
	validate_title();
	
	intro_message(user_logged_in);
	
}

function clear_sso_info() {
	$.ajax({
		url: '/sso/clear',
		type: 'POST'
	});
}

/*
 * answer_init()
 *	initializing the functionality for submitting an answer
 *
 */

function answer_init() {
	
	$('.wysiwyg').wysiwyg();
			
	// build events for voting
	$('.answer-rating').each(function(el) {
		var id = this.id.split("-");
	   var aid = id[2];
	   var qid = id[3];
		$(this).find(".thumbs-up").bind('click', function(e) {
	   	e.preventDefault();
	   	if(user_logged_in == true) {
	   		var data = {aid: aid, vote: 1, qid: qid};
	   		submit_vote($(this).parent(), data);
	   	}
	   	else {
	   		popup({type:"login"});
	   	}
	   		
	   });
		$(this).find(".thumbs-down").bind('click', function(e) {
	   	e.preventDefault();
	   	if(user_logged_in == true) {
				var data = {aid: aid, vote: -1, qid: qid};
	   		submit_vote($(this).parent(), data);
	   	}
	   	else {
	   		popup({type:"login"});
	   	}
	   });
	});
	
	// build events for report button
	$('.report').each(function(el) {
		$(this).find("a").bind('click', function(e) {
	   	e.preventDefault();
	   	if(user_logged_in == true) {
	   		var info = this.id.split("-");
	   		var id = info[2];
	   		var content = info[1];
	   		popup({type:"report",el:$(this),data:{id:id,content:content}});
	   	}
	   	else {
	   		popup({type:"login"});
	   	}
	   });
	});
	
	$('.edit').each(function(el) {
		$(this).find("a").toggle(function(e) {
	   	e.preventDefault();
	   	$(this).html("Cancel");
						
	   	var id = this.id.split("-");
	   	aid = id[2];
	   	qid = id[3];
	   	var container = $("#answer-text-" + aid);
	   	var answer_body = container.html();
	   		   	
	   	container.html("<form name='submit-update-answer-form-" + aid + "' id='submit-update-answer-form-" + aid + "'><input type='hidden' name='aid' value='" + aid + "'/><input type='hidden' name='qid' value='" + qid + "'/><p><textarea rows='4' name='answer_body' id='answer_body_" + aid + "'>" + answer_body + "</textarea></p><p><span id='loading' class='loading'><img src='/images/loader.gif'/></span><input type='image' src='/images/btn-update.png' value='Update Answer' class='submit' id='submit-update-answer-" + aid + "' name='submit-update-answer'></form></p>");
	   	$('#answer_body_' + aid).wysiwyg();
						
	  		// capture submitted update answer form
			$("#submit-update-answer-form-" + aid).submit(function(e) {
				e.preventDefault();
				$('#submit-update-answer-' + aid).hide();
				$('#submit-update-answer-form-'+ aid + ' #loading').show();
				$.ajax({
				   url: "/ajax/submit_update_answer.php",	
				   type: "POST",		
				   data: $("#submit-update-answer-form-" + aid).serialize(),		
				   success: function (html) {	
				   	var result = eval('(' + html + ')');			   
				   	
				   	if (result.status == "success") {
				   		location.reload(true);
				   	} 
				   }		
				});
			});
	   }, function(e){
	   	e.preventDefault();
	   	$(this).html("Edit");
	   		   	
	   	var id = this.id.split("-");
	   	aid = id[2];
	   	qid = id[3];
	   	var container = $("#answer-text-" + aid);
	   	var answer_container = $("#answer_body_" + aid);
	   	var text_html = answer_container.text();

	   	container.html(text_html);
	   });
	});
	
	//build events for subscriptions
	$('.subscribe').each(function(el) {
		var info = this.id.split("-");
	   var id = info[2];
	   var content = info[1]
	   
		$(this).bind('click', function(e) {
	   	e.preventDefault();
	   	if(user_logged_in == true) {
	   		var data = {id: id, content: content};
	   		submit_subscribe($(this),data);
	   	}
	   	else {
	   		popup({type:"login"});
	   	}
	   });
	});
	
	
	
	// capture submitted answer form
	$("#submit-answer-form").submit(function(e) {
		e.preventDefault();
		
		$(".error").remove();
		
		$('#submit-answer').hide();
		$('#loading').show();
		
		if(user_logged_in) {
			submit_answer();
		}
		else {
			if(answer_validate()) {
				if($("#login-submit").css("display") == "block") {				
					$.ajax({
					   url: "/ajax/submit_login.php",	
					   type: "POST",		
					   data: $('#submit-answer-form').serialize(),		
					   success: function (html) {	
					   	var result = eval('(' + html + ')');			   
					   	
					   	if (result.status == "error") {	
					   		var container = $("label[for*='login']");
					   		error(container, result.message);
					   	} 
					   	else submit_answer();
					   }		
					});
				}
				else {
					$.ajax({
					   url: "/ajax/submit_signup.php",	
					   type: "POST",		
					   data: $('#submit-answer-form').serialize(),		
					   success: function (html) {	
					   	var result = eval('(' + html + ')');			   
					   	
					   	if (result.status == "error") {	
					   		var container = $("label[for*='login']");
					   		error(container, result.message);
					   	} 
					   	else submit_answer();
					   }		
					});
				}
			}
		}
	});
	
	// submit vote
	function submit_vote(el, data) {
	   if(user_logged_in == true) {
	   	$.ajax({
	   	   url: "/ajax/submit_answer_vote.php",	
	   	   type: "GET",		
	   	   data: data,	
	   	   success: function (html) {
	   	   	var result = eval('(' + html + ')');	
	   	   	
	   	   	if (result.status == "success") {	
	   	   		el.html(result.html);
	   	   		el.fadeOut(600).fadeIn(600);
	   	   	} 
	   	   	else {
	   	   		alert(result.message);
	   	   	}	
	   	   }		
	   	});	
	   }
	   else {
	   	popup({type: "login"});
	   }
	}
	
	// submit the answer
	function submit_answer() {	
		if(answer_validate()) {
			$.ajax({
			   url: "/ajax/submit_answer.php",	
			   type: "POST",		
			   data: $('#submit-answer-form').serialize(),		
			   success: function (html) {	
			   	var result = eval('(' + html + ')');
			   		
			   	if (result.status == "error") {	
				      var container = $("label[for*='answer_body']");
				      error(container, result.message);
				   }
				   else {
				   	location.reload(true);
				   }
			   }		
			});
		}	
	}
	
	//validate the answer form
	function answer_validate() {
		if($("#answer_body").val() == null || $("#answer_body").val() == "") {
			var container = $("label[for*='answer_body']");
			error(container, "This field is required");
			
			return false;
		}
		else
			return true;
	}
	
	//show errors
	function error(container, msg) {
		$('#submit-answer').show();
		$('#loading').hide();
		container.append('<span class="error">' + msg + '</span>');
	}
}
// answer_init()


/*
 * ask_init()
 *	initializing the functionality for submitting an answer
 *
 */
function ask_init() {
	
	$('.wysiwyg').wysiwyg();

	// capture submitted ask form
	$("#submit-ask-form").submit(function(e) {
		e.preventDefault();
		
		$(".error").remove();
		
		$('#submit-ask').hide();
		$('#loading').show();
		
		if(user_logged_in) {
			submit_ask();
		}
		else {
			if(ask_validate()) {
				if($("#login-submit").css("display") == "block") {				
					$.ajax({
					   url: "/ajax/submit_login.php",	
					   type: "POST",		
					   data: $('#submit-ask-form').serialize(),		
					   success: function (html) {	
					   	var result = eval('(' + html + ')');			   
					   	
					   	if (result.status == "error") {	
					   		var container = $("label[for*='login']");
					   		error(container, result.message);
					   	} 
					   	else {
					   		login_user();
					   		submit_ask();
					   	}
					   }		
					});
				}
				else {
					$.ajax({
					   url: "/ajax/submit_signup.php",	
					   type: "POST",		
					   data: $('#submit-ask-form').serialize(),		
					   success: function (html) {	
					   	var result = eval('(' + html + ')');			   
					   	
					   	if (result.status == "error") {	
					   		var container = $("label[for*='login']");
					   		error(container, result.message);
					   	} 
					   	else {
					   		login_user();
					   		submit_ask();
					   	}
					   }		
					});
				}
			}			
		}
	});
	
	// submit the question
	function submit_ask() {	
		if(ask_validate() == true) {
			$.ajax({
			   url: "/ajax/submit_ask.php",	
			   type: "POST",		
			   data: $('#submit-ask-form').serialize(),		
			   success: function (html) {	
			   	var result = eval('(' + html + ')');

			   	if (result.status == "error") {	
				      var container = $("label[for*='" + result.error_reason + "']");
				      error(container, result.message);
				   }
				   else {
				   	location.href = "/" + result.slug;
				   }
			   }		
			});
		}	
	}
	
	// validate the form
	function ask_validate() {
		var valid = true;

		if($("#title").val() == "") {
			var container = $("label[for*='title']");
			error(container, "This field is required");
			valid = false;
		}
		if($("#details").val().replace(/(<([^>]+)>)/ig,"") == "" ) {
			var container = $("label[for*='details']");
			error(container, "It is important we get details about your question");
			valid = false;
		}
		if($("#tags").val() == "") {
			var container = $("label[for*='tags']");
			error(container, "This field is required");
			valid = false;
		}
		
		
		if(valid == false)	
			return false;
		else
			return true;
	}
	
	// show errors
	function error(container, msg) {
		$('#submit-ask').show();
		$('#loading').hide();
		container.append('<span class="error">' + msg + '</span>');
	}
}
// ask_init()


// submit flag
function submit_flag(el, data) {
   if(user_logged_in == true) {
   	$.ajax({
   	   url: "/ajax/submit_flag.php",	
   	   type: "GET",		
   	   data: data,	
   	   success: function (html) {
   	   	var result = eval('(' + html + ')');
   	   	
   	   	if (result.status == "success") {
   	   		el.fadeOut(600).html("<span class='attention_message'>Reported</span>").fadeIn(600);
   	   	} 
   	   	else alert(result.message);	
   	   }		
   	});	
   }
   else {
   	popup({type: "login"});
   }
}

//submit subscription
function submit_subscribe(el, data) {
   if(user_logged_in == true) {
   	$.ajax({
   	   url: "/ajax/submit_subscribe.php",	
   	   type: "GET",		
   	   data: data,	
   	   success: function (html) {	
   	   	var result = eval('(' + html + ')');
   	   	
   	   	if (result.status == "success") {
   	   		el.parent().fadeOut(600).html("<span class='success_message'>Subscribed!</span>").fadeIn(600);
   	   	} 
   	   	else alert(result.message);
   	   }		
   	});	
   }
   else {
   	popup({type: "login"});
   }
}


function popup(options) {
	if(options.type == 'login') {	   
	   $("#login").dialog('open');
	   if(options) {
	   	$("#close_login_open_signup").unbind('click').click(function(){$('#login').dialog('close');popup({type: "signup", action: options.action});return false;});
	   }
	   validate_login(options);

	   return false;			
	}
	else if(options.type == 'signup') {
	   $("#signup").dialog('open');
	   validate_signup(options);	
	}
	else if(options.type == 'profile') {	
		$("#profile").dialog('open');
		validate_profile();		
	}
	else if(options.type == 'recover-password') {	
		$("#recover-password").dialog('open');
		validate_password_recover();		
	}
	else if(options.type == 'report') {	   
	   $("#report").dialog('open');
	   $('input').click(function(){
	   	var return_options = {id:options.data.id,content:options.data.content,flag:$(this).val()};
	   	submit_flag(options.el,return_options);
	   	$("#report").dialog('close');
	   });		
	}
	else if(options.type == 'sso-link-users') {
		$('#sso-link-users').dialog('open');
		validate_sso_link_users();
	}
	else if(options.type == 'sso-link-users-auth-code') {
		$('#sso-link-users-auth-code').dialog('open');
		validate_sso_link_users_auth_code();
	}
	else if(options.type == 'sso-external-login') {
		$('#sso-external-login').dialog('open');
	}
}



/*
 * SUBMIT FUNCTIONS
 */
function submit_edit_profile() {
	$(".error").remove();
	$.ajax({
	   url: "/ajax/submit_edit_profile.php",	
	   type: "POST",		
	   data: $('#submit-user-edit-form').serialize(),		
	   success: function (html) {	
	   	var result = eval('(' + html + ')');
	   	
	   	if (result.status == 'success') {
	   		location.href = "/user/" + result.username; 
	   	} 
	   	else {
	   		$("input[name*='" + result.issue + "']").after('<span class="error">' + result.message + '</span>');
	   	}	
	   }		
	});
	return false;
} 
 




/*
 * VALIDATION FUNCTIONS
 */
function validate_login(options) {
	$("#submit-login-form").validate({
	   rules: {
	   	username: {
	   		required: true
	   	},
	   	password: {
	   		required: true
	   	}
	   },
	   errorElement: "span",
	   errorPlacement: function(error, element) {
			error.appendTo(element.prev() );
		},
	   submitHandler: function(form) {
	   	$(".error-login").remove();
	   	$.ajax({
	   	   url: "/ajax/submit_login.php",	
	   	   type: "POST",		
	   	   data: $('#submit-login-form').serialize(),		
	   	   success: function (html) {	
	   	   	

				   var result = eval('(' + html + ')');			   
				   	
				   if (result.status == "error") {	
				      var error_login ="<span class='error-login'>" + result.message + "</span>";
	   	   		$(error_login).prependTo($("#submit-login-form"));
				   } 
				   else {
				   	$('#login').dialog('close');
				   	login_user();
				   }

	   	   }
	   	});
	   }
	});
}

function validate_signup(options) {
	$.validator.addMethod("usernamevalid", function(value, element) {
		return this.optional(element) || /^[a-z0-9_]+$/i.test(value);
   }, "Numbers, letters, and underscores only");
    
	$("#submit-signup-form").validate({
	   rules: {
	   	username_signup: {
	   		required: true,
	   		remote: "/ajax/validate_username.php",
	   		usernamevalid: true
	   	},
	   	password_signup: "required",
	   	password_confirm: {
	   		equalTo: "#password_signup"
	   	},
	   	email_signup: {
	   		required: true,
	   		email: true,
	   		remote: "/ajax/validate_email.php"
	   	}
	   },
	   messages: {
	   	username_signup: {
	   		remote: "Username is already taken"
	   	},
	   	password_confirm: {
	   		equalTo: "Repeat your password"
	   	},
	   	email_signup: {
	   		remote: "Account with this email already exists",
	   		required: "Enter a valid email address",
	   		email: "Enter a valid email address"
	   	}
	   },
	   errorElement: "span",
	   errorPlacement: function(error, element) {
			error.appendTo(element.prev() );
		},
	   submitHandler: function(form) {
	   	$.ajax({
	   	   url: "/ajax/submit_signup.php",	
	   	   type: "POST",		
	   	   data: $('#submit-signup-form').serialize(),		
	   	   success: function (html) {	
	   	   
	   	   	var result = eval('(' + html + ')');
	   	   	if(result.status == "success") {
				   	$('#signup').dialog('close');
				   	login_user();
				   }

	   	   }		
	   	});
	   	return false;	
	   }
	});
}

function validate_profile() {
	$.validator.addMethod("usernamevalid", function(value, element) {
		return this.optional(element) || /^[a-z0-9_]+$/i.test(value);
   }, "Numbers, letters, and underscores only");
   
	$("#submit-profile-form").validate({
		rules: {
		   username: {
		   	required: true,
		   	remote: "/ajax/validate_username.php",
		   	usernamevalid: true
		   },
		   email: {
		   	required: true,
		   	email: true,
		   	remote: "/ajax/validate_email.php"
		   }
		},
		messages: {
		   username: {
		   	remote: "Username is already taken"
		   },
		   email: {
		   	remote: "Account with this email already exists",
		   	required: "Enter a valid email address",
		   	email: "Enter a valid email address"
		   }
		},
		errorElement: "span",
		errorPlacement: function(error, element) {
		   error.appendTo(element.prev() );
		},
		submitHandler: function(form) {
			$.ajax({
			   url: "/ajax/submit_profile.php",	
			   type: "POST",		
			   data: $('#submit-profile-form').serialize(),		
			   success: function (html) {	
			   	if (html) {	
			   		login_user();
			   		$('#profile').dialog('close');
			   	} 
			   	else alert('Sorry, unexpected error. Please try again later.');	
			   }		
			});
		}
	});
}

function validate_password_recover() {
	$("#submit-password-form").validate({
		rules: {
		   email: {
		   	required: true,
		   	email: true,
		   	remote: "/ajax/validate_password_recover.php"
		   }
		},
		messages: {
		   email: {
		   	email: "Enter a valid email address",
		   	remote: "No account exists with this email"
		   }
		},
		errorElement: "span",
		errorPlacement: function(error, element) {
		   error.appendTo(element.prev() );
		},
		submitHandler: function(form) {
			$.ajax({
			   url: "/ajax/submit_password_recover.php",	
			   type: "POST",		
			   data: $('#submit-password-form').serialize(),		
			   success: function (html) {	
			   	if (html) {	
			   		//New password has been sent to your email
			   		$("#submit-password-form").html("<div class='success'>"+html+"</div>");
			   	} 
			   	else alert('Sorry, unexpected error. Please try again later.');	
			   }		
			});
		}
	});
}

function validate_ask(data) {
	$("#submit-ask-form").validate({
   	rules: {
	   	title: {
	   		required: true,
	   		remote: "/ajax/validate_title.php"
	   	},
	   	details: {
	   		required: true
	   	},
	   	tags: {
	   		required: true
	   	}
	   },
	   messages: {
	   	title: {
	   		remote: "A question with that title already exists"
	   	},
	   	tags: {
	   		required: "You must enter at least 1 tag"
	   	}
	   },
	   errorElement: "span",
	   errorPlacement: function(error, element) {
			error.appendTo(element.prev() );
		},
		submitHandler: function(form) {
			submit_ask(data);
		}
   });
}

function validate_title() {
	$("#submit-sidebar-title-form").validate({
		rules: {
			title: {
				required: true,
				maxlength: 140
			}
		},
		messages: {
	   		title: {
	   			maxlength: "Exceeds 140 characters"
	   		}
	   	},
		errorElement: "span",
	   	errorPlacement: function(error, element) {
			error.appendTo(element.next());
		}
	});
}

function validate_sso_link_users() {
	$('#sso-link-users-form').validate({
		rules: {
			sso_link_password: {
				required: true
			}
		},
		messages: {
			sso_link_password: {
				required: "Required input"
			}
		},	 
		errorElement: 'span',
		errorPlacement: function(error, element) {
			error.appendTo(element.prev());
		},
		submitHandler: function(form) {
			$.ajax({
				url: '/ajax/submit_sso_link_users.php',
				type: 'POST',
				data: $('#sso-link-users-form').serialize(),
				success: function (html) {
					// close dialog, log them in in the ui, etc.
					login_user();
					$('#sso-link-users').dialog('close');
				},
				error: function() {
					alert('Incorrect password, please try again.');
				}
			});
		}
	});
}

function validate_sso_link_users_auth_code() {
	$('#sso-link-users-auth-code-form').validate({
		rules: {
			sso_link_auth_code: {
				required: true
			}
		},
		messages: {
			sso_link_auth_code: {
				required: "Required input"
			}
		},	 
		errorElement: 'span',
		errorPlacement: function(error, element) {
			error.appendTo(element.prev());
		},
		submitHandler: function(form) {
			$.ajax({
				url: '/ajax/submit_sso_auth_code.php',
				type: 'POST',
				data: $('#sso-link-users-auth-code-form').serialize(),
				success: function (html) {
					// close dialog, log them in in the ui, etc.
					login_user();
					$('#sso-link-users-auth-code').dialog('close');
				},
				error: function() {
					alert('Invalid auth code. Please be sure that you enter the four digit auth code exactly as it appears in the email.');
				}
			});
		}
	});
}


function facebook_onload(already_logged_into_facebook) {

	FB.ensureInit(function() {
	    FB.Facebook.get_sessionState().waitUntilReady(function(session) {

	        var is_now_logged_into_facebook = session ? true : false;

	        if (is_now_logged_into_facebook == already_logged_into_facebook)
	       		return;

				ajax_fb_login(); 
		});
	});
}

function facebook_logout() {
	FB.Connect.logoutAndRedirect("/logout");
}

function login_user() {
	if($("#submit-action-login").css("display") == "block") {
		//$("#submit-answer-login").css("display", "none");
		$("#submit-action-login").html("<div class='success'>You are now logged in</div>");
	}
	
	user_logged_in = true;
	
	$.get('/ajax/login_user.php', function(html) {
		$(".user-actions").html(html);
	});
	
}

function ajax_fb_login() {
	
	$.ajax({
	   url: "/ajax/submit_fblogin.php",			
	   success: function (html) {	
	   	var result = eval('(' + html + ')');

	   	if (result.status == 'success') {	
	   		$("#login").dialog('close');
	   		login_user();
	   	} 
	   	else {
	   		$("#profile #name").val(result.user_data[0].name);
	   		$("#profile #fb_uid").val(result.user_data[0].uid);
	   		$("#profile #location").val(result.user_data[0].current_location.city + ', ' + result.user_data[0].current_location.state);
	   		$("#profile #website").val(result.user_data[0].website);
	   		$("#login").dialog('close');
	   		popup({type:"profile"});
	   	}
	   }		
	});
	return false;
}

function auth_revoke_fb() {
	$.get('/ajax/revoke_authorization.php', function(html) {
		if(html == 'success') {}
		else
			alert('Sorry, unexpected error. Please try again later.');
	});
}

function close_login_open_signup(options) {
   $('#login').dialog('close');
   if(options)
   	popup({type:"signup",action:options.action});
   else
   	popup({type:"signup"});
}

function intro_message(user_logged_in) {
	if ($.cookie("intro")) return;
	if(user_logged_in == false && fb_only != 1)
		var html = "<div id='message' class='message'><a href='#' class='close-btn' onclick='setCookieIntro()'></a><p>This is a question and answer community for " + site_name + ", <a href='#' id='signup-link' onclick='popup({type:\"signup\"})'>Sign Up</a></p></div>";
	else
		var html = "<div id='message' class='message'><a href='#' class='close-btn' onclick='setCookieIntro()'></a><p>This is a question and answer community for " + site_name + "</p></div>";
	$("#intro_container").append(html);
}

function setCookieIntro() {
	$("#message").fadeOut();
	$.cookie("intro", "0", { path: '/', expires: 365 });
}


/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};