$(document).ready(function(){
	/* The following code is executed once the DOM is loaded */
	var active = false;
	var last;
	
	$('.camFlip').bind("click",function(){
		
		// $(this) point to the clicked .camFlip element (caching it in elem for speed):
		
		var elem = $(this);
		
	
		// Es wurde vorher noch gar nix angeklickt
		if (last == null && active != true)
		{
		    
		    // angeklicktes Element flippen
		    elem.flip({
			direction:'lr',
			speed: 350,
    		color: '#fad002',
			onBefore: function(){
			// Insert the contents of the .camData div (hidden from view with display:none)
			// into the clicked .camFlip div before the flipping animation starts:
                active = true;
			elem.html(elem.siblings('.camData').html());
			},
			onEnd: function() {
                active = false;
			}
			});

			// FLAG
   			elem.data('flipped',true);
			last = elem;
			
		}
		
		// Es wurde vorher schon mal was geklickt
		else if (last != null && active != true)
		{
		    // Wenn das selbe nochmal angeklickt wird
		    if (elem == last && active != true)
		    {
		        // Wenn Bild sichtbar
		        if (elem.data('flipped') == false && active != true)
		        {
		            
              		// angeklicktes Element flippen
				    elem.flip({
					direction:'lr',
					speed: 350,
		    		color: '#fad002',
					onBefore: function(){
					// Insert the contents of the .camData div (hidden from view with display:none)
					// into the clicked .camFlip div before the flipping animation starts:
                        active = true;
					elem.html(elem.siblings('.camData').html());
					},
					onEnd: function() {
                        active = false;
					}
					});

					// FLAG
		   			elem.data('flipped',true);
					last = elem;
					
		        }

		        // Wenn Textseite sichtbar
				else if (elem.data('flipped') == true && active != true)
				{

				}
		    }
		    
		    // Wenn ein anderes Element angeklickt wird als das vorher
			else if (last != elem)
			{
			    // Wenn letztes Element Bild sichtbar
			    if (last.data('flipped')==false && active != true)
				{
				    active=true;
					// angeklicktes Element flippen
				    elem.flip({
					direction:'lr',
					speed: 350,
		    		color: '#fad002',
					onBefore: function(){
					// Insert the contents of the .camData div (hidden from view with display:none)
					// into the clicked .camFlip div before the flipping animation starts:
						active=true;
					elem.html(elem.siblings('.camData').html());
					},
					onEnd: function() {
						active=false;
					}
					});

					// FLAG
		   			elem.data('flipped',true);
					last = elem;
				
				}
				
				// Wenn letztes Element Textseite sichtbar
				else if (last.data('flipped') && active != true)
				{
				    
				     // Revert Flip
				    last.revertFlip();

                    // angeklicktes Element flippen
				    elem.flip({
					direction:'lr',
					speed: 350,
		    		color: '#fad002',
					onBefore: function(){
					active=true;

					elem.html(elem.siblings('.camData').html());
					},
					onEnd: function() {
                        active=false;
					}
					});

					// FLAG
		   			elem.data('flipped',true);
					last.data('flipped',false);
					last = elem;
					
				}
			}
		}

	});

});

