/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
function slideSwitch() {
    var $active = $('.slides IMG.active');

    if ( $active.length == 0 ) $active = $('.slides IMG:last');
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('.slides IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
$(function() {
    setInterval( "slideSwitch()", 5000 );
	
	$('.galleryButton').click(function(e){
		e.preventDefault();
		var iIndex = $(this).attr('id').substring($(this).attr('id').length -1);
		var sCaption = $(this).attr('title');
		
		$('.slides').hide().removeClass('slides');
		$('#gallery' + iIndex).addClass('slides').show();
		$('#caption').text(sCaption);
		$('.on').removeClass('on');
		$(this).parent().addClass('on');
	});
	
	$('#openModal').click(function(e){
		e.preventDefault();
		$('#modalContainer').modal({onOpen: modalOpen, onClose: modalClose});
		OnModalLoad();
		closeModalWithOverlay();
	});
	
	function modalOpen(dialog) {
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.slideDown('fast', function () {
				dialog.data.show().fadeIn('fast', function(){
				});	 
			});
		});
	}
	function modalClose(dialog) { 
		dialog.data.fadeOut('fast', function() {
			dialog.container.fadeOut('fast', function () {
				dialog.overlay.fadeOut('fast', function () {
					$.modal.close();
				});
			});						
		});
	}
	function closeModalWithOverlay() {
		$("#simplemodal-overlay").bind("click", 
			function(objEvent) {
				$.modal.close();
			}							
		);  	
	}
	function OnModalLoad(){
		var sCaption = $('#thumbnails li:first a').attr('title');
		$('#caption').text(sCaption);
		$('#thumbnails li:first').addClass('on');		
	}
	function debug(sDebug){
		if(window.console){
			console.log(sDebug);
		}else{
			alert(sDebug);
		}
	}
});
