// readCookie
function readCookie(name) {
	if (document.cookie == '') {
		return false;
	} else {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		if(firstChar != -1) {
			firstChar += name.length + 1;
			lastChar = theBigCookie.indexOf(';', firstChar);
			if(lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} else {
			return false;
		}
	}
}

// When page is ready...
$(document).ready(function() {
	
	// Detect browser
	var browser=navigator.appName;
	
	// Display latest tweets on homepage
	if(browser=="Microsoft Internet Explorer"){
		$("#tweets").getTwitter({
			userName: "AAFIllinois",
			numTweets: 2,
			loaderText: "Loading tweets...",
			slideIn: false,
			slideDuration: 0,
			showHeading: false,
			headingText: "",
			showProfileLink: false,
			showTimestamp: true
		});
	}else{
		$("#tweets").getTwitter({
			userName: "AAFIllinois",
			numTweets: 2,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: false,
			headingText: "",
			showProfileLink: false,
			showTimestamp: true
		});
	}
	
	// Use prettyPhoto to display images and video
	$(".gallery a img").each(function(i){
		$(this).parent().attr("title",$(this).attr("alt"));
	});
	//$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
	if ($("a[rel^='prettyPhoto']").length) {
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.80, /* Value between 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			default_width: 500,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'light_simple', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false /* If set to true, only the close button will close the window */
		});
	};
	
	// Recent News & Upcoming Events interface
	
	var hashy = window.location.hash;
	var sliderOnly = $("#feeditems").hasClass("slider");
	
	// add .toggle
	$("#feeditems .item .more").addClass("collapse").prepend('<a href="#" class="toggle">Read more &raquo;</a>');
	
	// add .slider-controls
	if(sliderOnly){
		$("#feeditems").append('<div class="slider-controls"><a href="#" class="prev">Prev</a> <a href="#" class="next">Next</a></div>');
	}else{
		$("#feeditems").append('<div class="slider-controls"><a href="#" class="prev">Prev</a> <a href="#" class="list">List All</a> <a href="#" class="next">Next</a></div>');
	}
	
	// should we start in list mode or slider mode?
	if((hashy!="" && hashy!="#") || sliderOnly ){
		$("#feeditems .slider-controls").show();
		if(hashy!="" && hashy!="#"){
			$("#feeditems").addClass("slider");
			$("#feeditems .item a[name]").filter(function(){
				if($(this).attr("name")==hashy.substring(1)) return true;
				else return false;
			}).parents(".item").addClass("current").siblings(".item").hide();
		}else{
			$(".item:first").addClass("current").siblings(".item").hide();
		}
		if(browser!="Microsoft Internet Explorer"){
			if($("#feeditems .current").prev(".item").length==0) $("#feeditems .slider-controls a.prev").addClass("dead");
			if($("#feeditems .current").next(".item").length==0) $("#feeditems .slider-controls a.next").addClass("dead");
		}
	}else{
		$("#feeditems").addClass("list");
		$("#feeditems .slider-controls").hide();
	}
	
	// transitions between states...
	if(browser=="Microsoft Internet Explorer"){
		
		// use non-animated transitions for IE
		
		// links coming from side calendar
		$(".news .column2 .calendar a").click(function(){
			$("#feeditems").slideUp(1, function(){
				hashy = window.location.hash;
				if(hashy!=""){
					$("#feeditems").removeClass("list").addClass("slider");
					$("#feeditems .slider-controls").show();
					$("#feeditems .item a[name]").filter(function(){
						if($(this).attr("name")==hashy.substring(1)) return true;
						else return false;
					}).parents(".item").show().addClass("current").siblings(".item").removeClass("current").hide();
				}
				$("#feeditems").show();
			});
		});
		
		// list mode
		$("#feeditems .title a, #feeditems .toggle").click(function(){
			$(this).parents(".item").addClass("current");
			$("#feeditems").hide().removeClass("list").addClass("slider");
			$("#feeditems .current").siblings().hide();
			$("#feeditems .slider-controls").show();
			$("#feeditems").show();
		});
		
		// slider mode
		$("#feeditems .slider-controls a.list").click(function(){
			$("#feeditems").hide();
			$("#feeditems .item").show().removeClass("current");
			$("#feeditems .slider-controls").hide();
			$("#feeditems").removeClass("slider").addClass("list").show();
		});
		$("#feeditems .slider-controls a.prev").click(function(){
			if($("#feeditems .current").prev(".item").length!=0){
				$("#feeditems .slider-controls").hide();
				$("#feeditems .current").hide().removeClass("current").prev().addClass("current").show();
				$("#feeditems .slider-controls").show();
			}
		});
		$("#feeditems .slider-controls a.next").click(function(){
			if($("#feeditems .current").next(".item").length!=0){
				$("#feeditems .slider-controls").hide();
				$("#feeditems .current").hide().removeClass("current").next().addClass("current").show();
				$("#feeditems .slider-controls").fadeIn("fast");
			}
		});
		
	}else{
		
		// use cool animations for compatible browsers
	
		// reset dead end link class
		$("#feeditems .slider-controls a, .news .column2 .calendar").click(function(){
			$("#feeditems .slider-controls a").removeClass("dead");
		});
		
		// animation for links coming from side calendar
		$(".news .column2 .calendar a").click(function(){
			$("#feeditems").fadeOut(200, function(){
				hashy = window.location.hash;
				if(hashy!=""){
					$("#feeditems").removeClass("list").addClass("slider");
					$("#feeditems .slider-controls").show();
					$("#feeditems .item a[name]").filter(function(){
						if($(this).attr("name")==hashy.substring(1)) return true;
						else return false;
					}).parents(".item").show().addClass("current").siblings(".item").removeClass("current").hide();
					if($("#feeditems .current").prev(".item").length==0) $("#feeditems .slider-controls a.prev").addClass("dead");
					if($("#feeditems .current").next(".item").length==0) $("#feeditems .slider-controls a.next").addClass("dead");
				}
				$("#feeditems").fadeIn(200);
			});
		});
		
		// list mode animations
		$("#feeditems .title a, #feeditems .toggle").click(function(){
			$(this).parents(".item").addClass("current");
			$("#feeditems").fadeOut(300, function(){
				$("#feeditems").removeClass("list").addClass("slider");
				$("#feeditems .current").siblings().hide();
				$("#feeditems .slider-controls").show();
				if($("#feeditems .current").prev(".item").length==0) $("#feeditems .slider-controls a.prev").addClass("dead");
				if($("#feeditems .current").next(".item").length==0) $("#feeditems .slider-controls a.next").addClass("dead");
				$("#feeditems").fadeIn(300);
			});
		});
		
		// slider mode animations
		$("#feeditems .slider-controls a.list").click(function(){
			$("#feeditems").fadeOut(300, function(){
				$("#feeditems .item").show().removeClass("current");
				$("#feeditems .slider-controls").hide();
				$(this).removeClass("slider").addClass("list").fadeIn(300);
			});
		});
		$("#feeditems .slider-controls a.prev").click(function(){
			if($("#feeditems .current").prev(".item").length!=0){
				$("#feeditems .slider-controls").fadeOut("fast");
				$("#feeditems .current").hide("drop", {direction: "right", mode: "hide"}, 250, function(){
					$(this).removeClass("current").prev().addClass("current").show("drop", {direction: "left", mode: "show"}, 250, function(){
						if($("#feeditems .current").prev(".item").length==0) $("#feeditems .slider-controls a.prev").addClass("dead");
						$("#feeditems .slider-controls").fadeIn("fast");
					});
				});
			}
		});
		$("#feeditems .slider-controls a.next").click(function(){
			if($("#feeditems .current").next(".item").length!=0){
				$("#feeditems .slider-controls").fadeOut("fast");
				$("#feeditems .current").hide("drop", {direction: "left", mode: "hide"}, 250, function(){
					$(this).removeClass("current").next().addClass("current").show("drop", {direction: "right", mode: "show"}, 250, function(){
						if($("#feeditems .current").next(".item").length==0) $("#feeditems .slider-controls a.next").addClass("dead");
						$("#feeditems .slider-controls").fadeIn("fast");
					});
				});
			}
		});
		
	}
	
	// Remember which navigation submenus are open
	
	// read cookie from last time
	var cookieName = "AAF_Illinois";
	var flags = readCookie(cookieName);
	
	// restore state of submenus
	$("#navigation li > ul")
	.hide()
	.filter(function(index){
		if(flags){
			if(flags.charAt(index)) return flags.charAt(index) == "1";
			else return false;
		}else{
			return false;
		}
	})
	.show();
	
	// animate opening/closing submenus
	$("#navigation li > ul").parent().find("em:first-child").click(function(){
		var submenu = $(this).parent().find("ul");
		if(submenu.is(":hidden")){
			submenu.slideDown("fast");
		}else{
			submenu.slideUp("fast");
		}
	});
	
	// save submenu state to cookie
	$(window).unload(function(){
		var freshcookie = cookieName + "=";
		$("#navigation li > ul").each(function(){
			if($(this).is(":hidden")){
				freshcookie = freshcookie + "0";
			}else{
				freshcookie = freshcookie + "1";
			}
		});
		freshcookie = freshcookie + ";path=/;";
		document.cookie = freshcookie;
	});
	
});