
/* =====================================================================
	CLIMB JavaScript Library - Portland Community College
	Written by Gabriel Nagmay <gabriel.nagmay@pcc.edu>
	Compress with jsmin <http://fmarcia.info/jsmin/test.html>
	Change Log: 
		2010-07-05 gabriel nagmay:		Created
		2011-09-28 gabriel nagmay:		Tabs now use hash and can be linked to. Requires inclusion of jquery.hashchange.js
		2012-01-05 gabriel nagmay:		Removed "animation" of feature image, since we only have 3 sections now...							 
 * ===================================================================== */
 
 
 // Set breadcrumbs
fns["/climb"] = 'CLIMB';
fns["/climb/health/cna"] = 'CNA';

var imgWidth = 520;
var speed = 1500;
var profileCurr = 1;
var profileCount = 0;


// On Page Ready
$(document).ready(function() {
	climbDesignTime();					// Presentational Scripting
	homeAudience();
	homeProfiles();
});

function homeProfiles(){
	$(".profile").click(function(){	
		 location = $(this).find("a:first").attr("href");	
	});		
	$(".profile").hover(function () {
    	$(this).addClass("over");
 	},function () {
    	$(this).removeClass("over");
  	});
	
	// get profile count
	profileCount = $(".profile").size();
	//log("count = "+profileCount);
	
	// open animate
	setTimeout("profileSwitch(profileCurr)",2000);
	
	// buttons
	$("#profiles #left").click(function(){	
		if( profileCurr > 0 ){
			profileSwitch(profileCurr-1)
		}
	});	
	$("#profiles #right").click(function(){	
		if( profileCurr < profileCount-1 ){
			profileSwitch(profileCurr+1)
		}
	});
	profileButtons();
}

function profileSwitch(i){
	  var offset = i * -imgWidth;  // find offset
	  if( $("#profile-slider").css("left") == offset+"px" ){ return; }					// already there, do nothing
		
	  $("#profile-slider").stop().animate({left:offset},speed,"swing");
	  profileCurr = i;
	  profileButtons();
}

function profileButtons(){
	if( profileCurr > 0 ){
		$("#profiles #left").addClass("active").hover(function(){								   
			$(this).addClass("over");	
		}, function () {
			$(this).removeClass("over");	
		});	
		
	}
	else{
		$("#profiles #left").removeClass("active").unbind('mouseenter mouseleave');
	}
	if( profileCurr < profileCount-1 ){
		$("#profiles #right").addClass("active").hover(function(){								   
			$(this).addClass("over");	
		}, function () {
			$(this).removeClass("over");	
		});	
	}
	else{
		$("#profiles #right").removeClass("active").unbind('mouseenter mouseleave');

	}
}

function homeAudience(){
	/*
	var i, elem, elems = getElementsByClassName(document.getElementById('snav'), 'audience', 'div');	
	for (i=0; elem = elems[i]; i++) {	
		elem.onmouseover = function() { 
			this.className = this.className.addClass('over'); 
		};	
		elem.onmouseout = function() { 
			this.className = this.className.removeClass('over'); 
		};	
		elem.onclick = function() { 
			location = this.getElementsByTagName('a')[0].href; };	
		}	
	});	// add audience rollover	/*
	*/
	
	$("#snav div.audience").hover(function(){								   
		$(this).addClass("over");	
		// $("#feature").addClass("over-"+$(this).attr("id"));
	}, function () {
		$(this).removeClass("over");	
		// $("#feature").removeClass("over-"+$(this).attr("id"));
	});	
	$("#snav div.audience").click(function(){	
		 location = $(this).find("a:first").attr("href");	
	});	

}
function climbDesignTime(){
	
	// Add corner for action box
	$("div.action").append('<div id="action-br">'); 
	
	
	// Fix lists on Product Groups
	$("dl.products dt").each( function(){
		$(this).before('<dl class="product"><dt>'+$(this).html()+'</dt><dd>'+$(this).next("dd").html()+'</dl>');
		$(this).next("dd").remove();	
		$(this).remove();											   
	});
	$("dl.products").replaceWith( $("dl.products").contents() );
	$("dl.product").hover(function () {
    	$(this).addClass("over");
 	},function () {
    	$(this).removeClass("over");
  	});
	$("dl.product").click(function(){	
		 location = $(this).find("a:first").attr("href");	
	});	

	

	$("#main dl.product:nth-child(3n+2)").addClass("clear");
	
	
	
}

function log(msg) {
    setTimeout(function() {
        throw new Error(msg);
    }, 0);
}



/* =====================================================================
 * functions for term based tab interface
 * Requires following schema:
 * <div id="tabs" class="worldview">
 *   <div class="indexlist" id="Term200804">
 *     <h3 class="term">Fall 2008</h3>
 *	   ...
 *	 </div>
 * </div>  
 * ===================================================================== */

$(document).ready(function() {
		//alert("asd");
		var termNum = $("div.indexlist").size(); 							// find number of available terms
		var termList = '';
		for(var i=0; i<termNum; i++){										// build link text
			var termThis = $("div.indexlist:eq("+i+") h3").text(); 			// get  h3 text 
			var termCode = $("div.indexlist:eq("+i+")").attr("id");			// get indexlist id
			$("div.indexlist:eq("+i+") h3").remove();						// remove h3
			termList+= '<li><a href="#'+termCode+'" onclick="return updateHash(\''+termCode+'\');">'+termThis+'</a></li>';
		}
		$("#tabs").prepend('<ul id="tab-list">'+termList+'</ul>');			// insert list
		
		$.getScript('/_source/scripts/jquery.hashchange.js', function(){        // require hashchange
			$(window).hashchange( function(){
				var hash = location.hash;
				if( hash.substr(1,4) == "tab-" ){								// if hash "tab-term", use that tab
					showTerm( hash.substr(5));
				}
				else{
					showTerm($("div.indexlist:eq(0)").attr("id"), false );		   // or, use first
				}
			});
	
			$(window).hashchange();
		});	
});


function showTerm(termCode){
	$("div.indexlist:not(#"+termCode+")").hide();						// animate
	$("div.indexlist#"+termCode).fadeIn("slow");
	$("#tab-list  li").removeClass("current"); 							// set current state
	$("#tab-list li:has(a[href$="+termCode+"])").addClass("current");   // href=#"+termCode+" seems to bork in ie. Selector bug? Special char?
	return false;	
}	

function updateHash(termCode){
		// This will trigger hashchange
	 	window.location.hash = "tab-"+termCode; 							// update url
		return false;	
}






