var first=true;
var stop=false;
var items_per_scroll=0;
var items_height;
var max_scrolable_index;
var count_items;
$(document).ready(function(){
$(function() {
	$(".scrollable").scrollable({ vertical: true, mousewheel: false });
    var item_height=$(".item").height();
    var area_height=$("#featured").height();
    list = document.getElementById("lists");
    items = list.getElementsByTagName("li");
    count_items=items.length;
    items_height=item_height*count_items;
    items_per_scroll=Math.round(area_height/item_height);
    max_scrolable_index=count_items-items_per_scroll;

    if(count_items<=items_per_scroll)
    {
        $('.next').hide();
        $('.prev').hide();
    }

    var scrolable_index=$(".scrollable").scrollable().getIndex();
    if(scrolable_index>=max_scrolable_index)
            {
                $('.next').hide();
                $('.prev').hide();
            }

    $('.next').click(function(){
        var scrolable_index=$(".scrollable").scrollable().getIndex();
        if(scrolable_index>=max_scrolable_index)
            {
                $('.next').hide();
                $('.prev').show();               
            }
    });
    $('.prev').click(function(){
              $('.next').show();
    });
    $('.scrollable').mousewheel(function(event, delta) {                        
                        var scrolable_index=$(".scrollable").scrollable().getIndex();
						if (delta > 0)
							{
                                 $(".scrollable").scrollable().prev();
                                 $('.next').show();                                
                            }
						else if (delta < 0)
							{
                                if(scrolable_index>=(max_scrolable_index-1))
                                {
                                    $('.next').hide();
                                    $('.prev').show();               
                                }
                                if(scrolable_index<max_scrolable_index)
                                {
                                    $(".scrollable").scrollable().next();                                    
                                }
                            }
                         if(count_items<=items_per_scroll)
                            {
                                $('.next').hide();
                                $('.prev').hide();
                            }
                      event.preventDefault();
					});

    //akcja scrola na duzym obrazku
    $('.ui-tabs-panel').mousewheel(function(event, delta) {
                        var scrolable_index=$(".scrollable").scrollable().getIndex();
						if (delta > 0)
							{
                                var id_p=$('.ui-tabs-selected').attr('id');
                                var arr=id_p.split('-');
                                id_p=arr[2];
                                if(id_p>1){
                                    $(".scrollable").scrollable().prev();
                                    $('.next').show();
                                    show_picture_script(parseInt(id_p)-1,true);
                                }
                            }
						else if (delta < 0)
							{
                                if(scrolable_index>=(max_scrolable_index-1))
                                {
                                    $('.next').hide();
                                    $('.prev').show();
                                }
                                if(scrolable_index<max_scrolable_index)
                                {                                   
                                  $(".scrollable").scrollable().next();                                  
                                }
                                
                                var id_p=$('.ui-tabs-selected').attr('id');
                                var arr=id_p.split('-');
                                id_p=arr[2];
                                if(count_items>id_p){
                                    show_picture_script(parseInt(id_p)+1,true);
                                }
                            }

                         if(count_items<=items_per_scroll)
                            {
                                $('.next').hide();
                                $('.prev').hide();
                            }
                      event.preventDefault();
					});


});


$('#prev-slide').click(function(){
	var id_p=$('.ui-tabs-selected').attr('id');
	var arr=id_p.split('-');
	id_p=arr[2];
	if(id_p>1){
		$(".scrollable").scrollable().prev();
		$('.next').show();
		show_picture_script(parseInt(id_p)-1,true);
	}
});

$('#next-slide').click(function(){

     var scrolable_index=$(".scrollable").scrollable().getIndex();
	 if(scrolable_index>=(max_scrolable_index-1))
		{
			$('.next').hide();
			$('.prev').show();
		}
		if(scrolable_index<max_scrolable_index)
		{
		  $(".scrollable").scrollable().next();
		}

		var id_p=$('.ui-tabs-selected').attr('id');
		var arr=id_p.split('-');
		id_p=arr[2];
		if(count_items>id_p){
			show_picture_script(parseInt(id_p)+1,true);
		}
});


autoSwitch(6000);
});


function start_play()
{
    stop=false;
}

function stop_play()
{
    stop=true;
}

