(function($){
	
	$.fn.expCarousel = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		if (!isMethodCall)
			var options = $.extend({}, options, {});
			
		
		if (isMethodCall) {
			var tab = $(this[0]).data("expCarousel");
			if (tab) {
				if (tab[options] && typeof tab[options] == "function"){
					return tab[options].apply(tab, $.makeArray(args).slice(1));
				}
			}
			return;
		}
		
		$(this).each(function(){
			var tab = $(this).data("expCarousel");
			if (isMethodCall && tab) {
				if (tab[options] && typeof tab[options] == "function"){
					return tab[options].apply(tab, $.makeArray(args).slice(1));
				}
				return;
			} else {
				tab = new $.expCarousel(this, options);
				$(this).data("expCarousel", tab);
			}
		});
		return this;
	};
	
	$.expCarousel = function(elem, options) {
		this.elem = elem;
		this.options = $.extend({}, options, $.expCarousel.defaults);
		this.init();
	};
	
	$.extend($.expCarousel, {
		defaults: {
		},
		prototype: {
			init: function() {
				this.zoomScale = 71.2;
				this.zoomIn = {
					helper: {
						left:"143px",
						top:0,
						opacity:""
					},
					main: {
						width:"394px",
						height:"261px"
					},
					childs: {
						'slide_image': { 
							width:"394px", 
							height:"261px",
							opacity:""
						},
						'slide_content': {
							width:"394px", 
							height:"261px",
							fontSize:"16px",
							opacity:""
						}
					}
				};
				
				this.zoomOutLeft = $.extend(true, {}, this.zoomIn, {
					helper: {
						left:0,
						top:"29px",
						opacity:0.5
					},
					main: {
						width:"280px",
						height:"186px"
					},
					childs: {
						'slide_image': { 
							width:"280px", 
							height:"186px",
							opacity:0.5
						},
						'slide_content': {
							width:"280px", 
							height:"186px",
							fontSize:"10px",
							lineHeight:"1.5em",
							opacity:0.5
						}
					}
				});
				
				this.zoomOutRight = $.extend(true, {}, this.zoomOutLeft, {
					helper: {
						left:"427px",
						top:"29px"
					}
				});
				
				this.offScreenLeft = $.extend(true, {}, this.zoomOutLeft, {
					helper: {
						opacity:0
					},
					childs: {
						'slide_image': {
							opacity:0
						},
						'slide_content': {
							opacity:0
						}
					}
				});
				
				this.offScreenRight = $.extend(true, {}, this.zoomOutRight, {
					helper: {
						opacity:0
					},
					childs: {
						'slide_image': {
							opacity:0
						},
						'slide_content': {
							opacity:0
						}
					}
				});
				
				this.animOut = {queue:false,duration:500, easing:"easeInExpo", step:function(x, n, obj) {
						if ($(this).is(".slide")) {
							$(this).css({
								zIndex: this.animScaleOut(10000, n.state, 0.75)
							});
						}
					}, complete: function() {
						/*
						if ($(this).is(".slide")) {
							$(this).css({opacity:""});
						};
						*/
					}
				};
				
				this.animIn = {queue:false,duration:500, easing:"easeInExpo", step: function(x,n,obj){
					if ($(this).is(".slide")) {
						$(this).css({
							zIndex: this.animScaleIn(10000, n.state, 0.75)
						});
					}
				}, complete: function() {
					if ($(this).is(".slide")) {
						$(this).css({opacity:""});
					};
				}};
				
				this.animOffScreenIn = {queue:false,duration:400, easing:"easeInExpo", step: function(x,n,obj) {
					if ($(this).is(".slide")) {
						$(this).css({
							zIndex: this.animScaleIn(5000, n.state, 0.75)
						});
					}
				}};
				
				this.animOffScreenOut = {queue:false,duration:400, easing:"easeInExpo", step: function(x,n,obj) {
					if ($(this).is(".slide")) {
						$(this).css({
							zIndex: this.animScaleOut(5000, n.state, 0.75)
						});
					}
				}};
				
				this.slides = $('.slide', this.elem);
				//IE PNG Opacity Fix
				if ($.browser.msie) {
					this.slides.each(function(){
						var img = $(".slide_image", this)[0];
						$(img).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"', sizingMethod='scale')");
						img.src="images/x.gif";
					});
				}
				
				this.current = -1;
				if (this.slides.filter('.current').length > 0) {
					this.slides.filter('.current:gt(0)').removeClass("current");
					this.current = this.slides.index( this.slides.filter(".current") );
				} else {
					$('.slide:eq(0)', this.elem).addClass("current");
					this.current = 0;
				}
				
				this.init_slides();
				this.init_nav();
			},
			
			animScaleOut: function(v, state, s){
				var e = s * v;
				var d = e - v;
				
				if (state >= 1) {
					return e;
				} else if ( state <= 0 ){
					return v;
				} else {
					return v + (d * state);
				}
			},
			
			animScaleIn: function(v, state, s){
				var e = s * v;
				var d = v - e;
				
				if (state >= 1) {
					return v;
				} else if ( state <= 0 ){
					return e;
				} else {
					return e + (d * state);
				}
			},
			
			applyProp: function(slide, prop) {
				$(slide).css(prop['helper']);
				$('.slide_wrapper',slide).css(prop['main']);
				$('.slide_wrapper .slide_image',slide).css(prop['childs']['slide_image']);
				$('.slide_wrapper .slide_content',slide).css(prop['childs']['slide_content']);
			},
			
			animProp: function(slide, prop, animSetting) {
				var p = $.extend(true, {}, prop);
				var a = $.extend(true, {}, animSetting);
				
				if ($(slide).is(".slide")) {
					$(slide).animate(p['helper'], a);
					this.animProp.call(this, $('.slide_wrapper',slide), p['main'], a);
					this.animProp.call(this, $('.slide_wrapper .slide_image',slide), p['childs']['slide_image'], a);
					this.animProp.call(this, $('.slide_wrapper .slide_content',slide), p['childs']['slide_content'], a);
				} else {
					$(slide).animate(p, a);
				}
			},
			
			init_slides: function() {
				for (var i = 0; i < this.current-1; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenLeft );
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				
				for (var i = this.current+1; i < this.slides.length; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenRight );
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				
				if (this.current - 1 >= 0) {
					this.applyProp.call(this, this.slides[this.current-1], this.zoomOutLeft );
					$(this.slides[this.current-1]).css({
						zIndex: 7500
					});
				}
				if (this.current + 1 < this.slides.length) {
					this.applyProp.call(this, this.slides[this.current+1], this.zoomOutRight );
					$(this.slides[this.current+1]).css({
						zIndex: 7500
					});
				}
				
				this.applyProp.call(this, this.slides[this.current], this.zoomIn );
				$(this.slides[this.current]).css({
					zIndex: 10000
				});
				
				for (var i = 0; i < this.slides.length; i++) {
					$.extend(this.slides[i], {
						animScaleOut: this.animScaleOut
					});
					$.extend(this.slides[i], {
						animScaleIn: this.animScaleIn
					});
				}
			},
			
			init_nav: function() {
				var self = this;
				this.nav = $(".nav", this.elem);
				this.navNext = $(".next", this.nav);
				this.navPrev = $(".prev", this.nav);
				
				$(this.navPrev).click(function(event){
					self.movePrev.apply(self,[]);
					$(self.elem).trigger("expCarousel", {
						current: self.current
					});
					this.blur();
					return false;
				});
				
				$(this.navNext).click(function(event){
					self.moveNext.apply(self,[]);
					$(self.elem).trigger("expCarousel", {
						current: self.current
					});
					this.blur();
					return false;
				});
			},
			
			init_nav_state: function() {
				if (this.current <=0 ) {
					$(this.navPrev).addClass("prev_disabled");
				} else {
					$(this.navPrev).removeClass("prev_disabled");
				}
				if (this.current >= this.slides.length-1 ) {
					$(this.navNext).addClass("next_disabled");
				} else {
					$(this.navNext).removeClass("next_disabled");
				}
			},
			
			moveNext: function(){
				if (this.current >= this.slides.length-1) {
					this.current = this.slides.length-1;
					return;
				}
				var c = this.current++;
				
				var n = c+1;
				for (var i=0; i <= n - 3; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenLeft);
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				for (var i=n+1; i <= this.slides.length-1; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenRight);
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				
				
				if (c - 1 >= 0) {
					this.animProp.call(this, this.slides[c-1], this.offScreenLeft, this.animOffScreenOut);
					
				}
				
				this.animProp.call(this, this.slides[c], this.zoomOutLeft, this.animOut);
				this.animProp.call(this, this.slides[c+1], this.zoomIn, this.animIn);
				
				if (c + 2 < this.slides.length) {
					//this.applyProp.call(this, this.slides[c+2], this.offScreenRight);
					this.animProp.call(this, this.slides[c+2], this.zoomOutRight, this.animOffScreenIn );
					
					for (var i=c+3; i <= this.slides.length-1; i++) {
						//this.applyProp.call(this, this.slides[i], this.offScreenRight);
					}
				}
				
				this.init_nav_state();
			},
			
			movePrev: function(){
				if (this.current <= 0) {
					this.current = 0;
					return;
				}
				var c = this.current--;
				
				var n = c-1;
				for (var i=0; i <= n - 1; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenLeft);
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				for (var i=n+3; i <= this.slides.length-1; i++) {
					this.applyProp.call(this, this.slides[i], this.offScreenRight);
					$(this.slides[i]).css({
						zIndex: 5000
					});
				}
				
				if (c + 1 < this.slides.length) {
					this.animProp.call(this, this.slides[c+1], this.offScreenRight, this.animOffScreenOut);
					for (var i=c+2; i <= this.slides.length; i++) {
						//this.applyProp.call(this, this.slides[i], this.offScreenRight);
					}
				}
				
				this.animProp.call(this, this.slides[c], this.zoomOutRight, this.animOut);
				this.animProp.call(this, this.slides[c-1], this.zoomIn, this.animIn);
				
				if (c - 2 >= 0) {
					//this.applyProp.call(this, this.slides[c-2], this.offScreenLeft);
					this.animProp.call(this, this.slides[c-2], this.zoomOutLeft, this.animOffScreenIn );
					
					for (var i=0; i <= c-3; i++) {
						//this.applyProp.call(this, this.slides[i], this.offScreenLeft);
					}
				}
				this.init_nav_state();
			},
			
			moveTo: function(newIdx) {
				
				if (newIdx <= 0) {
					newIdx = 0;
				}
				if (newIdx >= this.slides.length - 1) {
					newIdx = this.slides.length - 1;
				}
				
				var c = this.current;
				
				
				if ( newIdx > c ) {
					for (var i=newIdx; i > c; i--) {
						this.moveNext();
					}
				} else if ( newIdx < c ) {
					for (var i=newIdx; i < c; i++) {
						this.movePrev();
					}
				}
				
				
			},
			
			getCurrent: function() {
				return this.current;
			}
		}
	});
	
	
	$(document).ready(function(){
		$(".experiencesShow").each(function(){
			var el = $(this);
			el.expCarousel();
		});
	});
})(jQuery);