/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
    Modified by Petr Zaparka
***/

 var interval_time = 0;
 var suffix = '_740x555';
 var interval_delay = 2000;
 var click_enable = true;
$(document).ready(function(){  

    $(".slide_me").bind("click", function(e){
    
        if (interval_time == 0) {
            interval_time = setInterval("slideSwitch()", interval_delay);
            $(e.target).hide();
            $('.slide_img_on').show();
        }
        else {
            clearInterval(interval_time);
            interval_time = 0;
            $(e.target).hide();
            $('.slide_img_off').show();
        }
    });
    
    $('.photo_dalsi IMG').bind('click', function(e){
        var class_name = $(e.target).attr('class');
        $('#slideshow IMG.active').removeClass('active');
        $('#' + class_name).addClass('active');
    	clearInterval(interval_time);
        interval_time = 0;
        $('.slide_img_on').hide();
        $('.slide_img_off').show();
    });
    
    $('.show_all_thumb').bind('click',function(e){
       $('.all_thumb').show(); 
    });
    
    $('#slideshow').bind('click',function(e){
           $('.white_content').show();
           $('.left_light').hide();
           var src = $('#slideshow IMG.active').attr('src'); 
           $('.white_content IMG.active').attr( 'src', src.replace(/_320x240_c/,suffix) ) ;
    });

    $('.left_light').bind('click',function(e){
            lightBoxInterval(false);			
		    slideLightBox('left');
    });
        
    $('.right_light').bind('click',function(e){
            lightBoxInterval(false);
			if(click_enable)
            slideLightBox('right');        
    });
    
    $('.slide_light').bind('click',function(e){
         lightBoxInterval(true);
    });
    
    $('#.white_content IMG').bind('click',function(e){
           $('.white_content').hide();
           if(interval_time!=0) clearInterval(interval_time);
           interval_time = 0;
    });
    
    $('.close_light').bind('click',function(e){
           $('.white_content').hide();
    });
    
    $('.prehled_novinek tr').bind('mouseover',function(event){
        highrise(event);
        var elem =  $(event.target);
        elem.css('cursor','pointer');
    });
    
    $('.prehled_novinek tr').bind('mouseout',function(event){
        highrise(event);
    });
      
});

function highrise(event)
{
      var elem =  $(event.target);
       elem.parent().toggleClass('tr_a');
       
       if(elem.parent().hasClass('tr_l'))
       {  
          if(elem.parent().next().hasClass('tr_l'))
          {
             elem.parent().next().toggleClass('tr_a');        
          }else 
          {
              elem.parent().prev().toggleClass('tr_a');    
          }
          
       }else if(elem.parent().next().hasClass('tr_s')){
             elem.parent().next().toggleClass('tr_a');        
       } else 
       {
             elem.parent().prev().toggleClass('tr_a');        
       }
      
}
function lightBoxInterval(run)
{ 
         if(interval_time!=0)
         {
           clearInterval(interval_time);
         }else 
         {
           if(run) interval_time = setInterval("slideLightBox('right')", interval_delay); 
         }
}


    

function slideLightBox(direction){
    $('.right_light').show();
    $('.left_light').show();
	
    var active = $('#slideshow IMG.active');
    var active_light = $('.white_content IMG.active');
	
    var next, next_light = '';
	
    if (active.length == 0) 
        active = $('#slideshow IMG:last');
		
    if (active_light.length == 0){
		active_light =  $('#light-1');
		next_light =  $('#light-2');
	}else{
		if(active_light.attr('id')== 'light-1' )
		 next_light =  $('#light-2');
    	else 
 		 next_light =  $('#light-1');
	}

	
 
    switch (direction) {
        case 'left':
            next = active.prev().length ? active.prev() : $('#slideshow IMG:last');
            break;
        case 'right':
        default:
            next = active.next().length ? active.next() : $('#slideshow IMG:first');
            break;
    }
	 active.addClass('last-active');
    next.css({
        opacity: 0.0
    }).addClass('active').animate({
        opacity: 1.0
    }, 100, function(){
        active.removeClass('active last-active');
    });
    
    
    var src = next.attr('src').replace(/_320x240_c/, suffix)	
	next_light.attr('src', src);

	active_light.hide();
	
    next_light.css({
        opacity: 0.0
    }).addClass('active').animate({
        opacity: 1.0
    }, 500, function(){
        active_light.removeClass('active');
		active_light.attr('src', '');
		active_light.show();
    });
	
   
    if (next.attr('id') == $('#slideshow IMG:last').attr('id')) 
        $('.right_light').hide();
    if (next.attr('id') == $('#slideshow IMG:first').attr('id')) 
        $('.left_light').hide();

}
    
function slideSwitch() {
    var active = $('#slideshow IMG.active');

    if ( active.length == 0 ) active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var next =  active.next().length ? active.next()
        : $('#slideshow IMG:first');


    active.addClass('last-active');

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

