var isIE = (navigator.appVersion.indexOf("MSIE")==-1) ? false : true;

$(document).ready(function() {
    
    // anime les pics des galleries
    var animSpeed = 1500, animDelay = 3500;
    var mesPics = $('.gallery .galleryPic' );
    var fadeToPic = null;
    var numEncour =0;
    var picsAff;
    var picsEff;
    mesPics.css('display', 'none');


    fadeToPic = function () {

    	picsAff = mesPics[numEncour];
    	picsEff = (numEncour == 0) ? mesPics[mesPics.length-1] : mesPics[numEncour-1];

    	if ( !mesPics.length ) return;
    	$(picsEff).fadeOut(animSpeed);
    	$(picsAff).css( "display", "none" ).fadeIn( animSpeed, function () {
    		if (numEncour<mesPics.length-1)numEncour++
    		else numEncour =0;
    		window.setTimeout( fadeToPic, animDelay  );
    	} );
    };
    
    if ( $('.gallery img' ).length > 1 ) window.setTimeout( fadeToPic, 1 );
    else $('.gallery img' ).show();    
    
	$('.rose #bottomCarrousel .bottomSlides ul li').mouseover(function() {
        var num = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_') +1);
        $('#bottomCarrousel .bottomSlides ul li').removeClass('active');
        $('#bottomCarrousel .bottomSlides ul li').children('.logoOverlay').show();
        $(this).addClass('active');
        $(this).children('.logoOverlay').hide();
        var elt = $(this);
        $.ajax ({
       	    type : "GET",
            url : rootUrl + "home/" + num + "/bandePhoto.html",
            success: function (rsp) {
                $('#content .contenairTof').html(rsp);
                $('.prevArrow').attr('id', 'idprev_' + elt.prev().attr('id'));
                if (elt.prev().attr('id') == undefined) {
                    $('.prevArrow').attr('id', 'idnext_' + $('#bottomCarrousel .bottomSlides ul li').last().attr('id'));
                }
                $('.nextArrow').attr('id', 'idnext_' + elt.next().attr('id'));
                if (elt.next().attr('id') == undefined) {
                    $('.nextArrow').attr('id', 'idnext_' + $('#bottomCarrousel .bottomSlides ul li').first().attr('id'));
                }
            }
        });
    });
    
    $('.rose .prevArrow').click(function() {
        var id = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_') +1);
        var elt = $('#logo_' + id + ', #Blogo_' + id);
        var me = $(this);
        $.ajax ({
       	    type : "GET",
            url : rootUrl + "home/" + id + "/bandePhoto.html",
            success: function (rsp) {
                $('#content .contenairTof').html(rsp);
                $('#bottomCarrousel .bottomSlides ul li').removeClass('active');
                elt.addClass('active');
                $('.logoOverlay').show();
                elt.children('.logoOverlay').hide();
                me.attr('id', 'idprev_' + elt.prev().attr('id'));
                if (elt.prev().attr('id') == undefined) {
                    me.attr('id', 'idprev_' + $('#bottomCarrousel .bottomSlides ul li').last().attr('id'));
                }
                $('.nextArrow').attr('id', 'idnext_' + elt.next().attr('id'));
                if (elt.next().attr('id') == undefined) {
                    $('.nextArrow').attr('id', 'idnext_' + $('#bottomCarrousel .bottomSlides ul li').first().attr('id'));
                }
               bottomCarrousel.nbDecal = Math.floor (104 / 8) ;
               bottomCarrousel.repeatScroll('left');
            }
        });
    });
    $('.rose .nextArrow').click(function() {
        var id = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_') +1);
        var elt = $('#logo_' + id + ', #Blogo_' + id);
        var me = $(this);
        $.ajax ({
       	    type : "GET",
            url : rootUrl + "home/" + id + "/bandePhoto.html",
            success: function (rsp) {
                $('#content .contenairTof').html(rsp);
                $('#bottomCarrousel .bottomSlides ul li').removeClass('active');
                elt.addClass('active');
                $('.logoOverlay').show();
                elt.children('.logoOverlay').hide();
                me.attr('id', 'idnext_' + elt.next().attr('id'));
                if (elt.next().attr('id') == undefined) {
                    me.attr('id', 'idnext_' + $('#bottomCarrousel .bottomSlides ul li').first().attr('id'));
                }
                $('.prevArrow').attr('id', 'idprev_' + elt.prev().attr('id'));
                if (elt.prev().attr('id') == undefined) {
                    $('.prevArrow').attr('id', 'idprev_' + $('#bottomCarrousel .bottomSlides ul li').last().attr('id'));
                }
				bottomCarrousel.nbDecal = Math.floor (104 / 8);
                bottomCarrousel.repeatScroll("right");
            }
        });
        
    });
    
    $('.peopleDesc').click(function() {
        $('.peopleResume').addClass('hidden');
        $('.peopleDesc').css('font-weight', 'normal');
        var id = $(this).attr('id').substr(7);
        $(this).css('font-weight', 'bold');
        $('#res_people_' + id).removeClass('hidden');
    });
    $('.peopleResume').children('.close').click(function() {
        $(this).parent().addClass('hidden');
        $('.peopleDesc').css('font-weight', 'normal');
    });
    
    $('.rollBlock, .checkIt').mouseover(function() {
        $(this).children('.rollBlock-hiddenText').show();
    });
    $('.rollBlock, .checkIt').mouseout(function() {
        $(this).children('.rollBlock-hiddenText').hide();
    });
    
    $('#formStory form').submit(function() {
        if ($('.formActions').html() == '' ) {
            return false;
        }
    });
    $('#formStory #text').keyup(function() {
        dispSubmitButton();
    });
    $('#formStory form, .commentForm form').validate();

	//$('.scroll-pane').jScrollPane();
	
	$('.filters').mouseover(function() {
	    $(this).children('ul').show();
	});
	$('.filters').mouseout(function() {
	    $(this).children('ul').hide();
	});
	$('.scroll-pane').jScrollPane({
	    scrollbarWidth: 8
	});
	
	// On supprime ça car ça gène le clic d'un lien qui se trouve à l'intérieur de 
	// jScrollPaneContainer
	//$('#content .jScrollPaneContainer').children().not('.super-wide').css('width', '295px');
	
	$('#contactGroupForm').validate({
	    errorLabelContainer: '#errorMessage',
	    wrapper: 'p'
	});
	
	$(document).click(function() {
	    window.setTimeout(function() { $('#moduleMenu > li ul').fadeOut(); }, 10, true);
	});
	
	/* custom radio buttons */
	$('div.radio input[type="radio"]').addClass('customRadio');
    $('div.radio input[type="radio"][checked]').parent().addClass('checked');
    
    $('input.customRadio').click(function() {
       if ($(this).attr('checked') == true) {
          $(this).parent().parent().children('span').removeClass('checked');
          $(this).parent().addClass('checked');
       } 
    });
    
    $('.colorbox').colorbox({
        iframe: true,
        innerWidth: 390, 
        innerHeight: 420,
        close: 'FERMER'
    });    
    
    $('.printBt').click(function() {
        var url = $(this).attr('href');
        //window.print();
        if (url.length != 0 && url != '#') {
            openPrintPopup(url)
        }
        return false;
    });
}); // document.ready


