// Signup page tracking (stores a cookie of the href to redirect to post signup
$('.upgrade,#premiumBtn').live("click", function(e){
	$.cookie('follower', document.location.href, { expires: 7, path: "../default.htm" }); 
}); 

$(function(){
	/**
	 * Currency selection facebox modal
	 */
	// Opens up a facebox modal allowing the currency to be changed
	$(".toggleCurrency").bind("click", function(e){
		e.preventDefault();
		
		// Show the modal
		$.facebox($("#currencyModal").html());
	});
	// Binds events to controls in the modal when it launches
	$(document).bind('reveal.facebox', function() {
		// Bind an event to the currency dropdown
		$("#facebox").find(".currency").unbind("change").bind("change", function(e){
			// Strip out the currency & package identifiers from the URL
			var location = document.location.href.replace(/(\?|&|&amp;)currency=[A-Z]+(?:&p=[0-9]+)?/ig, '');
			
			// Add the newly selected currency & package identifiers
			if (location.match(/\?/))
			{
				document.location.href = location+"&currency="+$(this).val();
			}
			else
			{
				document.location.href = location+"@currency="+$(this).val();
			}
			
			// Close the facebox
			$(document).trigger('close.facebox');
		});
	});

	/**
	 * jQuery coda slider
	 */
	if ($(".scrollPanel").length)
	{
		var $panels = $('#premiumContainerText .scrollContainer > div');
		var $container = $('#premiumContainerText .scrollContainer');
		$panels.css({
			'float' : 'left',
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});
		
		// calculate a new width for the container (so it holds all panels)
		$container.css('width', $panels[0].offsetWidth * $panels.length);
		
		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#premiumContainerText .scroll').css('overflow', 'hidden');
		
		// handle nav selection
		function selectNav() {
		  $(this)
		    .parents('div:first')
		      .find('a')
		        .removeClass('current')
		      .end()
		    .end()
		    .addClass('current');
		}
		
		$('#premiumContainer .sliderNav').find('a').click(selectNav);
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		  $("#premiumContainerIcon").find("img:visible").fadeOut(300);
			
		  var el = $('#premiumContainer .sliderNav').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		  
	  	  $("#"+data.id+"-icon").fadeIn(300);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('div.sliderNav a:first').click();
		}
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = 0;
		
		
		var scrollOptions = {
		  target: $scroll, // the element that has the overflow
		  
		  // can be a selector which will be relative to the target
		  items: $panels,
		  
		  navigation: '.sliderNav a',
		  
		  // allow the scroll effect to run in both directions
		  axis: 'xy',
		  
		  prev: $("#premiumContainerLeft .hitbox"),
		  next: $("#premiumContainerRight .hitbox"),
		  
		  onAfter: trigger, // our final callback
		  
		  constant: false, // scroll faster if we scroll more elements at once
		  
		  offset: offset,
		  
		  interval: 30000,
		  
		  // duration of the sliding effect
		  duration: 500,
		  
		  // easing - can be used with the easing plugin: 
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports// the indexed next and previous scroll along with hooking 
		// in to our navigation.
		$('#premiumContainerText').serialScroll(scrollOptions);
		
		// now apply localScroll to hook any other arbitrary links to trigger 
		// the effect
		$.localScroll(scrollOptions);
		
		// finally, if the URL has a hash, move the slider in to position, 
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);
	}
	
	/**
	 * Feature row mouseover panels
	 */
	$("#featureTable .row .feature").hover(
			function(e){
				var tipX = e.clientX + $(document).scrollLeft() + 25;
				var tipY = e.clientY + $(document).scrollTop() - 300;
				
				$(this).parent(".row").find(".infoOverlay").css({'top':tipY,'left':tipX}).show();
			},
			function(){
				$(this).parent(".row").find(".infoOverlay").hide();
			}
	).mousemove(function(e){
		var tipX = e.clientX + $(document).scrollLeft() + 25;
		var tipY = e.clientY + $(document).scrollTop() - 300;
		$(this).parent(".row").find(".infoOverlay").css({'top':tipY,'left':tipX});
	});
	
	// Fake a click on the first panel which'll start the auto slide
	$(".scrollContainer").trigger('goto', [0]);
	$(".scrollContainer").trigger('start');

	/**
	 * Image preloader
	 */
	$(window).bind("load", function(){
		// Preload images
		imgObj = new Array();
		// set image list
		images = new Array();
		// Slider panel
		images[0]="../img/premium/new/panel1.jpg";
		images[1]="../img/premium/new/panel2.jpg";
		images[2]="../img/premium/new/panel3.jpg";
		images[3]="../img/premium/new/panel4.jpg";
		images[4]="../img/premium/new/panel5.jpg";
		// Feature overlay
		images[5]="../img/premium/new/feature1.jpg";
		images[6]="../img/premium/new/feature2.jpg";
		images[7]="../img/premium/new/feature3.jpg";
		images[8]="../img/premium/new/feature4.jpg";
		images[9]="../img/premium/new/feature5.jpg";
		images[10]="../img/premium/new/feature6.jpg";
		images[11]="../img/premium/new/feature7.jpg";
		images[11]="../img/premium/new/feature8.jpg";
		
		// counter
		var i = 0;
		// start preloading
		for(i=0; i<images.length; i++) 
		{
			imgObj[i] = new Image();
			imgObj[i].src=images[i];
		}
	});

	/**
	 * Countdown timer (used for step 2 of the download process)
	 */
	if ($("#startWait").length)
	{
		var countNum = $("#startWait").val();
		var intervalID = 0;
		var countTimer = function(){
		    countNum = countNum-1;
		    
		    if (countNum === 0)
		    {
		        clearInterval(intervalID);
		        $(".basicBtn p").hide();
		        $(".basicBtn #downloadLink").show();
		    }
		    else
		    {
		        $("#countdown").text(countNum);
		    }
		}
		intervalID = setInterval(countTimer, 1000);
	}
	
	/**
	 * Special offer countdown timer
	 */
	if ($("#specialOfferContainer").length) 
	{
		var specialOfferTimer = function(){
			var currentNum = parseInt($("#specialOfferWarning span").text(), 10);
			
			if ((currentNum-1) <= 0)
			{
				clearInterval(specialTimer);
				
				$("#specialOfferContainer, .formHeading").slideUp(500, function(){
					$(this).remove();
				});
			}
			else
			{
				$("#specialOfferWarning span").text((currentNum-1));
			}
		};
		
		// Don't start the timer or bind the events if the container isn't present
		specialTimer = setInterval(specialOfferTimer, 1000);
		
		// Bind the radio buttons in the special offer form and cancel the timeout if they're clicked
		$("#specialOfferContainer").find("form").find("input[type='radio']").bind("click", function(e){
			clearInterval(specialTimer);
			
			$("#specialOfferWarning").slideUp(300);
		});
	}
	
	/**
	 * Amazing offer countdown timer
	 */
	if ($("#amazingOfferWarning").length) 
	{
		var amazingOfferTimer = function(){
			var currentNum = parseInt($("#amazingOfferWarning span").text(), 10);
			
			if ((currentNum-1) <= 0)
			{
				clearInterval(specialTimer);
				
				document.location.href = "default.htm";
			}
			else
			{
				$("#amazingOfferWarning span").text((currentNum-1));
				
				if ($("#amazingOfferWarningBottom").length)
				{
					$("#amazingOfferWarningBottom span").text((currentNum-1));
				}
			}
		};
		
		specialTimer = setInterval(amazingOfferTimer, 1000);
	}
	
	/**
	 * Gateway image radio selection helper
	 */
	$(".gatewayIcon").bind("click", function(e){
		$(this).prev("input[type='radio']").click();
	});
});