/*
galery_id:
mode:
  slider
  pager
slideshow:
slideshow_speed:
*/

var g_slider_width = new Object;
var g_slideshow_slider_width = new Object;
var g_page_number = new Object;
var g_image_number = new Object;
var g_images_number = new Object;
var g_time = new Object;

/* Image preload */

function imagePreload(div_galeria_bigimage, active_img_a_href) {

  width = div_galeria_bigimage[0].offsetWidth;
  height = div_galeria_bigimage[0].offsetHeight;
  div_galeria_bigimage.prepend('<img src="ajax-loader.gif" alt="Töltés" style="position:absolute; top:'+(height/2)+'px; left:'+(width/2)+'px;" />');
  
  var img = new Image();
  img.src = active_img_a_href;  
  here = div_galeria_bigimage;    
  
  img.onload = function () {    
    here.html(''+sorszamkiir+kepekszama+'<img style="margin:0 auto; '+css_bigimage+' display:none;" src="'+img.src+'" />');
    here.children('img').fadeIn("slow");
  }
  
}

/* Slideshow play */

function slideshowPlay(galery_id, sorszamozas, css_sorszam, css_bigimage, this_id) {

  if ( $('#'+galery_id+' img:last').attr('class') != 'active' ) {
    
    $('#'+galery_id+' li img.active').removeClass('active').animate({opacity: 0.4}).parent().parent().next().contents().find('img').addClass('active').animate({opacity: 1});

    id = $('#'+galery_id+' li img.active').attr('id');
    id = id.split('_');
    id = parseFloat(id[1]);
    kepekszama = '<div style="text-align:center; display:none;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';
    
    sorszam = $('#'+galery_id+' li img.active').parent().attr('href').split('id=');
    if (sorszamozas == true) sorszamkiir = '<div style="text-align:center; '+css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';
    else sorszamkiir = '';
    
    /* Image preload */
    
    div_galeria_bigimage = $('#'+galery_id).parent().parent().parent().children('.gergo_galeria_bigimage');
    active_img_a_href = $('#'+galery_id+' li img.active').parent().attr('href');
    imagePreload(div_galeria_bigimage, active_img_a_href);
    
    $('#'+this_id+'').children().children('.slideshow_kep_sorszam').html(''+id+'/'+g_images_number[this_id]+'');  
    $('#'+this_id+'').children().children('.slideshow_slider_line').animate({
      'width': '+='+g_slideshow_slider_width[this_id]
    }, 2000);
    
    id_ = id - 1;
    if(id_%(g_image_number[$('#'+galery_id+' img').parent().parent().parent().parent().parent().parent().attr('id')]) == 0){
    
      $('#'+galery_id).animate({
        'left': '-='+(parseFloat($('#'+galery_id+' img').parent().parent().parent().parent('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+galery_id+' li img').css('marginRight'))+parseFloat($('#'+galery_id+' li img').css('marginLeft')))+'px'
      }, 1000); 
      
      $('#'+galery_id).parent().parent().parent().children().children('.slider_line').animate({
        'left': '+='+$('#'+galery_id).parent().parent().parent().children().children('.slider_line').css('width')
      }, 1000);
      
    }
    
  } else {
    clearInterval(g_time[this_id]);
    $('#'+this_id+'').children().children('.end_of_slideshow').css("display","block");
  }
  
}

/* Slideshow pause */

function slideshowPause(this_id) {
  clearInterval(g_time[this_id]);
}

/* Main */

(function($){
$.fn.gergo_galeria = function(alloptions){

  galery_id = alloptions.galery_id;
  sorszamozas = alloptions.sorszam;
  css_bigimage = alloptions.css_bigimage;
  css_sorszam = alloptions.css_sorszam;
  css_kepekszama = alloptions.css_kepekszama;
  this_id = $(this).attr('id');
  sorszamkiir = '';
  kepekszama = '';

  var galeriacontent = $('#'+galery_id).html(); 
  firstimagehref = $('#'+galery_id+' a:first').attr('href');
  images_number = $('#'+galery_id+' li').length;
  g_images_number[this_id] = images_number;
  $('#'+alloptions.galery_id).remove();
  
  kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">1/'+g_images_number[this_id]+'</div>';
  sorszam = firstimagehref.split('id=');
  if (sorszamozas == true) sorszamkiir = '<div style="text-align:center; '+css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';
   
  $('#'+$(this).attr('id')).html('<div class="gergo_galeria_bigimage" style="position:relative; text-align:center;"><img style=" margin:0 auto; '+alloptions.css_bigimage+' visibility:hidden; " src="'+firstimagehref+'" /></div><div style=" margin:0 auto; '+alloptions.css_smallcontainer+'" class="smallcontainer"><div style="float:left; display:inline; width:20px; '+alloptions.css_arrow_left+'" class="arrows_back">&lt;</div><div class="gergo_galeria_smallcontent" style="position:relative; overflow:hidden; float:left; display:inline; '+alloptions.css_smallcontent+'"><ul style="position:absolute; width:99999px; '+alloptions.css_ul+'" id="'+alloptions.galery_id+'">'+galeriacontent+'</ul></div><div style="float:left; display:inline; width:20px; '+alloptions.css_arrow_right+'" class="arrows_forward">&gt;</div><div style="clear:both; height:0; overflow:hidden;"></div></div>');
  
  /* Képek sorszámozása a kurzoros mozgatáshoz és a képsorszám / képekszáma megjelenítéséhez */   
             
  $('#'+galery_id+' li img').each( function(i){
    $(this).attr({ id: '_' + (++i) });
  });
  
  /* Image preload */
  
  div_galeria_bigimage = $(this).children('.gergo_galeria_bigimage');
  active_img_a_href = firstimagehref;
  imagePreload(div_galeria_bigimage, active_img_a_href);
  
  $('#'+$(this).attr('id')).attr('style',alloptions.css_bigcontent);
  $('#'+alloptions.galery_id+' li img').attr('style', 'display:inline; float:left; '+alloptions.css_smallimage);
  $('#'+alloptions.galery_id+' li').css({
    'float':'left',
    'display':'inline',
    'height':$('#'+alloptions.galery_id+' li img').css('height')
  });
  
  $('#'+alloptions.galery_id+' li a').css({
    'float':'left',
    'display':'inline',
    'height':$('#'+alloptions.galery_id+' li img').css('height')
  });
  
  width = parseFloat($('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width'));
  width += parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'));
  width += parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft'));
  
  image_number = width/$('#'+alloptions.galery_id+' li')[0].offsetWidth;
  page_number = Math.ceil(images_number/image_number);
  
  slider_width = parseFloat($('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width'))/page_number;

  /* Slideshow slider width*/
  slideshow_slider_width = parseFloat($('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width'))/images_number;
  g_slideshow_slider_width[$(this).attr('id')] = slideshow_slider_width;

  g_slider_width[$(this).attr('id')] = slider_width;
  g_page_number[$(this).attr('id')] = page_number;
  g_image_number[$(this).attr('id')] = image_number;
  
  $('#'+$(this).attr('id')).append('<div style="position:relative; margin:0 auto; width:'+$('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width')+'; height:20px; '+alloptions.css_slider+'" class="slider"><div style="background:#000; position:relative; height:20px; '+alloptions.css_slider_line+' width:'+slider_width+'px;" class="slider_line"></div></div>');
  
  /*Slideshow slider line*/
  $('#'+$(this).attr('id')).append('<div style="display:none; position:relative; margin:0 auto; width:'+$('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width')+'; height:20px; '+alloptions.css_slideshow_slider+'" class="slideshow_slider"><div style="background:#000; position:relative; height:20px; '+alloptions.css_slideshow_slider_line+' width:'+g_slideshow_slider_width[this_id]+'px;" class="slideshow_slider_line"></div><div style="color:#000; position:absolute; right:10px; top:2px; z-index:10;" class="slideshow_kep_sorszam">1/'+g_images_number[this_id]+'</div></div>');
  
  if (alloptions.slideshow == true) {
    $('#'+$(this).attr('id')).append('<div style="width:'+$('#'+$(this).attr('id')+' .gergo_galeria_smallcontent').css('width')+'; margin:0 auto;" class="button_container"><div style="margin:0 auto; '+alloptions.css_slideshow_start+'" class="slideshow_start">Diavetítés</div></div>');
    slideshow = '<div style="float:left; display:inline; width:20px; '+alloptions.css_play+'" class="play">Indítás</div>';
    slideshow += '<div style="float:right; display:inline; width:20px; '+alloptions.css_pause+'" class="pause">Szünet</div>';
    $('#'+this_id+'').children('.button_container').append('<div style="margin:0 auto; display:none;'+alloptions.css_slideshow_container+'" class="slideshow_container">'+slideshow+'</div>');
    $('#'+this_id+'').children('.button_container').append('<div style="margin:0 auto; display:none;'+alloptions.css_back_to_galery+'" class="back_to_galery">Vissza a galériához</div>');
    $('#'+this_id+'').children('.button_container').append('<div style="margin:0 auto; display:none;'+alloptions.css_end_of_slideshow+'" class="end_of_slideshow">Vége a diavetítésnek!</div>');
  }
  
  $('#'+alloptions.galery_id).css('left','0');
  $('#'+$(this).attr('id')+' .slider_line').css('left','0');
  
  $('#'+alloptions.galery_id+' li img').animate({opacity: 0.4});
  
  $('#'+alloptions.galery_id+' li img[class!=active]').live("mouseover", function() {
    $(this).animate({opacity: 1});
  });
  
  $('#'+alloptions.galery_id+' li img[class!=active]').live("mouseout", function() {
    $(this).animate({opacity: 0.4});
  });
        
  /* First small picture set */
  
  $('#'+alloptions.galery_id+' li img.active').removeClass('active');
  $('#'+alloptions.galery_id+' li img:first').addClass('active');
  $('#'+alloptions.galery_id+' .active').animate({opacity: 1});

  /* Small picture click */
  
  $('#'+$(this).attr('id')+' a').live("click", function() {
  
    $('#'+alloptions.galery_id+' li img.active').removeClass('active').animate({opacity: 0.4});
    $(this).children('img').addClass('active');
    $(this).children('img').animate({opacity: 1});
    
    id = $('#'+galery_id+' li img.active').attr('id');
    id = id.split('_');
    id = parseFloat(id[1]);
    kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';
    
    sorszam = $(this).attr('href').split('id=');
    if (alloptions.sorszam == true) sorszamkiir = '<div style="text-align:center; '+alloptions.css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';
    
    /* Image preload */
    
    div_galeria_bigimage = $(this).parent().parent().parent().parent().parent().children('.gergo_galeria_bigimage');
    active_img_a_href = $(this).attr('href');
    imagePreload(div_galeria_bigimage, active_img_a_href);
    
    return false;
      
  });    

  /* PAGER */
  
  if (alloptions.mode == 'pager') {
  
    /* Pager back */
    
    $('#'+$(this).attr('id')+' .arrows_back').live("click", function() {
    
      if ((Math.floor( parseFloat($(this).parent().parent().children().children('.slider_line').css('left')))>0) &&( ((parseFloat($('#'+alloptions.galery_id).css('left')))%(parseFloat($(this).parent().children('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft'))))==0    )) {
        $('#'+alloptions.galery_id).animate({
          'left': '+='+(parseFloat($(this).parent().children('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft')))+'px'
        }, 1000);  
  
        $(this).parent().parent().children().children('.slider_line').animate({
          'left': '-='+$(this).parent().parent().children().children('.slider_line').css('width')
        }, 1000);
        
        makeactive = g_image_number[this_id] * Math.round( parseFloat($(this).parent().parent().children().children('.slider_line').css('left'))/parseFloat($(this).parent().parent().children().children('.slider_line').css('width')))-1;
        $('#'+alloptions.galery_id+' li img.active').removeClass('active').animate({opacity: 0.4});
        $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').addClass('active');
        $('#'+alloptions.galery_id+' .active').animate({opacity: 1}); 
        
        id = $('#'+galery_id+' li img.active').attr('id');
        id = id.split('_');
        id = parseFloat(id[1]);
        kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';
         
        sorszam = $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').parent().attr('href').split('id=');
        if (alloptions.sorszam == true) sorszamkiir = '<div style="text-align:center; '+alloptions.css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';
                
        /* Image preload */
        
        div_galeria_bigimage = $(this).parent().parent().children('.gergo_galeria_bigimage');
        active_img_a_href = $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').parent().attr('href');
        imagePreload(div_galeria_bigimage, active_img_a_href);
        
      } else return false;
      
    });
    
    /* Pager forward */
    
    $('#'+$(this).attr('id')+' .arrows_forward').live("click", function() {
  
      if ((parseFloat($(this).parent().parent().children().children('.slider_line').css('left'))<(parseFloat($(this).parent().parent().children().children('.slider_line').parent().css('width'))-Math.ceil(parseFloat($(this).parent().parent().children().children('.slider_line').css('width'))) )   ) &&( ((parseFloat($('#'+alloptions.galery_id).css('left')))%(parseFloat($(this).parent().children('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft'))))==0    )) {

        $('#'+alloptions.galery_id).animate({
          'left': '-='+(parseFloat($(this).parent().children('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft')))+'px'
        }, 1000);  
  
        $(this).parent().parent().children().children('.slider_line').animate({
          'left': '+='+$(this).parent().parent().children().children('.slider_line').css('width')
        }, 1000);  
        
        makeactive = Math.round ( g_image_number[$(this).parent().parent().attr('id')] * ( parseFloat($(this).parent().parent().children().children('.slider_line').css('left'))/parseFloat($(this).parent().parent().children().children('.slider_line').css('width')) +1 ) );
        
        $('#'+alloptions.galery_id+' li img.active').removeClass('active').animate({opacity: 0.4});
        $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').addClass('active');
        $('#'+alloptions.galery_id+' .active').animate({opacity: 1});

        id = $('#'+galery_id+' li img.active').attr('id');
        id = id.split('_');
        id = parseFloat(id[1]);
        kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';
          
        sorszam = $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').parent().attr('href').split('id=');
        if (alloptions.sorszam == true) sorszamkiir = '<div style="text-align:center; '+alloptions.css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';
                  
        /* Image preload */
        
        div_galeria_bigimage = $(this).parent().parent().children('.gergo_galeria_bigimage');
        active_img_a_href = $('#'+alloptions.galery_id+' li img:eq('+makeactive+')').parent().attr('href');
        imagePreload(div_galeria_bigimage, active_img_a_href);
        
      } else return false;
      
    });
    
  }

  /* Cursor left key (37) / Cursor right key (39) */

  $(document).keydown(function(event){
  
    switch (event.keyCode) {
        
        case 37:
        
          if ($('#'+alloptions.galery_id+' img:first').attr('class') != 'active' ) {
            $('#'+alloptions.galery_id+' li img.active').removeClass('active').animate({opacity: 0.4}).parent().parent().prev().contents().find('img').addClass('active').animate({opacity: 1});
            
            id = $('#'+galery_id+' li img.active').attr('id');
            id = id.split('_');
            id = parseFloat(id[1]);
            kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';
            
            sorszam = $('#'+alloptions.galery_id+' li img.active').parent().attr('href').split('id=');
            if (alloptions.sorszam == true) sorszamkiir = '<div style="text-align:center; '+alloptions.css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';  
                        
            /* Image preload */
            
            div_galeria_bigimage = $('#'+alloptions.galery_id).parent().parent().parent().children('.gergo_galeria_bigimage');
            active_img_a_href = $('#'+alloptions.galery_id+' li img.active').parent().attr('href');
            imagePreload(div_galeria_bigimage, active_img_a_href);
            
            id = $('#'+alloptions.galery_id+' li img.active').attr('id');
            id = id.split('_');
            id = parseFloat(id[1]);
            if(id%(g_image_number[$('#'+alloptions.galery_id+' img').parent().parent().parent().parent().parent().parent().attr('id')]) == 0){
            
              $('#'+alloptions.galery_id).animate({
                'left': '+='+(parseFloat($('#'+alloptions.galery_id+' img').parent().parent().parent().parent('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft')))+'px'
              }, 1000);
              
              $('#'+alloptions.galery_id).parent().parent().parent().children().children('.slider_line').animate({
                'left': '-='+$('#'+alloptions.galery_id).parent().parent().parent().children().children('.slider_line').css('width')
              }, 1000);
              
            }   
          }
          return false;
     
        break;
        
        case 39:

          if ($('#'+alloptions.galery_id+' img:last').attr('class') != 'active' ) {
            $('#'+alloptions.galery_id+' li img.active').removeClass('active').animate({opacity: 0.4}).parent().parent().next().contents().find('img').addClass('active').animate({opacity: 1});
            
            id = $('#'+galery_id+' li img.active').attr('id');
            id = id.split('_');
            id = parseFloat(id[1]);
            kepekszama = '<div style="text-align:center;'+css_kepekszama+'" class="kepek_sorszama">'+id+'/'+g_images_number[this_id]+'</div>';            
            
            sorszam = $('#'+alloptions.galery_id+' li img.active').parent().attr('href').split('id=');
            if (alloptions.sorszam == true) sorszamkiir = '<div style="text-align:center; '+alloptions.css_sorszam+'"><p>A kép száma: '+sorszam[1]+'</p></div>';

            /* Image preload */
            
            div_galeria_bigimage = $('#'+alloptions.galery_id).parent().parent().parent().children('.gergo_galeria_bigimage');
            active_img_a_href = $('#'+alloptions.galery_id+' li img.active').parent().attr('href');
            imagePreload(div_galeria_bigimage, active_img_a_href);
            
            id = $('#'+alloptions.galery_id+' li img.active').attr('id');
            id = id.split('_');
            id = parseFloat(id[1]) - 1;
            if(id%(g_image_number[$('#'+alloptions.galery_id+' img').parent().parent().parent().parent().parent().parent().attr('id')]) == 0){
            
              $('#'+alloptions.galery_id).animate({
                'left': '-='+(parseFloat($('#'+alloptions.galery_id+' img').parent().parent().parent().parent('.gergo_galeria_smallcontent').css('width'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginRight'))+parseFloat($('#'+alloptions.galery_id+' li img').css('marginLeft')))+'px'
              }, 1000); 
              
              $('#'+alloptions.galery_id).parent().parent().parent().children().children('.slider_line').animate({
                'left': '+='+$('#'+alloptions.galery_id).parent().parent().parent().children().children('.slider_line').css('width')
              }, 1000);
              
            }
          }
          return false;
          
        break;
        
    }
  });
 
  /* Slideshow */
  
  if (alloptions.slideshow == true) {   
    
    /* Slideshow_start */
    
    $('#'+$(this).attr('id')+' .slideshow_start').live("click", function() {
      $('#'+this_id+'').children().children('.kepek_sorszama').css("display","none"); 
      $('#'+this_id+'').children('.smallcontainer').css("display","none");
      $('#'+this_id+'').children('.slider').css("display","none");
      $('#'+this_id+'').children().children('.slideshow_start').css("display","none");
      $('#'+this_id+'').children('.slideshow_slider').css("display","block");
      $('#'+this_id+'').children().children('.slideshow_container').css("display","block");
      $('#'+this_id+'').children().children('.back_to_galery').css("display","block");
      id = $('#'+galery_id+' li img.active').attr('id');
      id = id.split('_');
      id = parseFloat(id[1]);
      meret = id * g_slideshow_slider_width[this_id];
      $('#'+this_id+'').children().children('.slideshow_kep_sorszam').html(''+id+'/'+g_images_number[this_id]+'');  
      $('#'+this_id+'').children().children('.slideshow_slider_line').css({
        'width': meret
      });
         
    });
    
    /* Play */
      
    $('#'+$(this).attr('id')+' .play').live("click", function() {
      $('#'+this_id+' .play').animate({opacity: 0.4});
      $('#'+this_id+' .play').css('cursor','default');
      $('#'+this_id+' .pause').animate({opacity: 1});
      $('#'+this_id+' .pause').css('cursor','pointer');      
      g_time[this_id] = setInterval( "slideshowPlay(galery_id, sorszamozas, css_sorszam, css_bigimage, this_id)", 5000 );
    });
    
    /* Pause */
    
    $('#'+$(this).attr('id')+' .pause').live("click", function() {
      $('#'+this_id+' .pause').animate({opacity: 0.4});
      $('#'+this_id+' .pause').css('cursor','default');
      $('#'+this_id+' .play').animate({opacity: 1});
      $('#'+this_id+' .play').css('cursor','pointer');
      slideshowPause(this_id);   
    }); 
  
    /* Back to the galery */
    
    $('#'+$(this).attr('id')+' .back_to_galery').live("click", function() {  
      slideshowPause(this_id);
      $('#'+this_id+' .play').animate({opacity: 1});
      $('#'+this_id+' .play').css('cursor','pointer');
      $('#'+this_id+' .pause').animate({opacity: 1});
      $('#'+this_id+' .pause').css('cursor','pointer'); 
      $('#'+this_id+'').children().children('.end_of_slideshow').css("display","none");
      $('#'+this_id+'').children().children('.kepek_sorszama').css("display","block");
      $('#'+this_id+'').children('.smallcontainer').css("display","block");
      $('#'+this_id+'').children('.slider').css("display","block");
      $('#'+this_id+'').children().children('.slideshow_start').css("display","block");
      $('#'+this_id+'').children('.slideshow_slider').css("display","none");
      $('#'+this_id+'').children().children('.slideshow_container').css("display","none");
      $('#'+this_id+'').children().children('.back_to_galery').css("display","none");
    }); 
  
  }
   
}
})(jQuery);
