/*MAIN JAVASCRIPT FILE FOR MEGHANCARTER.COM
** ORIGINALLY CREATED 4/28/2011 */


//starter function that initiates all other functions
var Start = 
{
	ie6: (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1), //detect IE6
	iOS: false, //initialize variable that tracks if user is iOS or not
	init: function()
	{
		if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') //if iOS device
		{
			TouchScrolling.init(); //manages touch scrolling controls for iOS devices (iPhone, iPad, iPod)
			Start.iOS = true; //it is an iOS device
		}
		
		if($('#salesPageSpreads').length)
		{	
			BookLookInside.init(); //manages book look inside app
		}
		
		EliminateOverlays.init(); //hides any overlays if window width is below 1024px
		
		if(Start.ie6) //if ie6, run to fix png transparency
		{
			$('head').append('<script type="text/javascript" src="js/supersleight.plugin.js"></script>');//insert png fixer jquery plugin script call
			$('.ie6PNGFIX').supersleight(); //if ie6, make selected areas have transparent png images
		}
		
		if($('div.drop1').length) //if dropdowns exists, manage them
		{	
			Dropdown.init();
		}
		
		/*if($('#worksheetHolder').length) //if it's the worksheets page, manage printing and dowlading lightbox for each worksheet
		{
			Worksheets.init();
		}*/
		
		if($('form.clearClick').length) //if there is a form that needs to be clearned when text inputs are clicked
		{
			ClearForm.init();
		}
		
		if($('#bookExcerpt').length)
		{
			Excerpt.init();
		}
		
		ALaModal.init();
		
		if ($('.colSplit').length)
		{
			ColSplit.init();
		}
		
		if ($('.stepbystep').length)
		{
			StepByStep.init();
		}
		
		if ($('.scrolldiv').length)
		{
			$('.scrolldiv').scrollThis();
		}
		
		if ($('#categoryList').length)
			$('#categoryList li:odd').addClass('right');$('#categoryList').show();
			
		if ($('#categoryList').length)
			VerticalCenter.init($('#categoryList div'));
			
		BtmToolScrollMngr.init(); //manages reveal of bottom tools when viwer scrolls to bottom of page
		
		if ($('#issuu').length)
		  Issuu.init();
	}
};


//starts up all functions when DOM is loaded
$(document).ready(Start.init);



//manages #nav dropdowns
var Dropdown =
{
	init: function ()
	{	
		$('div.drop1').hide();
		$('li.dropTrigger').hover(
			function() {
				//$(this).children('div.drop1').removeClass('hide');
				$(this).children('div.drop1').fadeIn(250);
			},
			function () {
				//$(this).children('div.drop1').addClass('hide');
				$(this).children('div.drop1').fadeOut(250);
			}
		);
	}
};




//manages the behvior of the bottom toolbar for desktop browsers
// * sets css styles for various browsers to bottom toolbar scrolls is fixed like browser native tool bar
// * when toolbar reaches the bottom of the #content, it changes from stuck to bottom of browser, to absolutely positioned on top of #foot
// * effect works in Safari, Firefox, Chrome, IE 7+ (basically all browsers with position:fixed support)
var BtmToolScrollMngr =
{
	absolutePosition: function ()//make the #tools bar absolutely positioned in the <body> element at the very bottom of the #content area, above #foot
	{
		$('#tools').css({ 
				'position': 'absolute',
				'top': $('#content').outerHeight(true) - $('#tools').height(),
				'bottom': 'auto'
			});
	},
	fixedPosition: function () // make the #tools position:fixed again, moving with the scrolling
	{
		$('#tools').css({ 
			'position': 'fixed',
			'top': 'auto',
			'bottom': '-1px'
		});
	},
	init: function ()
	{
		//if the height of #content is less than screen height, and not iOS device: make absolutly positioned
		if ($(window).height() >= $('#content').innerHeight() && Start.iOS != true)
		{
			BtmToolScrollMngr.absolutePosition();
		}
		else if (Start.ie6 != true && Start.iOS != true)
		{
			BtmToolScrollMngr.fixedPosition();
		}
		//listen to scroll event and move #tools along with scrolling to look liked fixed toolbar at bottom of browser
		$(window).scroll(function(){ //listen on all scrolling events of browser window
			//$('#tools > .fixedFloat').text(w1 + ' and ' + b1); //writes the numbers to the bar for debugging. NOT USED IN PRODUCTION
			var w1 = $(window).scrollTop() + $(window).height(); //calculate the current position of the bottom edge of browser window
			var b1 = $('#foot').offset().top; //calculate the point at which #tools bar becomes stationary
			if(w1 >= b1) //if the bottom of the browser window hits the very top of the #foot
			{
				if(!($.browser.msie && Start.ie6)) //if not IE6
				{
					BtmToolScrollMngr.absolutePosition();
				}
			}
			else
			{
				if(!($.browser.msie && Start.ie6)) //if not IE6
				{
					BtmToolScrollMngr.fixedPosition();
				}
			}//else
		});//scroll event
	}// init method
};// BtmToolScrollMngr object


