jQuery(document).ready(function($) {

	jQuery.fn.fadeIn = function(speed, callback) { 
	    return this.animate({opacity: 'show'}, speed, function() { 
	        if (jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	}; 

	jQuery.fn.fadeOut = function(speed, callback) { 
	    return this.animate({opacity: 'hide'}, speed, function() { 
	        if (jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	}; 

	jQuery.fn.fadeTo = function(speed,to,callback) { 
	    return this.animate({opacity: to}, speed, function() { 
	        if (to == 1 && jQuery.browser.msie)  
	            this.style.removeAttribute('filter');  
	        if (jQuery.isFunction(callback)) 
	            callback();  
	    }); 
	};
	
	function fixClear(el){
		el.removeAttr("filter");
	}
	
	fixClear($(".application *"));
	
	// fix ie transition

	$("#slider ul li blockquote").each(function() {
		var theHeight = ( 301 - $(this).height() ) / 2;
		$(this).css("paddingTop",theHeight + "px");
	});
	// center quotes
	
	function slider(){
	
		var howmany = $("#slider ul li").length;

		$("#slider ul li").hide();	

		var textSpeed = 800;
		var fadeSpeed = 1000;
		var waitSpeed = 4200;


		function getFirst(){
		$("#slider ul li:first").fadeIn(fadeSpeed).find("blockquote").show().animate({ left: 0 }, { queue: false, duration: textSpeed });
		nextSlide();
		}	

		getFirst();

		function nextSlide(){

			setTimeout(function() {

				var index = $("#slider ul li").index( $("#slider ul li:visible") );

				if(index == howmany - 1){
					$("#slider ul li:visible").fadeOut(fadeSpeed).find("blockquote").animate({ left: "-450px" }, { queue: false, duration: textSpeed }).css("left","");
					$("#slider ul li:hidden blockquote").css("left","");
					getFirst();
				} else {
					$("#slider ul li:visible").fadeOut(fadeSpeed).find("blockquote").animate({ left: "-450px" }, { queue: false, duration: textSpeed }).parent().next().fadeIn(fadeSpeed).find("blockquote").show().animate({ left: "0px" }, { queue: false, duration: textSpeed });
					$("#slider ul li:hidden blockquote").css("left","");
					nextSlide();
				}

			}, waitSpeed);

		}
		
	}
	
	slider();
	
	// end of slider
	
	function applicationAnim(){
	
		var fadeSpeed = 500;
		var slideSpeed = 500;
	
		$(".application img, .application .plus, .application ul, .application h1, .application h2").hide();
		$(".application h1:last").addClass("two");
	
		$(".application img:first").fadeIn(fadeSpeed);
		$(".application h2").fadeIn(fadeSpeed).animate({ left: "170px" }, { queue: false, duration: slideSpeed });
	
	
		$(".application ul li").hide();
		$(".application ul").css("display","block");
	
		var ulLength = $(".application ul li").length;
	
		function showFirstLi(){
		$(".application ul li:first").fadeIn(fadeSpeed).addClass("animated");
		//flashPlus(1);
		setTimeout(function() { showNextLi() }, 1500);
	
		}
	
		setTimeout(function() { showFirstLi() }, 1500);
		var showLoopCounter = 1;
		function showNextLi(el){
			showLoopCounter++;
		
			if(showLoopCounter > ulLength){
				finalAnim();
			} else {
				$(".application ul li.animated:last").next().fadeIn(fadeSpeed).addClass("animated");
				//flashPlus(2);

				setTimeout(function() { showNextLi(); }, 1500);
			}
		
		}
		var run = 1;
		function flashPlus(num){
			var flashSpeed = 100;
			var el = $(".application .plus");
		
		
			if(num == 1){
				el
				.css("opacity",0)
				.show()
				.animate({ opacity: .5 }, { queue: true, duration: flashSpeed })
				.animate({ opacity: 1 }, { queue: true, duration: 100 })
				.animate({ opacity: 0 }, { queue: true, duration: flashSpeed });	
			} else if(num == 2){
				run++;
			
				if(run > ulLength){
				} else {
					el
					.animate({ opacity: .5 }, { queue: true, duration: flashSpeed })
					.animate({ opacity: 1 }, { queue: true, duration: 100 })
					.animate({ opacity: 0 }, { queue: true, duration: flashSpeed });
				}
			
			}
		
		}
	
		function finalAnim(){
		
			$(".application ul, .application h2, .application .plus, .application img.bg1").fadeOut(fadeSpeed);
			setTimeout(function() {
			
				$(".application img.bg2").fadeIn(1750);
				
				setTimeout(function() {
					$(".application h1").fadeIn(1750);
				}, 1750);
				
				setTimeout(function() { 
					$(".application img.bg2, .application h1").fadeOut(fadeSpeed);
					
					setTimeout(function() {
						$(".application ul li.animated").removeClass("animated");
						applicationAnim();
					}, fadeSpeed);
					
				}, 6000);
			
			}, fadeSpeed);
		
			
		}
	
	}
	
	applicationAnim();
	
	// end of application section
	if(type == undefined || type == ''){
		$('.application').flash({
		    src: 'flash/KEL_mainFLSH.swf',
		    width: 870,
		    height: 300
		});
		$('.testimonials').flash({
		    src: 'flash/test_main.swf',
		    width: 425,
		    height: 300
		});
	}
	if(type == 'home'){
		$('.application').flash({
		    src: 'flash/KEL_mainFLSH.swf',
		    width: 870,
		    height: 300
		});
		$('.testimonials').flash({
		    src: 'flash/test_main.swf',
		    width: 425,
		    height: 300
		});
	}
	if(type == 'chem'){
		$('.application').flash({
		    src: 'flash/KEL_chemFLSH.swf',
		    width: 870,
		    height: 300
		});
		$('.testimonials').flash({
		    src: 'flash/test_chem.swf',
		    width: 425,
		    height: 300
		});
	}
	if(type == 'bio'){
		$('.application').flash({
		    src: 'flash/KEL_bioFLSH.swf',
		    width: 870,
		    height: 300
		});
		$('.testimonials').flash({
		    src: 'flash/test_bio.swf',
		    width: 425,
		    height: 300
		});
	}
	if(type == 'prof'){
		$('.application').flash({
		    src: 'flash/KEL_proFLSH.swf',
		    width: 870,
		    height: 300
		});
		$('.testimonials').flash({
		    src: 'flash/test_pro.swf',
		    width: 425,
		    height: 300
		});
	}
	
	
	// flash embed
	
	
	
});
