// JavaScript Document

function slider() {
//Show the paging and activate its first link
$(".sl_paging").show();
$(".sl_paging a:first").addClass("sl_active");
$(".sl_siding_r").show();
// $(".siding_l").show();

//Get size of the image, how many images there are, then determin the size of the image reel.
var imageWidth = $(".sl_window").width();
var imageSum = $(".sl_image_reel img").size();
var imageReelWidth = imageWidth * imageSum;
var nr_zdjecia = 1;
var sl_r = true;
var sl_l = false;

//Adjust the image reel to its new size
$(".sl_image_reel").css({'width' : imageReelWidth});

//Paging  and Slider Function
rotate = function(){
    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

    $(".sl_paging a").removeClass('sl_active'); //Remove all active class
    $active.addClass('sl_active'); //Add active class (the $active is declared in the rotateSwitch function)

    //Slider Animation
    $(".sl_image_reel").animate({
        left: -image_reelPosition
    }, 500 );

	nr_zdjecia = $('.sl_paging a.sl_active').attr('rel');
	if ( nr_zdjecia == imageSum && sl_r == true ) {
		$(".sl_siding_r").hide();
		sl_r = false;
	}
	if ( nr_zdjecia > 0 && nr_zdjecia < imageSum && sl_r == false ) {
		$(".sl_siding_r").show();
		sl_r = true;
	}
	if ( nr_zdjecia == 1 && sl_l == true ) {
		$(".sl_siding_l").hide();
		sl_l = false;
	}
	if ( nr_zdjecia > 1 && nr_zdjecia < imageSum && sl_l == false ) {
		$(".sl_siding_l").show();
		sl_l = true;
	}
}; 

//Rotation  and Timing Event
rotateSwitch = function(){
    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        $active = $('.sl_paging a.sl_active').next(); //Move to the next paging
        if ( $active.length === 0) { //If paging reaches the end...
            $active = $('.sl_paging a:first'); //go back to first
        }
        rotate(); //Trigger the paging and slider function
    }, 7000); //Timer speed in milliseconds (7 seconds)
};

rotateSwitch(); //Run function on launch

//On Hover
$(".sl_image_reel a").hover(function() {
    clearInterval(play); //Stop the rotation
}, function() {
    rotateSwitch(); //Resume rotation timer
});	

//On Click
$(".sl_paging a").click(function() {
    $active = $(this); //Activate the clicked paging
    //Reset Timer
    clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
    rotateSwitch(); // Resume rotation timer
    return false; //Prevent browser jump to link anchor
});

//On Click siding_r
$(".sl_siding_r a").click(function() {
	$active = $('.sl_paging a.sl_active').next(); //Move to the next paging
    clearInterval(play); //Stop the rotation
	rotate(); //Trigger the paging and slider function
    rotateSwitch(); // Resume rotation timer
    return false; //Prevent browser jump to link anchor
});

//On Click siding_l
$(".sl_siding_l a").click(function() {
	$active = $('.sl_paging a.sl_active').prev(); //Move to the prev paging
	clearInterval(play); //Stop the rotation
	rotate(); //Trigger the paging and slider function
    rotateSwitch(); // Resume rotation timer
    return false; //Prevent browser jump to link anchor
});
}