//manages touch scrolling for iOS devices such as iPads etc. Makes it seem like native scrolling on devices
var TouchScrolling =
{
	init: function ()
	{
		$('head').append('<script src="js/touch-scroll.min.js" type="text/javascript"></script>'); //insert touch scroll jquery plugin script call
		$('#container').wrap('<nav />'); //wrap #content in <nav>
		$('nav').wrap('<section />'); //wrap #content and <nav> in <section>
		//create css sytyles for newly created <nav> and <section>
		var bgImage = $('body').css('background-image');// get backround image from <body>
		var bgY = $('body').css('background-position-y'); //get Y positioning of <body> BG
		$('body').css({'overflow-y': 'hidden', 'background': 'none'});//then eliminate BG from <body>
		$('section').css({
			'display': 'block',
			'position': 'fixed',
			'left': '0',
			'top': '0',
			'bottom': '0',
			'right': '0',
			'overflow-y': 'auto'
		});
		$('nav').css({
			'display': 'block',
			'height': '100%',
			'-webkit-perspective': '1000',
			'-webkit-backface-visibility': 'hidden',
			'background-image': bgImage, //create background image
			'background-repeat': 'no-repeat', //don't let it repeat
			'background-position-x': '-280px',
			'background-position-y': bgY
		});
		$('nav').touchScroll(); //acrivate <nav> as scrollable
	}
};


