/** CEJ JavaScript Document
* @author sam rossetti / http://www.contreforme.ch
*/
function init(){
	var digests		= 	$$('.rolling_digest');
	// --- digests ---
	digests.each(function(el,i){
		if(el.getElements('li').length){
			el.header_i = 0;
			roll(el);
		}
	});
	// --- authors' list accordion and scroll ---
	if($$('.toggler')){
		var target,
			scrol = new Fx.Scroll(window),
			acc = new Fx.Accordion($$('.toggler'),$$('.stretcher'),{
				display:-1,
				alwaysHide:true,
				initialDisplayFx:false,
				onActive: function(tog,el){
					target = tog.getPrevious('h1.block_top');
					tog.set('html','Masquer les articles');
				},
				onBackground: function(tog,el){
					tog.set('html','Afficher les articles');
				},
				onComplete:function(){
					scrol.start(0,target.getPosition().y);
				}
			});
	}
	// --- external links ---
	$$('#main_col a','#social_networks a','#partners a','#footer a').each(function(el,i){
		// --- external ---
		if(el.get('rel') && el.get('rel') == 'external'){
			el.set('target','_blank');
		// --- check others ---
		}else if(el.get('href') && el.get('href')[0] != '?' && el.get('href')[0] != '#' && !el.get('href').test('mailto:')){
			el.set('target','_blank');
		}
	});
	// --- print button ---
	if($('article_print')){
		$('article_print').addEvent('click',function(ev){
			var ev = new Event(ev).preventDefault();
			window.print();
		});
	}
	// --- editions links slider ---
	if($('editions_list_bottom_link')){
		var list = $('editions_list');
		$('editions_list_bottom_link').setStyle('display','block');
		$$('#editions_list li').each(function(el,i){
			if(i>9) el.toggleClass('hidden');
		});
		$('editions_list_bottom_link').addEvent('click',function(ev){
			$$('#editions_list li').each(function(el,i){
				if(i>9) el.toggleClass('hidden');
			});
			$('editions_list_bottom_link').toggleClass('open');
			if($('editions_list_bottom_link').hasClass('open')){
				$('editions_list_bottom_link').set('text','Afficher moins de numéros');
			}else{
				$('editions_list_bottom_link').set('text','Afficher plus de numéros');
			}
		});
	}
	// --- welcome edito slider ---
	if($('edito_bottom_link')){
		var el = $('edito_bottom_link'),
			ed = el.getPrevious('div.article');
		el.setStyle('display','block');
		ed.setStyle('overflow','hidden');
		ed.addClass('edito');
		// --- get min and max height values ---
		el.maxy = ed.getSize().y.toInt();
		el.miny = ed.getFirst('img').get('height').toInt()+ed.getFirst('h2').getSize().y.toInt()+195;
		
		var edi_fx = new Fx.Tween(ed).set('height',el.miny);
		// afficher la suite / réduire
		el.addEvent('click',function(ev){
			// --- toggle ---
			this.toggleClass('open');
			if(this.hasClass('open')){
				edi_fx.start('height',this.maxy);
				this.set('text','Réduire');
			}else{
				edi_fx.start('height',this.miny);
				this.set('text','Afficher la suite');
			}
		});
	}
	// --- image popups ---
	if($$('img.popup')){
		$$('img.popup').each(function(el,i){
			var src = el.src.replace('/mini_','/');
			var a = new Element('a',{ rel:'milkbox',href:src}).inject(el,'before');
			a.grab(el);
		});
	}
	// --- subscription form ---
	if($('cej_shop_form')){
		init_shop_form();
	}
	// --- scroll partners always in sight ---
	window.addEvent('scroll',function(ev){
		$('partners').tween('top',Math.max(215,15+window.getScroll().y));
	});
}
function init_shop_form(){
	$$('#no_javascript','#cej_cart_submit').setStyle('display','none');
	$$('label.shop_item').addEvent('click',add_shop_item);
}
function remove_shop_item(){
	if(this.id == 'cart_item_subscribe'){
		$$('.shop_item.sub').removeClass('selected');
	}else{
		$(this.id.replace('cart_item_','')).removeClass('selected');
	}
	this.dispose();
	update_cart();
}
function add_shop_item(){
	var id;
	// --- id is not unique for subscription as we do not allow more than one subscription ---
	if(this.id.test('sub_')){
		id = 'cart_item_subscribe';
	// --- other items get their own id ---
	}else{
		id = 'cart_item_'+this.id;
	}
	// --- if id already exists quit process ---
	if($(id)) return false;
	
	// --- select current item ---
	this.addClass('selected');
	
	// --- inject new cart item ---
	new Element('li',{
		'id':id,
		'html':this.get('title'),
		'rel':this.get('rel'),
		'events':{
			'click':remove_shop_item
		}
	}).inject($('cej_cart'),'top');
	
	// --- calculate price ---
	update_cart();
}
function update_cart(){
	// --- if no more cart items remove total, clean classes, display empty message, hide form and return ---
	if(!$$('label.shop_item.selected').length){
		// --- display empty message and hide submit button ---
		$('cej_form_empty_msg').setStyle('display','block');
		$('cej_cart_submit').setStyle('display','none');
		// --- empty pp cart fields ---
		$('cej_pp_cart_fields').empty();
		// --- if cart total dispose ---
		if($('cart_price')) $('cart_price').dispose();
		return;
	}
	
	$('cej_cart_submit').setStyle('display','block');
	$('cej_form_empty_msg').setStyle('display','none');
	
	// --- if cart price item do not exists creates it ---
	if(!$('cart_price')){
		new Element('li',{
			'id':'cart_price'
		}).inject($('cej_cart'),'bottom');
	}
	
	// --- init total ---
	var total = 0;
	
	// --- empty pp cart fields ---
	$('cej_pp_cart_fields').empty();
	
	// --- process every cart items and add to pp cart ---
	$$('#cej_cart li').each(function(el,i){
		// --- bypass empty message and cart total ---
		if(el.id != 'cej_form_empty_msg' && el.id != 'cart_price'){
			// --- add item ---
			new Element('input',{
				'type':'hidden',
				'name':'item_name_'+(i+1),
				'value':el.get('html')
			}).inject($('cej_pp_cart_fields'));
			
			// --- add item's value ---
			new Element('input',{
				'type':'hidden',
				'name':'amount_'+(i+1),
				'value':el.get('rel')
			}).inject($('cej_pp_cart_fields'));
			
			// --- price is stored in rel attribute ---
			total += el.get('rel').toInt();
		}
	});
	
	// --- display total price ---
	$('cart_price').set('html','Total : '+total+' CHF<input type="hidden" name="amount" value="'+total+'" />');
}
function encode(str){
	if(Browser.Engine.trident){
		str = escape(str);
	}else{
		str = encodeURIComponent(str);
	}
	return str;
}
function roll(dig){
	var lis = dig.getElements('li');
	lis.each(function(el,i){
		el.setStyle('top',"-"+(dig.header_i*1.95)+"em");
	});
	dig.header_i++;
	
	if(dig.header_i>=lis.length){
		dig.header_i = 0;
	}
	setTimeout(function(){
		roll(dig);
	},3000);
}
window.addEvent('domready',init);
