﻿function ClearOptionsFast(id){

	var selectObj = document.getElementById(id);
	var selectParentNode = selectObj.parentNode;
	var newSelectObj = selectObj.cloneNode(false);
	selectParentNode.replaceChild(newSelectObj, selectObj);
	return newSelectObj;
	
}

function show_sub_cats(suffixe){
	
	cat = $('cat_exposant'+suffixe).get('value');
	
	if(cat != '0'){
		
		var get_sous_cats = new Request.JSON({
			url : 'http://www.salondelhabitat.com/wp-content/themes/continuum/continuum/ajax/search_sous_cat.php',
			method : 'post',
			onSuccess : function(responseJSON, responseText){
				
				ClearOptionsFast('sous_cat_exposant'+suffixe);
				Object.each(responseJSON, function(value, key){
					new Element('option', {'value': key, 'text': value}).inject($('sous_cat_exposant'+suffixe));
					$('sous_cat_exposant'+suffixe).removeProperty('disabled');
					$('submit_expo'+suffixe).removeProperty('disabled');
				});
				
				update_libelle('cat_exposant'+suffixe+'_libelle', 'cat_exposant'+suffixe);
				
			}
		}).post({'cat':cat});
		
	}else{
		ClearOptionsFast('sous_cat_exposant'+suffixe);
		new Element('option', {'value': '0', 'text': 'Choisissez...'}).inject($('sous_cat_exposant'+suffixe));
		$('sous_cat_exposant'+suffixe).setProperty('disabled', 'disabled');
		$('submit_expo'+suffixe).setProperty('disabled', 'disabled');
	}
	
}

function update_libelle(id, ref_id){
	$(id).set('value', $(ref_id).getSelected().get('text'));
}

function show_featured(container){

	/* settings */
	var showDuration = 5000;
	var container = $(container);
	var featured_posts = container.getElements('div.featured_fdm_post');
	var featured_btns = container.getElements('div.featured_btns');
	var currentIndex = 0;
	var interval;
	
	/* opacity and fade */
	featured_posts.each(function(featured_post,i){ 
		if(i > 0) {
			featured_post.set('opacity', 0);
		}
	});
	/* opacity and fade */
	featured_btns.each(function(featured_a,i){ 
		if(i > 0) {
			featured_a.set('opacity', 0);
		}
	});
	
	/* worker */
	var show = function() {
		featured_posts[currentIndex].set('opacity', 0);
		featured_btns[currentIndex].set('opacity', 0);
		featured_posts[currentIndex = currentIndex < featured_posts.length - 1 ? currentIndex+1 : 0].set('opacity', 1);
		featured_btns[currentIndex].set('opacity', 1);
	};
	
	/* start once the page is finished loading */
	window.addEvent('load',function(){
		interval = show.periodical(showDuration);
	});
	
}

function randomize_background() {
	var images = new Array('/wp-content/themes/continuum/continuum/images/fdm/bg_sphynx.jpg', '/wp-content/themes/continuum/continuum/images/fdm/bg_mouton.jpg', '/wp-content/themes/continuum/continuum/images/fdm/bg_oie.jpg');
	var imageNum = Math.floor(Math.random() * images.length);
	document.getElementById('wrapper').style.backgroundImage = "url('" + images[imageNum] + "')";
}

function replace_accueil_url(page_id){
	expo_pages = ['page-id-692', 'page-id-753', 'page-id-755', 'page-id-757'];
	presse_pages = ['page-id-694', 'page-id-763'];
	
	accueil_breadcrumb = $$('.breadcrumbs a')[0];
	
	if(expo_pages.contains(page_id)){
		accueil_breadcrumb.set('href', '/?page_id=692');
	}else if(presse_pages.contains(page_id)){
		accueil_breadcrumb.set('href', '/?page_id=694');
	}
}

window.addEvent('domready',function() {

	if($('commerciaux') != null){
		show_featured('commerciaux');
	}
	
	if($('actus') != null){
		show_featured('actus');
	}
	
	// randomize_background();
	
	body_classes = $$('body')[0].get('class').split(' ');
	replace_accueil_url(body_classes[1]);
	
});
