/*
# ---------------------------------------------------- #
#  Yellsites Plugins for jQuery.                       #
#   - Developed by Tristan Brehaut for Yell Global.    #
#   - Supporting xHTML developed by Brendan Meachen.   #
#                                                      #
#  Version 1.04 (Dancing Doctorfish) - 08/11/2011      #
# ---------------------------------------------------- #

# ---------------------------------------------------- #
#  Slideshow Plugin for jQuery.                        #
#   - Developed by Tristan Brehaut for Yell Global.    #
# ---------------------------------------------------- #
*/
(function( $ ){
	
	$.fn.slideshow = function( options ){

		var	slideshowIntervalID,
			slideshowTimeoutIntervalID;
		
		var settings = {
			target : this,
			slide : '> li',
			width : parseInt($('li',this).width()),
			height : parseInt($('li',this).height()),
			speed : 1800,
			interval : 5000,
			easing : 'swing',
			autoplay : true,
			timeout : 15000,
			transition : 'slide',
			direction : { x: 1, y: 0 }
		};
		
		if( options ){
			$.extend( settings, options );
		}

		var _el = settings.target;
		
		var methods = {
			initialise : function( settings ){

				_el.wrap( '<div class="slideshowWrapper"></div>' ).css( { width: settings.width, height: settings.height } );
				_el.wrapper = _el.parent( '.slideshowWrapper' );
				
				_el.currentSlide = 0;
				_el.nextSlide = 1;
				_el.transition = false;
				
				_el.slides = $( settings.slide, _el );
				_el.slides.count = _el.slides.length-1;
				
				_el.slides.each(
					function( index ){
						startLeft = ( index > 0 && settings.transition == 'slide' ) ? settings.width : 0;
						startOpacity = ( index > 0 && settings.transition == 'fade' ) ? 0 : 1;
						$(this)
							.addClass( 'slide' )
							.css( { position: 'absolute', top: 0, left: startLeft, opacity: startOpacity, width: settings.width, height: settings.height } )
							.attr( 'rel', index );
					}
				);
				
				_el.after( '<div class="btnSlideshowPrev"></div><div class="btnSlideshowNext"></div>' );
				_el.navigation = { btnNext: _el.nextAll( '.btnSlideshowNext' ), btnPrev: _el.nextAll( '.btnSlideshowPrev' ) };
				_el.navigation.btnNext.click(
					function(){
						if( _el.play ){
							methods.timeout();
						}
						if( !_el.transition  ){
							_el.direction = { x: settings.direction.x, y: settings.direction.y };
							methods.slide();
						}
					}
				);
			
				_el.navigation.btnPrev.click(
					function(){
						if( _el.play ){
							methods.timeout();
						}
						if( !_el.transition ){
							_el.direction = { x: -settings.direction.x, y: -settings.direction.y };
							methods.slide();
						}
					}
				);

				_el.wrapper.hover(
					function(){
						_el.navigation.btnNext.stop().animate({ opacity: 1 }, { queue: false, duration: 500 });
						_el.navigation.btnPrev.stop().animate({ opacity: 1 }, { queue: false, duration: 500 });
					},
					function(){
						_el.navigation.btnNext.stop().animate({ opacity: 0 }, { queue: false, duration: 500 });
						_el.navigation.btnPrev.stop().animate({ opacity: 0 }, { queue: false, duration: 500 });
					}
				);
				
				if( settings.autoplay ){
					methods.autoplay();
				}
				
			},
			
			slide : function(){
				_el.transition = true;
				
				if( _el.direction.x > 0 || _el.direction.y > 0 ) {
					_el.nextSlide = ( _el.currentSlide == _el.slides.count ) ? 0 : _el.currentSlide + 1;
				} else {
					_el.nextSlide = ( _el.currentSlide == 0 ) ? _el.slides.count : _el.currentSlide - 1;
				}
				
				switch( settings.transition ){
					case 'fade':
						$( _el.slides[_el.nextSlide] )
							.stop()
							.animate(
								{
									opacity: 1
								},
								{
									queue: false,
									duration: settings.speed,
									complete: function(){ _el.transition = false; },
									easing: settings.easing
								}							
							);
						
						$( _el.slides[_el.currentSlide] )
							.stop()
							.animate(
								{
									opacity: 0
								},
								{
									queue: false,
									duration: settings.speed,
									complete: function(){ _el.transition = false; },
									easing: settings.easing
								}
							);
						break;
					default:
						var left = settings.width * _el.direction.x;
						var top = settings.height * _el.direction.y;
						
						$( _el.slides[_el.currentSlide] ).animate(
							{
								left: -left,
								top: -top
							},
							{
								queue: false,
								duration: settings.speed,
								easing: settings.easing
							}
						);

						$( _el.slides[_el.nextSlide] ).css(
							{
								left: left,
								top: top
							}
						).animate(
							{
								left: 0,
								top: 0
							},
							{
								queue: false,
								duration: settings.speed,
								complete: function(){ _el.transition = false; },
								easing: settings.easing
							}
						);

						break;
				}

				_el.currentSlide = _el.nextSlide;

			},
			
			autoplay : function(){
				_el.play = true;
				_el.direction = { x: settings.direction.x, y: settings.direction.y };
				slideshowIntervalID = setInterval( methods.slide, settings.interval );
			},
			
			timeout : function(){
				clearInterval( slideshowIntervalID );
				_el.play = false;
				if( settings.autoplay ){
					clearInterval( slideshowTimeoutIntervalID );
					slideshowTimeoutIntervalID = setTimeout( methods.autoplay, settings.timeout );
				}
			}
		};
		
		methods.initialise( settings );
		
		return this;
	};
})( jQuery );