function autoSwitch()
{
   
    if(!stop)
    {

        list = document.getElementById("lists");
        items = list.getElementsByTagName("li");
        var count_items=items.length;
        var id=$('.ui-tabs-selected').attr('id');
        arr=id.split('-');

        next_number=parseInt(arr[2])+1;
        if(next_number>count_items)
           { next_number=1; $(".scrollable").scrollable().begin();}

        if(first==true)
            {next_number=1;first=false;}

        if(items_per_scroll==0)
        {
            var area_height=$("#featured").height();
            var item_height=$(".item").height();
            items_per_scroll=area_height/item_height;     
        }

        var scrolable_index=$(".scrollable").scrollable().getIndex();
        if(next_number>items_per_scroll && scrolable_index<max_scrolable_index)
           {$(".scrollable").scrollable().next();}

       

        next_id=arr[1]+'-'+next_number;

		show_picture_script(next_number,false);

        var cur_item=$('.ui-tabs-selected').attr('id').split('-')[2];
        if(cur_item==count_items || count_items<=items_per_scroll)
             $('.next').hide();
        else
             $('.next').show();

    }

   
    setTimeout("autoSwitch()",5000);
    
}

// funckeje do przegladania zdjęć z efektami


//podstawowe funkcje przełaczania
function show_picture(id)
{
    stop=true;
    var el_id=id.substring(1);
	var arr_id=el_id.split('-');
	switch (efect) {
		case 'animate':
			show_picture_slide_animate(arr_id[1],true);
			break;
		case 'fade':
			show_picture_fade(arr_id[1],true);
			break;
		case 'slide_down':
			show_picture_slide_down(arr_id[1],true);
			break;
		case 'standard':
			show_picture_standard(arr_id[1],true);
			break;
		default:
			show_picture_standard(arr_id[1],true);
		}
}

function show_picture_script(id,stop_value){

	switch (efect) {
		case 'animate':
			show_picture_slide_animate(id,stop_value);
			break;
		case 'fade':
			show_picture_fade(id,stop_value);
			break;
		case 'slide_down':
			show_picture_slide_down(id,stop_value);
			break;
		case 'standard':
			show_picture_standard(id,stop_value);
			break;
		default:
			show_picture_standard(id,stop_value);
		}
}

// efekt przejscia animate rozmycie zdjęcia i pokazywanie się nowego
function show_picture_slide_animate(id,stop_elem)
{
	stop=stop_elem;
	$('div .active').animate({opacity: 0.0}, 2500 );
	$('.ui-tabs-panel').removeClass('active');
	$('.ui-tabs-panel').removeClass('z-index-active');
	$('.ui-tabs-panel').addClass('z-index-none');
	$('#fragment-'+id).addClass('active');
	$('#fragment-'+id).animate({opacity: 1.0},2500);
	$('#fragment-'+id).removeClass('z-index-none');
	$('#fragment-'+id).addClass('z-index-active');
	$('.ui-tabs-nav-item').removeClass('ui-tabs-selected');
    $('#nav-fragment-'+id).addClass('ui-tabs-selected');
}



// efekt przejscia slide down
function show_picture_slide_down(id,stop_elem)
{
	stop=stop_elem;
	$('.ui-tabs-panel').attr('style','display:none');
	$('.ui-tabs-panel').removeClass('active');
	$('.ui-tabs-panel').removeClass('z-index-active');
	$('.ui-tabs-panel').addClass('z-index-none');
	$('#fragment-'+id).addClass('active');
	$('#fragment-'+id).slideDown(1200);
	$('#fragment-'+id).removeClass('z-index-none');
	$('#fragment-'+id).addClass('z-index-active');
    $('.ui-tabs-nav-item').removeClass('ui-tabs-selected');
    $('#nav-fragment-'+id).addClass('ui-tabs-selected');
}


//efek przejscia poprzez znikanie i pojawianie się
function show_picture_fade(id,stop_elem)
{
	stop=stop_elem;
    	$('.ui-tabs-panel').fadeTo("slow",0.0);
	$('.ui-tabs-panel').removeClass('active');
	$('.ui-tabs-panel').removeClass('z-index-active');
	$('.ui-tabs-panel').addClass('z-index-none');
	//$('.ui-tabs-panel').attr('style',$('.ui-tabs-panel').attr('style')+' z-index:1;');
	$('#fragment-'+id).addClass('active');
	$('#fragment-'+id).fadeTo("slow", 1.0);
	$('#fragment-'+id).removeClass('z-index-none');
	$('#fragment-'+id).addClass('z-index-active');

    $('.ui-tabs-nav-item').removeClass('ui-tabs-selected');
    $('#nav-fragment-'+id).addClass('ui-tabs-selected');
}

// brak efektu przejscia
function show_picture_standard(id,stop_elem)
{
    stop=stop_elem;
	$('.ui-tabs-panel').attr('style','display:none');
	$('.ui-tabs-panel').removeClass('active');
    $('#fragment-'+id).show();//fadeTo("fast",1.0);
	$('#fragment-'+id).addClass('active');
	$('.ui-tabs-nav-item').removeClass('ui-tabs-selected');
    $('#nav-fragment-'+id).addClass('ui-tabs-selected');
}

