// JavaScript Document

// ------------------------------ Main Header JS ------------------------------- //
// MN Online Dropdown javascript
var keywordJSON = "/inc/js/keyword-json.js";
var headerJSON;
var numItemsPerCol = 6;
var numColPerGroup = 4;
var currentItem = 1;
var currentCol = 1;
var currentGroup = 1;
var currentIndex = 1;
var maxIndex = 0;

/* ------ Getter/Setter Functions ------ */
function setJSON(json){	
	headerJSON = json;
}

function getJSON(){
	return headerJSON;
}

function getHeaderInfo(){
	$.getJSON(keywordJSON, function(data){
		setJSON(data);
		populateHeader();
	});
}

function incCurrentItem() {
	currentItem += 1;
	if(currentItem > numItemsPerCol) {
		currentItem = 2;
		incCurrentCol();
	}
}

function incCurrentCol() {
	currentCol += 1;
	if(currentCol > numColPerGroup) {
		currentCol = 1;
		incCurrentGroup();
	}
}

function incCurrentGroup(){	
	var newElement;
	newElement = getClonedSection(currentGroup + 1);
	$("#headerKeys").append(newElement);
	currentGroup += 1;
}

function setCurrentItem(cItem) {
	currentItem = cItem;
}

function getCurrentItem() {
	return currentItem;
}

function getCurrentCol() {
	return currentCol;
}

function getCurrentGroup() {
	return currentGroup;
}

function setCurrentIndex(cIndex) {
	currentIndex = cIndex;
}

function setMaxIndex(cMax) {
	maxIndex = cMax;
}

function getCurrentIndex() {
	return currentIndex;
}

function getMaxIndex() {
	return maxIndex;
}

/* ------ Main Function ------ */
function populateHeader() {
	// Count how many li tags added 
	// Pop Headers 
	var currentJSON = getJSON();
	var first = true;
	$.each(currentJSON, function(i, menuItem) {
		var cat = menuItem.cathead;
		var keyCount = menuItem.keycount;
		var keyLink;
		var linkEnd = " |\n";
		var nameLink;
		if(keyCount > 0) {
			if(!first) {
				// set to new col.
				setCurrentItem(1);
				incCurrentCol();
			} else {
				first = false;
			}
			// Add header for category
			$("#headerItem" + getCurrentGroup() + "_" + getCurrentCol() + "_" + getCurrentItem()).text(cat);
			incCurrentItem();
			// Add header link.
			keyLink = "<a href=\"javascript:groupNav('" + getCurrentGroup() + "');\">" + cat + "</a>" + linkEnd;
			$("#headerLinks").append(keyLink);
			// Cycle through keywords
			$.each(menuItem.keyword, function(j, key) {
				// Add the keyword and link
				nameLink = "<a href=\"" + key.url + "\">" + key.name + "</a>";
				$("#headerItem" + getCurrentGroup() + "_" + getCurrentCol() + "_" + getCurrentItem()).html(nameLink);
				incCurrentItem();
			});
		} else {
			$("#headerLinks").append(cat + linkEnd);
		}
	});
	// Add 'next' link
	$("#headerLinks").append("<a href=\"javascript:cycleNav('n');\">Next &gt;</a>");
	// Set max index
	setMaxIndex(getCurrentGroup());
}

/* ------ Clone Functions ------ */
function getClonedSection(numGroup){
	var lines;
	var clone = "<div id=\"headerGroup" + numGroup +"\" class=\"headGroup\" style=\"display:none;\">\n";
	lines = getClonedLines(numGroup, 1);
	clone += "<ul id=\"headerCol" + numGroup + "_1\">\n" + lines + "</ul>\n";
	lines = getClonedLines(numGroup, 2);
	clone += "<ul id=\"headerCol" + numGroup + "_2\">\n" + lines + "</ul>\n";
	lines = getClonedLines(numGroup, 3);
	clone += "<ul id=\"headerCol" + numGroup + "_3\">\n" + lines + "</ul>\n";
	lines = getClonedLines(numGroup, 4);
	clone += "<ul id=\"headerCol" + numGroup + "_4\">\n" + lines + "</ul>\n";
	clone += "</div>\n";
	return clone;
}

function getClonedLines(numGroup, numCol){
	var lineClone = "<li id=\"headerItem" + numGroup + "_" + numCol + "_1\" class=\"keyHead\">&nbsp;</li>\n";
	lineClone += "<li id=\"headerItem" + numGroup + "_" + numCol + "_2\">&nbsp;</li>\n";
	lineClone += "<li id=\"headerItem" + numGroup + "_" + numCol + "_3\">&nbsp;</li>\n";
	lineClone += "<li id=\"headerItem" + numGroup + "_" + numCol + "_4\">&nbsp;</li>\n";
	lineClone += "<li id=\"headerItem" + numGroup + "_" + numCol + "_5\">&nbsp;</li>\n";
	lineClone += "<li id=\"headerItem" + numGroup + "_" + numCol + "_6\">&nbsp;</li>\n";
	return lineClone;
}