function openPrintPopup( url ) {
    window.open( url, "Vivarte", "width=750,height=800,scrollbars=yes" );
}

function dispSubmitButton() {
    var html = '<button type="submit" class="formStory" onclick="$(\'#formStory form\').submit();">Envoyer</button>';
    $('.formActions').html(html);
}

function setVideoSrcValue( val ) {
    if (val.length != 0) {
        $('#videoSrc').val(val);
        dispSubmitButton();
    }
}

function Obs09Delegate( func, obj ) {
	return function () {
		return func.apply( obj, arguments );
	};
}

/*
 * id: ID du div ( les fleches doivent etre nommees leid-left et leid-right )
 * numSlides: Nombre de vignettes
 * offset: Largeur d'une vignette
 * 
 */

function initSlide( id, numSlides, offset, showWidth, nbItemScroll ) {
	var i,elt;
	var me =this;
	var lp;
	this.slideId = id;
	this.element = document.getElementById( id );
	if (this.element.tagName.toLowerCase() != "ul") {
		this.element = this.element.getElementsByTagName("ul")[0];
	}
	this.numSlides = numSlides;
	this.offset = offset;
	
	this.eltWidth = showWidth;
	//this.initPos = parseInt( this.element.style.left ) || 0;
	this.initPos = 0;
	this.element.style.left = this.initPos + "px";
	this.currentPos = this.initPos;
	this.maxPos = (this.numSlides * this.offset) - this.eltWidth + this.initPos;
	var me = this;
	
	
	this.doScroll = function ( dir ) {
		//var pos = this.currentPos + ( dir ? ( this.offset * nbItemScroll ) : - ( this.offset * nbItemScroll ) );
		var pos = dir ? this.maxPos : -this.maxPos;
		pos = Math.max( pos, this.initPos );
		pos = Math.min( pos, this.maxPos );
		this.currentPos = pos;
		this.targetPos = (0-pos);
		this.doAnim();
	}
	this.doAnim = function () {
		var p = parseInt( this.element.style.left ) || this.initPos;
		if (p != this.targetPos) {
			if (p < this.targetPos) {
				p = Math.min(this.targetPos, p + 8);
			} else {
				p = Math.max(this.targetPos, p - 8);
			}
		}

		this.element.style.left = p + "px";
	    me.lp = parseInt( this.element.style.left );
		var clp = p - this.initPos;
		if ( parseInt( this.element.style.left ) != this.targetPos )	window.setTimeout( Obs09Delegate( this.doAnim, this ), 50 );
	}
	
	/*this.doReplace = function () {
        var num = me.lp;
        
        if (num == undefined) num = 0;
        var item = 0;
	    var numItem = Math.floor( (-num) / me.offset );
	    var list = $('.bottomSlides ul').children();
	    
	    for (i = 0; i != numItem; i++) {
	        item = $('#item_' + i);
	        //item = $(list[i]);
	        item.appendTo('.bottomSlides ul');	        
	    }

	}*/
	// /

	//window.setInterval( Obs09Delegate( this.doReplace, this), 5000 )
	
	document.getElementById( id + "-left" ).onmouseover = Obs09Delegate( function () { this.doScroll(0); }, this );
	document.getElementById( id + "-right" ).onmouseover = Obs09Delegate( function () { this.doScroll(1); }, this );
	document.getElementById( id + "-right" ).onmouseout = document.getElementById( id + "-left" ).onmouseout = function () { 
		me.targetPos = me.element.style.left
	};
	this.doScroll();
}


