/* ********************************* APPEL DES FONCTIONS */

$(document).ready(function() {	
    if(window.chemin) jQuery('#conteneur').load(chemin + page_par_defaut + ' ' + conteneur);
	tabsMaker();						// tabs eco reportage
	//openCloseTirette();
	ecoForm();
	flashReflexe();
	scrollGeste();
	scrollEngagement();
	scrollGeste2();
	cycleActus();
	cycleProjets();
	cycleHistorique();					// cycle historique
	cycleHistoriqueSetDesc();			// affiche les descriptions de l'historique
	soutienForm();
	flashVideoTitanic();
	pushTabs();
	setTimeout(function(){
		flashTabsControls();                // handles flash stop playing by switching tabs
	},1500);
	addEventFormAutre();
	searchForm();
	communauteContactForm();
	toggleReceivers();
	anchorGestes();
	//toggle menu-outils
	if( jQuery("#lien-outils").length < 1 ) return false;
	jQuery('#lien-outils').hover(function(){
		$(this).toggleClass('active');
		$(this).prev().slideToggle();
		return false;
	});
});
/* ********************************* FLASH VIDEO NICOLAS HULOT */
var flashVideoTitanic = function() {
	if( jQuery("#flash-video-titanic").length < 1 ) return false;
	
	var flashvars = {};

	var params = {};
	params.menu = "false";
	params.quality = "high";
	params.wmode = "transparent";
	
	var attributes = {};
	swfobject.embedSWF("swf/Compteur.swf", "flash-video-titanic", "382", "227", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);	
}

function flashTabsControls() {
    var tabs  = jQuery('#push-tabs');
    if (!tabs[0]) {
        return false;
    }
    var items = tabs.find('.ui-tabs-nav li a');
    var fragments = tabs.find('.ui-tabs-panel');
    var init = function() {
        if (tabs.find('object, embed').length > 0) {
            items.each(function(i){
                this.iPosition = i;
            });
            items.bind('click',function(e){
                fragments.find('object, embed').each(function(){
                    var flash_id = this.id;
					try {
						swfobject.getObjectById(flash_id).stopVideo();
                    } catch(e) {
						throw('Err flash not found');
					}
                });
            });
        }
    }();
}

/* ********************************* TABS */
var tabsMaker = function() {
	if( $(".taber").size() < 1 )
		return false;
	
	$('.taber > ul').tabs()
};