/*
# ---------------------------------------------------- #
#  Lightbox Plugin for jQuery.                         #
#   - Developed by Tristan Brehaut for Yell Global.    #
# ---------------------------------------------------- #
*/
(function( $ ){

	$.fn.lightbox = function( options ){
		
		var	dimTheLightsHTML = '<div id="dimTheLights"></div>',
			lightBoxHTML = '<div id="lightBox"><div id="lightBoxIMGWrap"></div><div id="lightBoxLoading"></div><div id="lightBoxCaption"></div><div id="lightBoxBtnClose"></div><div id="lightBoxBackward"></div><div id="lightBoxForward"></div></div>';
		
		var settings = {
			target : this,
			imgwrap : 'a',
			captiontarget : 'p.caption',
			padding : 16
		};
		
		if( options ) {
			$.extend( settings, options );
		}
		
		var methods = {
			initialise : function( settings ){
				
				$('body').append( dimTheLightsHTML, lightBoxHTML );
				
				var _el = settings.target;

				_el.current = 0;
				_el.visible = false;
				_el.array = [];
				_el.images = $(settings.imgwrap, _el);
				_el.count = _el.images.length;
				_el.navigation = { btnNext: $('#lightBoxForward'), btnPrev: $('#lightBoxBackward'), btnClose: $('#lightBoxBtnClose') };
				
				_el.images.each(
					function( index ){
						var caption = $(this).children( settings.captiontarget ).html() || '';
						_el.array[index] = [$(this).prop('href'), caption];
						$(this).attr( 'rel', index );
					}
				).click(
					function(){
						$('#dimTheLights').css( { height: $(document).height(), width: $(window).width(), opacity: '0.8' } );
						$('#lightBox').css( { width: 200, height: 200 } ).center();
						_el.current = parseInt( $(this).prop( 'rel' ) );
						methods.loadImage( _el );
						return false;
					}
				);

				_el.navigation.btnNext.click(
					function(){
						_el.current += 1;
						if( _el.current > _el.count-1 ){
							_el.current = 0;
						}
						methods.loadImage( _el );
					}
				);
				
				_el.navigation.btnPrev.click(
					function(){
						_el.current -= 1;
						if( _el.current < 0 ){
							_el.current = _el.count-1;
						}
						methods.loadImage( _el );
					}
				);
				
				$('#dimTheLights, #lightBoxBtnClose').click(
					function(){
						$('#dimTheLights, #lightBox, #lightBoxLoading').fadeOut( 'fast' );
						_el.visible = false;
						return false;
					}
				);
				
				$('#lightBox').click(
					function(){
						return false;
					}
				).bind(
					'contextmenu',
					function(){
						$('#dimTheLights, #lightBox, #lightBoxLoading').fadeOut( 'fast' );
						return false;
					}
				);
				
				$(window).bind(
					'scroll resize',
					function(){
						if( _el.visible ){
							$('#dimTheLights').css( { height: $(document).height(), width: $(window).width() } );
							$('#lightBox').center();
						}
					}
				).bind(
					'keyup',
					function(e){
						if( _el.visible ){
							var key = e.keyCode;
							switch( key ){
								case 37: _el.navigation.btnPrev.trigger('click'); break;
								case 39: _el.navigation.btnNext.trigger('click'); break;
								case 27: _el.navigation.btnClose.trigger('click'); break;
								default: break;
							}
						}
					}
				);			
			},
			
			loadImage : function( _el ){
			
				$('#dimTheLights, #lightBox').fadeIn( 'slow' );
				$('#lightBoxLoading').css( { display: 'block' } );
				$('#lightBoxIMGWrap').html( '<img src=' + _el.array[_el.current][0] + ' id="lightBoxIMG" />');
				$('#lightBoxCaption').html( _el.array[_el.current][1] );
				$('#lightBoxIMG, #lightBoxCaption, #lightBoxBtnClose').css( { display: 'none' } );

				_el.visible = true;
				
				$('#lightBoxIMG').load(
					function(){

						var imgWidth = $(this).width();
						var imgHeight = $(this).height();
					
						$('#lightBoxLoading').css( { display: 'none' } );
												
						if( imgWidth === 0 ) {
							methods.loadImage( _el.current );
						}
						
						$('#lightBoxCaption').css( { width: imgWidth } );
						var captionHeight = $('#lightBoxCaption').height();
						
						if( captionHeight >= 1 ) { 
							captionHeight = captionHeight + ( settings.padding * 2 );
						} else {
							captionHeight = settings.padding;
						}
						
						if( _el.count <= 1 ){
							$('#lightBoxBackward, #lightBoxForward').css( { display: 'none' } );
						} else {
							$('#lightBoxBackward, #lightBoxForward').css( { height: imgHeight } );
						}
						
						var lbTop = (($(window).height() - ( imgHeight + captionHeight )) / 2) + $(window).scrollTop() - 10;
						var lbLeft = ($(window).width() - imgWidth) / 2;

						$('#lightBox').animate( {
								top: lbTop,
								left: lbLeft,
								width: imgWidth,
								height: imgHeight + captionHeight
							}, {
								queue: false,
								duration: 'fast',
								complete: function(){
									$('#lightBoxIMG, #lightBoxCaption, #lightBoxBtnClose').fadeIn( 'fast' );
									_el.visible = true;
								}
							}
						);
					}
				);
			}
		};
		
		methods.initialise( settings );
	
		return this;
	};
})( jQuery );

/*
# ---------------------------------------------------- #
#  Center Plugin for jQuery.                           #
#   - Developed by Tristan Brehaut for Yell Global.    #
# ---------------------------------------------------- #
*/
(function ($) {
	$.fn.center = function () {
		this.css({
			position: 'absolute',
			top: (($(window).height() - this.height()) / 2) + $(window).scrollTop()-10,
			left: (($(window).width() - this.width()) / 2)
		});
		return this;
	};
})( jQuery );
