/* ------------------------------------------------------------------------

    Title:      PearsonStudents JavaScript (behavior) file
    Filename:   main.js
    Method:     <link>
    Author:     Bob Prokop | bobprokop@yahoo.com
    Updated:    January 2010
    Notes:      ...		

------------------------------------------------------------------------- */
/*------ what to do onload of document ------*/
	// W3C DOM-standards user agents //
	if(window.addEventListener){
		window.addEventListener("load",init_page,false);
	}
	// Internet Explorer //
	else{
		window.attachEvent("onload",init_page);
	}
/*-------------------------------------------*/
function init_page(){
	/*--- make Moz happy and clean-up whitespace ---*/
	if(window.addEventListener){
		treeWalker(document);
	}
	/*--- hide Twitter ticker update timestamp hrefs & open links in new window ---*/
	twitterLinks();
}

//-------------------------------------------------------------------------------------------------------
// jQuery + plug-in stuff
//-------------------------------------------------------------------------------------------------------
$(document).ready(function(){

//--- preload images files ---//
$.preloadCssImages();

//--- zoom: thumbnail image gallery ---//
$("ul.thumb li").hover(function() {
	$(this).css({'z-index' : '9999'});
	$(this).find('img').addClass("hover").stop()
		.animate({
			marginTop: '-120px', 
			marginLeft: '-109px', 
			top: '50%', 
			left: '20%', 
			width: '240px', 
			height: '218px',
			padding: '10px',
			opacity: '1.0'
		}, 150);
	
	} , function() {
	$(this).css({'z-index' : '0'});
	$(this).find('img').removeClass("hover").stop()
		.animate({
			marginTop: '0', 
			marginLeft: '0',
			top: '0', 
			left: '0', 
			width: '120px', 
			height: '109px', 
			padding: '1px',
			opacity: '.7'
		}, 300);
});

//--- validate forms ---//
/* Contact Us */
if(document.getElementById('contact-us')){
	$('#name').focus();
	$('#contact-us').validate();
}

//--- modal window for movies/photos/flash/etc ---//
//$("a[rel^='prettyPhoto']").prettyPhoto(); -- use default parameters //
$("a[rel^='prettyPhoto']").prettyPhoto({
	animationSpeed: 'normal', /* fast/slow/normal */
	padding: 40, /* padding for each side of the picture */
	opacity: 0.65, /* Value betwee 0 and 1 */
	showTitle: true, /* true/false */
	allowresize: true, /* true/false */
	counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
	theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
	hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
	modal: false, /* If set to true, only the close button will close the window */
	changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
	callback: function(){} /* Called when prettyPhoto is closed */
});

//--- movie gallery thumbnails ---//
$('#movies p').css({opacity: '0.5', overflow: 'hidden'}); /* first, let's set their initial opacity */

/* fade thumbnail on hover */
$('#movies p').hover(
	function(){
		$(this).animate({opacity: '1.0'}, 'medium');
	},
	function(){
		$(this).animate({opacity: '0.5'}, 'fast');
});

//--- twitter ticker ---//
$(function(){
    $('#twitter_update_list').cycle({
		fx:     'scrollRight', 
    	speedIn:  700, 
    	speedOut: 200, 
    	delay:  -3000,
		timeout: 9000,
		pause: true
	})
});

//--- billboard controls ---//
$(function(){
    $('#bb-pause').click(function(){
			$('#billboard').cycle('pause');
			$('#bb-pause').css({display: 'none'});
			$('#bb-play').css({display: 'block'});
			return false;
			});
    $('#bb-play').click(function(){
			$('#billboard').cycle('resume');
			$('#bb-play').css({display: 'none'});
			$('#bb-pause').css({display: 'block'});
			return false;
			});
    
    $('#bb-container').hover(
        function() { $('#bb-control').fadeIn(); },
        function() { $('#bb-control').fadeOut(); }
    );
/* transition between panel content */
   $('#billboard').cycle({
		cleartype:  1, // enable cleartype corrections 
		delay: -2000,
        speed: 700,
		timeout: 6000,
		fx: 'fade',
		pause: true,
		next: '#bb-next',
		prev: '#bb-prev',
		before: onBeforeBillboard
	});
    function onBeforeBillboard(){
		$('#billboard p.notfirst').removeClass('notfirst');
        $('#bb_caption').html('<q lang=en-us>&#8220;' + this.lastChild.title + '&#8221;</q>' + '<p>' + this.lastChild.alt + '</p>' );
		$('#bb_caption').fadeIn(300);
    }
});
});