var BookLookInside = //manages the book look inside module
{

/*
	You must manually adjust the width of the moves to the with of the <div> that holds the <ul> (BookLookInside.moveDistance)
	Also, you must manually create the #scrollerPages numbers and html code
	
*/


	scrollPosition: 1, //variable that tracks the current position
	maxPosition: 2, //set total number of sections to display
	moveDistance: 855,//the numbe of pixes to shift during a move
	
	init: function()
	{	
		//this section sets up all the images and content to be swapped later
		//var checkoutLeft = $('#salesFeature').html(); //grab whatever html is first inside #salesFeature to save for later
		//grab the interior code of #salesCopy that handles the text and checkout and splap it together on the other side that holds the book image
		//var checkoutRight = '<div id="salesCover"><img src="http://www.meghancarter.com/lookInside/tmmBackCover_542x606.jpg" alt="The Meghan Method" width="542" height="606" /></div><!-- salesCover --><div id="salesCopy">' + $('#salesCopy').html(); + '</div><!-- salesCopy -->';
		
		var text = $('#salesText').html();
		var checkout = $('#checkout').html();
		var secondText = "<p class='quote' style='padding-bottom:20px;font-size:19px;text-align:center;'>The Answer to ALL Your Decorating Questions.<br /><!-- <span>- Praesent Sagittis</span> --></p><p class='justifiedSummary'>And by answer, I don’t mean snobby opinions or silly rules. I fully explain — with pictures — all the essential concepts most brush over, while never once imposing my style. </p><p>You'll discover:</p><ul><li>Why we really decorate.</li><li>The truth about style and why you’ve always had it — even if you don’t think you do.</li><li>How to get the most out of your room — both functionally and emotionally.</li><li>What designers mean when they talk about balance, proportion and the other 6 design principles.</li><li>Where to find fruitful inspiration and why it’s essential. </li></ul><p class='justifiedSummary' style='margin-top:15px'>By the time you reach the last page, you’ll know more than just the decorating basics. You’ll know exactly how to create the home of your dreams. Let’s get started. </p><!--<p><a href='http://www.meghancarter.com/about-book.html'>(Want to know how I discovered all this?)</a></p>--><!--<p class='seenIn'>As seen in: <span>Sodales nec Orci, Donec Vulputate, Pharetra Congue</span><br />As seen on:  <span>Blandit sem, Suspendisse Luctus, Luctus Leo, Aenean</span></p>-->";
		
		var spreads = [ //create array of code relating to each thumbail
			"<div id='salesCopy'><div id='salesText'>" + text + "</div><!-- salesText --><div id='checkout'>" + checkout + "</div><!-- checkout --></div><!-- salesCopy --><div id='salesCover'><img src='http://www.meghancarter.com/lookInside/tmmCoverBig_542x629.jpg' alt='The Meghan Method' width='542' height='629' /></div><!-- salesCover -->",
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7384.jpg" alt="The Meghan Method Signed Copy" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7386.jpg" alt="The Meghan Method Table of Contents" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7388.jpg" alt="The Meghan Method Introduction" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7391.jpg" alt="The Meghan Method Part 3" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7392.jpg" alt="The Meghan Method Worksheets" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7394.jpg" alt="The Meghan Method Creating Your Layout" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7395.jpg" alt="The Meghan Method Color and Pattern" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7396.jpg" alt="The Meghan Method Shopping" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7397.jpg" alt="The Meghan Method Room Plan" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			'<div class="spreadHolder"><img src="http://www.meghancarter.com/lookInside/IMG_7400.jpg" alt="The Meghan Method End Result" width="950" height="600" /><div class="buyTab ie6PNGFIX"><a href=""></a></div><div id="circlePlayer1" class="audioButton ie6PNGFIX"></div></div><!-- spreadHolder -->',
			"<div id='salesCover''><img src='http://www.meghancarter.com/lookInside/tmmBackCover_542x606.jpg' alt='The Meghan Method' width='542' height='606' /></div><!-- salesCover --><div id='salesCopy'><div id='salesText'>" + secondText + "</div><!-- salesText --><div id='checkout'>" + checkout + "</div><!-- checkout --></div><!-- salesCopy -->"
		];
		jQuery.preLoadImages( //preload images to prevent loading lag
			'http://www.meghancarter.com/lookInside/tmmBackCover_542x606.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7384.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7386.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7388.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7391.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7392.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7394.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7395.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7396.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7397.jpg',
			'http://www.meghancarter.com/lookInside/IMG_7400.jpg',
			'http://www.meghancarter.com/lookInside/buyNowHover64x64.png'
		);
		
		//this section magages the movement of thumbnails
		$('#rightScrol').click(function(e){ //when click right arrow, move sled to the left
			e.preventDefault();
			BookLookInside.moveLeft(1);//move the sled
		});
		$('#leftScrol').click(function(e){ //when click left arrow, move sled to the right
			e.preventDefault();
			BookLookInside.moveRight(1);//move the sled
		});
		//***************MANAGE CLICK OF #scrollerPages links, move the sled and update controls
		$('#scrollerPages a').click(function (e){
			e.preventDefault();
			var jumpAmount = $(this).attr('name') - BookLookInside.scrollPosition;
			if (jumpAmount > 0)
			{
				BookLookInside.moveLeft(jumpAmount);
			}
			else if (jumpAmount < 0)
			{
				BookLookInside.moveRight(-jumpAmount);
			}
		});
		
		//this section handles the switching of the feature image when thumbnail is clicked
		$('#salesPageSpreads li a').click(function (event){ // activate when thumbnail link is clicked
			event.preventDefault();	//prvent default action
			$('#salesPageSpreads li a').removeClass('current'); //remove .current from all links
			$(this).addClass('current'); //add current to the link just clicked
			$('#salesFeature').html(spreads[$(this).attr('title')]); //grab title attribute from link, use that as index for above array, insert as html
			if(Start.ie6) //if ie6, run to fix png transparency
			{
				$('.audioButton').supersleight(); //if ie6, make selected areas have transparent png images
			}
			
		});
		$('.buyTab a').live('click', function (event){// activate when click BUY NOW tab
			event.preventDefault();	//prvent default action
			$('#salesPageSpreads li a').removeClass('current'); //remove all .current
			$('#salesPageSpreads li a').last().addClass('current'); // add .current to last thumbnail
			$('#salesFeature').html(spreads[spreads.length-1]); //insert html from the spreads array realating to the last thumbnail (9)
			//move scrolling thumbnails to the last position to correspond with what's featured
			var jumpAmount = BookLookInside.maxPosition - BookLookInside.scrollPosition;//find how much needs to be moved
			if (jumpAmount > 0)//if it's not already at the last page of thumbnails...
			{
				BookLookInside.moveLeft(jumpAmount);//call the moveLeft method passing the amount needed to be moved
			}
			if(Start.ie6) //if ie6, run to fix png transparency
			{
				$('.audioButton').supersleight(); //if ie6, make selected areas have transparent png images
			}
		});
		
	},// init
	
	moveLeft: function(times) { //moves the sled to the left
		if (BookLookInside.scrollPosition < BookLookInside.maxPosition)//if not at maximum position, move
		{
			BookLookInside.scrollPosition += 1; //increment current position
			var moveAmount = '-=' + (times*BookLookInside.moveDistance); //calculate how much to move based on the number sent in
			$('#scroller ul').animate({ //move it
				left: moveAmount
			},1000, BookLookInside.updateControls()); //call method to update how the controls display
		}
	},
	
	moveRight: function (times) { //moves the sled to the right 
		if(BookLookInside.scrollPosition > 1)// if not at minimum postion, move
		{
			BookLookInside.scrollPosition -= 1; //decrement current positin
			var moveAmount = '+=' + (times*BookLookInside.moveDistance); //calculate how much to move based on number sent in
			$('#scroller ul').animate({ //move it
				left: moveAmount
			},1000, BookLookInside.updateControls()); //update display of controls
		}
	},
	
	updateControls: function () {// update how the controls display, previous and next buttons as well as page trackers at bottom
		if(BookLookInside.scrollPosition == 1)//if the sled is all the way at the starting position
		{
			$('#leftScrol a').removeClass('on');//turn off left arrow
			$('#rightScrol a').addClass('on');//turn on right arrow
		}
		else if (BookLookInside.scrollPosition == BookLookInside.maxPosition)// if the sled is all the way at end position
		{
			$('#rightScrol a').removeClass('on');//turn off right arrow
			$('#leftScrol a').addClass('on');//turn on left arrow
		}
		else //else turn on both arrows
		{
			$('#rightScrol a').addClass('on');//turn off right arrow
			$('#leftScrol a').addClass('on');//turn on left arrow
		}
		
		//turn on the current tracker and turn off all others
		$('#scrollerPages a').removeClass('current');
		var anchor = '#scrollerPages a[name="' + BookLookInside.scrollPosition + '"]';
		$(anchor).addClass('current');
	}	
};


