/* ------------------------------------------------------------------------

    Title:      Pearson MyLab/Mastering JavaScript (behavior) file
    Filename:   home-page.js
	Author:     Bob Prokop | bobprokop@yahoo.com
    Updated:    June 2011
    Notes:      To be served only for the home page

------------------------------------------------------------------------- */

/*------------------------------------------
	jQuery + plug-in(s)
------------------------------------------*/
$(document).ready(function(){		   
	init_slideshow();
	setVerticalAlignment();
	init_adRotation();

});/* end jQuery */

	/*------------------------------------------
		jQuery-powered slideshow
		Thanks to Mike Alsup @
		http://jquery.malsup.com/cycle
	------------------------------------------*/
	function init_slideshow(){
		
		//	first let's hide all but the first slide/panel/ad
		$('#slideshow-container > .panel:not(:first)').hide();
		
		$('#slideshow-container')
		.after('<div class="control-wrap"><a href="#" id="prev" title="Content for previous slide">&#8249;</a><div id="slideshow-controls">')
		$('#slideshow-controls').after('<a href="#" id="next" title="Content for next slide">&#8250;</a><a href="#" id="pauseSlideshow" title="Pause slideshow">Pause</a><a href="#" id="resumeSlideshow" title="Resume slideshow">Play</a>');
		$('#slideshow-container')
		.cycle({
			//cleartype:false,
			fx:'fade',
			speed:750,
			timeout:10000,
			pause:1,
			delay:2000,
			pager:'#slideshow-controls',
			prev:'#prev',
			next:'#next'
		});
		
		$('#slideshow-controls > a').each(function(){
			var slideNumber = $(this).text();
			$(this).attr('title', 'Content for slide ' + slideNumber);
			});
		
		//	the pause slideshow control
		$('#pauseSlideshow').click(function(e) { 
			$('#slideshow-container').cycle('pause');
			$(this).hide();
			$('#resumeSlideshow').show();
			e.preventDefault();
		});
		
		//	the play (resume) slideshow control
		$('#resumeSlideshow').click(function(e) { 
			$('#slideshow-container').cycle('resume'); 
			$(this).hide();
			$('#pauseSlideshow').show();
			e.preventDefault();
		});
		
		//	hide the resume/play control initially
		$('#resumeSlideshow').hide();
	}
	/*------------------------------------------
		Set vertical alignment
		Vertically align user-supplied image
		inside ad-box subcontainer
	------------------------------------------*/
	function setVerticalAlignment(){
		
		//	does the element exist?
		if($('.box-140').length == 0){
			//	no? stop.
			return;
		}
		
		//	fixed height of the containing element
		var a = 140;
		
		//	get the image
		var aa = $('.box-140').find('img');
		
		//	get the height of the image within the container
		var b = aa.height();
		
		//	subtract the height of the img from the container
		var c = (a-b);
		
		//	divide the result by 2 and add 'px' string
		var d = (c/2) + 'px';
		
		//	for img w/'snapshot' class applied
		//	we need to consider the padding,
		//	box shadow, and border dimensions
		var e = (c/2);
		var f = (e-8) + 'px';
		//alert(d);
		//alert(f);
		
		//	if the image has a class of 'snapshot'
		if(aa.hasClass('snapshot')){
			aa.css('margin-top', f);
		}
		
		//	if it does not
		else{	
			aa.css('margin-top', d);
		}
	}
	/*------------------------------------------
		Ad rotation
		Lower right-hand ad-box space on
		home page (user-supplied content)
	------------------------------------------*/
	function init_adRotation(){
		$('#ad-box-container').rotator({
			mode:'datetime',
			switch_every:'second'
		});
	}
