var aTabs = new Array("manchettes_24h", "manchettes_15j");
var aPubs = new Array("pub_banner", "pub_banner_2");

Event.observe(window, 'load', function() {
	
	addTargetBlank('a.blank');	
	
	var oTabs = new Tabs(aTabs, aPubs);
	oTabs.startTabs();

	if($('btn_partager')){
		initPartager();
	}
	
	if($('btn_envoyer')){
		initEnvoyer();	
	}
	
});

/**
 * This function will add a target _blank effect with the onclick attribute
 * on any css selector (i.e a.blank => all the a with the class blank)
 *
 */
addTargetBlank = function(sCssSelector){
    $$(sCssSelector).invoke('observe', 'click', function(e){
        // check if the element as the href attribute 
        if(this.href){
            window.open(this.href); 
            Event.stop(e);    
        }    
    });
}

function openFull(p,n) {
	var r     = null;
	r = window.open(p,n,'width='+screen.width+',height='+screen.height+',top='+0+',left='+0+','+'scrollbars=no,toolbar=no,location=no,status=no,menubar=no,resizable=yes,dependent=no');
	if(r!=null && window.focus) r.window.focus();
};

/**
 * Add event to show partager options in news details
 *
 */
function initPartager(){

	var x = {
		id: 'ctn_partager',
		hide: function(){ $(this.id).removeClassName('show'); this.visible = false; },
		show: function(){ $(this.id).addClassName('show'); this.visible = true; }, 
		over: false,
		visible: false
	}
	
	$('btn_partager').observe('click', function(e){
		x.visible == false ? x.show() : x.hide();
		Event.stop(e);
	});
	
	$$('body').invoke('observe', 'click', function(){
		(x.over == false && x.visible == true) ? x.hide() : null;
	});
	
	$('ctn_partager').observe('mouseover', function(){x.over = true;} );
	$('ctn_partager').observe('mouseout', function(){x.over = false;} );
}

/**
 * Add print button if the navigator can handle javascript print
 *
 */
function addPrintButton(){
	if (typeof(window.print) != "undefined") {
		document.write('<a href="javascript:window.print();" class="util" id="btn_imprimer">Imprimer</a>');
	}
}

/**
 * Create a popup to send to a friend
 *
 */
function initEnvoyer(){
	
	var sParam ='scrollbars=yes,resizable=yes,width=460,height=530';
	$('btn_envoyer').observe('click', function(e){
		
		window.open(this.href, 'win', sParam); 
		Event.stop(e);
	});
}

/************ TABS **************************/
var Tabs = Class.create({
	initialize: function(arrTabs, arrPubs) {
		this.aTabs = arrTabs;
		this.aPubs = arrPubs; 
	},

	// Set the click events on the tabs
	startTabs: function() {

		this.aTabs.each(function(name){
			
			// reference to the current tab and is content (div)
			var x = {
				tab: "btn_" + name, 
				div: name,
				setFocus: function(){
					$(this.tab).className = this.tab;
					$(this.div).className = 'holder';
				}
			};
			
			if($(x.tab)){
				
				// click event on each tabs
				$(x.tab).observe('click', function(e){
					// remove focus on all tab
					this.removeTabs();				   
					// set focus on current tab
					x.setFocus();
					// reset banner pos for IE
					this.resetBannerPos();
				}.bind(this));
			}
		}.bind(this));	
	},

	// Set all tabs and their content to off
	removeTabs: function(){

		this.aTabs.each(function(name){
			
			$('btn_' + name).className = 'btn_' + name + '_off';
			$(name).className = 'holder off';
		});
	},
	
	// Fix Ie bottom Position for the pubs
	resetBannerPos: function(){
		
		this.aPubs.each(function(name){
			
			if($(name)){
				var iPos = $(name).getStyle('bottom');
				$(name).setStyle( {bottom: ''});
				$(name).setStyle( {bottom: iPos});
			}
		});	
	}
});




