  var locked = false;
  var currentPhoto = 1;
  var totalPhotos = 1;
  var timeoutPhotoLoad = false;								  //Global defined timeout, false when there isnt a current timeout
  var loadedPhotosSequence = new Array();					  //The sequence in which the photos should be loaded
  var loadingPhotos = new Array();							  //Array containing all the photos that are currently loading
  var loadedPhotos = new Array();							  //Array containing all the photos that are loaded
  var slideShowOn = false;


  /**
  @description						shows a new photo
  @param string/integer action		'backward'		shows the photo before the current photo (starts at the last photo when it reached photo 1)
									'forward'		shows the photo after the current photo (starts at photo 1 when it reached the totalPhotos)
									default			the number of the photo which should be shown, when it doesnt exist photo one is shown
  */
  function photoSwitch(action) {
	
	locked = true;

	//Determine the next photo
	nextPhoto = currentPhoto;
	switch(action) {

		case 'backward':
			nextPhoto--;
			break;

		case 'forward':
			nextPhoto++;
			break;

		default:			
			
			if(photos[action]==undefined)			action = 1;
			nextPhoto = action;
			break;

	}
	if(nextPhoto>totalPhotos) 	nextPhoto = 1;
	if(nextPhoto<1) 			nextPhoto = totalPhotos;

	$("#photo"+currentPhoto).fadeOut(900, function() {

		//Making the current thumbnail normal again
		$("#thumbnail"+currentPhoto+" img").css('opacity', 1);
		
		//Make sure all the displays are set to none
		currentPhoto = nextPhoto;
		for(var i=1; i<=totalPhotos; i++) 
			$("#photo"+i).css('display', 'none');
		$("#loadingImage").css('display', 'none');			

		//Checks if the photo is loaded else the function checkPhotoLoad takes care of from this point on
		if(checkPhotoLoaded(currentPhoto)===false) {	
			prioriticyLoadingPhotos(currentPhoto);			
			waitLoadingPhoto(currentPhoto);
			return false;
		}
		
		$("#photo"+currentPhoto).fadeIn(900);	
		locked = false;

		//Making the selected thumbnail half transparent
		$("#thumbnail"+currentPhoto+" img").css('opacity', 0.5);
		return;

	});

  }


  function slideShow() {

	if(slideShowOn===false)		return;

	photoSwitch('forward');
	slideShowOn = setTimeout('slideShow()', 5000);

  }

  function slideShowStart() {

	$('#start').hide();
	$('#stop').show();
	slideShowOn = true;
	slideShow();

  }

  function slideShowStop() {

	$('#stop').hide();
	$('#start').show();
	slideShowOn = false;

  }

 
  /**
  @description					used when a thumbnail is clicked and the big photo should be shown.
								it will only run the photoSwitch function if there isnt already an other
								process using the photoSwitch function
  @param integer photoNumber	the number of the photo which should be displayed				
  */
  function thumbnailToPhoto(photoNumber) {

	if(locked===false)
	  photoSwitch(photoNumber);

  }

	
  /**
  @description				putting the actions next and back loading actions to the buttons on the photo
  */
  function onClickButtons() {

    $("#backward").click(function () {
		if(locked===false)  photoSwitch('backward');
		return false;
    });

    $("#start").click(function () {
		slideShowStart();
		return false;
    });

    $("#stop").click(function () {
		slideShowStop();
		return false;
    });

    $("#forward").click(function () {
		if(locked===false)  photoSwitch('forward');
		return false;
    });


  }


  /**
  @description					initialize all the settings for the galleryviewer for a fresh start
  */
  function initialize() {
		
    onClickButtons();
	loadPhotos();

  } 



  /**
  @description			loads all the photos into the  loadedPhotosSequence array when the photo
						is not yet in the loadedPhotos array
  @note					after filling the array it will start loading the photos with the function loadPhoto
  */
  function loadPhotos() {
	
	for(var i=1; i<=totalPhotos; i++) { 
		if(searchArray(loadedPhotos, i)===false
		&& searchArray(loadedPhotosSequence, i)===false)
			loadedPhotosSequence[loadedPhotosSequence.length] = i;
	}
	loadPhoto('system');

  }


  /**
  @description					search in the array for the value and will return the key of 
								the first match
  @param array array			the array that should be searched for the value
  @param string value			the value that needs to be found
  @return boolean, integer		false when the value was not found, the key when it was
  */
  function searchArray(array, value) {
	
	for(var i in array) {
		if(array[i]==value)		return i;
	}
	return false;
  }


  /**
  @description			prioriticy a photo in the loadedPhotos array, which will be loaded then as
						the first one in the line. 
  @param integer photo	the key of the photo in the photos array
  @note					replace the global array loadedPhotosSequence with the new created sequence
  */
  function prioriticyLoadingPhotos(photo) {

	var counter = 0;
	var prioriticyPhotos = new Array();
	prioriticyPhotos[counter] = photo;
	counter++;

   //Load the images after
   if((photo+1)>totalPhotos) {
	   photo = 1;
	   prioriticyPhotos[counter] = photo;
	   counter++;
   } 

	//Adds all the photos after the current selected photo as prioriticied photo
	for(var i=(photo+1); i<=totalPhotos; i++) {
		prioriticyPhotos[counter] = i;
		counter++;
	}
	
	
	//Add the prioriticied photos to the prioriticyPhotos array
	var counter = 0;
	var sortedArray = new Array();
	for(var i in prioriticyPhotos){
		if(checkPhotoLoaded(prioriticyPhotos[i])===false
		&& checkPhotoLoad(prioriticyPhotos[i])===false) {
			sortedArray[counter] = prioriticyPhotos[i];
			counter++;
		}
	} 

	//Add all the other photos that are still in the loadedPhotosSequence
	for(var i=1; i<=totalPhotos; i++) { 
		if(searchArray(prioriticyPhotos, i)===false 
		&& checkPhotoLoaded(i)===false
		&& checkPhotoLoad(prioriticyPhotos[i])===false) {
			sortedArray[counter] = i;
			counter++;
		}
	} 


	//Overwrite the global array
	loadedPhotosSequence = sortedArray;

  }


  /**
  @description					loads the next photo in the loadedPhotosSequence array
  @param string reference		'system'		will reload this function till all the photos are loaded
								'userDefined'	will load the first photo in line and stops then
  */
  function loadPhoto(reference) {

	if(loadedPhotosSequence.length>0) {

		var photo = loadedPhotosSequence[0];

		//Make sure the photo isn't loaded yet, otherwise remove it
		//and get the following one
		while(checkPhotoLoaded(photo)===true && checkPhotoLoad(photo)===true) {

			//Removed the photo from the loadedPhotosSequence
			loadedPhotosSequence.splice(0, 1);
			if(loadedPhotosSequence.length==0)
				return false;

			var photo = loadedPhotosSequence[0];

		}
		
		//Removed the photo from the loadedPhotosSequence
		loadedPhotosSequence.splice(0, 1);
		
		//Add the photo to the loadPhoto array
		loadingPhotos[loadingPhotos.length] = photo;

		//Make sure the first photo is shown
		var photoHide = ' displayNone';
		if($('img.photoDisplay').not('img.displayNone').length==0) {

			//Making the currently selected thumbnail half transparent
			$("#thumbnail"+photo+" img").css('opacity', 0.5);
			var photoHide = '';

		}

		//Add the img element with the photo to the layer #photoDisplay
		$('#photoDisplay').append('<img src="' + photos[photo].filename + '"'
								  + ' id="photo'+ photo +'" alt="" border="0" class="photoDisplay'+photoHide+'">');
		$('#photo'+ photo).attr('src', photos[photo].filename).load(function() {
			
			//Remove from loadPhoto array
			var key = searchArray(loadingPhotos, photo);
			if(key!==false) 
				loadingPhotos.splice(key, 1);

				
			//Add to loaded photo to the loadedPhotos array
			loadedPhotos[loadedPhotos.length] = photo;
			
			//photo is loaded and the system made the call, load an other photo
			if(reference=='system')		loadPhoto('system'); 	
			
		});
	} 
  }


  /**
  @description			  check if a photo is already loaded or not
  @param integer photo	  the key of the photo in the photos array
  @return boolean		  true when the photo is loaded, false on not
  */
  function checkPhotoLoaded(photo) {

	if(searchArray(loadedPhotos, photo)===false)
		return false;
	return true;

  }


  /**
  @description				check if a photo is not in the process of loading already
  @param integer photo		the key of the photo in the photos array
  @return boolean			true when the photo is loaded, false on not
  */
  function checkPhotoLoad(photo) {

	if(searchArray(loadingPhotos, photo)===false)
		return false;
	return true;

  }


  /**
  @description				waits for the photo to be loaded into the users cache and then displays it
							shows a loading image while waiting
  @param integer photo		the key of the photo in the photos array
  */
  function waitLoadingPhoto(photo) {

	if(checkPhotoLoaded(currentPhoto)===false) {

		if(timeoutPhotoLoad===false) {
			$("#loadingImage").fadeIn(900);
			loadPhoto('userDefined');
		} else	
			clearTimeout(timeoutPhotoLoad);		
		timeoutPhotoLoad = setTimeout("waitLoadingPhoto("+photo+")",1000);		
		return false;

	}	
	timeoutPhotoLoad = false;

	$("#loadingImage").fadeOut(900, function() {
		$("#photo"+photo).fadeIn(900);
		locked = false;		
	});
  }



function showFlicker(query) {

	var page_show_url = '%2Fsearch%2Fshow%2F%3Fq%3D'+query;
	var page_show_back_url = '%2Fsearch%2F%3Fq%3D'+query;
	var api_text = query;
	var params = 'offsite=true&lang=en-us&page_show_url='+page_show_url
				 +'&page_show_back_url='+page_show_back_url
				 +'&method=flickr.photos.search'
				 +'&api_tag_mode=bool&api_media=all&api_sort=relevance'
	             +'&start_index=1&api_text=' + api_text;
	createFlashObject('flicker', 'http://www.flickr.com/apps/slideshow/show.swf?v=71649', '#000000', 400, 300, params);

	$("#photoDisplay").css('display', 'none');
	$("#photoNavigation").css('display', 'none');
	$("#flicker").css('display', 'table-cell');

}