
(function($) {  // ENSURE PLUGIN WORKS IN CONFLICT MODE

// **********************
// **********************
// CAROUSEL
// **********************
$.fn.MAD_Carousel = function(o) {
	o = $.extend({
        paging: true,
		pagingPos: '',
		vertical: false,
		shown: 1,
        speed: 6000
    }, o || {});

	return this.each(function() {
		var $this = $(this);  // Declare as local variable
	
		var slideCount = $this.find('.window ul').children('li').length;
		
		if (slideCount <= 1) { // IF CAROUSEL ONLY CONTAINS 1 SLIDE HIDE CONTROLS
			$this.find('.control').hide();
			return
		}
		
		var slideWidth = $this.find('.window').find('li:first').outerWidth();
		var windowWidth = (slideWidth * o.shown);
		
		$this.find('.window').width(windowWidth);
		$this.find('.window ul').width((slideWidth * slideCount));
	
		var reelPosition = 0;
		
		if (o.paging) {  // IF PAGING APPEND ANCHORS TO PAGING DIV
			var pagingLinks = '';
			for (i = 1; i <= slideCount; i++) {
				pagingLinks += '<a href="#" title="" rel="' + (i-1) + '"><strong>' + i + '</strong></a>';
			}
			$this.find('.paging').append(pagingLinks);
			$this.find(".paging a:first").addClass("active");

			// PAGING CLICK
			$this.find('.paging a').click(function() {
				var targetPosition = (0 - ($(this).attr('rel') * slideWidth));
				reelPosition = targetPosition;  
					$this.find('.window ul').animate({
						left: reelPosition
					},500 );
				$this.find('.paging a').removeClass('active');
				$(this).addClass('active');
				disableControl()
				return false;
			});
		}
		
		// DISABLE PREVIOUS BUTTON
		$this.find('.prev').addClass('disabled');

		function disableControl(){
//			alert(reelPosition);
			if (reelPosition == 0) {
				$this.find('.prev').addClass('disabled');
				$this.find('.next').removeClass('disabled');
			}
			else if (reelPosition == (slideWidth - (slideWidth * slideCount))) {
				$this.find('.next').addClass('disabled');
				$this.find('.prev').removeClass('disabled');
			}
			else {
				$this.find('.control').removeClass('disabled');
			}
		}

		// PREVIOUS CLICK
		$this.find('.prev').click(function() {
			if (reelPosition < 0) {
				reelPosition = (reelPosition + slideWidth);  
				$this.find('.window ul').animate({
					left: reelPosition
				},500 );
				$this.find('.paging a.active').removeClass('active').prev().addClass('active');
			}
			disableControl()
			return false;
		});
		
		// NEXT CLICK
		$this.find('.next').click(function() {
			if (reelPosition > (slideWidth - (slideWidth * slideCount))) {
				reelPosition = (reelPosition - slideWidth);  
				$this.find('.window ul').animate({
					left: reelPosition
				},500 );
				$this.find('.paging a.active').removeClass('active').next().addClass('active');
			}
			disableControl()
			return false;
		});
	}); // END OF EACH
}



// **********************
// **********************
// MULTI LINK BANNER
// **********************
$.fn.MAD_MultiLinkBanner = function() {
	
	return this.each(function() {

		var $this = $(this);
		
		var clickArea = ($this.width() / $this.children('a').length);
		$this.children('a').css({width:clickArea});
	});
};
// END OF MULTILINK BANNER


// **********************
// **********************
// IMAGE FADER - BANNER ROTATOR
// **********************
$.fn.MAD_Slideshow = function(o) {
	o = $.extend({
        paging: true,
		transition: 'fade',
        speed: 6000
    }, o || {});

	return this.each(function() {

		var $this = $(this);  // Declare as local variable

		// CREATE PAGING
		var slideCount = $this.children('div').length;
		
		if (slideCount == 1) {
			return;	
		}
//		var clickArea = ($this.width() / $this.children('div').find('a').length);
//		$this.children('div').find('a').css({width:clickArea});
				
		if (o.paging) {
			var pagingLinks = '';
			for (i = 1; i <= slideCount; i++) {
				pagingLinks += '<a href="#" title="" rel="' + (i) + '">' + i + '</a>';
			}
			$this.parent().append('<div class="paging">' + pagingLinks + '</div>');
			$this.parent().find(".paging a:first").addClass("active");
		
			var active = $this.parent().find(".paging a:first");
			var play;
			
			// FADE TIMER
			function fadeImage(clear){
				if (clear) {
					$this.children('div').stop(true,true).fadeOut();
				}
				$this.children('div:eq(' + ((active.attr("rel"))-1) + ')').stop(true,true).fadeIn();

				play = setInterval(function() {
					$this.children('div:eq(' + ((active.attr("rel"))-1) + ')').stop(true,true).fadeOut();
					active = $this.parent().find(".paging a.active").removeClass('active').next();
					if ( active.length === 0) { // IF AT END RETURN TO FIRST
						active = $this.parent().find(".paging a:first");
					}
					active.addClass('active');
					$this.children('div:eq(' + ((active.attr("rel"))-1) + ')').stop(true,true).fadeIn();
				}, o.speed);

			};
					
			// CATCH CLICK EVENT
			$this.parent().find(".paging a").click(function() {
				active = $(this); //Activate the clicked paging
				$this.parent().find(".paging a").removeClass('active');
				$(this).addClass('active')
				clearInterval(play); //Stop the rotation
				fadeImage(true);
				return false;
			});
			
			// CATCH HOVER
			$this.children('div').hover(function() {
				clearInterval(play); //Stop the rotation
			}, function() {
				fadeImage(false);
			});	
		
		fadeImage(false);
		}
		else { // IF PAGING IS OFF SIMPLY LOOP THROUGH
			if (slideCount > 1) {
				$this.children('div:first-child').show()
				setInterval(function(){
				  $this.children('div:first-child').fadeOut()
					 .next('div').fadeIn()
					 .end().appendTo($this);
				}, o.speed);
			}
			else {
				$this.children('div').show();	
			}
		}
	});
};
// END OF FADER PLUGIN





})(jQuery); 

// LIGHTBOX
