jQuery.fn.slideshow = function(options) {
	var settings = {
		timeout: '2000',
		type: 'sequence',
		pauselink: null,
		playcallback: null,
		pausecallback: null
	}
	if(options)
		jQuery.extend(settings, options);
	
	var pauseState = 0;
	var current = 1;
	var last = 0;
	var timer = '';
	
	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0');
			jQuery(slides[current]).css('zIndex', '1').fadeIn('slow');
			
			if ( settings.type == 'sequence' ) {
				if ( ( current + 1 ) < slides.length ) {
					current = current + 1;
					last = current - 1;
				}
				else {
					current = 0;
					last = slides.length - 1;
				}
			}
			else if ( settings.type == 'random' ) {
				last = current;
				while (	current == last ) {
					current = Math.floor ( Math.random ( ) * ( slides.length ) );
				}
			}
			else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			timer = setTimeout(change, settings.timeout);
		}
	}
	
	var pause = function() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
			if ( settings.playcallback != null ) {
				settings.pausecallback(jQuery('#' + settings.pauselink));
			}
		}
		else {
			pauseState = 0;
			change();
			if ( settings.playcallback != null ) {
				settings.playcallback(jQuery('#' + settings.pauselink));
			}
		}
		return false;
	}
	
	var slides = this.find('img').get();
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
	if ( settings.type == 'sequence' ) {
		timer = setTimeout(change, settings.timeout);
	}
	else if ( settings.type == 'random' ) {
		do { current = Math.floor ( Math.random ( ) * ( slides.length ) ); } while ( current == 0 )
		timer = setTimeout(change, settings.timeout);
	}
	else {
		alert('type must either be \'sequence\' or \'random\'');
	}
	
	if ( settings.pauselink != null ) {
		jQuery('#' + settings.pauselink).click(pause);
	}
	
	return this;
};

//Start slideshow
$(function(){

    if ($(".hp-animate-bg").length) {
        var $aniContainer = $(".hp-animate-bg");
        
        //append slideshow
        var slideShow = $("<div id='slideshow'></div>").appendTo("body");
        
        //locale specific images
        var arrBGImages = [];
		arrBGImages = [
			"http://static.eharmony.com/assets/corp/609/bg-joshmonica-loft.jpg",
			"http://static.eharmony.com/assets/corp/609/bg-jennifer.jpg",
			"http://static.eharmony.com/assets/corp/609/bg-joshuatanyalee-pool.jpg",
			"http://static.eharmony.com/assets/corp/609/bg-catherine-leaning.jpg"
		];
        
        //append to slideshow
        $(arrBGImages).each(function(i,src){
            $("<img>").attr("src",src).appendTo("#slideshow");
        });
        
        //z-index fixes
        $("#testcontainer").css("z-index",10);
        $("#slideshow").css("z-index",0);
        
        function positionSlideshow(){
            $("#slideshow").css({
                'top': $aniContainer.offset().top,
                'left': $aniContainer.offset().left,
                'width': $aniContainer.outerWidth(),
                'height': $aniContainer.outerHeight(),
                'position':'absolute',
                'display':'block',
                'overflow':'hidden'
            });
        }
        
        positionSlideshow();
        
        //position slideshow behind page
        setInterval(positionSlideshow,100);
        
        //give container positioning and z-index
        if ($aniContainer.css("position") == "static") {
            $aniContainer.css({
                'position':'relative',
                'zIndex':50,
                'backgroundImage':'none'
            });
        }
        //start slideshow
        $('#slideshow').slideshow({
            timeout: 6000,
            type: 'sequence'
        });
        
    }
    
});
