// JavaScript Document

/**
 * Java-Script/PHP - Galerie
 * -------------------------
 * 
 *   
 */   

// VARS für Galerie
var a = "";
var image_src = "";
//--------------------------
// VARS für NAVIGATION
var NEXT = 0;
var PREV = 1;
var RAND = 2;
// VARS für STARS
var src_empty  = 'files/template/images/star_empty.gif';
var src_filled = 'files/template/images/star_filled.gif'; 
var stars  = 0;
var save   = 0;
var cache  = 0;
var backup = 0;

$(document).ready(function() {

/****** GALERY-NAVIGATION START -- GALERY-NAVIGATION START  *******************/

    $('.next').click(function(){                                                          
        image_src = $('img.main_image').attr('src');
        getImage(NEXT, image_src);
     });
     
     $('.prev').click(function(){
        image_src = $('img.main_image').attr('src');
        getImage(PREV, image_src);
     });
     
     $('.rand').click(function(){
        interaction = 2;
        image_src = $('img.main_image').attr('src');
        getImage(RAND, image_src);
     });
     
     $(document).keydown(function(e)
    	{
    		switch(e.which)
    		{
    			//Pfeiltaste-links 
    			case 37:	image_src = $('img.main_image').attr('src');
                    getImage(PREV, image_src);
    						break;	
    
    			//Pfeiltaste-rechts
    			case 39:	image_src = $('img.main_image').attr('src');
                    getImage(NEXT, image_src);
    						break;
          
          case 38:
          case 40:  image_src = $('img.main_image').attr('src');
                    getImage(RAND, image_src);
                break;
    			default: 
                break;
    		}
      });     
             
    /***** GALERY-NAVIGATION END -- GALERY-NAVIGATION END *********************/  


   /*** Anzeige der Sterne ***************************************************
   **************************************************************************/ 
    
    $("div.content_content a.rating").click(function(){
      save = 1
    }); 
    
    $('a.next, a.rand, a.prev').click(function(){
        save = 0;
      });
    
    $("#rating_box").mouseenter(function(){
      if( backup == 0) { 
        backup = 1;
        stars = 0;
        $("a.rating").each(function(){
          if( $(this).children().attr('src') == 'files/template/images/star_filled.gif')
            stars++;
        });
       }
    });
      
    $("a.rating").mouseenter(function(){
      cache = $(this).attr('rel');
      if( save == 0 ){
        $("a.rating").each(function(){
            displayStar(cache, $(this));            
        });
      }
    });
    
    $("#rating_box").mouseleave(
      function(){
        if( save == 0 ) {
          $('a.rating').each(function(){
              displayStar(stars, $(this));
          });
        }  
      });
  /*********** END **********************************************************/        
    
}); // DOCUMENT-READY FUNCTION END
    
/**Funktion getImage(interaction, src)
 * interaction: (NEXT =0, PREV = 1, RAND = 2)
 * src: pfad vom aktuellen Bild
 *          ***********
 * Anhand der interaction liefert das PHP-Script entweder das nächste, vorige 
 * oder ein zufälliges Bild, zugehörige Anzahl von Sternen und Anzahl von 
 * Bewertungen zurück. Die Bild_src, Bewertungen und Sterne_src wird dann 
 * getauscht. 
 ***/    
function getImage(interaction, img_src) {
  //Sends the Name of the selected Folder to the PHP-Script
      $.ajax({
        url: ("files/scripts/galerie/navigation.php"),
        data: {
           src : image_src, action : interaction
        },
        type:     "POST",
        timeout:  2000,
        dataType: 'json',
  
        error: function(request,error) {
          alert('No Return Values');
          if (error == "timeout") {
           alert("script timeout");              
          }
        },            
        success: function(json){ 
          a = json;  
          new_imgsrc = a[0];
          ratings    = a[1];
          stars      = a[2];
          
          $('img.main_image').attr('src', new_imgsrc);
          $('a.rating').each(function() {
                if($(this).attr('rel') <= stars) {
                  $(this).children().attr('src', 'files/template/images/star_filled.gif');
                } else {
                  $(this).children().attr('src', 'files/template/images/star_empty.gif');
                }
            });
          $("#num_ratings").replaceWith('<span id="num_ratings">' + ratings + ' Bewertung(en)</span>');
        }
      }); // AJAX-FUNCTION END
}

function displayStar(maxStars, obj) {
   if(obj.attr('rel') <= maxStars) {
     obj.children().attr('src', src_filled);
   } else {
     obj.children().attr('src', src_empty);
   }
}
   
  
