jQuery(document).ready(function () {
    //Table functions - Faiz
    jQuery.fn.initMenu = function () {
        return this.each(function () {
            var theMenu = $(this).get(0);
            $('.acitem', this).hide();
            $('li.expand > .acitem', this).show();
            $('li.expand > .acitem', this).prev().addClass('active');
            $('li a', this).click(
				function (e) {
				    e.stopImmediatePropagation();
				    var theElement = $(this).next();
				    var parent = this.parentNode.parentNode;
				    if ($(parent).hasClass('noaccordion')) {
				        if (theElement[0] === undefined) {
				            window.location.href = this.href;
				        }
				        $(theElement).slideToggle('normal', function () {
				            if ($(this).is(':visible')) {
				                $(this).prev().addClass('active');
				            }
				            else {
				                $(this).prev().removeClass('active');
				            }
				        });
				        return false;
				    }
				    else {
				        if (theElement.hasClass('acitem') && theElement.is(':visible')) {
				            if ($(parent).hasClass('collapsible')) {
				                $('.acitem:visible', parent).first().slideUp('normal',
								function () {
								    $(this).prev().removeClass('active');
								}
							);
				                return false;
				            }
				            return false;
				        }
				        if (theElement.hasClass('acitem') && !theElement.is(':visible')) {
				            $('.acitem:visible', parent).first().slideUp('normal', function () {
				                $(this).prev().removeClass('active');
				            });
				            theElement.slideDown('normal', function () {
				                $(this).prev().addClass('active');
				            });
				            return false;
				        }
				    }
				}
		);
        });
    };
    // Do Table
    $('.menuFS').initMenu();
    //FS add
    var returnToStart = true;
    var transitionTime = 500;
    var affichePlayPause = true;
    var lectureAutomatique = true;
    var tempsAttente = 9000;
    var icones = new Array();
    icones['play'] = '/play_slider.png';
    icones['pause'] = '/pause_slider.png';

    var currentPosition = 0;
    var slideWidth = 642;
    var slides = $('.slide');
    var numberOfSlides = slides.length;

    //FS add
    var interval;
    var lectureEnCours = false;

    // Remove scrollbar in JS
    $('#slidesContainer').css('overflow', 'hidden');

    // Wrap all .slides with #slideInner div
    slides
	.wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
	    'float': 'left',
	    'width': slideWidth
	});

    // Set #slideInner width equal to total width of all slides
    $('#slideInner').css('width', slideWidth * numberOfSlides);

    // Insert controls in the DOM
    $('#slideshow')
	.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
	.prepend('<span class="control" id="leftControlEmpty">No earlier clips to show</span>')
	.append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Hide left arrow control on first load
    manageControls(currentPosition);

    // Create event listeners for .controls clicks
    $('.control')
	.bind('click', function () {
	    // Determine new position
	    currentPosition = ($(this).attr('id') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;

	    //FS add
	    //	if(currentPosition == numberOfSlides && returnToStart == false ){
	    //		currentPosition--;
	    //		pause();
	    //	}

	    // Hide / show controls
	    manageControls(currentPosition);
	    // Move slideInner using margin-left
	    $('#slideInner').animate({
	        'marginLeft': slideWidth * (-currentPosition)
	    }, transitionTime);
	});

    // manageControls: Hides and Shows controls depending on currentPosition
    function manageControls(position) {
        // Hide left arrow if position is first slide
        if (position == 0) { $('#leftControl').hide(); $('#leftControlEmpty').show(); } else { $('#leftControl').show(); $('#leftControlEmpty').hide(); }
        // Hide right arrow if position is last slide
        //if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }

        if (position == numberOfSlides - 1 && returnToStart == false) {
            $('#rightControl').hide();
        } else {
            $('#rightControl').show();
        }
        if (position == numberOfSlides && returnToStart == true) {
            currentPosition = 0;
            $('#leftControl').hide(); $('#leftControlEmpty').show();
        }
    }
    //FS add - Deal with Next
    function suivant() {
        //clearInterval(interval);
    	//lectureEnCours = false;
    	$('#rightControl').click();
    }
    function start() {
        lectureEnCours = true;
        interval = setInterval(suivant, tempsAttente);
    }
    function pause() {
        lectureEnCours = false;
        clearInterval(interval);
    }

    //FS add - Navigation and Autostart
    if (lectureAutomatique == true) {
        start();
    }
    if (affichePlayPause == true) {
        $('#slidesContainer').prepend('<img id="navigationSwitch" src="" alt="Navigation" />');
        if (lectureAutomatique == true) {
            $('#navigationSwitch').attr('src', icones['pause']);
        } else {
            $('#navigationSwitch').attr('src', icones['play']);
        }
        $('#navigationSwitch').bind('click', function () {
            if (lectureEnCours == true) {
                $(this).attr('src', icones['play']);
                pause();
            } else {
                $(this).attr('src', icones['pause']);
                start();
            }
        });
    }
    //FS add - Caption Sliding (Partially Hidden to Visible)
    $('.slide').hover(function () {
        $(".cover", this).stop().animate({ top: '90px' }, { queue: false, duration: 160 });
        $(".cover", this).stop().animate({ width: '480px' }, { queue: false, duration: 160 });
        $('.slide div').css('opacity', '1.0');
        $('.slide div').css('filter', 'alpha(opacity=100)');
        $('.slide div').css('filter', '"alpha(opacity=100)"');
    }, function () {
        $(".cover", this).stop().animate({ top: '90px' }, { queue: false, duration: 160 });
        $(".cover", this).stop().animate({ width: '480px' }, { queue: false, duration: 160 });
        $('.slide div').css('opacity', '0.8');
        $('.slide div').css('filter', 'alpha(opacity=80)');
        $('.slide div').css('filter', '"alpha(opacity=80)"');
    });
});
