$.fn.infiniteSlider = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('.slider-wrapper', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('.panels'),
            $items = $slider.find('.panel'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            

			$wrapper.after('<div class="sliderNav"></div>');

			$('.panel').each(function(i){
				$('.sliderNav').append('<a class="pageNav" href="#' + parseInt(i+1) + '">' + parseInt(i+1) + '</a>');
			});
			
			$('.sliderNav a:first').addClass('current');

        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<div class="panel empty" />', visible - ($items.length % visible)));
            $items = $slider.find('.panel');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('.panel'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
                $('.sliderNav a').removeClass('current');
                $('.sliderNav a:eq(' + parseInt(currentPage - 1) + ')').addClass('current');
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&laquo;</a><a class="arrow forward">&raquo;</a>');
        var hyperCurrentPage = $('.sliderNav .current').text();
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
        	var hyperCurrentPage = $('.sliderNav .current').text();
        	return gotoPage(parseInt(hyperCurrentPage) - 1);                
        });
        
              
        $('a.forward', this).click(function () {
        	var hyperCurrentPage = $('.sliderNav .current').text();
            return gotoPage(parseInt(parseInt(hyperCurrentPage) + 1));
        });
        
            
       	$('a.pageNav', this).click(function() {
        	return gotoPage($(this).text());
        });
        
        $('.more-toggle a').click(function(){
        	if($(this).hasClass('collapsed')) {
        		$('.infiniteSlider, .slider-wrapper').animate({
        			'height' : '1111px'
        		}, 300);
        		$('.more-wrapper').slideDown(300);
        		$('.more-toggle a').text('hide info');
        		$(this).removeClass('collapsed');
        	} else {
        		$('.infiniteSlider, .slider-wrapper').animate({
        			'height' : '600px'
        		}, 300);
        		$('.more-wrapper').slideUp(300);
        		$('.more-toggle a').text('more info');
        		$(this).addClass('collapsed');
        	};
        	
        	return false;
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
        
        
    });  
};

function tabSwitcher(){

	$('.top-item').each(function(){
        $(this).hide();
        $('.top-item:first').show();
        $('#main-nav-tabs a:first').addClass('active');
    });

    $('#main-nav-tabs a').click(function(){
        var targetTab = $(this).attr('href');
        $('.top-item').hide();
        $('#main-nav-tabs a').removeClass('active');
        $(this).addClass('active');
        $(targetTab).show();
        return false;
    });

    $('#memora .icon').hover(function(){
        $('#memora .icon').css({'opacity' : '0.5'});
        $(this).animate({'opacity' : '1'}, 200);
    }, function(){
        $('#memora .icon').css({'opacity' : '1'});
    });

};

function toasterInit(){
	
	

		
	$('.toaster-close').click(function(){
		toasterClose();
	});
	
	$('.toaster-toggle').click(function(){
		if($('#toaster').hasClass('open')) {
			$(this).addClass('toggle-closed');
			toasterClose();	
		} else {
			$(this).removeClass('toggle-closed');
			toasterOpen();
		};
	});
	
	
	
	function toasterClose() {
		$('#toaster').animate({
			'marginTop' : '-172px'
		}, 1000);
		
		$('#toaster').removeClass('open');
		return false;
	};
	
	function toasterOpen() {
		$('#toaster').animate({
			'marginTop' : '0'
		}, 1000);
		
		$('#toaster').addClass('open');
		return false;
	};
	
	setTimeout("toasterOpen()", 7000);
	
	
	
};

function toasterOpen() {
	$('#toaster').animate({
		'marginTop' : '0'
	}, 1000);
	
	$('#toaster').addClass('open');
	return false;
};

setTimeout("toasterOpen()", 5000);


function remoteIframeInit() {
	return false;
};

function quickThickbox(childSlide) {
	$('body').prepend('<div class="tb-bg"></div><div class="thickbox"><a class="close">close</a><div class="thickbox-inner"><img src="'+ childSlide +'"/></div></div>');
	
	var shadow = $('.tb-bg');
	var thickbox = $('.thickbox');

	var tbWidth = thickbox.outerWidth();
	var tbHeight = thickbox.outerHeight();
	
	thickbox.css({
		marginLeft : -tbWidth/2,
		marginTop : -tbHeight/2
	});
	
	$('a.close').click(function(){
		thickbox.fadeOut('fast', function(){
			thickbox.remove();
		});
		shadow.fadeOut('fast', function(){
			shadow.remove();
		});
	});
	
};

function localIframeInit() {
	var childSlide = window.location.hash.split("#")[1];
	
	if(childSlide !== undefined) {
		window.top.quickThickbox(childSlide);
	};
	
	return false;
};





// @author - Eli Horne
// @function - itemFader
// @brief - versatile fade/cycler

function itemFader(element, target, speed) {

	// set up nav items (styled externally)
	$(element).prepend('<div class="fader-nav"><a class="fader-back replaced" href="#">Previous Page</a><a class="fader-forward replaced" href="#">Next Page</a></div>');
	
	// hide the children
	$(element +' '+ target).hide();
	
	// show the first child
	$(element +' '+ target + ':first').show();
	
	//$('.fader-nav').hover(function(){
		//codaTip('.fader-nav');
	//});
					
	// if you click the back buttong				
	$('.fader-back').click(function(){
	
		// if we are on the first page
		if($(element +' '+ target + ':first').is(':visible')) {
			
			// dont do anything
			return false;		
										
		} else {
		
			// otherwise fade out the visible one
			$(element +' '+ target + ':visible').fadeOut(speed, function(){
			
				// find the previous image, fade it in
				$(this).prev().fadeIn(speed);
			});
			
		};
		
		// kill the click
		return false;
	});
	
	// if you click the forward button (same as above)	
	$('.fader-forward').click(function(){
		
		// if we are on the last page
		if($(element +' '+ target + ':last-child').is(':visible')) {
			
			return false;
		
		} else {
			$(element +' '+ target + ':visible').fadeOut(speed, function(){
				
				$(this).next().fadeIn(speed);
			
			});
			
		};
		
		return false;
		
	});

};


function codaTip(element) {	
	$(element).each(function(){
		activeEquation = $(this);
		activeTarget = activeEquation.find('a');
		linkText = activeTarget.text();
		activeHref = activeTarget.attr('href');
		clickWidth = activeEquation.innerWidth();
		clickHeight = activeEquation.innerHeight();
		$(activeEquation).prepend('<a href="'+ activeHref +'" class="click-details"><span class="lcap"></span><span class="mid"><span class="nipple">' + linkText + '</span></span><span class="rcap"></span></a>');
		activeClick = $(activeEquation).find('.click-details');
		
	});
	
	
	
	$(element).hover(function(){
		activeEquation = $(this);
		clickWidth = activeEquation.innerWidth();
		clickHeight = activeEquation.innerHeight();
		activeClick = $(activeEquation).find('.click-details');
					
		$(activeEquation).find('a.target').addClass('active');
		
		activeClick.animate({
			'display' : 'block',
			'bottom' : '65%',
			'opacity' : '1'
		}, 250);
		
	}, function() {
		
		$('a.target').removeClass('active');
		activeClick.stop().animate({
			'bottom' : '50%',
			'opacity' : '0'
		}, 100);
	
	});
};