// PLhome.js - A jQuery Plugin
// Version 1.0.1
// Written by Andy Tennison

// creates JS tabbed content

(function($) {
	$.fn.PLhome = function(options) {
		var opts = $.extend({}, $.fn.PLhome.defaults, options);
		return this.each(function() {
			$('body').append('<div id="temp"></div>')
			$('#finder').append('<div id="miniLB"><span><a href=""></a></span></div>')
			
			// heroImage vars
			var $h = $('#heroImage');
			var $hLink = $h.find('a');
			//var $hIMG = $h.find('img');
			
			// hero description vars
			var $d = $('#heroDescription');
			var $dNO = $d.find('.pagination span');
			var $dTITLE = $d.find(opts.title);
			var $dDESC = $d.find('div.summary');
			var $dLINK = $d.find('h2 a.link');
			
			// carousel vars
			var $c = $(this);
			var $cLIST = $(this).find('ul');
			var cLength = $cLIST.children('li').length-1;
			var liWidth = $c.find('li').width();
			var cINDEX = 0;
			
			var thumb = [];
			
			
			$cLIST.css('width',($c.find('li').length * liWidth));		// sets UL width
			var maxOffset = $cLIST.width() - $c.width();
			//if (maxOffset <= 0) maxOffset = 0;
			var curOffset = 0;
			
			if (opts.prevNext){
				$c.before('<a id="thumb_prev" href="#">Previous thumbnails</a><a id="thumb_next" href="#">Next thumbnail</a>') // insert previous + next buttons if needed
			}
			$cLIST.find('li').eq(cINDEX).addClass('on');
			
			
			// previous button action
			$('#thumb_prev').click(function(e){
				e.preventDefault();
				
				if (maxOffset > 0){
					curOffset  = curOffset - liWidth*opts.liCount;
					if (curOffset <= 0 ){curOffset = 0;}
					$cLIST.animate({'left': -curOffset},200);
				}
				
				if(cINDEX == 0){
					var $this = $cLIST.find('li').eq(cLength);
					thumbClick($this);
				}
				else if(cINDEX > 0){
					var $this = $cLIST.find('li').eq(cINDEX-1);
					thumbClick($this);
				}
				
			});
			
			// next button action
			$('#thumb_next').click(function(e){
				e.preventDefault();
	            
				if (maxOffset > 0){
					curOffset  = curOffset + liWidth*opts.liCount;
					if(curOffset >= maxOffset){curOffset = maxOffset;}
					$cLIST.animate({'left': -curOffset},200)
				};
				
				if(cINDEX == cLength){
					var $this = $cLIST.find('li').eq(0);
					thumbClick($this);
				}
                else if(cINDEX <= cLength-1){
					var $this = $cLIST.find('li').eq(cINDEX+1);
					thumbClick($this);
				}
				
			});
			
			
			
			// show thumbnails
			var thumbContainerOffset = $('#thumbContainer').css('bottom');
			$('#thumbContainer').css('opacity',0.95)
			$h.find('a').bind('mouseenter focus', function(){
				if($('#thumbContainer').css('bottom') != '0px'){
					$('#thumbContainer').animate({'bottom':0},200)
				}
			})
			// hide thumbnails
			$('#header, #branding, #heroDescription, #container').bind('mouseenter',function(){
				if($('#thumbContainer').css('bottom') == '0px'){
					$('#thumbContainer').animate({'bottom':thumbContainerOffset},200)			
				};
			});
			
			
			
			
			$cLIST.find('li a').bind('mouseenter', function(e){	// li - mouse enter
        		$(this).parent('li').addClass("hover");
				var p = $(this).parent('li').offset();
				//var t = ($('#heroImage').offset()).top;
				$('#miniLB a').text($(this).find('img').attr('alt'))
				$('#miniLB a').attr('href', $(this).attr('href'))
				$('#miniLB').css({'left':p.left, 'bottom':35, 'display':'block'});				
    		}).bind('mouseleave', function(e){					// li - mouse leave
        		$(this).parent('li').removeClass("hover");
			}).bind('click', function(e){
				e.preventDefault();
				var $this = $(this).parent('li');
				thumbClick($this);
				
				if (opts.autoPlay){ 
					stopCount();
				}
			});
			
			$('#header, #branding, #heroDescription, #heroImage img, #thumb_next, #thumb_prev').bind('mouseenter', function(){
				$('#miniLB').css({'display':'none'})
			})
			
			
			
			setState(cINDEX)
			function setState(i){
				/*if(i <= 0){
					$('#thumb_prev').addClass('off')
					$('#thumb_next').removeClass('off')
				} else if (i >= cLength-1) {
					$('#thumb_prev').removeClass('off')
					$('#thumb_next').addClass('off')
				} else {
					$('#thumb_prev, #thumb_next').removeClass('off')
				}*/
			};
			
			function thumbClick($this){
			    
			    var tHREF = $this.find('a').attr('href');
			    var nTITLE = $this.find('a').text();
			    var nDESC = $this.find('img').attr('alt');
			    var nIMG = $this.find('img');
			    
				$h.prepend('<div class="loader"></div>');
				$h.find('.loader').animate({'opacity':0.7}, 200);
				
                $h.find('.loader').animate({'opacity':0}, 200, function(){
                    $(this).remove();
                });			    

                if(nIMG.attr('src') == ''){
                    nIMG.attr('src', '/media/images/homeCarousel_missing.jpg');
                }
                
                $dTITLE.empty().text(nTITLE);									// swap title
                $dDESC.empty().html('<p>'+nDESC+'</p>')		// description text
                $dLINK.attr('href',tHREF);							// swap project link HREF
                $hLink.attr('href',tHREF);										// swap image HREF
                $h.find('img').addClass('first');							// add class 'first' to current image
                var newIMG = nIMG.clone();
                newIMG.attr('style','display:block;');
                $h.find('a').prepend(newIMG);										// load new image
                $h.find('img.first').animate({'opacity':0},350, function(){		// ## animation on image
                    $(this).remove();
                });
                
                $cLIST.find('li').eq(cINDEX).removeClass('on');		// currant Index - remove 'on' class
                var tINDEX = $cLIST.find('li').index($this);		// work out the index of this item
                $cLIST.find('li').eq(tINDEX).addClass('on');		// add class to this item
                $dNO.text(tINDEX + 1);									// update - case study 'x' of 'x'
                cINDEX = tINDEX;									// set the current index to this index
                $('#temp').empty();									// empty the temporary html holder
                setState(cINDEX)
			}
			
			
// auto play onLoad
			if (opts.autoPlay){ 
				initialDelay();
			}
			var c = 0;

// initial gallery delay
			function initialDelay(){ 
				c++;
				t=setTimeout(timedCount,opts.autoPlaySpeed);
			}

// delay between slides
			function timedCount(){ 
				c++;
				
				if (c >= $cLIST.find('li').length) c=0;
				
				var $this = $cLIST.find('li').eq(c);
				thumbClick($this);
				
				t=setTimeout(
					function(){timedCount()},
					opts.autoPlaySpeed
				);
			}
			
			function stopCount(){
				clearTimeout(t);
			}
			
			
			
			
			
		});


		
	}; // end plugin PLhome


// end of closure
})(jQuery);

$.fn.PLhome.defaults = {
	liCount: 1,
	title: 'h1',
	prevNext: true,
	autoPlay: true,
	autoPlaySpeed: 3000
};
