// preload images function
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// preload images
$.preloadImages(
	"http://www.hensonfranklyn.co.uk/images/templates/down_over.gif",
	"http://www.hensonfranklyn.co.uk/images/templates/up_over.gif",
	"http://www.hensonfranklyn.co.uk/images/templates/previous_over.gif",
	"http://www.hensonfranklyn.co.uk/images/templates/next_over.gif",
	"http://www.hensonfranklyn.co.uk/images/templates/pause_over.gif"
);

$(function() {

	// Slide show duh
    $('#slideshow_container').cycle({
        fx:      'fade',
		speed:  2000,
		timeout:  14000,
		cleartypeNoBg: true,
        prev:    '#previous',
        next:    '#next',
        pager:   '#slideshow_pager',
        pagerAnchorBuilder: pagerFactory
    });

	$('#slideshow_pager li:first-child').addClass("activeLI");

	if( $("#image_switcher li").length == 1 ) {
		$('#image_switcher').addClass("hide");
	}


	$('#slideshow_pager li').click(function() { 
	    $('#slideshow_container').cycle('pause'); 
	 	$('#play').show();
		$('#pause').hide();
	});

	$('#play').hide();

	// redefine Cycle's updateActivePagerLink function
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
	    $(pager).find('li').removeClass('activeLI')
	        .filter('li:eq('+currSlideIndex+')').addClass('activeLI');
	};
	
	$('#pause').click(function() { 
	    $('#slideshow_container').cycle('pause');
	 	$('#play').show();
		$('#pause').hide();
	});
	
	$('#play').click(function() { 
	    $('#slideshow_container').cycle('resume'); 
		$('#play').hide();
		$('#pause').show();
	});


	$('#previous,#next').click(function() { 
	    $('#slideshow_container').cycle('pause');
	 	$('#play').show();
		$('#pause').hide(); 
	});

    function pagerFactory(idx, slide) {
        var s = idx > 6 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };

	$("a[rel='popup']").colorbox();
	
	$('.up').css({'visibility':'hidden'});
	
	$('.down').click(function() {
		$('.up').css({'visibility':'visible'});
		$('#scroll').animate({"marginTop": "-=137px"}, function() {
			var postop = $('#scroll').css('marginTop');
			postop = postop.replace('px', '');
			if(postop == -685) {
				$('.down').css({'visibility':'hidden'});
			} else {
				$('.down').css({'visibility':'visible'});
			}
		});
		return false;
	});
	
	$('.up').click(function() {
		$('.down').css({'visibility':'visible'});
		$('#scroll').animate({"marginTop": "+=137px"}, function() {
			var postop = $('#scroll').css('marginTop');
			postop = postop.replace('px', '');
			if(postop == 0) {
				$('.up').css({'visibility':'hidden'});
			} else {
				$('.up').css({'visibility':'visible'});
			}
		});
		return false;
	});
	
	$(function() {
	    $('td pre code').each(function() {
	        eval($(this).text());
	    });
	});
	
	$('#image_switcher').find('img').click(
		function(){
			// get image path of clicked image and replace large image
			var image_path = $(this).attr('src');
			$('#news_img').attr('src',image_path);
			return false;
		}
	);

});

