function fbConnect() {

	FB.ensureInit(function() {
		FB.Connect.requireSession();
		FB.Facebook.get_sessionState().waitUntilReady(function() {
			window.location = "/dashboard/connect";
		});
	});
	
}

$(document).ready(function() {

	$('body').addClass('js');
	$("#landing").slideDown();
	
	$("#landing").click(function(){
		$("#landing").slideUp();
		$.cookie("landing", "no", {  path: '/', expires: 30 });
	});
	
	$('.discount .reveal').click(function() {
	
		href = $(this).attr("href");
		storeWin = window.open(href,'_blank','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
		
		if($(this).parents("div.discount").hasClass('code')) {
			storeWin.blur();
			$(".discount_button",$(this).parents("div.discount")).hide();
			$(".product_button",$(this).parents("div.discount")).hide();
			$(".discount_reveal",$(this).parents("div.discount")).show();
		}
		
		return false;
	});
	
	$('.store .reveal').click(function() {
	
		href = $(this).attr("href");
		storeWin = window.open(href,'_blank','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
		storeWin.blur();
		
		$(".code .discount_button").hide();
		$(".code .discount_instructions").hide();
		$(".code .discount_reveal").show();
		
		return false;
	
	});
	
	$(".splash_stores li").hover(function(){
		$(".splash_store_title",$(this)).css({'backgroundPosition': '-148px 0', 'color': '#ff9900'});
		$(".splash_store_logo",$(this)).animate({'top': '-15px'},'fast');
		
	},function(){
		$(".splash_store_title",$(this)).css({'backgroundPosition': '0 0', 'color': '#ffcc00'});
		$(".splash_store_logo",$(this)).animate({'top': '0px'},'fast');
	});
	
	$("#fbConnect").click(function(){
		fbConnect();
		return false;
	});
	
	$("#emailer").click(function(){
		$('#codeWindow').load('/ajax/email.php',function(){
			$(".initial").bind("focus", function(){
				if(this.value == this.defaultValue ) {
					this.value = "";
					$(this).addClass("focus");
				}
			});
			$(".initial").bind("blur", function(){
				if( !this.value.length ) {
					this.value = this.defaultValue;
					$(this).removeClass("focus");
				}
			});
			$("#codeWindow .submit").click(function(){
				$("#emailError").hide();
				$.post("/ajax/email.php",{
					name: $("#emailName").val(),
					email: $("#emailEmail").val(),
					message: $("#emailMessage").val(),
					page: document.location.href
				},function(result){
					if(result.type=="true") {
						//success
						$("#emailError").hide();
						$('#codeWindow').jqm().jqmShow();
						$("#emailForm").hide();
						$("#emailConf").show();
					} else {
						$("#emailError").html(result.message);
						$("#emailError").fadeIn(200);
					}
					
				}, "json");
				return false;
			});
			$(".jqmClose").click(function(){
				$('#codeWindow').jqm().jqmHide();
				return false;
			});
		});
		$('#codeWindow').jqm().jqmShow();
		return false;
	});
	
	$("#main .tab_item").hide(); //Hide all content
	$("#main .tabs li:first").addClass("active").show();
	$("#main .tab_item:first").show(); //Show first tab content

	//On Click Event
	$("#main .tabs li").click(function() {

		$("#main .tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#main .tab_item").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});

	$("#sidebar .tab_item").hide(); //Hide all content
	$("#sidebar .tabs li:first").addClass("active").show();
	$("#sidebar .tab_item:first").show(); //Show first tab content

	//On Click Event
	$("#sidebar .tabs li").click(function() {

		$("#sidebar .tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#sidebar .tab_item").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});	
	
	$(".initial").bind("focus", function(){
        if(this.value == this.defaultValue ) {
            this.value = "";
			$(this).addClass("focus");
        }
    });
    $(".initial").bind("blur", function(){
        if( !this.value.length ) {
            this.value = this.defaultValue;
			$(this).removeClass("focus");
        }
    });
	
	$(".add_favourite").autocomplete("/js/stores.php", {
		selectFirst: false
	});
	
	$(".signup .submit").click(function(){
		$.post("/ajax/register.php",{
			email: $("#signupEmail").val(),
			store_id: $("#signupStore").val()
		},function(result){
			if(result.type=="true") {
				//success
				$("#signupError").hide();
				user_id = result.message;
				$(".signup").html("<div class=\"conf\">Thank you! We've sent you an email, please confirm your address and we'll keep you up to date.</div>");
			} else {
				$("#signupError").hide();
				$("#signupError").html(result.message);
				$("#signupError").fadeIn(200);
			}
		}, "json");
		return false;
	});
	
	
});

