
var rotateIntervalId;

$(function() {

	$('a[rel="lightbox[roadtrip]"]').lightBox({
		overlayBgColor: '#dadada',
		overlayOpacity: 0.6,
		imageLoading: 'templates/486/g_files/lightbox-ico-loading.gif',
		imageBtnClose: 'templates/486/g_files/lightbox-btn-close.gif',
		imageBtnPrev: 'templates/486/g_files/lightbox-btn-prev.gif',
		imageBtnNext: 'templates/486/g_files/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Zdjêcie',
		txtOf: 'z'
	});
	
});

function theRotator() {
    //Set the opacity of all images to 0
    $('div.rotator ul li').css({opacity: 0.0});
    
    //Get the first image and display it (gets set to full opacity)
    $('div.rotator ul li:first').css({opacity: 1.0});
	
    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    if ( $('div.rotator ul li').length > 1)
    {    
        rotateIntervalId=setInterval('rotate()',6000);
    }
}

function rotate() {	
    //Get the first image
    var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator ul li:first');

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
    
    var last = ((current.last().length) ? ((current.last().hasClass('show')) ? $('div.rotator ul li:first') :current.last()) : $('div.rotator ul li:first'));
//alert(last);
//alert(next.index());

    //Un-comment the 3 lines below to get the images in random order
    
    //var sibs = current.siblings();
    //var rndNum = Math.floor(Math.random() * sibs.length );
    //var next = $( sibs[ rndNum ] );
	    

    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 3000);

    //Hide the current image
    current.animate({opacity: 0.0}, 3000)
    .removeClass('show');
    
};

function setRotateLi(index)
{
	var i=0;
	$("div.rotator ul li").each(function(){
	    if (i==index)
	    {
		//.css({opacity: 1.0})
		$(this).css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 3000);
	    }
	    else
		$(this).animate({opacity: 0.0}, 3000).removeClass('show');
	    i++;
        });
}

function clicker()
{
    var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));
    if ( current.length == 0 ) current = $('div.rotator ul li:first');
    var rotateLength = $('div.rotator ul li').length;

    if (rotateLength<2)
	return 0;

    $('div.rotator').append('<div class="left" style="display: block; position: absolute; left: 30px; top: 100px; height: 50px; width: 50px;"></div>');
    $('div.rotator').append('<div class="right" style="display: block; position: absolute; right: 30px; top: 100px; height: 50px; width: 50px;"></div>');
/*
    var outer_mousemove = false;

    $('div.rotator .left').hover(function(){
       outer_mousemove = true;
    }, function(){
       outer_mousemove = false;
    }).mousemove(function(){
       if( outer_mousemove ){
       }
    });
*/
    $('div.rotator .left').mouseenter(function() {
	//$('div.rotator .left').css("border", "solid 1px red");
	if ($('div.rotator .left .clicker').length == 0){
    	    $('div.rotator .left').append('<div class="clicker" style="display: block; position: absolute; height: 10px; width: 10px;color: #FFF; font-size: 20px;"><<</div>');
	}
	$('div.rotator .left .clicker').click(function() {
	    //alert('clcik');
	    
	    var last = current.index();
	    if (last == 0)
	    {
	        last=rotateLength;
	    }
	    last--;
	    setRotateLi(last);

	    current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

	    clearInterval(rotateIntervalId);
            rotateIntervalId=setInterval('rotate()',6000);
	});
    });
    $('div.rotator .left').mouseleave(function() {
	//$('div.rotator .left').css("border", "none");
	$('div.rotator .left .clicker').remove();
    });

    $('div.rotator .right').mouseenter(function() {
	if ($('div.rotator .right .clicker').length == 0){
    	    $('div.rotator .right').append('<div class="clicker" style="display: block; position: absolute; height: 10px; width: 10px;color: #FFF; font-size: 20px;">>></div>');
	}
	$('div.rotator .right .clicker').click(function() {
	    var next = current.index();
	    var last = rotateLength;
	    next++;
	    if (next == last)
	    {
	        next=0;
	    }
	    setRotateLi(next);

	    current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

	    clearInterval(rotateIntervalId);
            rotateIntervalId=setInterval('rotate()',6000);
	});
    });
    $('div.rotator .right').mouseleave(function() {
	$('div.rotator .right .clicker').remove();
    });
}

$(document).ready(function() {		
    //Load the slideshow
    theRotator();
    $('div.rotator').fadeIn(1000);
    $('div.rotator ul li').fadeIn(1000); // tweek for IE
    //clicker();
});
