/* The slide images are contained in the slides array. */
//--homepage

var pathHeadSlide = 'img/header/';
var pathSlideThumb = 'img/slider/thumbs/';
var pathSlideDiapo = 'img/slider/diapo/';

var slides = new Array(
    pathHeadSlide + 'index-header-1.jpg',
    pathHeadSlide + 'index-header-2.jpg',
    pathHeadSlide + 'index-header-3.jpg'
    );

//--catalogue
var slidesCatalogue = new Array(
    pathHeadSlide + 'catalogue-header-1.jpg',
    pathHeadSlide + 'catalogue-header-2.jpg',
    pathHeadSlide + 'catalogue-header-3.jpg'
    );

//--realisation
var slidesRealisations = new Array(
    pathHeadSlide + 'realisations-header-1.jpg',
    pathHeadSlide + 'realisations-header-2.jpg',
    pathHeadSlide + 'realisations-header-3.jpg'
    );

//--contact
var slidesContact = new Array(
    pathHeadSlide + 'contact-header-1.jpg',
    pathHeadSlide + 'contact-header-2.jpg'
    );


$(document).ready(function(){
	
	var formMessages = $('div.message');
	if (typeof(formMessages) != 'undefined' && formMessages.length > 0) {
		formMessages.show();
		
		setTimeout(function() {
			formMessages.fadeOut(1500);
		},3000);
		
		formMessages.hover(function() {
			formMessages.fadeOut(1500);
		});
		
	}
	
	
    //--SLIDER-MOVING-BOXES

    $('#project-slider').movingBoxes({
        startPanel   : 1,    // start with this panel
        wrap         : true, // psuedo wrapping - it just runs to the other end
        width        : 700,  // overall width of movingBoxes
        panelWidth : 0.4,
        fixedHeight: true,
        reducedSize: 1,
        imageRatio   : 16/9    // Image ration set to 1:1 (square image)
       // buildNav     : true, // if true, navigation links will be added
        /*navFormatter : function(){
            return "&#9679;";
        } // function which returns the navigation text for each panel*/
    });
	
    //--SLIDER-HEAD--//
   /* $('.arrow.left').click(function(){
        prev();
        clearInterval(auto);
    });
	
    $('.arrow.right').click(function(){
        next();
        clearInterval(auto);
    });*/

    /* Preloading all the slide images: */
    if (typeof($('.arrow-right')) != 'undefined') {
        for(var i=0;i<slides.length;i++)
        {
            (new Image()).src=slides[i];
        }
        transition(1);
        var auto;
        auto=setInterval(function(){
            next();
        },5*1000);
    }
    
    
    /**CONTACT**/
    
	var form = $('form#contact');
	if (typeof(form) != 'undefined') {
            
		/*var radioTypeClient = $('input[name=type-client]');
		var cbxParticulier = $('#cbx-particulier');

		var societeFields = $('.societe');
		var particulierFields = $('.particulier');
		var investissementFields = $('.investissement');
		var residenceFields = $('.residence');
		
		radioTypeClient.change(function(evt,obj) {
			var activeType = $('form#contact').find('input[name=type-client]:checked');
			if (activeType.val() === 'particulier') {
				particulierFields.show().attr('disabled','');
				societeFields.hide().attr('disabled','disabled');
			} else {
				particulierFields.hide().attr('disabled','disabled');
				societeFields.show().attr('disabled','');
			}
			
			if ("checked",form.find('input[name=type-client]:checked').val() === 'particulier') {
				
				cbxParticulier.trigger('change');
			}
			
		});
		
		cbxParticulier.change(function(evt,obj) {
			if($(this).val() === 'investissement-locatif') {
				investissementFields.show().attr('disabled','');
				residenceFields.hide().attr('disabled','disabled');
			} else {
				investissementFields.hide().attr('disabled','disabled');
				residenceFields.show().attr('disabled','');
			}
		});
		
		
		radioTypeClient.first().attr('checked','checked');
		
		radioTypeClient.trigger('change');*/
		
	}
    $(".fancy-img, .preloading-content a").fancybox({
        'opacity'		: true,
        'overlayShow'	: false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'none'
    });

    /** PATCH **/
        $('#project-slider').width(870);


    var pixelLength = 8.7;
    var h2 = $('.slider h2');
    var parentSize;
    var realSize;

    h2.each(function(index) {
        parentSize = $(this).width();
        realSize = $(this).html().length * pixelLength;
        var toInsert = '';
        for (var cpt = realSize; cpt < parentSize ; cpt+= pixelLength) {
            toInsert += '_';
        }
        $(this).parent().find('.surlign').html(toInsert);

    });

});

var current = {};
function transition(id)
{
	/* This function shows the individual slide. */
	
	if(!slides[id-1]) return false;
	
	if(current.id)
	{
		if(current.id == id) return false;
		current.layer.css('z-index',100);
		$('.mosaic-slide').not(current.layer).remove();
	}

	var newLayer = $('<div class="mosaic-slide">').html(generateGrid({rows:11,cols:10,image:slides[id-1]}));
	newLayer.css('z-index',10).css('height',213).css('overflow','hidden');
	
	$('#mosaic-slideshow').append(newLayer);
	
	var opac;
	if (typeof(jQuery.browser.msie) != 'undefined') {
		opac = 0.4;
	} else {
		opac = 0.1;
	}
	
    if(current.layer)
    {
        var tab = $(current.layer).children('.tile').length ;
        var temp=[];
        for(var i=0;i<tab;i++) temp[i]=i;
        temp.sort(function() {
            return 0.5 - Math.random()
        })
        $('.tile',current.layer).each(function(i){
            var currentTile = $(this);
            setTimeout(function(){
                
                //currentTile.css('visibility','hidden');
                var interval = setInterval(function() {
                   
                    currentTile.css('opacity',currentTile.css('opacity') - opac);
                    if (currentTile.css('opacity') <= 0.1) {
                        clearInterval(interval);
                        currentTile.css('opacity',0);
                    }
                },30);

            },temp[i]*10);
        })
    }
	
	/* Adding the current id and newLayer element to the current object: */
	current.id = id;
	current.layer = newLayer;
}

function next()
{
	if(current.id)
	{
		transition(current.id%slides.length+1);
	}
}

function prev()
{
	if(current.id)
	{
		transition((current.id+(slides.length-2))%slides.length+1);
	}
	
}

/* Width and height of the tiles in pixels: */
var tabwidth=65, tabheight=60;

function generateGrid(param)
{
	var elem = $([]),tmp;
	for(var i=0;i<param.rows;i++)
	{
		for(var j=0;j<param.cols;j++)
		{
			tmp = $('<div>', {
					"class":"tile",
					"css":{
						"background":'url('+param.image+') no-repeat '+(-j*tabwidth)+'px '+(-i*tabheight)+'px'
					}
			});
			elem = elem.add(tmp);
		}
		elem = elem.add('<div class="clear"></div>');
	}
	return elem;
}
