//	JS ENHANCEMENTS

$(function(){
	$('#slide').slideshow({
		delay: 6000,
		animtime: 1000
	});
	$('#subnav').navSlider({
		animtime: 300
	});
});

(function($) {

	$.fn.dummy = function(){
		if (this.length == 0){
			return $(this);
		}
		return $(this);
	}

	$.fn.navSlider = function(dat){
		if (this.length == 0){
			return $(this);
		}
		var loc = {
			animtime: 600
		}
		loc = $.extend(loc, dat);
		var $li = $(this).children('li').children('a');
		for (i=0; i<$li.length; i++){
			var $i = $li.eq(i);
			var $p = $i.parent();
			var ht = $p.outerHeight();
			var nht = $i.outerHeight();
			$p.css({
				height: nht,
				overflow: 'hidden'
			});
			$i.data({
				hton: ht,
				htof: nht
			});
		}
		var clfn = function(){
			if ($(this).hasClass('on')){
				var ht = $(this).data('htof');
			}
			else {
				var ht = $(this).data('hton');
			}
			$(this).toggleClass('on').parent().animate({
				height: ht
			}, loc.animtime);
			return false;
		}
		$li.click(clfn);
		return $(this);
	}

	$.fn.slideshow = function(dat){
		if (this.length == 0){
			return $(this);
		}
		var loc = {
			count: 0,
			delay: 6000,
			animtime: 1200
		}
		$.extend(loc, dat);
		var slide_styles = {
			top: 0,
			left: 0,
			position: 'absolute'
		}
		var $slides = $(this).css('position', 'relative').children('img').css(slide_styles).hide().eq(0).show().end();

		var cl = '';
		var ul = '<ul>';
		for (i=0; i<$slides.length; i++){
			if (i==0){
				cl = ' class="act"';
			}
			else {
				cl = '';
			}
			ul += '<li' + cl + ' title="Slide ' + (i+1) + '">' + (i+1) + '</li>';
		}
		ul += '</ul>';
		$(this).append($(ul));
		var $li = $(this).find('li');

		var showslide = function(num){
			$li.eq(loc.count).removeClass('act');
			$slides.eq(loc.count).fadeOut(loc.animtime);
			$li.eq(num).addClass('act');
			$slides.eq(num).fadeIn(loc.animtime);
			loc.count = num;
		}

		var loop = function(){
			loc.int = setInterval(function(){
				var num = loc.count+1;
				if (num == $slides.length){
					num = 0;
				}
				showslide(num);
			}, loc.delay);
		}

		var clfn = function(){
			if ($(this).hasClass('act')){
				return false;
			}
			var num = $li.index(this);
			showslide(num);
			clearInterval(loc.int);
			loop();
		}

		loop();
		$li.click(clfn);

		return $(this);
	}

//	LOGGING FUNCTIONS FOR TROUBLESHOOTING

	$.fn.log = function(){
		if (typeof window.console !== 'undefined'){
			console.log($(this));
		}
		return $(this);
	}

	$.log = function(inp){
		if (typeof window.console !== 'undefined'){
			console.log(inp);
		}
		return inp;
	}

})(jQuery);