//--- movie gallery slider carousel ---//
$(function(){
    $('#movies').cycle({
		fx: 'scrollHorz',
		delay: -1000,
		speed: 500,
		timeout: 6000,
		pause: true,
		prev: '#prev',
		next: '#next'
	})
});

//--- sidebar quote cycler ---//
$(function(){
    $('#sidebar > div.quote-container').cycle({
		cleartype:  1, // enable cleartype corrections 
		delay: -1000,
        speed: 600,
		timeout: 6000,
		fx: 'blindX',
		pause: true,
		before: onBeforeSB
	});
    function onBeforeSB(){
		$('#sidebar div.notfirst').removeClass('notfirst');
    }
});

//--- sidebar: highlight the citation when :hover on img ---//
$(function(){
	$('#sidebar img').hover(
	function(){
		$(this).next().next().addClass('hilite');
	}, function(){
		$(this).next().next().removeClass('hilite');
	})
});

/* Equalize column height */
//equalHeight($('.col-container ul'));

$("div span:last-child")
        .css({color:"red", fontSize:"80%"})
        .hover(function () {
              $(this).addClass("solast");
            }, function () {
              $(this).removeClass("solast");
            });
		

//--- scroll anchor links ---//
$('a[rel=anchor]').live("click", function(){
	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;
});

/* FAQ expand */
$(function(){
	if(document.getElementById('faq')){
		// open~close single question //
		$('#faq dt').click(function(){
			$(this).toggleClass('open');
			$(this).next().slideToggle('fast');
		});
		// open~close all questions //
		var isToggled = false;
		$('#toggle-faq').click(function(){
		
			if(isToggled == false){
				$(this).addClass('open');
				$(this).html('Hide all');
				isToggled = true;
				$('#faq dt').addClass('open');
	   			$('#faq dt').next().show('fast');
			}
			else{
				$(this).removeClass('open');
				$(this).html('Show all');
				isToggled = false;
				$('#faq dt').removeClass('open');
	   			$('#faq dt').next().hide('fast');
			}
	   	return false;
		});
	}
});

/* accordion expand~contract */
$(function(){
	$('.accordion > dd').hide();
	
	$('.accordion > dt').click(function(){
		$(this).toggleClass('expanded').next().slideToggle('fast');
		$(this).parent('dl.accordion').toggleClass('marBot');
	});
});


//--- widgets ---//
/* flickr */
$(function(){ /* let's open these in a new window */
	$('td.flickr_badge_image a').attr({ target: "_blank" });
});

//--- toggle copy ---//
$(function(){
    $('.toggle_container').hide();

	$('#switch').toggle(function(){
		$(this).addClass('less'); 
		}, function () {
		$(this).removeClass('less');
	});
	
	$("#switch").click(function(){
		$(this).text($(this).text() == 'Less information' ? 'More information' : 'Less information');
		$('.toggle_container').slideToggle('slow,');
	});
});

//-------------------------------------------------------------------------------------------------------
// The following scripts are called from the init_page function (and some of these call other functions) 
//-------------------------------------------------------------------------------------------------------
/* equal height columns (used for multi-col taxonomy browse elements), 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);
}*/
/* make Moz happy and clean up any whitespace */
function treeWalker(node){
	var notWhitespace = /\S/
	for(var x = 0; x < node.childNodes.length; x++){
		var childNode = node.childNodes[x];
			if((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))){
      			node.removeChild(node.childNodes[x])
     			 x--
    		}
    		if(childNode.nodeType == 1){
      			treeWalker(childNode)
    		}
  		}
}
/*--- don't show the timestamp links in Twitter ticker element and open Twitter ticker links in new window ---*/
function twitterLinks(){
	if(document.getElementById('ticker')){
		var tickerHrefs = document.getElementById('twitter_update_list').getElementsByTagName('a');
			for(var i=0; i<tickerHrefs.length; i++){
				//tickerHrefs[i].target = '_blank';
				if(tickerHrefs[i].style.fontSize == '85%'){
					tickerHrefs[i].style.display = 'none';
				}
			}
	}
}