var pushTabs = function() {
	if( $("#push-tabs").size() < 1 )
		return false;
		$('#push-tabs > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 7000);

};

function tabsStopRotation() {
    $('#push-tabs > ul').tabs('rotate',0);
}


/* ********************************* OPEN CLOSE MOBILISEZ VOUS */
var openCloseTirette = function() {
	if( $(".tirette").size() < 1 )
		return false;
	
	$('.oc-content').hide();
	$('.oc-link a').click(function() {
		if($('.oc-content').is(":hidden")) {
			$('img', this).attr('src','css/skin/fr/bt-close-tirette.gif');
			$('img', this).attr('alt','Fermer');
		}
		else {
			$('img', this).attr('src','css/skin/fr/bt-open-tirette.gif');
			$('img', this).attr('alt','Ouvrir');
		}
		$('.oc-content').animate({height: "toggle"}, 300);
		return false;
	});
};

/* ********************************* FORM ECO REPORTAGE */
var ecoForm = function() {
	if( $("#ecoreportageForm").size() < 1 )
		return false;
		
	if( ($("#pays option:selected").val() != 'FR') && ($("#pays option:selected").val() != 'BE') ) {
		$('#cpZone').hide();
	}
	
	$('#pays').change( function() {
	if( ($("#pays option:selected").val() == 'FR' ) || ($("#pays option:selected").val() == 'BE')){
		 $('#cpZone').show(); 
	}
	else{
		 $('#cpZone').hide();
	   	 }
	});
};
/* ********************************* FLASH BONS REFLEXES */
var flashReflexe = function() {
	if( jQuery("#anim-reflexe").length < 1 ) return false;
	
	var flashvars = {};
	flashvars.xmlPath = "/sites/all/themes/sitenicolashulot/xml/conf-carousel.xml";

	var params = {};
	params.menu = "false";
	params.quality = "high";
	params.wmode = "transparent";
	
	var attributes = {};
	swfobject.embedSWF("/sites/all/themes/sitenicolashulot/swf/anim-reflexe.swf", "anim-reflexe", "600", "595", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);	
};

/* ********************************* JSCOLLPANE */
var scrollGeste = function() {
	if( jQuery("#pane2").length < 1 ) return false;
	
	$('#pane2').jScrollPane({showArrows:true, scrollbarWidth: 8});
};

/* ********************************* JSCOLLPANE */
var scrollEngagement = function() {
	if( jQuery("#pane3").length < 1 ) return false;
	$('#pane3').jScrollPane({showArrows:true, scrollbarWidth: 8});
};

/* ********************************* JSCOLLPANE */
var scrollGeste2 = function() {
	if( jQuery(".scroll").length < 1 ) return false;
		$('.scroll').jScrollPane({showArrows:true, scrollbarWidth: 8});
	};


/* ********************************* CYCLE ACTUS */
function cycleActus() {
	if( $(".my-actu").length < 1) return false;
	
	if($(".my-actu li").length <= 4)
	{
		$('.btPrevious').remove();
		$('.btNext').remove();
		return false;
	}
	
	$('.my-actu').cycle({ 
		fx: 	 'scrollHorz',
		prev:   '.btPrevious', 
	    next:   '.btNext', 
		after: 	onAfter,
		timeout: 0,
		nowrap:	1,
		pager:  '#my-actu-pager span'
    });
		
	function onAfter(curr, next, opts) {
		var index = opts.currSlide;

		if(index == 0)
			$('.btPrevious img').attr('src', $('.btPrevious img').attr('src').replace('_on.gif', '_off.gif'));
		else
			$('.btPrevious img').attr('src', $('.btPrevious img').attr('src').replace('_off.gif', '_on.gif'));
		
		if(index == opts.slideCount - 1)
			$('.btNext img').attr('src', $('.btNext img').attr('src').replace('_on.gif', '_off.gif'));
		else
			$('.btNext img').attr('src', $('.btNext img').attr('src').replace('_off.gif', '_on.gif'));
	}
};

/* ********************************* CYCLE HISTORIQUE */




function cycleHistorique() {
	if( $('.histoCycle').length < 1 ) return false;
	$('#histoCycle-i').cycle({ 
		fx: 	 'scrollHorz',
		prev:   '.btPrevious-1', 
	    next:   '.btNext-1', 
		nowrap:	1,
		after: 	onAfter1,
	    timeout: 0,
		pager:  '#pagerCycle div',
		pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
        	return '#pagerCycle li:eq(' + idx + ') a'; 
    	}
    });
	
	$('.histoCycle #pagerCycle div').cycle({ 
		fx: 	 'scrollHorz',
		prev:   '.btPrevious-2', 
	    next:   '.btNext-2',  
		nowrap:	1,
		after: 	onAfter2,
	    timeout: 0
    });
	
	function onAfter1(curr, next, opts) {
		var index = opts.currSlide;
		if(index == 15)
        {
            $('.histoCycle #pagerCycle div').cycle(1); 
        }
        else if(index == 14)
        {
            $('.histoCycle #pagerCycle div').cycle(0); 
        }

		else if(index == 0)
			$('.btPrevious-1 img').attr('src', $('.btPrevious-1 img').attr('src').replace('_on.png', '_off.png'));
		else
			$('.btPrevious-1 img').attr('src', $('.btPrevious-1 img').attr('src').replace('_off.png', '_on.png'));
		
		if(index == opts.slideCount - 1)
			$('.btNext-1 img').attr('src', $('.btNext-1 img').attr('src').replace('_on.png', '_off.png'));
		else
			$('.btNext-1 img').attr('src', $('.btNext-1 img').attr('src').replace('_off.png', '_on.png'));
	}
	
	function onAfter2(curr, next, opts) {
		var index = opts.currSlide;
		
		if(index == 0)
			$('.btPrevious-2 img').attr('src', $('.btPrevious-2 img').attr('src').replace('_on.gif', '_off.gif'));
		else
			$('.btPrevious-2 img').attr('src', $('.btPrevious-2 img').attr('src').replace('_off.gif', '_on.gif'));
		
		if(index == opts.slideCount - 1)
			$('.btNext-2 img').attr('src', $('.btNext-2 img').attr('src').replace('_on.gif', '_off.gif'));
		else
			$('.btNext-2 img').attr('src', $('.btNext-2 img').attr('src').replace('_off.gif', '_on.gif'));
	}

  initcycle();
};


