function initMenus() {
	
	$.each($('ul.menu'), function(){
		var cookie = $.cookie(this.id);
		if(cookie === null || String(cookie).length < 1) {
			$('#' + this.id + '.expandfirst ul:first').show();
		}
		else {			
			$('#' + this.id + ' .' + cookie).next().show();
		}
	});
	$('ul.menu li.next a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					if($(this).next().is(':visible')) {
						$.cookie(parent, null);
					}
					else {
						$.cookie(parent, this.className);
					}
					$(this).next().slideToggle('normal');
				}				
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					$.cookie(parent, this.className);
				}
				checkElement.slideDown('fast');
				return false;
			}
		}
	);
	
}


jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};

function loadContent(url){
	openContent = true;
	$("#lamenu").slideUp( function () {
		$("#baroque").append('<div id="append"><p class="rc"><a href="#" onclick="returnMenu(); return false;">&#139; return to menu</a> | <a href="#" onclick="closeDown(); return false;">close</a><div id="internal"></div></div>');
		$("#internal").load(url + " #page-content", initMenus);
		$(".rc").fadeIn('slow', function () {
			$("#internal").fadeIn('slow');
		});
		$("a.arrive").addClass("return");
		$("a.return").removeClass("arrive");
		return false;
	});
}

function loadExternal(url){
	$("#baroque").fadeOut('fast', function () {
		$(location).attr('href',url);
	});
}

function returnMenu() {
	if (openContent) {
	$('#append').fadeOut(function() {
		$('#append').remove();
	});
	$("#lamenu").slideDown('slow');
	openContent = false;
	} else {
	$("#baroque").fadeOut('slow', function() {
		$("body.internal-page #page").fadeIn('fast');
	});
	return false;
	}
}

function closeDown() {
	$('#append').fadeOut(function() {
		$('#append').remove();
	});
	$("#lamenu").slideDown('slow', function () {
		$("#baroque").fadeOut('slow');
	});
	openContent = false;
}




$(document).ready(
	function(){

		initMenus();
		
		// scroll stuff
	
	$(".r-over").hover(function() {
		  this.src = this.src.replace(".png","_over.png"); },
		 	function() {
		  this.src = this.src.replace("_over.png",".png");
		 }
	);
	
	$.serialScroll({
		items:'.single-image',
		prev:'.scroll-p',
		next:'.scroll-n',
		offset: 0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:600,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		jump: true //click on the images to scroll to them
	});
	
		$.serialScroll({
		items:'.single-news',
		prev:'.scroll-p',
		next:'.scroll-n',
		offset: 0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:600,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		jump: false //click on the images to scroll to them
	});
		
		
		$(".showmenu").click(
			function(){
				$("#baroque").fadeToggle('fast');
				openContent = false;
				$("body.internal-page #page").fadeToggle('fast');
				return false;
			}
		);
	
		$("body.home .h-image a").click(
			function(){
				$("#baroque").fadeToggle('fast');
				openContent = false;
				return false;
			}
		);

		$(".arrive").click(
			function(){
				$("#baroque").fadeOut('slow');
				return false;
			}
		);
		
		
		$(".showinfo").click(
			function(url){
				$.scrollTo(0, function () {
					$("#baroque2").fadeIn('slow');
					$("#project-images").removeClass('closed').addClass('open');
					}
				);
				return false;
			}
		);
		
		$(".rc-info").click(
			function(url){
				$("#baroque2").fadeOut('slow');
				$("#project-images").removeClass('open').addClass('closed');
				return false;
			}
		);
		
		$(".return").click(
			function() {
				if (openContent) {
				$('#append').fadeOut(function() {
					$('#append').remove();
				});
				$("#lamenu").slideDown('slow');
				openContent = false;
				} else {
				$("#baroque").fadeOut('slow', function() {
					$("body.internal-page #page").fadeIn('fast');
				});
				return false;
				}
			}
		);
		
		$("div.single-news h4").click(
			function(){ 
				$(this).toggleClass('on');
				$(this).next(".news-links").slideToggle();
				return false;
 			}
    	);
    	
    	$("#practice-slideshow #slides").cycle({
    	    fx: "scrollLeft",
        	speed: 180,
        	timeout: 5000,
        	next: "#practice-slideshow #slides",
        	after: onAfter
    	});
    	
    	 $("#practice-slideshow #slides img").click(function () {
        	$("#next").fadeOut("fast")
   		 });
   		 
   		 $("#practice-slideshow").mouseenter(function () {
        	$("#next").fadeIn("fast");
        	$("#practice-slideshow #slides").cycle("pause")
    	});
    	
    	$("#practice-slideshow").mouseleave(function () {
        	$("#next").fadeOut("fast");
        	$("#practice-slideshow #slides").cycle("resume");
        	offSlide = true
    	});
    	
		$("#practice-slideshow #next").click(function () {
        	$("#practice-slideshow #slides").cycle("next");
        	return false;
    	});
    	
    	function onAfter(a, b, c) {
    		var d = c.currSlide + 1 + " / " + c.slideCount;
    		$("#paginator").html(d)
		}
		
		

	}
);
