$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('div.slider'),
            $items = $slider.find('div.page'),
            $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);            

		
		
		
		$single.addClass("firstPage");
		
		// pagging
		
		$wrapper.after("<div class=\"paging\"></div>");
		var $paging = $("div.paging",this);
		var paging = [];
		
		for(i=1; i<=pages; i++){
			paging.push(
				"<a class=\"pagingPage\" rel=\"",
				i,
				"\">",
				i,
				"</a>"
			);
		}
		
		$paging.append(paging.join(""));
        $("div.paging a:first",this).addClass("selected");
		$("a.pagingPage",this).click(function(){
			var $element = $(this);
			return gotoPage($element.attr("rel"));
		});

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

        // 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('div.page'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $slider.css({"left":-(singleWidth * visible)+"px"});
		
        // 4. paging function
        function gotoPage(page) {
			
           var dir = -1,
			left = (singleWidth * page * dir) + "px";
			
			$slider.filter(":not(:animated)").animate({
				"left": left
			}, 500, function () {
			  
			  	if (page == 0) {
                    $slider.css({"left":-(singleWidth * visible * pages)+"px"});
                    page = pages;
                } else if (page > pages) {
                    $slider.css({"left":-(singleWidth * visible)+"px"});
                    // reset back to start position
                    page = 1;
                } 
			  
			  	// upate selected paging
				$("a:eq("+(currentPage-1)+")",$paging).removeClass("selected");
				$("a:eq("+(page-1)+")",$paging).addClass("selected");
				
				currentPage = page;
			  
			});
			
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
		
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(eval(currentPage) - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(eval(currentPage) + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};


function openOverlay(element){
	$("#blackout").show().click(function(){
	 	closeOverlay(element);
	 });
	var $element = $(element);
	$element.prepend("<div id=\"closeOverlay\"></div>");
	$("#closeOverlay").click(function(){
		closeOverlay(element);
	});
	
	$element.fadeIn(800);
	
}

function closeOverlay(element){
	
	$(element).fadeOut(600,function(){
		$("#blackout").unbind("click").hide();
		$("#closeOverlay").remove();
		$("#backToTop").hide();
	});
}


$(function() { 
    
	$('div.carousel').infiniteCarousel();
	
	//$("div.links img[rel]").overlay({effect: 'apple',expose: '#fff'});
	$("div.links img[rel]").click(function(){
   
   		openOverlay($(this).attr("rel"));
		
   
   });
 	setInterval('swapImages()', 5000);
	if($("#tweets").length > 0){
	
	$("#tweets").tweet({
		username: "meacher",
		favorites: true,
		avatar_size: 32,
		count: 10000,
		loading_text: "loading tweets..."
	});
	
	}
	$('#backToTop').click(function(e){
		e.preventDefault();
        $('html, body').animate({scrollTop:0}, 'slow',function(){
	   $('#backToTop').hide();
	   });
		

    });
	
	$(window).scroll(function(){
		if($('body').scrollTop() > 0){
			$('#backToTop').show();	
		}
		else {
			$('backToTop').hide();	
		}
	});

});

function swapImages(){
	$('.Gallery img').hide();
  	var $active = $('.Gallery .active');
  	var $next = ($('.Gallery .active').next().length > 0) ? $('.Gallery .active').next() : $('.Gallery img:first');
  $active.fadeOut(function(){
    $active.removeClass('active');	
    $next.fadeIn().addClass('active');
  });
}

function validate_form(thisform)
{
with (thisform)
  {
	if(Name.value == "" || Name.value == null)
	{
		alert("Please enter your name");
		Name.focus;
		return false;
	}  
	if(Email.value == "" || Email.value == null)
	{
		alert("Please enter your email");
		Email.focus;
		return false;
	}
	
	if((Email.value.indexOf(".") < 2) || (Email.value.indexOf("@") < 1))
	{
		alert("Email " + Email.value + " has an incorrect format");
		Email.focus();
		return false;
	}
	if(Message.value == "" || Message.value == null)
	{
		alert("Please enter a message");
		Message.focus;
		return false;
	}	

}
}
