jQuery(function($){

	if(window.location.pathname != '/'){
		$("#pano_w_slider_wrap").addClass('invis');
	}
	
	for(var i=1;i<=3;i++){
		$('.top_menu ul.menu li ul.c'+i+' .center').css({'width':($('.top_menu ul.menu li ul.c'+i).width()-16)+'px'});
	}

	var item_show = 5;
	var count = 0;
	var scrolling = 0;
	$('.carousel ul li img').each(function(){
		count++;
	});
	
	if(count==0){
		scrolling = 0;
	}else{
		if((count-item_show) < item_show ){
			scrolling = count - item_show;
		}else{
			scrolling = item_show;
		}
	}    

    /* carousel carousel kto ouspel - tot prisel */
    function activate_carousel(){
        $(".carousel").jCarouselLite({
            btnNext: ".slide_right",
            btnPrev: ".slide_left",
            mouseWheel: true,
            visible: item_show,
            circular: false,
			scroll: scrolling
        });
        $('.slider_bottom .center').css({'width':($('.slider').width()-6) + 'px'})
    }


    activate_carousel();
/*	$('.slider_bottom .center').css({'width':($('.slider').width()-6) + 'px'}) */
    document.activate_carousel = activate_carousel;

    var mouse_on_element;


    $(".carousel .photo").each(function(){
        $(this).data('bubble', $('.info_bubble', this));
        $('.info_bubble', this).data('parent', $(this));
    });

    function show_bubble(parent){
        var bubble_parent = $(parent);
        var bubble = bubble_parent.data('bubble');

        bubble.appendTo('body');
        var tt_offset = bubble_parent.offset();
        bubble.css('top', tt_offset.top - bubble.height()+ 20);
        bubble.css('left', tt_offset.left - (bubble.width() - (bubble_parent.width() + parseInt(bubble_parent.css('padding-left')) + parseInt(bubble_parent.css('padding-right')))) / 2);
        bubble.show();

    }

    function hide_bubble(parent){
        parent = $(parent);
        parent.data('bubble').hide().appendTo(parent);

    }

    $(".carousel .photo img")
        .bind('mouseover' ,function(e){
            var li = $(this).parent();
            if ($('body > .info_bubble').length > 0 && $('body > .info_bubble').data('parent')[0] == li[0]) {
                clearTimeout(window.bubble_mouseleave_timeout);
            }
            show_bubble(li[0]);
        })
        .bind('mouseleave', function(){
            var li = $(this).parent();
            window.photo_mouseleave_timeout = setTimeout(function(){
                hide_bubble(li[0]);
            },1);
        });

    $(".carousel .photo .info_bubble")
        .bind('mouseover' ,function(){
            clearTimeout(window.photo_mouseleave_timeout);
        })
        .bind('mouseleave' ,function(){
            var _this = this;
             window.bubble_mouseleave_timeout = setTimeout(function(){
                hide_bubble($(_this).data('parent')[0]);
            },10);
        });


    var protocol = window.location.protocol + '//';
    if(window.location.hostname != 'localhost'){
        var base_url  = protocol + window.location.hostname;
    }else{
        var base_url  = protocol + window.location.hostname + '/buschini';
    }


    $('.photo img').click(function(){
        $('.pano>img').attr('src',$(this).attr('src').replace('_slider','_pano'));

        var bubble = $('body>.info_bubble'); /*if user clicks on img*/
        if (bubble.size() == 0) bubble = $(this).siblings().first(); /*if we triggered 'click'*/
        var pano = $('.pano');

        $('.project span',pano).text($('.project span',bubble).text());
        $('.lieu span',pano).text($('.lieu span',bubble).text());

        var travaux_array = [];
        $('.travaux ul li',bubble).each(function(){
            travaux_array[travaux_array.length] = $(this).text();
        });

        $('.travaux span',pano).text(travaux_array.join(', '));
    }).first().trigger('click');

    $('#pano_toggle').click(function(){
        $('#pano_w_slider_wrap').slideToggle(500);
        return false;
    });

    if ($('#pano_toggle').size()) $('#pano_w_slider_wrap').hide()
        $('#pano_w_slider_wrap').removeClass('megamargin');
});
