Cufon.replace('.logo', {
	textShadow: '#000 2px 2px'
});
Cufon.replace('.cufon, #content h1, #content h2', {
	textShadow: '#000 1px 1px'
});
Cufon.replace('.cufon-plain', {
	hover: 'true'			  
});

$(window).ready(function() {
	//$('#notice').slideDown('slow');
	
	initSlideShow();
	
	$('.reveal_list li').each(function(index, li) {
		var closeBtn = $('<a href="#" class="close" />').click(function() {
			$(this).parent().slideUp();																
		});
		$(li).children('.group').prepend(closeBtn);
		$(li).children('a.toggle').click(function() {
			$(this).next('.hide').slideToggle();
			return false;
		});
	});
});

function initSlideShow() {
	var slideshow = $('#notice');
	var slides = slideshow.children('.item');
	var fadeNum = 0;
	
	//if at least one slide:
	if(slides.length != 0) {
		//wrap each with a div and overlay the title
		slides.each(function() {
			var slide = $(this).detach();
			var alt = slide.attr('alt');
			var title = $('<div class="title" />').html(alt);
			var wrap = $('<div class="slide" />').appendTo(slideshow);
			wrap.append(title).append(slide);
		});
	}
	
	//if more than one slide:
	if(slides.length > 1) {
		//create the menu
		var menu = $('<div class="slide_menu" />').prependTo(slideshow);
		slides.each(function(index) {
			//add buttons for each slide
			if(index == 0) {
				menu.append($('<a href="#" class="active">' + (index + 1) + '</a>'));
			} else {
				menu.append($('<a href="#">' + (index + 1) + '</a>'));
			}
			//add id's to each slide
			$(this).parent().attr('id', 'slide' + (index + 1));
		});
		//add events to each button
		var buttons = menu.children('a');
		buttons.each(function(index) {
			$(this).click(function() {
				fadeNum = index;
				nextFade();
				return false;					   
			});
		});
		//adjust slide title positioning to accommodate a gigantic menu
		if(slides.length > 1) {
			var menu_h = menu.height();
			slides.each(function() {
				var title = $(this).parent().children('.title');
				var title_y = parseInt(title.css('top')) + menu_h - 25;
				title.css('top', title_y);					 
			});
		}
		var auto_fade = null;
		function nextFade() {
			if(auto_fade != null) {
				clearInterval(auto_fade);
			}
			var slide_items = $('.slide');
			var fadeLength = slide_items.length;
			//fade out everything except target slide
			$(slide_items).filter(function(index) {
				return index != fadeNum;						 
			}).fadeOut();
			//fade in target slide
			$(slide_items[fadeNum]).fadeIn();
			//set active menu button
			buttons.removeClass('active');
			buttons.eq(fadeNum).addClass('active');
			//find next slide
			if (fadeNum < (fadeLength-1)) {
				fadeNum++;
			} else {
				fadeNum = 0;
			}
			auto_fade = setInterval(nextFade, 5000);
		}
		nextFade();		
	}
}

function dialog(htmlContent, w, h, preventClose) {
	if($('.dialog').length) {
		//use existing
		var container = $('.dialog');
		container.hide();
	} else {
		//create new
		var target = $('body');
		var position = target.offset();
		var width = $(window).width();
		var height = $(window).height();
		var overlay = $('<div class="dialog spinner" />');
		var container = $('<div class="dialog content" />');

		target.css('position', 'relative');

		overlay.css({
			position: 'fixed',
			top: position.top,
			left: position.left,
			width: width,
			height: height
		})
		.appendTo(target)
		.fadeTo('fast', 0.8);
		
		if(!w) w = 600;
		if(!h) h = 400;
		
		container.css({
			width: w,
			height: h,
			marginTop: -(h/2),
			marginLeft: -(w/2)		  
		}).appendTo(target);
		
		if(!preventClose || preventClose == false) overlay.click(function() { closeDialog(); });
		
	}
	container.html(htmlContent).fadeIn();
}

function closeDialog() {
	$('.dialog').fadeOut('fast', function() { $(this).remove(); });	
}
