$(document).ready(function() {
	/* Font Replacement */
	Cufon.replace(
		".rockwell", 
		{fontFamily: "Rockwell", hover: 'true'}
	);
	
	/* Tarot Card Reader */
	$("a.tarot").fancybox({
		'overlayShow'			: true,
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack',
		'opacity'				: '0.8'
	});
	
	/* popup window */
	$("a.popupWin").fancybox({
		'width'				: '49%',
		'height'			: '92%',
        'autoScale'     	: false,
		'type'				: 'iframe',
	});
	
	/* Ask Spinning Doctor a Question */
	$('#askspinning').submit(function() {
		var askEmail = this.ask_email.value;
		var askQuestion = this.ask_question.value;
		if(askEmail == "" || askQuestion == "" || ((askEmail.indexOf(".") > 2) && (askEmail.indexOf("@") > 0)) == false) {
			alert("Enter your email address and a question to ask the Spinning Doctor!");
			return false;
		} else {
			var dataString="email="+askEmail+"&question="+askQuestion
			$.ajax({
			  type: "POST", 
			  url: "includes/ajax/askquestion.inc.php", 
			  data: dataString, 
			  timeout: 3000,
			  error: function() { 
				alert('There was a problem trying to send your question, please try again.'); return false;
			  },
			  success: function() {
				  $(".askstatus").html('<div class="loading"><img src="images/assets/loading.gif" alt="Loading.." /></div>');
			  },
			  complete: function() {
				$(".askstatus").html('<div class="loading"><img src="images/assets/loading.gif" alt="Loading.." /></div>');
				setTimeout("$('.questionform').hide(); $('.questionform').html('<div class=\"title rockwell\">Question Sent</div>Your question has been sent to the Spinning Doctor, now sit and eagerly await your reply!');", [3000]);
				setTimeout("$('.questionform').fadeIn(function() {Cufon.refresh()});", [3100]);
			  }
			});
			return false;
		}
	});
	
	/* News TV Scroller */
	$(".tv-news").scrollable({ 
		vertical: true,
		scroll: 5,
		size: 4,
		items: '.news-hold',
		item: 'a',
		disabledClass: 'disabled'
   });
	$(".news-hold a").click(function() {
		$(".news-hold a").removeClass("current");
		$(this).addClass("current");
		$('.lookdown').fadeIn(150).animate({top: '50px'}, 200).animate({top: '20px'}, 400).animate({top: '50px'}, 200).animate({top: '20px'}, 400).fadeOut(150);
		
		var news_id = $(this).attr('rel');		
		$("div.news-item").load("includes/ajax/news.inc.php?id="+$(this).attr('rel')+"", function() {Cufon.refresh()});
		return false;
	});
	
	/* Contact Form */
	$('#contactform').submit(function() {
		var contactName = this.contactName.value;
		var contactEmail = this.contactEmail.value;
		var contactEnquiry = this.contactEnquiry.value;
		if(contactName == "" || contactEmail == "" || contactEnquiry == "" || ((contactEmail.indexOf(".") > 2) && (contactEmail.indexOf("@") > 0)) == false) {
			alert("Enter your name, email address and enquiry.");
			return false;
		} else {
			var dataString="name="+contactName+"&email="+contactEmail+"&enquiry="+contactEnquiry
			$.ajax({
			  type: "POST", 
			  url: "includes/ajax/contactform.inc.php", 
			  data: dataString, 
			  timeout: 3000,
			  error: function() { 
				alert('There was a problem trying to send your enquiry, please try again.'); return false;
			  },
			  success: function() {
				  $(".contactstatus").html('<div class="loading"><img src="images/assets/loading.gif" alt="Loading.." /></div>');
			  },
			  complete: function() {
				$(".contactstatus").html('<div class="loading"><img src="images/assets/loading.gif" alt="Loading.." /></div>');
				setTimeout("$('.theform').slideToggle('slow', function() {$('.theresult').slideToggle('fast', function() {Cufon.refresh()});});", [3000]);
			  }
			});
			return false;
		}
	});
	
	/* Steal This Page - Wallpapers */
	$(".wallpapers-scrollable").scrollable({ 
		scroll: 1,
		size: '3',
		item: 'div',
		disabledClass: 'disabled'
    });
	
	/* Steal This Page - Steal Page */
	$(".stealpages-scrollable").scrollable({ 
		vertical: true,
		scroll: 1,
		size: 1,
		items: '.items',
		item: 'div',
		disabledClass: 'disabled',
		clickable: false
    });
	
	$(".product-scrollable").scrollable({
		vertical: true,
		scroll: 1,
		size: 2,
		items: '.product-scrollable',
		item: 'div',
		disabledClass: '',
		clickable: false
    });
	
	/* Featured - Scrollable */
	$(".issues-scrollable").scrollable({ 
		scroll: 1,
		size: '5',
		item: 'div',
		disabledClass: 'disabled',
		clickable: false
    });
	
	/* Newsletter Init */
	$('#newslettersignup').ajaxNewsletter();
	
	/* Shop Page */
	$('#basketOverview').click(function() {
		if($('#basket').css('display') == 'block') {
			$('#basketOverview').removeClass('basketActive');
			$("#basketRetrieve").load("includes/ajax/basket.inc.php", function() {$('#basket').fadeOut(200);});
		} else {
			$('#basketOverview').addClass('basketActive');
			$("#basketRetrieve").load("includes/ajax/basket.inc.php", function() {$('#basket').fadeIn(200);});
		}
	});
	$('.shopNav').click(function() {
		$('.shopNav').removeClass('activeCat');
		$(this).addClass('activeCat').animate({opacity: 0.2}).animate({opacity: 1.0});
		
		var category_id = $(this).attr('rel');		
		$("div.product-list").css({top: '0'}).load("includes/ajax/products.inc.php?id="+$(this).attr('rel')+"", function() {Cufon.refresh()});
		return false;
	});
	
	$.fn.loadProduct = function(id) {
		if($('#basket').css('display') == 'block') {
			$('#basket').fadeOut(200);
		}
		var product_id = id;
		if($('#product').css('display') == 'none') {
			setTimeout("$('#product').load('includes/ajax/product.inc.php?id="+product_id+"', function() {Cufon.refresh();});", 0);
			setTimeout("$('#product').fadeIn(400);", 200);
		} else {
			setTimeout("$('#product').fadeOut(400);", 0);
			setTimeout("$('#product').load('includes/ajax/product.inc.php?id="+product_id+"', function() {Cufon.refresh();});", 200);
			setTimeout("$('#product').fadeIn(400);", 500);
		}
		return false;
	}
	$.fn.updateBasketOverview = function() {
		$("#basketOverview").load("includes/ajax/basketoverview.inc.php");
	}
	$.fn.addToBasket = function(id, qty, option1, option2) {
		var dataString="action=add&id="+id+"&quantity="+qty+"&opt1="+option1+"&opt2="+option2
		$.ajax({
		  type: "GET", 
		  url: "includes/ajax/updatebasket.inc.php", 
		  data: dataString, 
		  timeout: 3000,
		  error: function() { 
			alert('Product couldn\'t be added to basket, please try again.'); return false;
		  },
		  success: function() {
			//do nothing.
		  },
		  complete: function() {
			$("#basketRetrieve").load("includes/ajax/basket.inc.php", function() {$('#product').fadeOut(200, function() {$('#basket').fadeIn(200);});});
		  }
		});
		$(this).updateBasketOverview();
		return false;
	}
	$.fn.removeFromBasket = function(id) {
		var dataString="action=delete&id="+id
		$.ajax({
		  type: "GET", 
		  url: "includes/ajax/updatebasket.inc.php", 
		  data: dataString, 
		  timeout: 3000,
		  error: function() { 
			alert('Problem updating cart, please try again.'); return false;
		  },
		  success: function() {
			//do nothing.
		  },
		  complete: function() {
			$("#basketRetrieve").load("includes/ajax/basket.inc.php");
		  }
		});
		$(this).updateBasketOverview();
		return false;
	}
});


