(function($){
    
    $.fn.homeLoop = function(options){      
                
        var defaults = {
            ID : '',
            speed : ''
        }
        
        var options = $.extend(defaults, options);
        var base = this;
        var $parent = (this);
        var $children = $parent.children();
        var $length = $children.length;
        var $active = '';
        var $next = '';
        var $current = '';
        var paused = options.speed;
        
        $children.first().addClass('shown');
        
        this.nextImg = function() {
            $active = $('.shown', base);
            $next = $active.next();    
            $current = $active.index();
            
            if ( $current == $length - 1 ) {
                $children.first().addClass('shown');
            } else {
                $next.addClass('shown');
            };
            
            $active.removeClass('shown');
            letsGo();
        }
    
        function letsGo(){  
            interval = setTimeout(function(){
                base.nextImg();
            }, Math.floor((Math.random() * (paused / 2)) + (paused / 6) + 100));
        }

        letsGo();
        
        return this;                        
    }
        
})(jQuery);

function add_det(){
	if ($('#email').val()=="" || $('#email').val()=="Enter email address for updates" || $('#email').val()=="Please enter a valid email address"){
		err= ("Please enter a valid email address")
		$('#email').val(err);
		return false;
	} else {
		$.ajax({
			url: "/fbook/09/form.php?email="+escape($('#email').val()),
			cache: false,
			success: function(html){
				$("#hello").css({"background-image": "url('http://pipedownson.com/fbook/09/img/bgiii.jpg')"});
				$("#input1, #enter").hide();
				$("#thanks").show();
			}
		});
	}
};

$(document).ready(function(){
	
	$('.loop').each(function() {
		var ID = $(this).attr('id');
		var speed = $(this).attr('rel');
		var children = $('#'+ID).children(); 
		$('#'+ID).homeLoop({
			'speed': speed
		}); 
	});
	
	$(window).keypress(function(e) {
    	if(e.keyCode == 13) {
        	add_det();
    	}
	});

});
