//Fichier JS pour les "petits plus" propre au site
// STVI

$(document).ready(function(){
	$('#wantToSuscribe').click(function () {
        $('#newLogin').css('display','none');
        $('#suscribe').css('display','block').show('slow');
    });
    
    $('#wantToLogin').click(function () {
        $('#suscribe').css('display','none');
        $('#newLogin').css('display','block').show('slow');
    });
    
    // creation du menu :
    if($('#suscribe #frmMember').hasClass('styledform')){
    	var tabs = '<ol id="summaryTabs">';
    	var listBlock = Array('ownerMemberBlock','ownerAdrBlock');
    	var activeBlock = 'ownerMemberBlock';
    	
    	tabs +='<li class="ownerMemberBlock"><a href="#" class="active">'+$('#ownerMemberBlock legend').text()+'</a></li>';
    	tabs +='<li class="ownerAdrBlock"><a href="#">'+$('#ownerAdrBlock legend').text()+'</a></li>';
    	if($('#ownerVdiAdrBlock').text() != ''){
    		listBlock.push('ownerVdiAdrBlock');
    		tabs +='<li class="ownerVdiAdrBlock"><a href="#">'+$('#ownerVdiAdrBlock legend').text()+'</a></li>';
    	}
    	if($('#owneryourConjoint legend').text() != ''){
    		listBlock.push('owneryourConjoint');
    		tabs +='<li class="owneryourConjoint"><a href="#">'+$('#owneryourConjoint legend').text()+'</a></li>';
    	}
    	if($('#ownerJustifBlock legend').text() != ''){
    		listBlock.push('ownerJustifBlock');
    		tabs +='<li class="ownerJustifBlock"><a href="#">'+$('#ownerJustifBlock legend').text()+'</a></li>';
    	}
    	if($('#pgmBlock legend').text() != ''){
    		listBlock.push('pgmBlock');
    		tabs +='<li class="pgmBlock"><a href="#">'+$('#pgmBlock legend').text()+'</a></li>';
    	}
    	
    	
    	$('#frmMember fieldset').add('#frmMember fieldset h3').css('display','none');
    	$('#prevSet').css('visibility','hidden');
    	$('#frmMember fieldset:first').add('#frmMember fieldset#sbt').add('#frmMember fieldset#offers').add('#frmMember fieldset#offers h3').css('display','block');
    	
    	tabs+='</ol>';
    	$('#suscribe #frmMember').prepend(tabs);
    	$('#summaryTabs li a').click(function(e){
    		e.preventDefault();
    		activeBlock =$(this).parent().attr('class');
    		changeActiveBlock(activeBlock,listBlock);
    		return false;
    	});
    	
    	$('#nextSet').click(function(e){
    		e.preventDefault();
    		activeBlock = $('#summaryTabs li a.active').parent().attr('class');
    		for(i=0;i< (listBlock.length -1);i++){
    			if(listBlock[i] == activeBlock){
    				activeBlock =listBlock[i+1];
    				break;
    			}
    		}
    		changeActiveBlock(activeBlock,listBlock);
    		return false;
    	});
    	$('#prevSet').click(function(e){
    		e.preventDefault();
    		activeBlock = $('#summaryTabs li a.active').parent().attr('class');
    		for(i=(listBlock.length-1);i>0;i--){
    			if(listBlock[i] == activeBlock){
    				activeBlock =listBlock[i-1];
    				break;
    			}
    		}
    		changeActiveBlock(activeBlock,listBlock);
    		return false;
    	});
    	
    	//gestion des boutons precedents
    }
    
  //masquage / affichage du input file d'upload :
    $('#vdipropertyMode').click(function () {
        if($(this).val() =='upload'){
            $('#vdipropertyBlock').css('display','block');
            $('#vdipropertyBlock').removeClass('hidden');
         }else{
            $('#vdipropertyBlock').css('display','none');
            $('#vdipropertyBlock').val('');
         }
     });
    //init :
    if($('#vdipropertyMode')){
    	if($('#vdipropertyMode').val() !='upload'){
    		$('#vdipropertyBlock').css('display','none');
    	}
    }
    
 	//masquage / affichage de la recup des coupons
    $('#pgm').click(function () {
        if($(this).val() =='season'){
            $('#recupcouponBlock').css('display','block');
            $('#recupcouponBlock').removeClass('hidden');
         }else{
            $('#recupcouponBlock').css('display','none');
            $('#recupcouponBlock').val('');
         }        
		$('#txtvip').css('display',($('#pgm').val() !='holiski')?('none'):('block'));
     });
    //init :
    if($('#pgm')){
    	$('#recupcouponBlock').css('display',($('#pgm').val() !='season')?('none'):('block'));
    	$('#txtvip').css('display',($('#pgm').val() !='holiski')?('none'):('block'));
    }
    
    //mak pr la date de naissance:
    $("#birthdate").add("#cbirthdate").add('.birth input').mask("99/99/9999");

    var childIndex = 0;
    if($('#nbChildren') && !isNaN(parseInt($('#nbChildren').val()))){
       childIndex = parseInt($('#nbChildren').val());
    }
    $('#addChildren').click(function(){
    	newChild =$('#yourfamilly').clone().attr('id','child'+childIndex);
        if(childIndex > 0){
            i=parseInt(childIndex-1);
            while(i>=0){
                if($('#child'+i)){
                    $('#child'+(i)).after(newChild);
                    i=-1;
                }
                i--;
            }
        }else{
            $('#yourfamilly').after(newChild);
        }
        $('#child'+childIndex+' input').each(function(){
            $(this).attr('name',$(this).attr('id')+childIndex);
            $(this).attr('id',$(this).attr('id')+childIndex);
        });
        $('#child'+childIndex+' label').each(function(){
            $(this).attr('for',$(this).attr('for')+childIndex);
        });
        $('#child'+childIndex).prepend('<legend>Enfant '+ (childIndex * 1 +1)+'</legend>');
        $('#child'+childIndex+' legend').add('#child'+childIndex).css('display','block');
        $('#child'+childIndex+' .birth input').mask("99/99/9999");
        childIndex++;
        $('#nbChildren').val(childIndex);
        return false;
     });
     
     //passage des images en lightbox:
     $('input.lightbox').lightBox(); // Select all links with lightbox class
     
     //gestion de sliens de validation / refus des justifs :
     $('#validFile').click(function(e){
        e.preventDefault();
        title= $(this).attr('title');
        url = $(this).attr('href');
        $('#confirm').modal({
            close:false,
            overlayId:'confirmModalOverlay',
            containerId:'confirmModalContainer',
            
            onShow: function (dialog) {
                dialog.data.find('.message').append(title);
                dialog.data.find('.yes').click(function () {
                    window.location.href = url;
                    // close the dialog
                    $.modal.close();
                });
             }
        });
        return false;
     });
     $('#rejectFile').click(function(e){
        e.preventDefault();
        title= $(this).attr('title');
        url = $(this).attr('href');
        $('#frmReject').modal({
            close: false,
            overlayId: 'contact-overlay',
            containerId: 'contact-container',
            onShow: function (dialog) {
                dialog.data.find('form').attr('action',url);
                dialog.data.find('.contact-title').html(title);
             }
        });
        return false;
     });
     
    
});


function changeActiveBlock(active,list)
{
	$('#summaryTabs li a').removeClass('active');
	$('.'+active+' a').addClass('active');
	$('#frmMember fieldset').add('#txtCnil').css('display','none');
	$('#frmMember fieldset#'+active).add('#frmMember fieldset#sbt').css('display','block');
	$('#prevSet').add('#nextSet').css('visibility','visible');
	
	if(active =='ownerMemberBlock'){
		$('#frmMember fieldset#offers').add('#txtCnil').css('display','block');
		$('#prevSet').css('visibility','hidden');
	}else if(active =='owneryourConjoint'){
			$('#frmMember fieldset[id^=child]').css('display','block');
	}
	
	if(active == list[list.length -1]){
		$('#nextSet').css('visibility','hidden');
	}
}