//if the browser window dips below 1024px wide hide the overlay so that when the <body> background images continue to move the overlays don't look weird when left behind
var EliminateOverlays =
{
	init: function ()
	{	
		if ($(window).width() <= '1024')
		{
			$('.overlays').hide();
		}	
		$(window).resize(function(){
			if ($(window).width() <= '1024')
			{
				$('.overlays').hide();
			}
			else if (($(window).width() >= '1024'))
			{
				$('.overlays').show();
			}
		});
	}
};


//manage printing and dowlading lightbox for each worksheet on the worksheets page
/*
DEACTIVATED AND MOVED INSIDE ALAMODAL TO PREVENT DOUBLE INCLUSIONT OF FANCYBOX .JS IN HEADER
var Worksheets =
{
	init: function ()
	{
	 if($('a.worksheet').length)
		{
			$('head').append('<script type="text/javascript" src="js/fancybox/jquery.fancybox-1.3.4.pack.js"></script><link rel="stylesheet" type="text/css" href="js/fancybox/jquery.fancybox-1.3.4.css" media="screen" />');
		}
		$("a.worksheet").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition'		: 'inside',
				'onComplete'		: function(){
					if(Start.ie6)
					{
						$('#fancybox-title-inside').append('&nbsp;&nbsp;| <a href="javascript:;" onClick="$.fancybox.close();">Click to Close</a>');
					}
				}
			});
	}
};*/


//clears the default value of text input form fields if .clearClick is applied to the parent <form> tag
var ClearForm =
{
	init: function ()
	{ 
		var formsToClear = $('form.clearClick input[type|="text"], form.clearClick textarea');
		formsToClear.each(function (){ //set varaible to track clicks on each input element
			this._onetime = 0;
		});
		formsToClear.click(function(){ //if not already cleared, clear value attribute when clicked
			if (this._onetime == 0) 
			{
				$(this).val('').css('color','#000');
				this._onetime = 1;
			}
		});
	}
};


