/* 

Credit to http://www.impactmediadesign.co.uk/

*/
$(function() {
		
	var slides = [];
	var slideButtons = [];
	var slideBtnsActive = false;	
	var animation = '';
	
	$('#type-slides div[id*=slide-]').each(function(event) {		
		slides.push(this);
	});
		
	$(slides).each(function(event) {
		$(this).hide();
	});	
	
	var current = 0;
	$(slides[current])
		.show().css({opacity:1})
		.find(".top-bar").css({opacity: 0}).end()
		.find(".middle-bar").css({opacity: 0, top: -200}).end()
		.find(".middle-bartwo").css({opacity: 0, top: -200}).end()
		.find(".bottom-bar").css({opacity: 0});	
				//addClass("active");
	
	$.fn.doAnimate = function(type, callback) {  
		slideBtnsActive = false;
		if (typeof callback != "function") { 
			callback = function(){} 
		}
		if (type === "in") {
			$(this)
				.show().css({opacity:1})
				.find(".top-bar").css({opacity: 0}).end()
				.find(".middle-bar").css({opacity: 0, top: -200}).end()
				.find(".middle-bartwo").css({opacity: 0, top: -200}).end()
				.find(".bottom-bar").css({opacity: 0});
			$(".top-bar", this).delay(1000).animate({opacity: 1}, 500);	
			$(".middle-bar", this).delay(2000).animate({opacity: 1, top: 0}, 500);
			$(".middle-bartwo", this).delay(3000).animate({opacity: 1, top: 0}, 500);
			$(".bottom-bar", this).delay(4000).animate({opacity: 1}, 1000, function(event) { 
				slideBtnsActive = false;
				callback.call(this); 
			});
		} else if (type === "out") {
			$(this).fadeOut(350).delay(150).animate({opacity:0}, 0, function(event) { 
				slideBtnsActive = false;
				callback.call(this);
			});
		}
	}
	
	function nextSlide(options) {
		var settings = {
			'next': null
		}
		$.extend(settings,options);
		$(slides[current]).doAnimate("out", function(event) {			
			$(slideButtons[current]).find('img').css({opacity: 0}).removeClass("active");
			if (current >= (slides.length - 1)) { current = 0; } else { current++; }
			if (settings.next != null) { current = settings.next; }
			$(slideButtons[current]).find('img').css({opacity: 1}).addClass("active");
			
			/* Secondary Animation */
			$(slides[current]).find('a').css({opacity: 0});
				
			$(slides[current]).doAnimate("in", function(event) {	
														
				/* Secondary animation */
				var tempAnchor = [];
				var fixAnchor = [];
								
				$(slides[current]).find('a').each(function(event) {		
						tempAnchor.push(this);
						fixAnchor.push(this);
					}).mouseenter(function(event) {
						/* incase we want to add any additional callbacks in the future */
						//if ($(this).css("opacity") > 0) {
						//	$(this).css({"border-bottom": "0px solid #FFFFFF", cursor: "pointer"}).animate({"border-bottom-width": 5}, 100);
						//}
					}).mouseleave(function(event) {
						/* incase we want to add any additional callbacks in the future */
						//$(this).animate({"border-bottom-width": 0}, 100);
					});
				
				//var tempDur = 6000 / tempAnchor.length;
				var tempDur = 1500;
				function nextLink() {
					$(tempAnchor[0]).animate({opacity:1}, tempDur, function(event) {
						tempAnchor.splice(0,1);
						nextLink();
					});
				}
				var tempDurNext = (tempAnchor.length * tempDur) + 100000;
				nextLink();
				animation = setTimeout(function() { nextSlide(); }, tempDurNext);
			});				
		});
	}

	// hide the dummy mask covering the slides
	$('#dummy').hide();
	nextSlide({next: 0});
	//animation = setTimeout(function() { nextSlide(); }, 8000);
	
});
