//Common.js -- Common javascript functions for the www.dordt.edu website

//Set up search autocomplete for use with categories
$.widget("custom.catcomplete", $.ui.autocomplete, {
	_renderMenu: function( ul, items ) {
		var self = this,
			currentCategory = "";
		$.each( items, function( index, item ) {
			if ( item.category != currentCategory ) {
				ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
				currentCategory = item.category;
			}
			self._renderItem( ul, item );
		});
	}
});

$(document).ready(function() {
	// do stuff when DOM is ready

	$('.reveal').click(function() {
		if ($(this).siblings("ul").is(':visible')) {
			$(this).html('<img class="arrow" src="/images/icons/arrow_down.png" alt="Arrow"> reveal');
		} else {
			$(this).html('<img class="arrow" src="/images/icons/arrow_up.png" alt="Arrow"> hide');
		}

		$(this).siblings("ul").toggle('slide');
	});

	//Initialize Main Menu
	$("ul.sf-menu").superfish({
		autoArrows:	false,
		animation:   {opacity:'show',height:'show'},
		dropShadows: false,
		delay: 400,
		speed: 'normal',
		onHide: function(){
			$(this).parent("li").removeClass("selected");
		}
	});

	//Set height of submenu 2nd columns to match 1st column
	$("ul.sf-menu ul.menu_col1").each(function() {
		$(this).css("height", ($(this).height() + 10) + "px");
		$("~ ul.menu_col2", this).css("height", $(this).height() + "px");
	});

	//Move submenus that extend past right edge of template content body
	//Athletics
	$("ul.sf-menu a[href*='/athletics/']").nextAll("ul.menu_col1").css("left", "-71px");
	$("ul.sf-menu a[href*='/athletics/']").nextAll("ul.menu_col2").css("left", "129px");
	//Events
	$("ul.sf-menu a[href*='/events/']").nextAll("ul").css("left", "-74px");

	//Check URI of current page and set its top level menu item (if one exists) to 'current'
	var thisPage = window.location.pathname;
	var menuItem = thisPage.match(/admissions|academics|services_support|campus_life|athletics|arts|events/gi);
	if (menuItem) {
		$("ul.sf-menu > li > a[href*='/" + menuItem + "/']").parent().addClass("current");
	}

	//If this is the home page, display main groups links
	if (thisPage == '/') {
		$('#groups').css("display", "block");
	}

	//Add 'first' class to first submenu item
	$("ul.sf-menu li ul li:first-child").addClass("first");

	//Add 'last' class to last submenu item
	$("ul.sf-menu li ul li:last-child").addClass("last");

	//Show 'tab' background for the selected menu
	$("ul.sf-menu li ul").mouseover(function(){
		$(this).parent("li").addClass("selected");
	});

	//Zebra stripe tables
	$(".zebra tbody tr:nth-child(odd)").addClass("alt");

	//Zebra stripe li's
	$("ul.zebra li:nth-child(odd)").addClass("alt");
	$("div.zebra ul li:nth-child(odd)").addClass("alt");

	//Show/Hide Roster Details on Athletics Pages
   	$('#roster_show_hide_all').click(function(){
		if ($('#roster_show_hide_all').html().match(/Show/)) {
			$('.hidden').show();
			$('#roster_show_hide_all').html('Hide all roster details');
		} else {
			$('.hidden').hide();
			$('#roster_show_hide_all').html('Show all roster details');
		}
	});

	//Search Autocomplete
	$("#search_box").catcomplete({
		source: "/cgi-bin/main/search/autocomplete.pl",
		minLength: 3,
		delay: 0,
		focus: function(event, ui) {
				//$('#search_box').val(ui.item.label);
				return false;
		},
		select: function(event, ui) {
				$('#search_box').val(ui.item.label);
				window.location = ui.item.link;
				//$('#project-id').val(ui.item.value);
				//$('#project-description').html(ui.item.desc);
				//$('#project-icon').attr('src', '../images/' + ui.item.icon);
				
				return false;
		}
	})
	.data( "catcomplete" )._renderItem = function( ul, item ) {
			var append = "<a>";
			if (item.img) {
				append = append + item.img;
			} else if (item.parent) {
				append = append + item.parent + " > ";
			}
			append = append + item.label + "<div class=\"clear\"></div></a>";
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( append )
				.appendTo( ul );
	};

	//Adjust top margin for 'main' content section on pages that do not have breadcrumb visible
	if (!$('#breadcrumb').length) {
		$('#main').css("padding-top", "30px");
	}

	if (jQuery.browser.msie && jQuery.browser.version <= 7) {
		//Because IE is stupid
		$('#sidebar li a').hover( function() {
			$(this).css("background-color","transparent");
		});
		$('#sidebar li li').css("padding", "7px 6px 7px 10px");
		$('#sidebar li ul').css("margin-top", "10px");
		$('#sidebar li li').css("width", "210px");
	} else {
		//Adjust the padding to make the sidebar rollovers look decent
		$('#sidebar li a').not('#today_at_dordt li a').parent('li').css("padding", "0");
		$('#sidebar li p.title').parent('li').css("padding", "0");
		$('#sidebar li a').not('#today_at_dordt li a').css("padding", "7px 6px 7px 10px");
		$('#make_contact li li a').css("margin-right", "50px");
		$('#make_contact li li a.chat').css("margin-right", "30px");
		$('#make_contact li li a.chat').css("margin-top", "10px");
		$('#make_contact li li a.chat').css("padding", "0");
		$('#worth_sharing li a').css("padding", "10px 10px 10px 10px");
		$('#recruit_news li a').css("padding", "10px 10px 10px 10px");
	}

	//Add link to 'Today @ Dordt' h3 image
   	$('#today_at_dordt h3').click(function(){
		window.location.href = '/events/';
	});
	
});

//
// mailto - This function will obfuscate an email address so that it isn't
//	harvestable by spammers.
//
//	var1  - An email address with -AT-
//	style - The style of the A HREF that is printed out by this function
//	text  - The text of the A HREF link
//
function mailto( var1, style, text ) {
	var str1 = var1.replace( / -AT- /, "@" );
	var str2 = '';
	if ( style != '' && style != undefined ) {
		str2 = ' style="' + style + '" ';
	}
	var str3 = text;
	if ( str3 == '' ) {
		str3 = str1;
	}
	document.write( '<a href="' + 'mail' + 'to:' + str1 + '"' + str2 + '>' + str1 + '</a>' );
}

// Cookie manipulation
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function playAudio(filename, target) {
	$(target).html('<embed id="myaudio" src="' + filename + '" autostart="true" loop="false" hidden="true"></embed>');
}

