(function($) {
	$(function(){
		//Elements
		var el = {};
		el.gallery = $('.content_image');
		el.carousel = el.gallery.find('.carousel');
		el.gallThumb = el.gallery.find('div.carousel ul li a');
		el.galleryLinks = el.gallery.find('.galleryNavLinks');
		el.ppButton = el.galleryLinks.find('a.play_slides').eq(0);
		el.prevImage = el.galleryLinks.find('a.prev_image').eq(0);
		el.nextImage = el.galleryLinks.find('a.next_image').eq(0);
		el.prevSet = el.galleryLinks.find('a._previous').eq(0);
		el.nextSet = el.galleryLinks.find('a._next').eq(0);
		
		/*-- variables --*/
		var playing = playing || true;
		var imgCount = el.gallThumb.length;
		
		/*--------------------------------------------*/
		// Carousel setup
		$('.content_image .carousel').carousel({ slideTransition:true,slideSpeed: 750, loop: false, nextPrevContainer: el.galleryLinks });
		
		/*--------Functions----------*/
		//click events
		el.ppButton.click(function playPause (){
			playToggle.toggle();
			return false;
		});
		//Thumbnail Click
 		el.gallThumb.mousedown(function() {
			playToggle.pause();
        });
        el.gallThumb.click(function (event) {
            
            var $this = $(this);
            var image = $this.attr('href');
			var box = $this.parents('.carousel').eq(0).find('.largeImage img');
            // perform some actions the first time the anonymous click responder is called
            if (!el.gallery.data('gallThumbInit')) {
                el.gallery.data('gallThumbInit', true);
 
                box.load(function() {
                    box.fadeIn('fast');
             	});
             }
 
            box.fadeOut('fast', function () {
                    box.attr('src', image);
            });
           
            i = el.gallThumb.index(this);
            el.gallThumb.removeClass('selected');
            
            $this.addClass('selected');          
            return false;
        });
		el.nextImage.click(function (){
			clickThumb();
			playToggle.pause();
			return false;
		});
		el.prevImage.click(function (){
			if (i >= 1) {
				el.gallThumb[--i].click();
			}
			playToggle.pause();
			return false;
		});
		// Simulate Click based on an interval
		var clickThumb = function(){
			if (i >= imgCount -1) {
				// Reset the rotation
				i= -1;
				//TODO: SEND ALL THE WAY BACK NOT JUST PREVIOUS
				el.prevSet.click();
			}else if(i>0 && i%12==0){
				el.nextSet.click();
			}
			el.gallThumb[++i].click();
		};
		// Start on second image, we've already seen the first

		var i = 0,
		thumbTimer = setInterval(clickThumb,4000);
		// play/pause the slide show

		var playToggle = {
			pause : function () {
				el.ppButton.addClass('paused');
				playing = false;
				clearInterval(thumbTimer);
			},
			play : function () {
				el.gallThumb[++i].click();
				el.ppButton.removeClass('paused');
				playing = true;
				thumbTimer = setInterval(clickThumb,4000);
			},
			toggle : function () {
				if (playing == true) {
					playToggle.pause();
				} else if(playing == false) {
					playToggle.play();
				}
			}
		};
	});
})(jQuery);