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


	$("#PantheMation").pantheMation();
	

 });  //end jQuery(document).ready

    
 
 
(function($){  

    $.fn.pantheMation = function(PMoptions) {       

	var PMdefaults = {
		typePM : 		"slider",
		displayTime : 	7000,
		slideWidth : 	620,
		captionWidth : 	260
	}
	
	//assign current element to variable
	var obj = $(this);
	
	//merge default options with input options
	var PMoptions = $.extend(PMdefaults, PMoptions);
	
	//distibute options /not necessary but makes it cleaner down the road
	var typePM = PMoptions.typePM;
	var displayTime = PMoptions.displayTime;
	var slideWidth = PMoptions.slideWidth;
	var captionWidth = PMoptions.captionWidth;
	
	//assign slide and caption elements of current PantheMation element to varible
	var slides = $('.slide', obj);
	var caption = $('.caption', obj);
	var numberOfSlides = slides.length;
	var whereIsIt = 0;
	
	
	
	//check for the existence of class and change type accordingly
	if (this.hasClass("PM-fader")){typePM = "fader";}
	if (this.hasClass("PM-slider")){typePM = "slider";}

	// remove scrollbar in from frame
	//this is needed if  overflow is set to auto in CSS file
	$('#slidewrapper', obj).css('overflow', 'hidden');
  
	switch(typePM){
		case 'slider':
			// wrap all classes .slides with #slideInner div
			slides.wrapAll('<div id="slideInner"></div>')
			//and float left to display horizontally, readjust .slides width
			.css({
			'float' : 'left',
			'width' : slideWidth
			});
		
			// wrap all classes .caption with #captionInner div
			caption.wrapAll('<div id="captionInner"></div>')
			//and float left to display horizontally, readjust .slides width
			.css({
			'float' : 'left',
			'width' : captionWidth
			});

			// set #slideInner width equal to total width of all slides
			$('#slideInner').css('width', slideWidth * numberOfSlides);
			// set #captionInner width equal to total width of all caption
			$('#captionInner').css('width', captionWidth * numberOfSlides);
		
			break;
		
		case 'fader':
			//hide all slides and caption
			slides.css('display', 'none');
			caption.css('display', 'none');
			//display the first ones only
			slides.filter(":eq(0)").css('display', 'block');
			caption.filter(":eq(0)").css('display', 'block');
			break;
	}

	// hide left arrow control on first load
	manageControls(whereIsIt);

	// sets the interval for tha autoslide
	interval = setInterval(start_autoslide, displayTime);
		// the autoslide driver
	function start_autoslide(){
			// increment position or reset to 0
			whereIsIt+1 < slides.length ? whereIsIt++ : whereIsIt = 0;
			// do the animation
			switch(typePM){
				case 'slider':
					pantheMation_anim();
					break;
				case 'fader':
					pantheMation_fade();
					break;
			}
	}

  
	// create event listeners for .controls clicks
	$('.control',obj).bind('click', function(){
		// Determine new position
		whereIsIt = ($(this).attr('id')=='rightControl') ? whereIsIt+1 : whereIsIt-1;
		//if (whereIsIt < 0) {whereIsIt = 0};
		//if (whereIsIt > numberOfSlides-1) {whereIsIt = numberOfSlides-1};
		
		// in the event of a click on any of the control buttons reset the animation interval
		clearInterval(interval);
		interval = setInterval(start_autoslide, displayTime+3000);
		// do the animation
		switch(typePM){
			case 'slider':
				pantheMation_anim();
				break;
			case 'fader':
				pantheMation_fade();
				break;
		}
	});
    
    
	//the animation driver
	function pantheMation_anim(){
		// hide / show controls
		manageControls(whereIsIt);
		// Move slideInner using margin-left
		$('#slideInner').animate({'marginLeft' : slideWidth*(-whereIsIt)});
		// move captionInner using margin-left
		$('#captionInner').animate({'marginLeft' : captionWidth*(-whereIsIt)});
		
		//alert('slider: whereIsIt : '+whereIsIt)
	}
	
	//the animation driver
	function pantheMation_fade(){
		// hide / show controls
		manageControls(whereIsIt);
		
		//load the new slide/caption pair to be faded in
		new_slide = slides.filter(":eq("+whereIsIt+")");
		new_caption =  caption.filter(":eq("+whereIsIt+")");
		
		//if implemented for future developments / not necessarily required here
		if( new_slide.css("display") == "none" ){
			//fade out the visible pai and fade in the new pair
			slides.filter(":visible").fadeOut(1000, function(){	
				new_slide.fadeIn(1000);
			});
			caption.filter(":visible").fadeOut(400, function(){	
				new_caption.fadeIn(400);
			});
		}
	//	alert('slide: whereIsIt : '+whereIsIt)
	}

	// manageControls: hides and shows controls depending on whereIsIt
	function manageControls(position){
		// hide left arrow if position is first slide
		if(position==0){
			$('#leftControl',obj).hide()
		}else{
			$('#leftControl',obj).show()
		}
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1){
			$('#rightControl',obj).hide()
		}else{
			$('#rightControl',obj).show()
		}
	}
	} //close .pantheMation

})(jQuery);  