//manages the excerpts slideshow on the product page of shopping cart
var Excerpt =
{
	init: function ()
	{
		$('ul.slideControls li a').click(function(e) {
			if (!$(this).hasClass('ctlBuy'))//if this isn'the buy button (.ctlBuy)
			{
				e.preventDefault();//prevent default action
				var index = ($('ul.slideControls li a').index(this)); //get index location of button in array
				var moveAmount = ($('ul.slideshow li').outerWidth()*index);//move amount calculated from outer widht of <li> * index location of control
				var it = $(this); //set item clicked to variable for propper closure in below function, aka pass the object
				$('ul.slideshow').animate({ //move slides
					marginLeft: -moveAmount //move by modifying marginLeft of entire containing <ul>
				}, 1000, function (){
					$('ul.slideControls li a').removeClass('current'); //when move is complete, update display of controls with appropriate css classes
					it.addClass('current');
				});
			}
			
		});
	}
};


//manages the display of the modal hover box generated from clicking link in #tools bar
var ALaModal =
{
	init: function ()
	{
		if($('#customerCare').length || $('.alamodal').length || $('.alamodalVid').length || $('a.worksheet').length)
		{
			$('head').append('<script type="text/javascript" src="js/fancybox/jquery.fancybox-1.3.4.pack.js"></script><link rel="stylesheet" type="text/css" href="js/fancybox/jquery.fancybox-1.3.4.css" media="screen" />');
		}
		
		if($('#costomerCare').length)
		{
			$("#supportLink").fancybox({
				'titlePosition'		: 'inside',
				'overlayColor'  : '#fff',
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});
		}
		if($('.alamodal').length)
		{
			$(".alamodal").fancybox({
				'titlePosition'		: 'inside',
				'overlayColor'  : '#fff',
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});
		}
		if($('.alamodalVid').length)
		{
			$(".alamodalVid").fancybox({
				'autoScale'     : false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'width'         : 853,
				'height'        : 510,
				'overlayColor'  : '#fff',
				'type'			: 'swf',
				'swf'			: {
				   	'wmode'		        : 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		}
		if($('a.worksheet').length)
		{
		  $("a.worksheet").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition'		: 'inside',
				'onComplete'		: function(){
					if(Start.ie6)
					{
						$('#fancybox-title-inside').append('&nbsp;&nbsp;| <a href="javascript:;" onClick="$.fancybox.close();">Click to Close</a>');
					}
				}
			});
		}
	}//int
};


//splits text into 2 columns
var ColSplit =
{
	init: function ()
	{
		var text = $('.colSplit').html() //get body of text to be split
		if (text.indexOf('***br***') >= 0) //if there's a force break character, break there
		{
			var secCol = text.substring(text.indexOf('***br***'), text.length); //second column is from force break to end of text
			secCol = secCol.replace("***br***", ''); //delete ***br***
			var firstCol = text.substring(0, text.indexOf('***br***')); //first colun is beginning up till force break 
		}
		else //if no force break, break between words towards the middle of text
		{
			var spaceAfterHalf = text.indexOf(' ', text.length/2+57); //find the first space character after the middle of text plus 57 more to finish out the line of text
		
			var secCol = text.substring(spaceAfterHalf, text.length); //second column is designated space till end of text
			var firstCol = text.substring(0, spaceAfterHalf); //first column is beginnig till designated space
		}
		$('.colSplit').html('<div class="first column">' + firstCol + '</div>' + '<div class="second column">' + secCol + '</div>'); //create the two columns and insert the two columns text into them and insert them back into the original holder tag
		
		
	}
};



//manage step by step slideshow swap
var StepByStep =
{
	init: function ()
	{
		$('.stepbystep #stepList img').hover(
			function () {
				if( !($(this).hasClass('real')) )
					$(this).addClass('current');
			},
			function () {
				if( !($(this).hasClass('real')) )
				$(this).removeClass('current');
			}
		);
	
		$('.stepbystep #stepList img').click(function (e){
			if (!($(this).hasClass('real')))
			{
				var smallImageNum = StepByStep.getImageNum($(this).attr('src')); //get images number by parsing didgit from src
				
				var bigSRC = $('.stepbystep #currentStep img').attr('src');//get big images src
				var bigImageNum = StepByStep.getImageNum(bigSRC);//get images number by parsing didgit from src
				
				var newSRC = bigSRC.replace(bigImageNum, smallImageNum); //create new big image src
				
				$('.stepbystep #currentStep img').attr('src', newSRC); //swap the current big image for the new one that corresponds to the clicked thumbnail
			
				$(this).siblings().removeClass('current real'); //remove .current off all thumbnails
				$(this).addClass('current real'); // add .current to clicked thumbnail
			}
		});
	},
	getImageNum: function(src)
	{
		var imageNum = "/step" + src.substring(src.indexOf('/step')+5, src.indexOf('/step')+6); //gets the digit out of the src, ex) "xxxxxx/stepDIDGETxxxxxx", it gets "DIDGET"
		return imageNum;
	}
};



//jQuery plugin that makes a UL inside a designated DIV scrollable with arrows to click
/*
	The goal is to only put a UL with LIs inside one DIV and then it automatically makes it scrollable. 
	You also need to create your controls, Anchor tags with the classes that correspond to scrollRight and scrollLeft attributes.
	
		- It only works horiziontally.
		- all <li> must be same height, width, padding, margins, etc.
	
	
	
	*Basic HTML structure needed*
	
		<div id="someID" style="display:none">        <!--I recommend hiding everything until it's loaded, I call .show() at end to auto-reveal -->
			<a class=".scrollThisLeft" href=""></a>
			<ul>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
			</ul>
			<a class=".scrollThisRight" href=""></a>
		</div>	
		
	*Default Script Call*
	
		$('#someID').scrollThis();
		
	*Script Call With Options*
	
		$('#someID').scrollThis({
			'scrollSpeed'	:	400,
			'sledTag'		:	'.sledThis',
			'passengers'	:	'.thesePassengers',
			'scrollRight'	:	'<a>',
			'displayNum'	:	4
		});		

*/
(function ($) {  
	$.fn.scrollThis = function(options) 
	{  
  		var defaults = {				// SETS THE DEFAULT OPTIONS, YOU CAN CHANGE THEM WITH YOUR CALL
  			scrollSpeed	: 	700, 				// speed at which transition happens, in milliseconds
  			sledTag		: 	'ul',				// what is the element that will be moving, can have css attributes in needed
  			passengers	:	'li',				// what elements are riding on the sled
  			scrollRight	: 	'.scrollThisRight', // identifies the element that when clicked scrolls sled right
  			scrollLeft	:	'.scrollThisLeft',	// identifies the elemnt that when clicked scrolls sled left
  			displayNum	:	3					// sets the number of items to display at a time
  		};
  		var options = $.extend(defaults, options); //updates options
  		
  		/*PRIVATE METHODS*/
  			
  			_passengerWidth = function (passenger) { // send in the passenger object, and it returns it's width, margins included
  				var p = $(passenger); // extend object w/ jQ
  				var width = p.outerWidth(); // get width
  				width += parseInt(p.css('marginLeft')); //add margins to width
  				width += parseInt(p.css('marginRight'));
  				return width;
  			},
  			_passengerHeight = function (passenger) { // send in the passenger object, and it returns it's height, margins included
  				var p = $(passenger); // extend object w/ jQ
  				var height = p.outerHeight(); // get height
  				height += parseInt(p.css('marginTop')); //add margins to height
  				height += parseInt(p.css('marginBottom'));
  				return height;
  			},
  			
  		
  		
  		
  		this.each(function() { // for each sled on a page do the below stuff
  			obj = $(this); // set object for context/scope
  			obj.hide(); // hide object until it's ready
  			
  			var sled = $(options.sledTag, obj); //get the sled that will be moving
  			var rArrow = $(options.scrollRight, obj); // get the right scroll controller
  			var lArrow = $(options.scrollLeft, obj); // get the left scroll controller
  			var passengersArray = $(options.passengers, obj) // create array holding passengers
  			var passengerWidth = _passengerWidth(passengersArray[0]);
  			var passengerHeight = _passengerHeight(passengersArray[0]);
  			var passengerCount = passengersArray.length;
  			var sledWidth = passengerWidth * passengerCount; // holds width of all passengers
  			var stageWidth = passengerWidth * options.displayNum; // holds how wide the stage/visible area should be
  			var scrollPosition = 1; // tracks the current position of sled
  			var maxScrollPosition = Math.ceil(sledWidth/stageWidth); // the number of moves possible based on available content
  			sled.width(sledWidth); // set sled width to make to total width all passengers take up
  			
  			//Create screen and set its attributes
  			sled.wrap('<div class="stWrapSled" />'); //wrap sled in screen to hide overflow
  			var screen = $('.stWrapSled', obj); // get the screening div that surrounds and hides sled
			screen.css({ // set styles on screen
				'float': 'left',
				'overflow': 'hidden',
				'height': passengerHeight,
				'width': stageWidth,
				'position': 'relative'
				
			});		
			// scrollAmount is the same as stageWidth		
  			
  			//Set up Events for left and right arrow controls
			rArrow.click(function(e){ //when click right arrow, move sled to the left
				e.preventDefault();
				_moveLeft(1, sled, obj);//move the sled
			});
			lArrow.click(function(e){ //when click left arrow, move sled to the right
				e.preventDefault();
				_moveRight(1, sled, obj);//move the sled
			});
  			
  		//Manage the movement of the sled
  		_moveLeft = function(times, sled, scope) { //moves the sled to the left
				if (scrollPosition < maxScrollPosition)//if not at maximum position, move
				{
					scrollPosition += 1; //increment current position
					var moveAmount = '-=' + (times*stageWidth); //calculate how much to move based on the number sent in
					sled.animate({ //move it
						left: moveAmount
					},options.scrollSpeed, _updateControls()); //call method to update how the controls display
				}
			}
			_moveRight = function (times, sled, scope) { //moves the sled to the right 
				if(scrollPosition > 1)// if not at minimum postion, move
				{
					scrollPosition -= 1; //decrement current positin
					var moveAmount = '+=' + (times*stageWidth); //calculate how much to move based on number sent in
					sled.animate({ //move it
						left: moveAmount
					},options.scrollSpeed, _updateControls()); //update display of controls
				}
			}
			//Updates the display of the arrows
			_updateControls = function () {
				if(scrollPosition == 1)//if the sled is all the way at the starting position
				{
					lArrow.removeClass('on');//turn off left arrow
					rArrow.addClass('on');//turn on right arrow
				}
				else if (scrollPosition == maxScrollPosition)// if the sled is all the way at end position
				{
					rArrow.removeClass('on');//turn off right arrow
					lArrow.addClass('on');//turn on left arrow
				}
				else //else turn on both arrows
				{
					lArrow.addClass('on');//turn off right arrow
					rArrow.addClass('on');//turn on left arrow
				}
			}
			if (passengerCount > options.displayNum) // if there are more passengers to show than can fit on the stage
  				_updateControls(); // set controls
  			
  			obj.show(); // reveal slider in all its glory
  		});
 	};  
})(jQuery); 


// vertically centers an element inside it's parent. Just pass the element you want centered to 
// VerticalCenter.init(element);
var VerticalCenter =
{
	init: function (elements)
	{
		elements.each(function (){ // for each element needed to be centered
			var obj = $(this); // extend this
			var topMargin = (obj.parent().height() - obj.outerHeight(true))/2; // parent's height - objects height divided by 2
			obj.css('marginTop', topMargin); // set the margin
		});
	}
};


//issuu, use swfobject to embed player
var Issuu = 
{
  init: function()
  {
    $('script:last').after('<script type="text/javascript" src="js/swfobject/swfobject.js"></script>');
    if (Start.iOS){$('#issuu').append('<h1 style="text-align:center;line-height:1.3em;font-weight:normal;">Download the .pdf version<br />for your iPad, iPhone or mobile device.</h1>').height('auto');}
    
    var attributes = {
        id: 'issuu'
    },
     params = {
        allowfullscreen: 'true',
        menu: 'false',
        wmode: 'transparent'
    },
     flashvars = {
          jsAPIClientDomain: 'www.meghancarter.com',
          mode: 'mini',
          documentId: '111121022541-bd61f239911944929b86a22960f80e23'
      };
    

    swfobject.embedSWF("http://static.issuu.com/webembed/viewers/style1/v2/IssuuReader.swf", "issuu", "900", "601", "9.0.0","js/swfobject/expressInstall.swf", flashvars, params, attributes);
    
    
  
  }//init
};



//utility function for preloading images
//usage: jQuery.preLoadImages("image1.gif", "/path/to/image2.png");
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);






























