function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function add_fb_points(x) {
	if(x != null){
		$.ajax({
			type: "GET",
			url: "/ajax/reg_fb/"+FB.Connect.get_loggedInUser(),
			success: function(msg){
				//$("#fbconnect").html("Thank you for signing up through Facebook.");
				$('#fbconnect').css({ display: 'none' });
				$('#fbapp').css({ display: 'block' });
			}
		});
	}
}

function add_fb_points_stats(x){
	if(x != null){
		$.ajax({
			type: "GET",
			url: "/ajax/reg_fb/"+FB.Connect.get_loggedInUser(),
			success: function(msg){
				$("#fbconnect").html('<img src="/images/contest/fb.png" title="You are connected through Facebook" />');
			}
		});
	}
}

var Construct = new function ConstructObject() {
	this.current_location = window.location.href.replace(new RegExp("^http://" + window.location.hostname), "");
	
	this.init = function() {
		$(document).ready(function(){
			Events.bindIndexEvents();
			Events.bindRegistrationEvents();
			Events.bindStatsEvents();
			Events.bindCompareEvents();
			setTimeout("Timing.startTimingEvents()",5*60*1000);
		});
	};
};

var Timing = new function TimingObject(){
	this.startTimingEvents = function(){
		$.ajax({
			type: "GET",
			url: "/ajax/get_task_updates",
			success: function(msg){
				$("#tasks").html(msg);
				$(".taskbutton").unbind("click").click(function(){
					var th = $(this);
					var ids = $(this).attr("id").split("k");
					var task_id = ids[1];
					var id = $(this).attr("id");
					switch(id.substr(0,2)){
						case "fb":
							$.ajax({
								type: "GET",
								url: "/ajax/get_facebook_task/"+task_id,
								success: function(msg){
									$("#post_pop").html(msg);
									$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
									$("#post_pop").fadeIn(500);
									$("#closepop").unbind("click").click(function(){
										$("#post_pop").fadeOut(300);
										$("#dither").fadeOut(500);
									});
									$("#posttwitter").unbind("click").click(function(){
										$.ajax({
											type: "POST",
											url: "/ajax/fb_post",
											data: "authenticity_token="+$("#fbpostform input").eq(0).val()+"&task_id="+task_id,
											success: function(msg){
												$("#message").text("You have successfully posted to your Facebook");
												th.attr({src: "/images/contest/fb_icon_off.png"});
												$.ajax({
													type: "GET",
													url: "/ajax/get_user_points/"+uid,
													success: function(msg){
														$("#totalpoints").text(msg);
													}
												});
											}
										});
									});
								}
							});
							break;
						case "tw":
							$.ajax({
								type: "GET",
								url: "/ajax/get_twitter_task/"+task_id,
								success: function(msg){
									$("#post_pop").html(msg);
									$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
									$("#post_pop").fadeIn(500);
									$("#closepop").unbind("click").click(function(){
										$("#post_pop").fadeOut(300);
										$("#dither").fadeOut(500);
									});
									$("#posttwitter").unbind("click").click(function(){
										$.ajax({
											type: "POST",
											url: "/ajax/tw_post",
											data: "authenticity_token="+$("#twpostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#twitter_username").val()+"&pass="+$("#twitter_password").val(),
											success: function(msg){
												$("#message").text("Success!  Your task has been posted to your Twitter!");
												th.attr({src: "/images/contest/twitter_icon_off.png"});
												$.ajax({
													type: "GET",
													url: "/ajax/get_user_points/"+uid,
													success: function(msg){
														$("#totalpoints").text(msg);
													}
												});
											}
										});
									});
									$("#showlogin").unbind("click").click(function(){
										$("#signin").css({display: "block"});
									});
								}
							});
							break;
						case "em":
							$.ajax({
								type: "GET",
								url: "/ajax/get_email_task/"+task_id,
								success: function(msg){
									$("#post_pop").html(msg);
									$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
									$("#post_pop").fadeIn(500);
									$("#closepop").unbind("click").click(function(){
										$("#post_pop").fadeOut(300);
										$("#dither").fadeOut(500);
									});
									$("#posttwitter").unbind("click").click(function(){
										$.ajax({
											type: "POST",
											url: "/ajax/email_send",
											data: "authenticity_token="+$("#empostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#user").val(),
											success: function(msg){
												$("#post_pop").fadeOut(300);
												$("#dither").fadeOut(500);
												th.attr({src: "/images/contest/email_icon_off.png"});
												$.ajax({
													type: "GET",
													url: "/ajax/get_user_points/"+uid,
													success: function(msg){
														$("#totalpoints").text(msg);
													}
												});
											}
										});
									});
								}
							});
							break;
					}
				});
			}
		});
		
		$.ajax({
			type: "GET",
			url: "/ajax/get_completed_tasks",
			success: function(msg){
				$("#completedtasks").html(msg);
			}
		});
		
		$.ajax({
			type: "GET",
			url: "/ajax/get_new_completed_tasks",
			success: function(msg){
				$("#allctasks").html(msg);
				$(".userphoto").unbind("hover").hover(
					function(e){
						var u = $(this).attr("id").split("r");
						var uid = u[1];
						$.ajax({
							type: "GET",
							url: "/ajax/get_user_info/"+uid,
							success: function(msg){
								var info = msg.split("|");
								$("#popusername").text(info[0]);
								$("#poppoints").text(info[1]);
								$("#popicons").html(info[2]);
								$("#poprank").text(info[3]);
								$("#popshoutout").text(info[4]);
								$("#popphoto").attr({src: "/user_images/" + info[5]});
								
								$("#userpop").css({top: (e.pageY + 5) + "px",left: (e.pageX + 5) + "px"});
								$("#userpop").fadeIn(300);
							}
						});
					},
					function(){
						$("#userpop").fadeOut(300);
					}
				);
			}
		});
		
		$.ajax({
			type: "GET",
			url: "/ajax/get_ranks",
			success: function(msg){
				$("#fulllist").html(msg);
				$(".userphoto").unbind("hover").hover(
					function(e){
						var u = $(this).attr("id").split("r");
						var uid = u[1];
						$.ajax({
							type: "GET",
							url: "/ajax/get_user_info/"+uid,
							success: function(msg){
								var info = msg.split("|");
								$("#popusername").text(info[0]);
								$("#poppoints").text(info[1]);
								$("#popicons").html(info[2]);
								$("#poprank").text(info[3]);
								$("#popshoutout").text(info[4]);
								$("#popphoto").attr({src: "/user_images/" + info[5]});
								
								$("#userpop").css({top: (e.pageY + 5) + "px",left: (e.pageX + 5) + "px"});
								$("#userpop").fadeIn(300);
							}
						});
					},
					function(){
						$("#userpop").fadeOut(300);
					}
				);
			}
		});
		
		setTimeout("Timing.startTimingEvents()",5*60*1000);
	};
};

