/*!
* jquery.slideShow. jQuery Slideshow plugin
*
* Copyright (c) 2009 Craig Thompson
* http://craigsworks.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*/

"use strict"; // Enable ECMAScript "strict" operation for this function. See more: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
/*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
/*global window: false, jQuery: false */
(function($) {
	function SlideShow(target) {
		var self = this,

		/* Current image index */
		current = 0,

		/* Sldieshow timer store */
		timer = null,

		/* Determines speed of slideshow in milliseconds */
		speed = 7000,

		/* Determines duration of fade in milliseconds - ALWAYS LESS THAN THE ABOVE!!! */
		duration = 600;

		// Global target and slideshow references
		this.slideshow = $('.relative.play', target);
		this.nav = $('.nav', target);
		this.desc = $('.description', target);

		$.extend(self, {
			'init': function() {
				// Setup nav
				self.nav.empty();
				var i;
				for(i = 0; i < $('.slide', target).length; i++) {
					$('<li />', {
						'class': !i ? 'active' : '',
						'html': '&bull;',
						'mousedown': false,
						'click': function() {
							self.stop();
							self.goto( $(this).index() );
							self.start();
							
							return false;
						}
					})
					.appendTo(this.nav);
				}
				
				// Setup mousewheel (if available)
				self.slideshow.add(self.desc)
					.bind('mousewheel', function(event, delta) {
						self[ delta < 0 ? 'next': 'prev' ]();
						
						return false;
					})
					
					// Stop the slideshow on mouseover
					.bind('mouseenter mouseleave', function(event) {
						self[ event.type === 'mouseleave' ? 'start': 'stop' ]();
					})

				self.goto(0);
				self.start();
			},
			
			'goto': function(index) {
				if($(".relative").hasClass("play")){
					var slides = $('.slide', target),
						slide = slides.eq(index),
						s = !!self.timer;

					self.stop();

					self.slideshow.stop(0,0).animate({
						'margin-left': -( slide.position().left ),
						'easing': 'swing',
						'duration': duration
					});

					$('li', self.nav).removeClass('active')
						.eq(slide.hasClass('clone') ? index - slides.length - (slides.length - 2) : index)
						.addClass('active');
					
					self.nav.detach();
					self.desc.empty();
					self.desc.html( slide.html() ).prepend(self.nav);
					
					if(s) { self.start(); }
				}
			},
			
			'next': function() {
				if($(".relative").hasClass("play")){
					var over = current >= $('.slide', target).length - 1;

					if(over) {
						$('.slide:first', target).clone().addClass('clone')
							.appendTo(self.slideshow);
					}

					self.goto(++current);

					if(over) {
						self.slideshow.queue(function() {
							$('.slide.clone', target).remove();
							self.slideshow.css({ 'margin-left': 0 });
							current = 0;

							$(this).dequeue();
						})
					}
				}
			},
			
			'prev': function() {
				if(current <= 0) {
					current = $('.slide', target).length;
				}

				self.goto(--current);
			},
			
			'start': function(state) {
				if(state !== false) {
					(function interval(wait) {
						if(timer && wait !== true) { self.next(); }
						clearTimeout(timer);
						timer = setTimeout(interval, speed);
					}(true));
				}
				else {
					clearTimeout(timer);
				}

				// Toggle playing class
				target.toggleClass('playing', state !== false);
			},

			'stop': function(){ self.start(false); }
		});

		self.init();
	}

	$.fn.slideShow = function() {
		return $(this).each(function() {
			var obj = new SlideShow( $(this) );
			$(this).data('slideShow', obj);
		});
	};
}(jQuery));

function startSlideshow(){
	$(".relative").addClass("play");
}

function stopSlideshow(){
	$(".relative").removeClass("play");
}

// Andy Langton's show/hide/mini-accordion - updated 23/11/2009
// Latest version @ http://andylangton.co.uk/jquery-show-hide
 
// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {
 
// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='Show More';
var hideText='Hide';
 
// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.additional-content-container').next().append(' (<a href="#" class="toggleLink">'+showText+'</a>)');
 
// hide all of the elements with a class of 'toggle'
$('.additional-content-container').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;
 
// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);
 
// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().prev('.additional-content-container').slideToggle('slow');
 
// jump to...
$.scrollTo( '#' + $(this).parent().attr('class') );
 
// return false so any link destination is not followed
return false;
 
});
});
