var theTime;
var slideSwitchActive = false;

$(document).ready(function () {
	$('a.pictureLink').click( function( event ) {
		set_ajax_link( $(this), event, true, false );
	});
  // check if the picture div is opened already
  var pictureContent = $('#pictureContent').length;
  if ($('#pictureContent').length == 0) {
	  var args;
		var currentHash = document.location.hash;  
	  //if there is not anchor, open default site
	  if(currentHash.length = 0) {
	  	args = '?';
	  } else {
	   	//Creates the  string callback. This converts the url URL/#a=b&c=d in URL/?a=b&c=d
	   	var args = currentHash.substring(1)
	  }
	  load_page( args, true, false);
	}
});

function set_ajax_link( el, event, animation, stopSlideshow ){
	event.preventDefault();
	var urlArgs = el.attr("href");
	load_page( urlArgs, animation, stopSlideshow);
}

function load_page( urlArgs, animation, stopSlideshow ){
	if (animation) {
		$('#picture').hide('fast',loadContent);
	  $('#load').remove();
	  $('#wrapper').append('&lt;span id="load"&gt;LOADING...&lt;/span&gt;');
	  $('#load').fadeIn('normal');
	} else {
		loadContent();
	}
	if (stopSlideshow){
	  stopSlideSwitching();
	} else if (!slideSwitchActive) {
		startSlideSwitching();
	}
	
	function loadContent() {
		$("#picture").load( 'load_picture.php'+urlArgs, { 'ajax': 'true' }, function(){
			$('#picture a.pictureLink').click( function( event ) {
				set_ajax_link( $(this), event, false, true );
			});
			var test = $('#picture a.pictureLink');
			if (animation) {
				showNewContent();
			}
			window.location.hash = urlArgs;
		  }
	  );
	}
	
  function showNewContent() {
  $('#picture').show('normal',hideLoader());
  }
  
  function hideLoader() {
  $('#load').fadeOut('normal');
  }
	
}

function slideSwitch() {
	if (slideSwitchActive) {
    var $active = $('#pictureContent .activePicture');
    if ( $active.length == 0 ) $active = $('#pictureContent DIV:last');

    var $next =  $active.next().length ? $active.next()
        : $('#pictureContent DIV:first');

    var $pictureList = $('#pictureList a');
    if ($pictureList.length <= 1)
        return;

    $active.addClass('lastActivePicture');
    $next.css({opacity: 0.0})
        .addClass('activePicture')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('activePicture lastActivePicture');
        });

//    $next.addClass('activePicture');
    $active.removeClass('activePicture');
    $active.removeClass('lastActivePicture');
    $active.addClass('inactivePicture');
    $next.removeClass('inactivePicture');

    $active = $('#pictureList a.activeThumb');
    if ( $active.length == 0 ) $active = $('#pictureList a:last');

    var $next =  $active.next().length ? $active.next()
        : $('#pictureList a:first');

    $('#pictureList a.activeThumb img').attr('src','img/Fotoindex1.jpg');

    $active.addClass('inactiveThumb');
    $active.removeClass('activeThumb');

    $next.addClass('activeThumb');
    $next.removeClass('inactiveThumb');
    $('#pictureList a.activeThumb img').attr('src','img/Fotoindex2.jpg');
	}
}

$(function() {
   theTime = setInterval( "slideSwitch()", 6000 );
});

function startSlideSwitching() {
	 slideSwitchActive = true;
}

function stopSlideSwitching() {
	 slideSwitchActive = false;
}
