$(document).ready(function(){
	//slideshow thinger, uses jqcycle
    $('#photoblock ul, #photoblock-home ul').cycle({
		fx: 'fade',
		speed: 3000,
		timeout: 3000,
		pause: 1,
		random: 1
	});
	
	$('#mce-EMAIL').example('Email Address');
	$('title').text($('.contents h2').text()+' - TrainBuilder');
		
	//$('td.addremovelivery a img[src="img/addlivery.png"]').click(function(){ 
	//	$(this).parent().parent().parent().next('tr').show();
	//	return false;
	//});
	
	//image swapper (add and remove -hover to end of filename)
	$('a.pdforder img').hover(function() {
		var hoverString = '_hover.jpg';
		var fileName = $(this).attr('src');
		var newFileName = fileName.replace(".jpg", hoverString);
		$(this).attr('src', newFileName);
	}, function() {
		var hoverString = '_hover.jpg';
		var fileName = $(this).attr('src');
		var newFileName = fileName.replace(hoverString, ".jpg");
		$(this).attr('src', newFileName);
	});
	$('a.pdforder').click(function() {
		_gaq.push(['_trackPageview', location.href.replace('http://trainbuilder.com/','')+'/pdf/download']);				
	});
	//function writePhotoLink() {
	//	thisHTML = $(this).children('img').attr('src').split('.',1);
	//	$('.photomask').html('<a href="'+thisHTML+'_large.png" title="hi there!">');
	//}
	
	//super expanding images of awesomeness
	$('#photoblock .photomask:not(.nozoom)').toggle(function() {
	//when the photomask is first clicked (it's overlayed on top of the images)
		$('#photoblock ul').cycle('pause');
		$('#photoblock ul li img').fadeOut('100');
		$('#photoblock').addClass('block-wide');
		_gaq.push(['_trackPageview', location.href.replace('http://trainbuilder.com/','')+'/img/large'])				
		$('#photoblock').animate({
		//animate the photoblock to be wider, taller and move over
		    marginLeft: '-380',
			width: '980',
			height: '526'
		}, 500, function() {
			//and scroll down (smooooothly) to the photo block
			$("html, body").animate({
				scrollTop: $('#photoblock').offset().top + "px"
			}, { duration: 500,	easing: "swing"
			});
			//window.location = '#photoblock';
			//once the animation is complete,
			$('#photoblock ul li img').each(function() {
				var smallIMG = $(this).attr('src');
				var largeIMG = smallIMG.replace('.jpg', "_large.jpg");
				$(this).attr('src',largeIMG);
				//and for each image inside, replace it with the larger image
				$(this).fadeIn('100');
				$('#photoblock ul').cycle('resume');
			});
		});
		$('.photomask').addClass('mask-wide');
		$('.photomask, #photoblock ul, #photoblock ul li').animate({
		//also at the same time, animate the size of the internal elements of the slideshow
			width: '980',
			height: '506'
		}, 500, function() {
		});
	}, function() {
	//when the mask is clicked again
		$('#photoblock ul').cycle('pause');
		$('#photoblock ul li img').fadeOut('100');
		$('#photoblock').animate({
		    marginLeft: '0',
			width: '594',
			height: '435'
			//animate all the sizes back to the original sizes
		}, 500, function() {
			$('#photoblock').removeClass('block-wide');
			//and once that animation is compelte, switch back to the smaller block size
			$('#photoblock ul li img').each(function() {
				var smallIMG = $(this).attr('src');
				var largeIMG = smallIMG.replace('_large.jpg', ".jpg");
				$(this).attr('src',largeIMG);
				$(this).fadeIn('100');
				//and switch the images back to the smaller ones
				$('#photoblock ul').cycle('resume');
			});
		});
		$('.photomask, #photoblock ul, #photoblock ul li').animate({
		//and at the same time animate all the internal elements back to the right size
			width: '594',
			height: '435'
		}, 500, function() {
			$('.photomask').removeClass('mask-wide');
			//and remove the wider masks.
		 });	
	});
	//theLink = $('.photomask a').attr('href');
	//$('.photomask').click(window.location = theLink);


});
