/* ------------------------------------------------------------------------

    Title:      Pearson MyLab/Mastering JavaScript (behavior) file
    Filename:   master.js
	Author:     Bob Prokop | bobprokop@yahoo.com
    Updated:    July 2011
    Notes:      ...		

------------------------------------------------------------------------- */
/*	jQuery + plug-in(s)
----------------------------------------------*/
$(document).ready(function(){
						   
	//	Initialize various modules
	
	//	universal functions
	init_productLogoSwitch();
	
	//	secondary screens
	init_removeBorders();
	init_faq();
	init_orderInfo();
	
	


/*	Equalize column height
----------------------------------------------*/
	equalHeight($('.equal-column'));
	//equalHeight($('.anchor-toc'));

/*	Removes the bottom border from the last child of a given parent.
	Frees development team from having to implement in template logic.
------------------------------------------------------------------------*/
	$('.product-list > li:last-child').addClass('no-border');
	
	//	When generated dynamically, we need bind events with 'live'
	$('.product-list > li:last-child').live('load', function(){
	$('.product-list > li:last-child').addClass('no-border');
	});

/*	prettyPhoto
	Use to open content in modal window. Content can be:
	--inline
	--external file
	--iframe
	--video (.mov, .avi, .swf, .ogg, etc.)
------------------------------------------------------------------------*/
	//$("a[rel^='prettyPhoto']").prettyPhoto(); //-- use default parameters //
	$("a[rel^='prettyPhoto']").prettyPhoto({
			animation_speed: 'fast', /* fast/slow/normal */
			slideshow: 5000, /* false OR interval time in ms */
			autoplay_slideshow: false, /* true/false */
			opacity: 0.80, /* Value between 0 and 1 */
			show_title: true, /* true/false */
			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
			default_width: 500,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding: 20, /* The padding on each side of the picture */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			deeplinking: false, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback: true,
			markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_top"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
						<div class="pp_content_container"> \
							<div class="pp_left"> \
							<div class="pp_right"> \
								<div class="pp_content"> \
									<div class="pp_loaderIcon"></div> \
									<div class="pp_fade"> \
										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
										<div class="pp_hoverContainer"> \
											<a class="pp_next" href="#">next</a> \
											<a class="pp_previous" href="#">previous</a> \
										</div> \
										<div id="pp_full_res"></div> \
										<div class="pp_details group"> \
											<div class="pp_nav"> \
												<a href="#" class="pp_arrow_previous">Previous</a> \
												<p class="currentTextHolder">0/0</p> \
												<a href="#" class="pp_arrow_next">Next</a> \
											</div> \
											<p class="pp_description"></p> \
											{pp_social} \
											<a class="pp_close button" href="#">Close</a> \
										</div> \
									</div> \
								</div> \
							</div> \
							</div> \
						</div> \
						<div class="pp_bottom"> \
							<div class="pp_left"></div> \
							<div class="pp_middle"></div> \
							<div class="pp_right"></div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',
			gallery_markup: '<div class="pp_gallery"> \
								<a href="#" class="pp_arrow_previous">Previous</a> \
								<div> \
									<ul> \
										{gallery} \
									</ul> \
								</div> \
								<a href="#" class="pp_arrow_next">Next</a> \
							</div>',
			image_markup: '<img id="fullResImage" src="{path}" />',
			flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
			quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
			iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no" title="Video"></iframe>',
			inline_markup: '<div class="pp_inline">{content}</div>',
			custom_markup: '',
			social_tools: false/*'<div class="pp_social"><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?locale=en_US&href='+location.href+'&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div></div>'*/ /* html or false to disable */
		});
		   
/*	Footer: dynamically generate the 'Copyright + symbol + current year'
	Template is populated with 'Copyright + symbol' (no year) as
	a fallback for visitors w/o JavaScript enabled; if JS is enabled
	this script will print out the string and the current year.
------------------------------------------------------------------------*/
	var currentYear = (new Date).getFullYear();
	$('#current-year-footer').html('Copyright ' + '&copy; ' + (new Date).getFullYear() + ' ');

/*	Sets 'title' attribute for dynamically generated <a href> links
------------------------------------------------------------------------*/
	// Table Column Sort
	$('.col-sort').each(function(){
		var colHeading = $(this).text();
		$(this).attr('title', 'Sort tabular data by ' + colHeading);
	});

/*	Scroll anchor links for more fluid and natural navigation for in-page
	links. Must set 'rel' attribute on link to 'anchor' for this to work.
------------------------------------------------------------------------*/
	/* $('a[rel=anchor]').click(function(e){
		e.preventDefault();							  
		var full_url = this.href;
		var parts = full_url.split("#");
		var trgt = parts[1];
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
			$('html, body').animate({scrollTop:target_top}, 325);
			return false;
		});
		*/

});/* end jQuery */