/* Tarot Cards */
$.fn.myTarot = function() {
	tarots = new Array(23);
	tarots[0] = "death.jpg";
	tarots[1] = "judgement.jpg";
	tarots[2] = "justice.jpg";
	tarots[3] = "strength.jpg";
	tarots[4] = "the_sun.jpg";
	tarots[5] = "temperance.jpg";
	tarots[6] = "the_chariot.jpg";
	tarots[7] = "the_devil.jpg";
	tarots[8] = "the_emperor.jpg";
	tarots[9] = "the_empress.jpg";
	tarots[10] = "the_fool.jpg";
	tarots[11] = "the_hanged_man.jpg";
	tarots[12] = "the_hermit.jpg";
	tarots[13] = "the_hierophant.jpg";
	tarots[14] = "the_lovers.jpg";
	tarots[15] = "the_magician.jpg";
	tarots[16] = "the_moon.jpg";
	tarots[17] = "the_priestess.jpg";
	tarots[18] = "the_star.jpg";
	tarots[19] = "the_tower.jpg";
	tarots[20] = "the_world.jpg";
	tarots[21] = "wheel_of_fortune.jpg";
	tarots[22] = "the_twat.jpg";
	
	//Choose Tarot and Sync
	chosen = Math.floor(Math.random() * tarots.length);
	$('a.tarot').attr('href','images/tarots/'+tarots[chosen]).click();
}

/* Newsletter Signup */
$.fn.ajaxNewsletter = function(e) {
	this.submit(function(){
	  var email = this.email.value;
	  if(email == "") {
		alert("Please enter you email address to which you would like to be notified about the website launch.");
		return false;
	  } else {
		var dataString="email="+email
		$.ajax({
		  type: "POST", 
		  url: "includes/ajax/newsletter.inc.php", 
		  data: dataString, 
		  timeout: 3000,
		  error: function() { 
		  	alert('There was a problem, please try again.');
			return false;
		  },
		  success: function() {
			  	$('.status').show();
		  },
		  complete: function() {
			setTimeout("$('#newsletterDone').show().animate({bottom: '0px'}, 110).animate({bottom: '6px'}, 100).animate({bottom: '0px'}, 60).animate({bottom: '3px'}, 100).animate({bottom: '0px'}, 40);", 0);
		  }
		});
		return false;
	  }
	});
}