function carouselTabsShowHide (){
    $('.tabbed-carousels').each(function() {
        var tabs = $(this).find('ul.tabs:first');
        var panes = $(this).find('div.panes:first');

        tabs.find('li:first').addClass('first selected1');
        tabs.find('li:eq(1)').addClass('second');

        //add first-pane to first pane
        panes.find('div:first').addClass('first-pane displayed');

        //label and hide second pane
        panes.find('div:first').siblings('div').addClass('second-pane');
        panes.find('div:first').siblings('div').hide();

        //second tab click
        tabs.find('li:last a').click(function(evt) {

            $parentUL = $(this).parent().parent();

            //switch second tab to selected and remove from first tab (li)
            $parentUL.find('li:first').removeClass('selected1');
            $parentUL.find('li:eq(1)').addClass('selected2');

            //show second, hide first pane 
            $parentUL.siblings('div.panes:first').find('div:first').siblings('div').show().addClass('displayed');
            $parentUL.siblings('div.panes:first').find('div:first').hide().removeClass('displayed');
            evt.preventDefault();
        });

        //first tab click
        tabs.find('li:first').find('a').click(function(evt) {

            //switch first tab to selected and remove from second tab (li)
            tabs.find('li:eq(1)').removeClass('selected2');
            tabs.find('li:first').addClass('selected1');

            panes.find('div:first').siblings('div').hide();
            panes.find('div:first').show();
            evt.preventDefault();
        });

    });
}

function wrapLooseListItemsInTags () {
	$('.CarouselA, .CarouselB, .CarouselC, .CarouselD').each(function(){
		$(this).find('ul.navi:first a').wrap('<li></li>');
	});
}

function disabledotControls  (){
		$("ul.navi li a").unbind('click');
		$('ul.navi li a').click(function(){ 
			return false;
		});
		$('ul.tabs li a').removeAttr('href');
}

function hideEmptyNavigationElements () {
	$('.CarouselA, .CarouselB, .CarouselC, .CarouselD').each(function(){
		var eachList = $(this).find('ul.items:first');
		$(function (){
			if (eachList.find('.liItem').length < 1) {
				eachList.parents().siblings('ul.navi, .prev, .next, .controlButton').hide();
			}
			else return false;
		});
	});
}


function initCarousel(carouselId, itemsPerPage, naviId, naviItemsSel, intervalMs, speedMs){
    // initialize scrollable item
    var options = {
        size: itemsPerPage,
        navi: naviId,
        naviItem: naviItemsSel,
        interval: intervalMs,
        speed: speedMs,
        next: '.next',
        prev: '.prev',
        loop: true,
        easing: 'linear',
        api: true,
        onBeforeSeek: function(e) {
            
            if (this.getIndex() == this.getItems().length - itemsPerPage) {
                var carousel = $(carouselId + ' ul');
                carousel.fadeOut(intervalMs / 2);
            }
            return true;
        },
        onSeek: function() {
            
           if (this.getIndex() == 0) {
                var carousel = $(carouselId + ' ul');
                carousel.fadeIn(intervalMs / 2);
            }
        }
    }
    $(carouselId).scrollable(options);
    
   
}


function initCarouselControl(carouselId, controlId, autostart) {
    //console.log(typeof autostart, autostart);
    var control_button = $(controlId);
    var scrollableDiv = $(carouselId);
    var carousel = scrollableDiv.scrollable();
    var control_text;
    var control_class;
    ;
    // if only one page we hide the controls
    if (carousel.getPageAmount() <= 1) {
        control_button.hide();
        scrollableDiv.parent().find('.nextPage, .prevPage, .navi').hide();
        carousel.pause();
    } else {

        // if more than one page
        if (autostart !== "False") {
            control_text = 'Stop';
            control_class = 'stop';
            //carousel.play();
        }
        else {
            control_text = 'Play';
            control_class = 'play';
            carousel.pause();
            //console.log(carouselId + " is not running")
        }
        control_button.addClass(control_class).text(control_text);

        //onclick handler
        control_button.click(function() {
            var thisbutton = $(this);
            var thiscarousel = $(carouselId).scrollable();

            if (thisbutton.hasClass('play')) {
                //console.log(thiscarousel + " is running")
                thisbutton.text("Play");
                thisbutton.removeClass('play').addClass('stop');
                thiscarousel.play();
            }
            else {
                //console.log(thiscarousel + " is not running")
                thisbutton.text("Stop");
                thisbutton.removeClass('stop').addClass('play');
                thiscarousel.pause();
            }
        });
    }
}


// on DOM loaded
$(function() {

    carouselTabsShowHide();
    wrapLooseListItemsInTags();
    //disabledotControls();
    //hideEmptyNavigationElements();
    $('ul.items li:first').addClass('first');
});