function bigPlayer() {
    $('#vidPlayer').addClass('big');
    $('#brandText, .breadcrumbs, .prevArrow, .nextArrow, .filters, .vid h3, .vid p').hide();
    $('#player').attr('width', '570').attr('height', '321');
}

function smallPlayer() {
    $('#vidPlayer').removeClass('big');
    $('#brandText, .breadcrumbs, .prevArrow, .nextArrow, .filters, .vid h3, .vid p').show();
    $('#player').attr('width', '380').attr('height', '214');
}

function displayMenu() {
    $('#moduleMenu > li ul').show();
    window.setTimeout(function() { $('#moduleMenu > li ul').fadeOut(); }, 3000, true);
}



function carrousel( nbItems, offset, showWidth ) {
    this.items = nbItems;
    this.itemWidth = offset;
    this.setMove = 0;
    this.nbDecal = 0;

    this.id1 = $('#slideA');
    this.id2 = $('#slideB');
    
    this.speed = 8;
    this.maxWidth = this.items * this.itemWidth;
    
    var me = this;
    
    this.id2.css('left', this.maxWidth + 'px');
    
    this.moveCarrousel = function (sens, fixPos) {
        var moving1, moving2;

        if ( sens == 'right' ) {
        	// Decalage left
            moving1 =  fixPos != 0 ? fixPos : parseInt(me.id1.css('left')) - me.speed;
            moving2 =  fixPos != 0 ? fixPos + me.maxWidth : parseInt(me.id2.css('left')) - me.speed;
           	me.id1.css('left', moving1 + 'px');
        	me.id2.css('left', moving2 + 'px' );
        
			// replcaement de la 1ere liste        
	        if (parseInt(me.id1.css('left')) <= -(me.maxWidth)) me.id1.css('left', (me.maxWidth) + 'px');
	   		
	   		// replcaement de la 2nde liste     
	        if (parseInt(me.id2.css('left')) <= -(me.maxWidth)) me.id2.css('left', (me.maxWidth) + 'px');
        
        } else {
        	// Decalage left
            moving1 = parseInt(me.id1.css('left')) + me.speed;
            moving2 = parseInt(me.id2.css('left')) + me.speed;
           /* moving1 = pxMove != 0  ? parseInt(me.id1.css('left')) + pxMove : parseInt(me.id1.css('left')) + me.speed;
            moving2 = pxMove != 0  ? parseInt(me.id2.css('left')) + pxMove : parseInt(me.id2.css('left')) + me.speed;*/
            me.id1.css('left', moving1 + 'px');
        	me.id2.css('left', moving2 + 'px' );

        	var t1 =  parseInt( me.id1.css('left') );
        	var t2 =  parseInt( me.id2.css('left') );
        	
        	// replcaement de la 1er liste
        	if (parseInt(me.id2.css('left')) >= 0 && parseInt(me.id2.css('left')) <= 10 ) me.id1.css('left', t2-me.maxWidth + 'px'); // me.id1.css('left', -(me.maxWidth) + 'px');
        	
        	// replcaement de la 2nde liste
        	if (parseInt(me.id1.css('left')) >= 0 && parseInt(me.id1.css('left')) <= 10 ) me.id2.css('left', t1-me.maxWidth + 'px'); //me.id2.css('left', -(me.maxWidth) + 'px');
        	
        }       
    }


    this.lanceScroll = function (sens, decal) {
        this.setMove = window.setInterval(this.moveCarrousel, 50, sens, 0);
    }
    
    
    this.repeatScroll = function (sens) {
    	me.moveCarrousel( sens, 0 );
    	me.nbDecal--
    	if (!isIE) {
    	    if (me.nbDecal > 0)	window.setTimeout(bottomCarrousel.repeatScroll, 50, sens);
    	} else {
    	    if (me.nbDecal > 0)	window.setTimeout("bottomCarrousel.repeatScroll('"+sens+"')", 50);
    	}
    }
    
    this.stopScroll = function () {
        clearInterval(this.setMove);
    }
    
    
    $("#bottomCarrousel-right" ).mouseover(function() {
        me.lanceScroll('right');
    });
    $("#bottomCarrousel-right" ).mouseout(function() {
        me.stopScroll();
    });
    
    $("#bottomCarrousel-left" ).mouseover(function() {
        me.lanceScroll('left');
    });
    $("#bottomCarrousel-left" ).mouseout(function() {
        me.stopScroll();
    });

}

function goToCarrousel ( id ) {
	var dataCarou = $(".bottomSlides ul:first li" );
	var nb = dataCarou.length;

	var trouve;
	for (i = 0 ; i!= nb; i++) {
		if ( $(dataCarou[i]).attr('id') == 'item_' + id) trouve = i;
	}

	if (trouve != 0 )bottomCarrousel.moveCarrousel ('right', -104*trouve );
}