var Events = new function EventsObject() {
	this.bindIndexEvents = function(){
		$("#regform").unbind("submit").submit(function(){
			var message = '';
			
			if($("#username").val() == ''){
				message += 'Please enter a username\n';
			}
			if(!$("#email").val().match(/^([a-z]+[\w-]*\.?)+@([\w-]+\.)+[a-z]{2,4}$/i)){
				message += 'Please enter your email address\n';
			}
			if(!document.getElementById("agree").checked){
				message += 'Please agree to the terms and conditions\n';
			}
			
			if(message != ''){
				alert('The following errors have occured:\n\n' + message);
				return false;
			} else {
				return true;
			}
		});
		
		$("#forgot").unbind("click").click(function(){
			window.open("/contest/forgot_password","forgot","height=300,width=300");
			return false;
		});
	};
	
	this.bindRegistrationEvents = function(){
		$("#photoform").unbind("submit").submit(function(){
			if($("#picupload").val() == ''){
			
			} else {
				$("#changeimage").css({display: "none"});
			}
		});
	};
	
/*
	this.bindRegistrationEvents = function(){
		$("#picupload").unbind("blur").blur(function(){
			console.log("photo");
			$("#photoform").submit();
			$(this).parent().ajaxSubmit({
				complete: function(XMLHttpRequest, textStatus){
					$("#photo").attr({src: "/user_images/"+XMLHttpRequest.responseText});
					$("#changeimage").css({display: "none"});
				}
			});
		});
	};
*/
	
	this.bindStatsEvents = function(){
		$("#questionpop").unbind("hover").hover(function(e){
			e.stopPropagation();
		});
		
		$("#chpw").unbind("click").click(function(){
			if($("#changepassword").css("display") != "block"){
				$("#changepassword").css({display: "block"});
			} else {
				$("#changepassword").css({display: "none"});
			}
		});
		
		$("#chpic").unbind("click").click(function(){
			if($("#changeimage").css("display") != "block"){
				$("#changeimage").css({display: "block"});
			} else {
				$("#changeimage").css({display: "none"});
			}
		});
		
		$("#photo").unbind("click").click(function(){
			if($("#changeimage").css("display") != "block"){
				$("#changeimage").css({display: "block"});
			} else {
				$("#changeimage").css({display: "none"});
			}
		});
		
		$("#updatepassword").unbind("click").click(function(){
			$.ajax({
				type: "GET",
				url: "/ajax/get_pw/"+uid,
				success: function(msg){
					var message = '';
					
					if(msg != $("#current").val()){
						message = "Your password is incorrect";
					}
					if($("#new1").val() != $("#new2").val()){
						message = "Your passwords do not match";
					}
					
					if(message != ''){
						alert(message);
					} else {
						$.ajax({
							type: "GET",
							url: "/ajax/update_password/"+$("#new1").val(),
							success: function(msg){
								$("#changepassword").html("Your password has been changed");
								setTimeout(1500,'$("#changepassword").fadeOut(500)');
							}
						});
					}
				}
			});
		});
		
		$(".closetask").unbind("click").click(function(){
			var close = confirm("Are you sure you want to delete this task?  Once you delete, you cannot post it and the points will be lost.");
			if(close){
				$.ajax({
					type: "GET",
					url: "/ajax/remove_task/"+$(this).parent().attr("id"),
					success: function(msg){
						$.ajax({
							type: "GET",
							url: "/ajax/get_task_updates",
							success: function(msg){
								$("#tasks").html(msg);
								$(".taskbutton").unbind("click").click(function(){
									var th = $(this);
									var ids = $(this).attr("id").split("k");
									var task_id = ids[1];
									var id = $(this).attr("id");
									switch(id.substr(0,2)){
										case "fb":
											$.ajax({
												type: "GET",
												url: "/ajax/get_facebook_task/"+task_id,
												success: function(msg){
													$("#post_pop").html(msg);
													$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
													$("#post_pop").fadeIn(500);
													$("#closepop").unbind("click").click(function(){
														$("#post_pop").fadeOut(300);
														$("#dither").fadeOut(500);
													});
													$("#posttwitter").unbind("click").click(function(){
														$.ajax({
															type: "POST",
															url: "/ajax/fb_post",
															data: "authenticity_token="+$("#fbpostform input").eq(0).val()+"&task_id="+task_id,
															success: function(msg){
																$("#message").text("You have successfully posted to your Facebook");
																th.attr({src: "/images/contest/fb_icon_off.png"});
																$.ajax({
																	type: "GET",
																	url: "/ajax/get_user_points/"+uid,
																	success: function(msg){
																		$("#totalpoints").text(msg);
																	}
																});
															}
														});
													});
												}
											});
											break;
										case "tw":
											$.ajax({
												type: "GET",
												url: "/ajax/get_twitter_task/"+task_id,
												success: function(msg){
													$("#post_pop").html(msg);
													$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
													$("#post_pop").fadeIn(500);
													$("#closepop").unbind("click").click(function(){
														$("#post_pop").fadeOut(300);
														$("#dither").fadeOut(500);
													});
													$("#posttwitter").unbind("click").click(function(){
														$.ajax({
															type: "POST",
															url: "/ajax/tw_post",
															data: "authenticity_token="+$("#twpostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#twitter_username").val()+"&pass="+$("#twitter_password").val(),
															success: function(msg){
																$("#message").text("You have successfully posted to your Twitter");
																th.attr({src: "/images/contest/twitter_icon_off.png"});
																$.ajax({
																	type: "GET",
																	url: "/ajax/get_user_points/"+uid,
																	success: function(msg){
																		$("#totalpoints").text(msg);
																	}
																});
															}
														});
													});
													$("#showlogin").unbind("click").click(function(){
														$("#signin").css({display: "block"});
													});
												}
											});
											break;
										case "em":
											$.ajax({
												type: "GET",
												url: "/ajax/get_email_task/"+task_id,
												success: function(msg){
													$("#post_pop").html(msg);
													$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
													$("#post_pop").fadeIn(500);
													$("#closepop").unbind("click").click(function(){
														$("#post_pop").fadeOut(300);
														$("#dither").fadeOut(500);
													});
													$("#posttwitter").unbind("click").click(function(){
														$.ajax({
															type: "POST",
															url: "/ajax/email_send",
															data: "authenticity_token="+$("#empostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#user").val(),
															success: function(msg){
																$("#post_pop").fadeOut(300);
																$("#dither").fadeOut(500);
																th.attr({src: "/images/contest/email_icon_off.png"});
																$.ajax({
																	type: "GET",
																	url: "/ajax/get_user_points/"+uid,
																	success: function(msg){
																		$("#totalpoints").text(msg);
																	}
																});
															}
														});
													});
												}
											});
											break;
									}
								});
							}
						});
						$.ajax({
							type: "GET",
							url: "/ajax/get_completed_tasks",
							success: function(msg){
								$("#completewrapper").css({display: "block"});
								$("#completedtasks").html(msg);
							}
						});
					}
				});
			}
		});
		
		$("#updateshoutout").unbind("click").click(function(){
			$.ajax({
				type: "GET",
				url: "/ajax/update_shoutout",
				data: "shoutout=" + $("#shoutout").val(),
				success: function(msg){
					$("#shoutresp").css({ display: 'block' }).text(msg);
					$('#updateshoutout').remove();
					$('#shoutout').attr('disabled', 'disabled');
				}
			});
		});
		
		$(".taskbutton").unbind("click").click(function(){
			var th = $(this);
			var ids = $(this).attr("id").split("k");
			var task_id = ids[1];
			var id = $(this).attr("id");
			switch(id.substr(0,2)){
				case "fb":
					$.ajax({
						type: "GET",
						url: "/ajax/get_facebook_task/"+task_id,
						success: function(msg){
							$("#post_pop").html(msg);
							$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
							$("#post_pop").fadeIn(500);
							$("#closepop").unbind("click").click(function(){
								$("#post_pop").fadeOut(300);
								$("#dither").fadeOut(500);
							});
							$("#posttwitter").unbind("click").click(function(){
								$.ajax({
									type: "POST",
									url: "/ajax/fb_post",
									data: "authenticity_token="+$("#fbpostform input").eq(0).val()+"&task_id="+task_id,
									success: function(msg){
										$('#message').siblings('.float').children('img.pointerimage').css({ display: 'none' });
										$("#message").text("Success!  Your task has been posted to your Facebook!");
										$('#message').css({
											display: 'block',
											marginTop: '-20px',
											padding: '5px 10px',
											width: '345px',
											background: '#d4e7eb',
											border: '1px solid #becfd3'
										});
										th.attr({src: "/images/contest/fb_icon_off.png"});
										$.ajax({
											type: "GET",
											url: "/ajax/get_user_points/"+uid,
											success: function(msg){
												$("#totalpoints").text(msg);
											}
										});
									}
								});
							});
						}
					});
					break;
				case "tw":
					$.ajax({
						type: "GET",
						url: "/ajax/get_twitter_task/"+task_id,
						success: function(msg){
							$("#post_pop").html(msg);
							$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
							$("#post_pop").fadeIn(500);
							$("#closepop").unbind("click").click(function(){
								$("#post_pop").fadeOut(300);
								$("#dither").fadeOut(500);
							});
							$("#posttwitter").unbind("click").click(function(){
								$.ajax({
									type: "POST",
									url: "/ajax/tw_post",
									data: "authenticity_token="+$("#twpostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#twitter_username").val()+"&pass="+$("#twitter_password").val(),
									success: function(msg){
										$("#message").text("You have successfully posted to your Twitter");
										th.attr({src: "/images/contest/twitter_icon_off.png"});
										$.ajax({
											type: "GET",
											url: "/ajax/get_user_points/"+uid,
											success: function(msg){
												$("#totalpoints").text(msg);
											}
										});
									}
								});
							});
							$("#showlogin").unbind("click").click(function(){
								$("#signin").css({display: "block"});
							});
						}
					});
					break;
				case "em":
					$.ajax({
						type: "GET",
						url: "/ajax/get_email_task/"+task_id,
						success: function(msg){
							$("#emailcontacts").unbind("click").click(function(e){
								$.ajax({
									type: "GET",
									url: "/ajax/get_contacts",
									success: function(msg){
										if(msg != ''){
											var emails = msg.split(",");
											var html = "";
											html += "<b>Add New Addresses</b><br /><br />Email your friends:<br />Type your friends' emails in the field below (separated by commas)<br /><textarea id='emails' name='emails'></textarea><br />Your friends can unsubscribe at any time.<br /><br />Click the checkbox to remove a friend's email:<br />";
											for(x in emails){
												if(emails[x] != ''){
													html += "<input type='checkbox' name='"+emails[x]+"' value='"+emails[x]+"' /> "+emails[x]+"<br />";
												}
											}
											html += "<br /><img src='/images/contest/finishbutton.png' id='connectemailbutton' class='pointerbutton' /><br /><br /><a href='#' id='closeemail' style='color: #000'>[X] Close</a>";
											$("#connectpop").html(html);
											$("#connectpop").css({top: e.pageY + "px", left: (e.pageX - 150)+ "px"});
											$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
											$("#connectpop").fadeIn(500, function(){
												$("#connectemailbutton").unbind("click").click(function(){
													var data = "authenticity_token="+token_tag+"&emails="+$("#emails").val()+"&todelete=";
													var del = '';
													for(x in $("#connectpop input:checked")){
														if(x.match(/^[0-9]+$/)){
															del += $("#connectpop input:checked").eq(x).val() + ",";
														}
													}
													$.ajax({
														type: "POST",
														url: "/ajax/em_edit",
														data: data+del,
														success: function(msg){
															$("#connectpop").fadeOut(300);
															$("#dither").fadeOut(500);
														}
													});
												});
												$("#closeemail").unbind("click").click(function(){
													$("#connectpop").fadeOut(300);
													$("#dither").fadeOut(500);
												});
											});
										} else {
											$("#connectpop").html("<b>Add New Addresses</b><br /><br />Email your friends:<br />Type your friends' emails in the field below (separated by commas)<br />Your friends can unsubscribe at any time.<br /><textarea id='emails' name='emails'></textarea><br /><img src='/images/contest/finishbutton.png' id='connectemailbutton' class='pointerbutton' /><br /><br /><a href='#' id='closeemail' style='color: #000'>[X] Close</a>");
											$("#connectpop").css({top: e.pageY + "px", left: (e.pageX - 150)+ "px"});
											$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
											$("#connectpop").fadeIn(500,function(){
												$("#connectemailbutton").unbind("click").click(function(){
													$.ajax({
														type: "POST",
														url: "/ajax/em_connect",
														data: "authenticity_token="+token_tag+"&emails="+$("#emails").val(),
														success: function(msg){
															$("#connectpop").fadeOut(300);
															$("#dither").fadeOut(500);
															$("#emailconnect").attr({src: "/images/contest/em.png"});
														}
													});
												});
												$("#closeemail").unbind("click").click(function(){
													$("#connectpop").fadeOut(300);
													$("#dither").fadeOut(500);
												});
											});
										}
									}
								});
							});
							$("#post_pop").html(msg);
							$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
							$("#post_pop").fadeIn(500);
							$("#closepop").unbind("click").click(function(){
								$("#post_pop").fadeOut(300);
								$("#dither").fadeOut(500);
							});
							$("#posttwitter").unbind("click").click(function(){
								$.ajax({
									type: "POST",
									url: "/ajax/email_send",
									data: "authenticity_token="+$("#empostform input").eq(0).val()+"&task_id="+task_id+"&user="+$("#user").val(),
									success: function(msg){
										$("#post_pop").fadeOut(300);
										$("#dither").fadeOut(500);
										th.attr({src: "/images/contest/email_icon_off.png"});
										$.ajax({
											type: "GET",
											url: "/ajax/get_user_points/"+uid,
											success: function(msg){
												$("#totalpoints").text(msg);
											}
										});
									}
								});
							});
						}
					});
					break;
			}
		});
		
		$("#showbanners").unbind("click").click(function(){
			$.ajax({
				type: "POST",
				url: "/tracking/create",
				data: "authenticity_token="+token_tag+"&uid="+uid+"&points=10&url=http://www.byebyebirdieonbroadway.com/&task_id=0",
				success: function(msg){
					$("#bannerlink").text('<a href="http://www.byebyebirdieonbroadway.com/track/'+msg+'" target="_blank"><img src="http://www.byebyebirdieonbroadway.com/images/contest/contest_banner.jpg" border="0" /></a>');
					$("#bannerlink").unbind("click").click(function(){
						$(this).select();
					});
					$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
					$("#banners").fadeIn(500);
				}
			});
		});
		
		$("#closebanner, #dither").unbind("click").click(function(){
			$("#banners").fadeOut(300);
			$("#dither").fadeOut(500);
		});
		
		$(".questions").unbind("mouseenter").mouseenter(function(e){
			if($(this).attr("flip") == "yes"){
				$("#questiontext").text($(this).attr("name"));
				$("#questionpop").css({top: $(this).position().top + "px", left: ($(this).position().left - 260) + "px"});
				$("#questionpop").fadeIn(1000);
			} else {
				$("#questiontext").text($(this).attr("name"));
				$("#questionpop").css({top: $(this).position().top + "px", left: ($(this).position().left + 17) + "px"});
				$("#questionpop").fadeIn(1000);
			}
		});
		
		$(".questions").unbind("mouseout").mouseout(function(e){
			e.stopPropagation();
			$("#questionpop").fadeOut(200);
		});
		
		$("#emailconnect").unbind("click").click(function(e){
			$.ajax({
				type: "GET",
				url: "/ajax/get_contacts",
				success: function(msg){
					if(msg != ''){
						var emails = msg.split(",");
						var html = "";
						html += "<b>Add New Addresses</b><br /><br />Email your friends:<br />Type your friends' emails in the field below (separated by commas)<br /><textarea id='emails' name='emails'></textarea><br />Your name: <input type='text' name='fname' /><br /><br />Your friends can unsubscribe at any time.<br /><br />Click the checkbox to remove a friend's email:<br />";
						for(x in emails){
							if(emails[x] != ''){
								html += "<input type='checkbox' name='"+emails[x]+"' value='"+emails[x]+"' /> "+emails[x]+"<br />";
							}
						}
						html += "<br /><img src='/images/contest/finishbutton.png' id='connectemailbutton' class='pointerbutton' /><br /><br /><a href='#' id='closeemail' style='color: #000'>[X] Close</a>";
						$("#connectpop").html(html);
						$("#connectpop").css({top: e.pageY + "px", left: (e.pageX - 150)+ "px"});
						$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
						$("#connectpop").fadeIn(500, function(){
							$("#connectemailbutton").unbind("click").click(function(){
								var data = "authenticity_token="+token_tag+"&emails="+$("#emails").val()+"&todelete=";
								var del = '';
								for(x in $("#connectpop input:checked")){
									if(x.match(/^[0-9]+$/)){
										del += $("#connectpop input:checked").eq(x).val() + ",";
									}
								}
								$.ajax({
									type: "POST",
									url: "/ajax/em_edit",
									data: data+del,
									success: function(msg){
										$("#connectpop").fadeOut(300);
										$("#dither").fadeOut(500);
									}
								});
							});
							$("#closeemail, #dither").unbind("click").click(function(){
								$("#connectpop").fadeOut(300);
								$("#dither").fadeOut(500);
							});
						});
					} else {
						$("#connectpop").html('<div style="position:relative;padding:20px 0 0 0;"><div style="font-weight:bold;font-size:16px;border-bottom:1px solid #906383">Add New Addresses</div><div style="font-size:12px;margin:5px 0 10px 0;">Type your friends\' emails in the field below (separated by commas). Your friends can unsubscribe at any time.</div><textarea style="padding:5px;width:340px;font-family:Arial,Helvetica,Sans-serif;font-size:12px;" id="emails" name="emails"></textarea><div style="margin:15px 0;"><div style="margin: 0 0 3px 0;font-size:12px;">Your name:</div><input style="padding:3px;font-size:12px;font-family:Arial,Helvetica,Sans-serif;" type="text" name="fname" /></div><img style="position:relative;left:-2px;" src="/images/contest/finishbutton.png" id="connectemailbutton" class="pointerbutton" /><a href="#" id="closeemail" style="color: #000;position:absolute;top: 5px;right:5px;">[X] Close</a></div>');
						$("#connectpop").css({top: e.pageY + "px", left: (e.pageX - 150)+ "px"});
						$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
						$("#connectpop").fadeIn(500,function(){
							$("#connectemailbutton").unbind("click").click(function(){
								$.ajax({
									type: "POST",
									url: "/ajax/em_connect",
									data: "authenticity_token="+token_tag+"&emails="+$("#emails").val(),
									success: function(msg){
										$("#connectpop").fadeOut(300);
										$("#dither").fadeOut(500);
										$("#emailconnect").attr({src: "/images/contest/em.png"});
									}
								});
							});
							$("#closeemail, #dither").unbind("click").click(function(){
								$("#connectpop").fadeOut(300);
								$("#dither").fadeOut(500);
							});
						});
					}
				}
			});
		});
		
		$("#twitterconnect").unbind("click").click(function(e) {
			$("#connectpop").html('<div style="position:relative;padding:20px 0 0 0;"><div style="font-weight:bold;font-size:16px;border-bottom:1px solid #906383">Connect Your Twitter</div><div style="margin:15px auto;width:150px;"><div style="margin: 0 0 3px 0;font-size:12px;">Username:</div><input style="padding:3px;font-size:12px;font-family:Arial,Helvetica,Sans-serif;" type="text" name="twitteruser" /></div><div style="margin:15px auto;width:150px;"><div style="margin: 0 0 3px 0;font-size:12px;">Password:</div><input style="padding:3px;font-size:12px;font-family:Arial,Helvetica,Sans-serif;" type="text" name="twitterpass" /></div><div style="text-align:center;"><img src="/images/contest/finishbutton.png" id="connecttwitterbutton" style="position:relative;left:-42px;" class="pointerbutton" /></div><a href="#" id="closeemail" style="color: #000;position:absolute;top: 5px;right:5px;">[X] Close</a></div>');
			//$("#connectpop").html("Connect your Twitter:<br />Username: <input type='text' id='twitteruser' name='twitteruser' /><br />Password:<input type='password' id='twitterpass' name='twitterpass' /><br /><br /><br /><a href='#' id='twitterclose' style='color: #000;'>[X] Close</a>");
			$("#connectpop").css({top: e.pageY + "px", left: (e.pageX - 150)+ "px"});
			$("#dither").css({ display: "block", opacity: 0 }).fadeTo(300, 0.6);
			$("#connectpop").fadeIn(500,function(){
				$("#connecttwitterbutton").unbind("click").click(function(){
					$.ajax({
						type: "POST",
						url: "/ajax/tw_connect",
						data: "authenticity_token="+token_tag+"&user="+$("#twitteruser").val()+"&pass="+$("#twitterpass").val(),
						success: function(){
							$("#connectpop").fadeOut(300);
							$("#dither").fadeOut(500);
							$("#twitterconnect").attr({src: "/images/contest/tw.png"});
						}
					});
				});
				$("#twitterclose").unbind("click").click(function(){
					$("#connectpop").fadeOut(300);
					$("#dither").fadeOut(500);
				});
			});
		});
	};
	
	this.bindCompareEvents = function(){
		$(".tabs .inactive").unbind("click").click(function(){
			if($("#stats").css("display") != "none"){
				$("#stats").css({display: "none"});
				$("#compare").css({display: "block"});
			} else {
				$("#stats").css({display: "block"});
				$("#compare").css({display: "none"});
			}
		});
		
		$("#listup").unbind("click").click(function(){
			if($("#fulllist").css("top") != "0px"){
				var top = $("#fulllist").css("top").split("p");
				var newTop = top[0] - -57;
				$("#fulllist").css({top: newTop + "px"});
			}
		});
		
		$("#listdown").unbind("click").click(function(){
			var top = $("#fulllist").css("top").split("p");
			var newTop = top[0] - 57;
			//alert((total - 2) * -57 + " " + newTop);
			if(newTop > (total - 2) * -57){
				$("#fulllist").css({top: newTop + "px"});
			}
		});
		
		$(".userphoto").unbind("hover").hover(
			function(e){
				var u = $(this).attr("id").split("r");
				var uid = u[1];
				$.ajax({
					type: "GET",
					url: "/ajax/get_user_info/"+uid,
					success: function(msg){
						var info = msg.split("|");
						$("#popusername").text(info[0]);
						$("#poppoints").text(info[1]);
						$("#popicons").html(info[2]);
						$("#poprank").text(info[3]);
						$("#popshoutout").text(info[4]);
						$("#popphoto").attr({src: "/user_images/" + info[5]});
						
						$("#userpop").css({top: (e.pageY + 5) + "px",left: (e.pageX + 5) + "px"});
						$("#userpop").fadeIn(300);
					}
				});
			},
			function(){
				$("#userpop").fadeOut(300);
			}
		);
		
		$("#list").unbind("hover").hover(function(){
			$("#userpop").fadeOut(300);
		});
		
		$("#compare").unbind("hover").hover(function(){
			$("#userpop").fadeOut(300);
		});
	};
};