var topYloc = null;

$(window).scroll(function () { 
	var scrollTop = $(document).scrollTop();
	scrollTop = parseInt(scrollTop);
	
	var offset = topYloc+scrollTop+"px";  
	$("#toplink").animate({top:offset},{duration:500,queue:false});
});
	
$(document).ready(function() {

	$(".flashversion").fancybox({
	    'padding'           : 0,
		'centerOnScroll'	: 'true',				
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'  	: 0.75,
		'overlayColor'  	: '#9c0'
	});


	$('body').prepend('<div id="toplinkwrapper"><div id="toplink"><a href="#top">Back to top &gt;</a></div></div>');

	//back to top scroll function. Any link with a hash (#) will scroll to that id on the page
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				targetOffset = targetOffset - 35;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});

	topYloc = parseInt($("#toplink").css("top").substring(0,$("#toplink").css("top").indexOf("px")));

	/* set up for two columns list */
	$('nav ul li:odd').addClass('odd');

	/* set up default current items */
	$('nav ul li a[href*=mills]').parent().addClass('current');
	$('section.quickview article a[href*=mills]').parent().parent().addClass('current');

	/* Mac-style menu effect */
	$("footer menu ul li").hover(function() {
		$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup */
			.animate({
				width: '60px', /* Set new width */
				height: '60px', /* Set new height */
			}, 200);

		} , function() {
		$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup */
			.animate({
				width: '48px', /* Set width back to default */
				height: '48px', /* Set height back to default */
			}, 400);
	});

	/* Hijack the main links to bring in animation without page refresh */
	var blockMainUpdate = false;
	$('nav ul li a').live('click',function(){
		if (blockMainUpdate != true){
			blockMainUpdate = true;
			$(this).after('<span class="throbber"><img src="images/indicator.gif" alt="Loading..." height="16" width="16"></span>');
			var the_url = $(this).attr('href');
			var the_alt = $(this).text();
			var new_content = '<div><img src="' + the_url + '" width="320"  height="240" alt="' + the_alt + '"></div><p>' + the_alt + '</p>';
			$('#maincontent article').animate( {opacity: 0 }, 1000, function() {
				$(this).empty().append(new_content);
				});
			$('#maincontent article').animate( {opacity: 1 }, 1000, function() {
				$('nav ul li span.throbber').animate( {opacity: 0}, 500, function() {
					$(this).remove();
					blockMainUpdate = false;
					});
				});
			$('nav ul li.current').removeClass('current');
			$('section.quickview article.current').removeClass('current');
			$('section.quickview article.wheeee').removeClass('wheeee');			
			$(this).parent().addClass('current');
			$('section.quickview article a[href=' + the_url + ']').parent().parent().addClass('current wheeee');			
			$('title').empty().append('millsmess.co.uk &raquo; 3 ball juggling tricks &raquo; ' + the_alt);			
			return false
		}
		else {
			return false /* handle click if animation already running */
		}
	});	

	/* Hijack the quickview links to bring in animation without page refresh */
	var blockQuickViewUpdate = false;
	$('section.quickview article a').live('click',function(){
		if (blockQuickViewUpdate != true){
			blockQuickViewUpdate = true;
			$(this).after('<span class="throbber"><img src="images/indicator.gif" alt="Loading..." height="16" width="16"></span>');
			var the_url = $(this).attr('href');
			var the_alt = $(this).parent().next().text();
			var new_content = '<div><img src="' + the_url + '" width="320"  height="240" alt="' + the_alt + '"></div><p>' + the_alt + '</p>';
			$('#maincontent article').animate( {opacity: 0 }, 1000, function() {
				$(this).empty().append(new_content);
				});
			$('#maincontent article').animate( {opacity: 1 }, 1000, function() {
				$('section.quickview article span.throbber').animate( {opacity: 0}, 500, function() {
					$(this).remove();
					blockQuickViewUpdate = false;
					});
				});
			$('nav ul li.current').removeClass('current');
			$('section.quickview article.current').removeClass('current');
			$('section.quickview article.wheeee').removeClass('wheeee');						
			$(this).parent().parent().addClass('current');
			$('nav ul li a[href=' + the_url + ']').parent().addClass('current');						
			$('title').empty().append('millsmess.co.uk &raquo; 3 ball juggling tricks &raquo; ' + the_alt);			
			return false
		}
		else {
			return false /* handle click if animation already running */
		}
	});

});