function cycleHistoriqueSetDesc() {
    if( $('.histoCycle').length < 1 ) return false;
	
	$('.histoCycle .histo-frise li a').click(function(){
        jQuery('#conteneur').load(jQuery(this).attr('href').split('#')[0] + ' ' + conteneur);
        return(false);
	});
};


function initcycle(){

    var hash = window.location.hash;
    if(hash)
    {
        window.scrollTo(0,0);

        jQuery('a[href]').each(function(){
        	var ancre = jQuery(this).attr('href').split('#')[1];
            
            
            if("#" + ancre == hash){
            	var annee = hash.slice(0,9);
            	var i = 0;
            	jQuery('#pagerCycle div li a').each(function(){
            		//var txt = jQuery(this).text();
            		var txt = jQuery(this).attr("href");
            		
            	    if(annee == txt)
            	    {
            	    	$('#histoCycle-i').cycle(i);
            	    	if(i<=13){
            	    		$('.histoCycle #pagerCycle div').cycle(0);
            	    	}
            	    	else if(i>13){
            	    		$('.histoCycle #pagerCycle div').cycle(1);
            	    	}
            	    	else if(i>26){
            	    		$('.histoCycle #pagerCycle div').cycle(2);
            	    	} 	
            	    }
            	    i++;
            	});
            }
        });
        
    }
};

/* ********************************* FORMULAIRE SOUTIEN */
function soutienForm(){
	$('#fnh-soutien-demande-form fieldset').not('.first').hide();
	
	// affichage du formulaire a la selection dun type de soutien
	$('#fnh-soutien-demande-form #edit-type').each(function(){
		switch($('#fnh-soutien-demande-form #edit-type').attr('value')){
			case '2':
				$('#fnh-soutien-demande-form fieldset').not('.first').show();
				$('#fnh-soutien-demande-form .none').hide();
				$('.coeur').show();
				break;
			case '1':
				$('#fnh-soutien-demande-form fieldset').not('.first').show();
				$('#fnh-soutien-demande-form .none').hide();
				$('.pouce').show();
				break;
			case '3':
				$('#fnh-soutien-demande-form fieldset').not('.first').show();
				$('#fnh-soutien-demande-form .none').hide();
				$('.bourses').show();
				break;
			default:
				$('#fnh-soutien-demande-form fieldset').not('.first').hide();
				$('#fnh-soutien-demande-form .none').hide();
				break;
		}
		
		$('#fnh-soutien-demande-form #edit-type').change(function(){
			switch($('#fnh-soutien-demande-form #edit-type').attr('value')){
				case '2':
					$('#fnh-soutien-demande-form fieldset').not('.first').show();
					$('#fnh-soutien-demande-form .none').hide();
					$('.coeur').show();
				break;
				case '1':
					$('#fnh-soutien-demande-form fieldset').not('.first').show();
					$('#fnh-soutien-demande-form .none').hide();
					$('.pouce').show();
				break;
				case '3':
					$('#fnh-soutien-demande-form fieldset').not('.first').show();
					$('#fnh-soutien-demande-form .none').hide();
					$('.bourses').show();
				break;
				default:
					$('#fnh-soutien-demande-form fieldset').not('.first').hide();
					$('#fnh-soutien-demande-form .none').hide();
				break;
			}
		});
	});
	
	// masquage de certains champs qui seront affiches selon certaines conditions affichees plus bas
	$('p.edit-id-type-organisme-1, p.edit-id-type-organisme-4, p.edit-type-projet-8, p.edit-entree-payante-oui').hide();
	
	// gestion du choix du type de structure
	$('input[name="id_type_organisme"]').each(function(){
		if($(this).attr('checked')==true){
			if($(this).attr('id')=='edit-id-type-organisme-1'){
				$('p.edit-id-type-organisme-4').hide();
				$('p.edit-id-type-organisme-1').show();
			}
			else if($(this).attr('id')=='edit-id-type-organisme-4'){
				$('p.edit-id-type-organisme-1').hide();
				$('p.edit-id-type-organisme-4').show();
			}
			else
				$('p.edit-id-type-organisme-1, p.edit-id-type-organisme-4').hide();
		}
		
		$('input[name="id_type_organisme"]').click(function(){
			if($(this).attr('checked')==true){
				if($(this).attr('id')=='edit-id-type-organisme-1'){
					$('p.edit-id-type-organisme-4').hide();
					$('p.edit-id-type-organisme-1').show();
				}
				else if($(this).attr('id')=='edit-id-type-organisme-4'){
					$('p.edit-id-type-organisme-1').hide();
					$('p.edit-id-type-organisme-4').show();
				}
				else
					$('p.edit-id-type-organisme-1, p.edit-id-type-organisme-4').hide();
			}
		});
	});
	
	// gestion du choix du type de projet
	$('input[name="type_projet"]').each(function(){
		if($(this).attr('checked')==true){
			if($(this).attr('id')=='edit-type-projet-8')
				$('p.edit-type-projet-8').show();
			else
				$('p.edit-type-projet-8').hide();
		}
		
		$('input[name="type_projet"]').click(function(){
			if($(this).attr('checked')==true){
				if($(this).attr('id')=='edit-type-projet-8')
					$('p.edit-type-projet-8').show();
				else
					$('p.edit-type-projet-8').hide();
			}
		});
	});
	
	// affichage du montant de lentree
	$('input[name="entree_payante"]').each(function(){
		if($(this).attr('checked')==true){
			if($(this).attr('id')=='edit-entree-payante-oui')
				$('p.edit-entree-payante-oui').show();
			else
				$('p.edit-entree-payante-oui').hide();
		}
		
		$('input[name="entree_payante"]').click(function(){
			if($(this).attr('checked')==true){
				if($(this).attr('id')=='edit-entree-payante-oui')
					$('p.edit-entree-payante-oui').show();
				else
					$('p.edit-entree-payante-oui').hide();
			}
		});
	});
};