/* ------ Nav Functions ------ */
function cycleNav(direction) {
	var curr = getCurrentIndex();
	var m = getMaxIndex();
	if(direction == "n") {
		if(curr < m) {
			curr++;
			setCurrentIndex(curr);
		}
		groupNav(curr);
	} else if(direction == "p") {
		if(curr > 1) {
			curr--;
			setCurrentIndex(curr);
		}
		groupNav(curr);
	}		
}

function groupNav(group){
	// Hide all groups
	$(".headGroup").hide();
	// Show current group
	$("#headerGroup" + group).show();
	// Set current Index
	setCurrentIndex(group);
}

function headerToggle(){
	$("#headerCollapse").toggle("slow");
	$(".navIcon").toggle();
}
// ---------------------------- END Main Header JS ----------------------------- //

function ballnFor(tabId) {
	$('#' + tabId).addClass('open').next('dd').fadeIn();
}

function setActivePage() {
	var u = window.location.pathname;
	var uParts = u.split('/');
	var currSplit = new Array();
	$(uParts).each( function (i) {
		if(this.search(/\.html/) != -1) {
			currSplit = this.split('.html');
			// Need to check for the Licensure Search to ensure that both 
			//  the main and results page show correct page on left nav.
			if(currSplit[0].search(/results_2/) != -1) {
				currSplit[0] = "index";
			}
			$('#' + currSplit[0]).parent().addClass("active");
		} else if(this.search(/\.php/) != -1) {
			currSplit = this.split('.php');
			$('#' + currSplit[0]).parent().addClass("active");
		}
	});
}

function dropTracking(dropdown, section) {
	var selInd = dropdown.selectedIndex;
	var val = dropdown[selInd].value;
	if(dropdown[selInd].id != '') {
	var tracking = val + "/" + section + "/" + dropdown[selInd].id;
	//alert("Drop down tracking = " + tracking);
	pageTracker._trackPageview(tracking);
	}
}

$(document).ready(function() {	
	// ------------------------- header information ------------------------------- //
	var headCheck = $('#fullHeader').val();
	if (headCheck != null) {
		getHeaderInfo();				
	}
	// ----------------------- END header information ----------------------------- //	
	// ---------------------- navMiddle update balloon	--------------------------- //	
	// initialize ballons
	var pointerPos = 0;
	var topPos;
	
	$('#navMiddle dt').each(function() {
		// set balloon title
		var ballnTitle = $(this).text();
		$(this).next('dd').prepend('<strong>' + ballnTitle + '<strong>');
		
		// set balloon pointer
		if (pointerPos == 0) {
			topPos = "0";
		} else if (pointerPos != 0) {
			topPos = "-" + pointerPos;
		}
		$(this).next('dd').append('<span style="background-position: 0 ' + topPos + 'px;"></span>');
		$(this).next('dd').append('<a href="#" class="close-balln" title="Close">(x)</a>');
		pointerPos += 11; // height of each pointer slice
	});
	
	
	// manually close balloon
	$('.close-balln').click(function() {
		$(this).parent('dd').fadeOut();
		$(this).parent('dd').prev('dt').removeClass('open');
	});
	
	// open and close nav balloons
	$("#navMiddle dt").hover(
		function() {
			if ($(this).hasClass('open') == false) {
				$(this).siblings('dd').fadeOut();
				$(this).next('dd').fadeIn();
			}
		},
		function() {
			$(this).siblings('dt').removeClass('open');
			$(this).addClass('open');
		}
	);
	
	// ---------------------- END navMiddle update balloon	--------------------------- //
	// ------------------------ Left Nav Update ----------------------------- //	
	setActivePage();
	// ---------------------- END Left Nav Update --------------------------- //	
	// ------------------------- flash embed -------------------------------- //
	var flashCheck = $('#flashEmbed').val();
	if (flashCheck != null) {
		$('#flashEmbed').flash({ 
			src: '/inc/flash/MNonline.swf',
			
			width: 980,
			height: 356,
			wmode: 'transparent',
			style: 'margin: -20px 0 -20px -20px;'
		},{ 
			version: 8 
		});
	}
	
	flashCheck = $('#studentVids').val();
	if (flashCheck !=null) {
		$('#studentVids').flash({ 
			src: '/inc/flash/WorksForMe.swf',
			
			width: 593,
			height: 450,
			wmode: 'transparent',
			style: 'margin-top: 20px;'
		},{ 
			version: 9
		});
	}
	// ---------------------- END flash embed	----------------------------- //
});