// contest.js



var contest = {

	do_once:true,

	status:function() {
	
		if (contest.do_once) {
			
			contest.do_once = false;
	
			$.ajax({
	
				type:"GET",
				
				url:"/apps/contest.php",
				
				data:"p=status",
	
				success:function(data) {
	
					if($.browser.opera) {
	
						var height = document.innerHeight;
	
					} else {
	
						var height = $(document).height();
	
					}
	
					$("#ShowContest").html(data);
					
					$("#SlideDown").hide()
					
					.slideDown(800);
	
					$("#overlay").height(height)
	
						//.html(data)
	
						.show()
	
						.fadeTo("fast", 0.59);
	
					var x = ($(window).width()/2)-($("#ShowContest").width()/2);
	
					var y = 100;
	
					$("#ShowContest").show()
	
						.css("left", x)
	
						.css("top", y);
	
					$.scrollTo("#ShowContest", 800);
					
					$(".contest").hide();
					
					contest.do_once = true;
	
				}
	
			});
			
		}

	},
	
	end:function() {

		$("#game-window").hide();

		$("#ShowContest").html("");

		$("#overlay").fadeTo("fast", 0, function() {

			$("#overlay").html("")

			.hide();

		});
		
		$(".contest").show();

	}, //End
	
	check:function() {
		
		if (contest.do_once) {
			
			contest.do_once = false;
			
			var num = $("#contest_mobilenumber").val();
			
			$.ajax({
				
				type:"GET",
				
				url:"/apps/contest.php",
				
				data:"mobile="+num,
	
				success:function(data) {
	
					alert(data);
					
					contest.do_once = true;
	
				}
	
			});
			
		}
		
	},
	
	lottery:function(special) {
		
		if (contest.do_once) {
			
			contest.do_once = false;
			
			var num = $("#contest_mobilenumber").val();
			
			$.ajax({
				
				type:"GET",
				
				url:"/apps/contest.php",
				
				data:special+"lottery="+num,
	
				success:function(data) {
	
					alert(data);
					
					contest.do_once = true;
	
				}
	
			});
			
		}
		
	},
	
	reveal:function(id) {
		
		$("#contest_adid"+id).show();
		
	},
	
	unveal:function() {
		
		$(".contest_find_me").hide();
		
	}
	
};

var birthday = {
	
	success: "Din beställning har blivit skickad.",
	
	error: "Ett fel uppstod.",
	
	cinema:function(id) {

		$.ajax({

			type:"POST",

			url:"/apps/orders.php",

			data:"q="+id,

			success:function(data) {

				if($.browser.opera) {

					var height = document.innerHeight;

				}

				else {

					var height = $(document).height();

				}	
				
				$("#OrdersConfirm").html(data)
				
				.find(".disbutton").fadeTo(0,0.5);
			
				$("#SlideDown").hide()
				
				.slideDown("fast");

				$("#overlay").height(height)

					//.html(data)

					.show()

					.fadeTo("fast", 0.59);

				var x = ($(window).width()/2)-($("#OrdersConfirm").width()/2);

				//var y = ($(window).height()/2)-($("#NewsRead").height()/2);

				var y = 100;

				$("#OrdersConfirm").show()

					.css("left", x)

					.css("top", y);

				$.scrollTo("#OrdersConfirm", 800);
								

			}

			

		});

		

	}, //Launch birthday
	
	end:function() {

		

		$("#game-window").hide();

		$("#OrdersConfirm").html("");

		$("#overlay").fadeTo("fast", 0, function() {

			

			$("#overlay").html("")

			.hide();

			

		});

		

	}, //end()
	
	checked:function(value) {
		
		if (value == true) {
			
			$("#OrdersConfirm").find(".disbutton").css("display","none");
			
			$("#OrdersConfirm").find(".button").css("display","");

		} else {
			
			$("#OrdersConfirm").find(".disbutton").css("display","");
			
			$("#OrdersConfirm").find(".button").css("display","none");
							
		}
		
	},
	
	accept:function(article) {
		
		$.ajax({

			type:"GET",

			url:"/apps/contest.php",

			data:article+"=accept",

			success:function(data) {

				if (data == "success") {
					alert(birthday.success);
					birthday.end();
				} else {
					alert(birthday.error);
				}

			}

		});
		
	}
	
};

var christmas = {
	
	error:"Ett fel uppstod.",
	
	success:"Du är nu registrerad för tävlingen.",
	
	sign:function() {
		
		$.ajax({

			type:"GET",

			url:"/apps/contest.php",

			data:"christmas=true",

			success:function(data) {

				if (data == "success") {
					$("#key_fade").fadeTo("slow",0.5);
					$("#secret_gift").fadeTo(0,0)
					.show()
					.fadeTo("slow",0.5, function () {
						
						alert(christmas.success);
						
					});
				} else {
					alert(christmas.error);
				}
										
			}

		});
		
	},
	
	browse_val:0,
	
	browse:function(id) {
		
		if ($("#day_info_"+christmas.browse_val)[0])
			$("#day_info_"+christmas.browse_val).hide();
		
		christmas.browse_val = id;
		
		$("#day_info_"+christmas.browse_val).show();
		
	},
	
	box_height:null,
	
	content_height:null,
	
	url:"",
	
	tabs:function(id) {
		
		if (!christmas.box_height)
			christmas.box_height = $("#christmas_hints").height();
		if (!christmas.content_height)
			christmas.content_height = $("#christmas_hints").find(".content").height();
		
		christmas.url = "-"+id;
		
		$(".xmas_li").removeClass("chosen");
		$(".xmas_span").removeClass("show");
		
		$("#xmas_"+id).addClass("chosen");
		$("#season_"+id).addClass("show");
		
		var handle = ($("#season_"+id).height()+10);
		
		$("#christmas_hints").animate({
			
			height: (christmas.box_height-christmas.content_height)+handle+"px"
			
		}, "slow");
		
		$("#christmas_hints").find(".content").animate({
			
			height: handle+"px"
			
			
		}, "slow");
		
	},
	
	offer:function(id) {
		
		var Speed = "slow";
				
		if ($("#shop_offer_"+id).css("display") == "none") {
			
			$("#shop_offer_"+id).css("display","block");
			
		} else {
			
			$("#shop_offer_"+id).css("display","none");
			
		}
		
	}
	
};

var moustache	=	{
	
	loader:function() {
		var h	=	$("img#uploaded_entry");
		if (h[0]) {
			h.after('<img id="uploaded_entry" userid="'+h.attr("userid")+'" src="/moustache/user_'+h.attr("userid")+'.jpg" />')
				.remove();
		}
	},
	
	entered:function() {
		setTimeout("moustache.loader", 5000)
	}
	
}

$(function() {
	
	$("#button_buy_tickets").bind("click", function() {
		$.ajax({
			type:"POST",
			url:"/apps/contest.php",
			dataType:"json",
			data:$("form#form_buy_tickets").serialize(),
			success:function(data) {
				if (data.success == "true") {
					update.saldo(-data.cost);
					$("#boughtalert").show()
						.attr("display", "block")
						.fadeOut(2500);
					var h	=	$("#my_allotments");
					h.fadeOut(500, function() {
						h.html(parseInt(h.html()) + parseInt(data.amount))
							.fadeIn(500);
					});
				} else alert(data.echo);
			}
		});
	});
	
});