

$(document).ready(function(){
    $('#espace_perso a[rel=confirm]').click(function () {
 	
        var res = confirm(this.title);
        if(res) {
            $.post(this.href,{
                ajax: "1"
            }); 		
            $(this).closest('div[rel=removable]').hide('slow'); 		
        }
        return false;
 	
    });

    if($("#main").find("#homeVideo").length > 0){
        $("#homeVideo").click().prettyPhoto({
            theme:'light_square',
            padding: 0,
            showTitle:false,
            allowresize:false,
            opacity:0.5,
            minWidth:300,
            showresize:true
        }); 
    }
    
    //if($("#main").find(".boxview").length > 0){
    $(".boxview").prettyPhoto({
        theme:'light_square',
        padding: 0,
        showTitle:false,
        allowresize:true,
        opacity:0.5,
        minWidth:300,
        showresize:true,
        doresize:true
    }); 
    //}
    
    
    if($("#main").find("#gallery").length > 0){
        setInterval('swapImg()', 5000);
    }
    
    if($("#main").find(".viewSpectacle").length > 0){
        $(".viewSpectacle").click(function(){
            $(location).attr('href',$(this).find("a").attr("href"));
        });
    }
    
    if($("#main").find(".read-next").length > 0){
        $(".read-next").mouseover(function(){
            $(this).find('img').attr('src',bu+'/img/read-next-hover.png');
        });
        
        $(".read-next").mouseout(function(){
            $(this).find('img').attr('src',bu+'/img/reservation.png');
        });
        
    }
    
    
    
    if($("#container").find("#defil").length > 0){
        
        $("#defil").click(function(e){
            $parent = $("#medias");
            //alert($parent.find("li").eq(0).find('img').attr("src"));
            saveelt =  $parent.find("li:eq(0)").find(".boxview");
                       
            $parent.find("li").each(function(){
               
                //alert($(this).attr("class"));
               
                current++;
                //if($(this).next('li').length > 0 ){
               
                if($parent.find("li").eq(current).length > 0 ){
         
                    //Enregistre element suivant
                    $next = $parent.find("li").eq(current).find(".boxview");
                    //remplace suivant par précédent
                    $next.replaceWith(saveelt);
                    saveelt = $next;
                }else{
                    dernier = $parent.find("li:eq("+ ($parent.find("li").length - 1) +")");
                    
                    //Le premier element reçoit le dernier
                    $('.m0').html(saveelt);  
                    
                    //Curseur 0
                    current = 0;
                }

                $(this).find(".boxview").prettyPhoto({
                    theme:'light_square',
                    padding: 0,
                    showTitle:false,
                    allowresize:true,
                    opacity:0.8,
                    minWidth:300
                });
                /*
                     * Corrige bug de decalage
                     */
                $(this).find(".boxview").css("marginLeft",0);
            });
            
            $parent.find("li:eq(0)").find('.boxview').hide().animate({
                'marginLeft' : -30
            },500, function() {
                // Animation complete.
                $(this).fadeIn(500).animate({
                    'marginLeft' : 0
                },500);
            });
            
            //            $parent.find("li:eq(0)").find('.boxview').fadeIn(500).animate({
            //                'marginLeft' : 0
            //            },500);
            
            $parent.find("li:eq(0)").find('.boxview').prettyPhoto({
                theme:'light_square',
                padding: 0,
                showTitle:false,
                allowresize:false,
                opacity:0.5,
                minWidth:300,
                showresize:false
            });
            $parent.find("li:eq(5)").find('.boxview').hide();
            
            e.preventDefault();
        });
        
    }
    
    
    if($("#container").find("#spectacles").find("fieldset").length > 0){
//$fieldsets = $("#container").find("#spectacles");
// $fieldsets.removeClass('opacity');
       
}
    
});







var current = 0;
var saveelt;

function swapImg(){

    var nbel = $("#main").find("#gallery").find("img").length;
    if(current < nbel -1){
        current++; 
    }else{
        current = 0; 
    }
    

    var $active = $('#gallery .active');
    var $next = $('#gallery img').eq(current);
    
    //alert($('#gallery .active').next('img').attr("alt"));
    
    $next.fadeIn(function(){
        $active.fadeOut().removeClass('active');
        $next.addClass('active');
    });

}

/*
$(function() {
	// here, we allow the user to sort the items
	$('#sort_video').sortable({
		axis: "y",
		placeholder: 'ui-state-highlight'

	});
 
	// here, we reload the saved order

});
     */


jQuery.fn.first = function() {
    return this.eq(0)
};
jQuery.fn.last = function() {
    return this.eq(this.size() - 1)
};


function moveup(obj) {
	
    myLi = ($(obj).closest('LI'));
    previousLi = myLi.prev();
    if(!previousLi[0]) {
        return;
    }
	
    myClone = myLi.clone();
    myClone.insertBefore(previousLi);
    //	myClone.hide();
    //	myLi.hide('fast',function() {$(this).remove();reInitArrows()});
    //	myClone.show('fast');
    myLi.remove();
    reInitArrows();
    return;
	
    myClone.hide();

    previousLi.animate({
        'top':(previousLi.height()+25)+"px"
    },'normal');
    myLi.animate({
        'top':-(previousLi.height()+25)+"px"
    },'normal',function(){
        myClone.show();
        myLi.hide();
        myLi.remove();
        previousLi.css('top',0);
        reInitArrows();
    });

	
	
}

function movedown(obj) {
    //alert(obj);
    myLi = ($(obj).closest('LI'));
    nextLi = myLi.next();
	
    if(!nextLi[0]) {
        return;
    }
		
    myClone = myLi.clone();
    myClone.insertAfter(nextLi);
    //myClone.hide();
    //myLi.hide('fast',function() {$(this).remove();reInitArrows();});
    //myClone.show('fast');
    myLi.remove();
    reInitArrows();
    return;
	
    nextLi.animate({
        'top':-(nextLi.height()+25)+"px"
    },'normal');
    myLi.animate({
        'top':(nextLi.height()+25)+"px"
    },'normal',function(){
        myClone.show();
        myLi.hide();
        myLi.remove();
        nextLi.css('top',0);
        reInitArrows();
    });
	
}

function agendaSelect(obj) {
    $('.agendaselected').removeClass('agendaselected').removeClass('bgcolored');
    $(obj).parent().addClass('agendaselected').addClass('bgcolored');
    $(obj).parent().fadeOut(100).fadeIn(100);
    return false;
}
function agendaUpdate(res) {
    $('#dates').fadeOut(100).fadeIn(100);
    $('#dates a:last')[0].focus();
    $('#dates a:last')[0].blur();
}


function agendaMonth() {
    $('#ajaxCalendar').fadeOut(100).fadeIn(100);
    return false;
}