/*------------------------------------------------------------------------

	Note: The following are either functions called by event listeners
	found above or are non-jQuery function(s)
	
-------------------------------------------------------------------------*/

/*	FAQs: We're using the semantically correct definition list <dl>
	element for structure; the definition term <dt> is the question
	and the definition data <dd> is the answer.
-------------------------------------------------------------------------*/
	function init_faq(){
		
		//	First, for those users who have JavaScript enabled,
		//	let's hide the answers (which will be revealed via
		//	toggle when the corresponding <dt> is clicked)
		//	
		//	Note: modified for accessibility 21-June-2011 per
		//	Pearson guidelines: removed class name toggle and
		//	placed images into markup. Ugly for SEO, but
		//	accessibility appears to trump semantic, uncluttered
		//	markup in this instance.
		
		$('.faq > dd').hide();
		
		//	Click a question <dt> to reveal the answer <dd>
		$('.faq > dt').live('click', function(){
			$(this).toggleClass('faq-open').next().slideToggle(100);
			
			var src = ($(this).find('img').attr('src') === '../assets/images/global/minus.png')
				? '../assets/images/global/plus.png' 
				: '../assets/images/global/minus.png';
				$(this).find('img').attr('src', src);
			
			return false;
		});
		
		//	Toggle the state of all questions
		var isToggled = false;
		$('#faq-toggle-all').live('click', function(){
		
			if(isToggled == false){
				$(this).html('Hide all');
				isToggled = true;
				$('.faq dt').addClass('faq-open');
				$('.faq-expand').attr('src', '../assets/images/global/minus.png');
				$('.faq-expand').attr('alt', 'Collapse');
				$('.faq-expand').removeClass('faq-expand').addClass('faq-collapse');
	   			$('.faq dt').next().show(100);
			}
			else{
				$(this).html('Show all');
				isToggled = false;
				$('.faq dt').removeClass('faq-open');
				$('.faq-collapse').attr('src', '../assets/images/global/plus.png');
				$('.faq-collapse').attr('alt', 'Expand');
				$('.faq-collapse').removeClass('faq-collapse').addClass('faq-expand');
	   			$('.faq dt').next().hide(100);
			}
		
		return false;
		
		});
	}


/*	Set source path for product logo based on body id
	so image coordinates with current color palette
-------------------------------------------------------------------------*/
	function init_productLogoSwitch(){
		
		//	what is the current ID of the body tag?
		var currentColor = $('body').attr('id');
		
		//	set the image path based on the current color
		
		//	if this is the home page
		if($('body').hasClass('home')){
			$('.product-logo').find('img').attr('src', 'assets/images/wordmarks/' + currentColor + '/wordmark-for-header.png');
		}
		else{
			$('.product-logo').find('img').attr('src', '../assets/images/wordmarks/' + currentColor + '/wordmark-for-header.png');
		}
	}
	
/*	Remove borders from specific constructs
-------------------------------------------------------------------------*/
	function init_removeBorders(){
		$('ul.side-nav > li > ul > li:last-child').addClass('no-border');
		$('ul.side-nav > li > ul > li > ul > li:last-child').addClass('no-border');
	}

/*	Expand the 'Ordering information' for the 'Books Available' profiles
-------------------------------------------------------------------------*/
	function init_orderInfo(){
		
		//	first let's hide the order info
		$('ul.table').hide();
		$('ul.table-narrow').hide();
		
		//	toggle display of ordering info
		//	on individual book level
		$('.order-info').live('click', function(){
			$(this).toggleClass('pointer-open');
			$(this).parent().find('ul.table').toggle(200);
			$(this).parent().find('ul.table-narrow').toggle(200);
			return false;
		});
		
		//	toggle display of ordering info
		//	for all books at group level
		$('#toggle-all-order-info').live('click', function(){
			if($(this).text() == 'Show all ordering info'){
				$(this).text('Hide all ordering info');
				$('ul.table').show(200);
				$('ul.table-narrow').show(200);
				$('.order-info').addClass('pointer-open');
			}
			else{
				$(this).text('Show all ordering info');
				$('ul.table').hide(200);
				$('ul.table-narrow').hide(200);
				$('.order-info').removeClass('pointer-open');
			}
		return false;
		});
	}

/*	Equal height columns (used for multi-col taxonomy browse elements
	and more (courtesy of Rob Glazerbrook)
-------------------------------------------------------------------------*/
	function equalHeight(group){
		var tallest = 0;
		group.each(function(){
			var thisHeight = $(this).height();
			if(thisHeight > tallest){
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}