function windowLocationReload() {
    window.location.reload();
}

function addEventFormAutre() {
    if(jQuery('#edit-field-evenement-type-427:checked').length == 1 || jQuery('#edit-taxonomy-8-427:checked').length == 1) {
      jQuery('.field-autre').show();
    } 
  
    jQuery('#edit-field-evenement-type-427').click(function(){
        jQuery('.field-autre').toggle();
    });
    
    jQuery('#edit-taxonomy-8-427').click(function(){
        jQuery('.field-autre').toggle();
    });
}



/* ********************************* JCYCLE projets  */
function cycleProjets() {
	if( $('.projetsCycle').length < 1 ) return false;
	$('#projetsCycle-i').cycle({ 
		fx: 	 'scrollHorz',
		prev:   '.btPrevious-1', 
	  next:   '.btNext-1', 
		nowrap:	0,
		pause: 1
    });
};



function contactsForm( dest, typeContact, destNetwork, oi_session_id) {
	var network = new Array('facebook', 'hi5', 'friendster', 'orkut', 'twitter', 'myspace');

	if(network.join().indexOf(typeContact)>=0){
		var email = $("#edit-field-dest-socialnetwork").val();
		field = 'edit-' + typeContact;
		fieldsession = 'edit-session' + typeContact;
		$("#"+field).val(destNetwork);
		$("#"+fieldsession).val(oi_session_id);
		
		var email = email.replace(new RegExp('<'+ typeContact +'.*?>', "ig"), '');
		if(email != ''){
			emails = email + ',' + dest;
		}
		else{
			emails = dest;
		}
		var emails = emails.replace(new RegExp(',,', "ig"), '');
		$("#edit-field-dest-socialnetwork").val(emails);
	}
	else{
		var email = $("#edit-field-dest").val();
		if(email != ''){
			emails = email + ',' + dest;
		}
		else{
			emails = dest;
		}
		var emails = emails.replace(new RegExp(',,', "ig"), '');
		$("#edit-field-dest").val(emails);
	}
	
}

function searchForm(){
	if( jQuery('#search-form').length < 1 ) return false;

	
}

function communauteContactForm(){
	if( jQuery('#pm-contact-display-form').length < 1 ) return false;
	jQuery('#pm-contact-display-form #edit-submit').click(function(){
		$('#pm-contact-display-form #edit-field-dest-socialnetwork').removeAttr("disabled"); 
	});
	
	
}
/* ********************************* contact communautaire toggle  */
var toggleReceivers = function() {
	if( jQuery('div.toggle').length < 1 ) return false;
	jQuery('.toggle-box').hide();
	jQuery('div.toggle .toggle-link').click(function(){
		jQuery(this).next('.toggle-box').slideToggle();
	});
}

function anchorGestes() {
	var hash = window.location.hash;
	if(hash && hash=='#engagements') {
		window.location.hash = hash;
	}
}
