// (c) Ariacom 2008 - $Revision: 34 $ $Date: 2008-02-18 11:28:39 +0100 (Mon, 18 Feb 2008) $
var abrmenu = {
	make: function () {
		var aTip = new Tips($$('.Tips'), {
			// fixed: true,
			initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
		});
	}
}

var abrpaneloverlay = {
	
	show: function(panelPrefix) {
		fxpanel = new Fx.Style($(panelPrefix+'_panel'), 'opacity', {duration: 500, wait: false}).set(0);
		fxfade = new Fx.Style($(panelPrefix+'_fade'), 'opacity', {duration: 500, wait: false}).set(0);
		fxpanel.start(1);
		fxfade.start(0.6);
		$(panelPrefix+'_panel').style.display='block';
		$(panelPrefix+'_fade').style.display='block';
	},
	
	hide: function(panelPrefix) {
		fxpanel = new Fx.Style($(panelPrefix+'_panel'), 'opacity', {duration: 500, wait: false}).set(1);
		fxfade = new Fx.Style($(panelPrefix+'_fade'), 'opacity', {duration: 500, wait: false}).set(0.6);
		fxpanel.start(0);
		fxfade.start(0);
		(function(){$(panelPrefix+'_panel').style.display='none'; $(panelPrefix+'_fade').style.display='none';}).delay(500);
	}
};
	
var abrlogin = {
	displayWithDomain: function(aDomain, aGUID, autoLoginURL){
		if (autoLoginURL) {
			top.location.target="_top";
			top.location=autoLoginURL;
		} else {
			abrpaneloverlay.show('login');
			var dragContainer = (window.ie6)?$('menu_container'):$('login_fade');
			$('login_panel').makeDraggable({'container': dragContainer, 'handle' : $('login_drag_handle')});
			$('login_domain').value=aDomain;
			$('domain').value=aGUID;
			// IE Workaround: a delay is necessary before calling the "focus" function
			(function(){this.focus();}).delay(600, $('user_name'));
		}
	}
};

var abrmain = {

	makeCommon: function () {
		this.makeErrorPanel();
		this.makeMessagePanel();
	},
	
	makeSearch: function () {
		abrmenu.make();
		this.makeCommon();
		// Make search panel draggable
		var dragContainer = (window.ie6)?$('main_container'):$('search_fade');
		$('search_panel').makeDraggable({'container': dragContainer, 'handle' : $('search_drag_handle')});
		this.removeEditLinks4ReportResults();
	},
	
	makeFolderPanel: function() {
		var folderWithoutEdition = false;
		var folderPanelLink = $('folder_panel_link');
		if (folderPanelLink.rev != 'disabled') {
			folderPanelLink.addEvent('click', function() {abrpaneloverlay.show('folder')});
			// Change opacity for "disabled" buttons in the folder panel
			$$('#folder_panel input[type=submit]').each(function(item, index){
				if (item.disabled) { 
					item.setOpacity(0.5);
					folderWithoutEdition = true;
				}
			}, this );
		
			// Make folder panel draggable
			var dragContainer = (window.ie6)?$('main_container'):$('folder_fade');
			$('folder_panel').makeDraggable({'container': dragContainer, 'handle' : $('folder_drag_handle')});
		} else {
			folderPanelLink.setOpacity(0.5);
			folderPanelLink.add
		}
		return folderWithoutEdition;
	},
	
	makeDetail: function () {
		var reportActions;
		var reportList;
		
		// Hide the report list if we are at domain level
		if ($('data_folder_path').getText()) {
			// Setup actions...
			reportActions = new ActionList({
				actions: $('reportActions').getElements('input'),
				threshold: ['1+','1+','0','1+','1','1']
			}); 

			// Activate checkbox list handling for reports
			reportList = new CheckboxList({
				boxes: $('reportListForm').getElements('input[name=ReportSelection]'),
				allboxes: $('checkall'),
				listeners: [reportActions]
			});
		} else {
			$('report_list').setStyle('display','none');
		}
			
		// Remove links for non-reports in the list
		this.removeEditLinks4ReportResults();
		var folderWithoutEdition = this.makeFolderPanel();
		this.makeSearch();
	},
	
	makeErrorPanel: function() {
		// Hide the error panel
		var mySlide;
		mySlide = new Fx.Slide('error_panel');
		mySlide.hide();
		// Show it (with Slide In effect) only if an error occured
		if (abrErrorMessage) {
			$('error_panel').setStyle('display','block');
			$('error_panel').setText(abrErrorMessage);
			mySlide.slideIn();
		}
	},
	
	makeMessagePanel: function() {
		// Hide the error panel
		var mySlide;
		mySlide = new Fx.Slide('message_panel');
		mySlide.hide();
		// Show it (with Slide In effect) only if an error occured
		if (abrMessage) {
			$('message_panel').setStyle('display','block');
			$('message_panel').setHTML(abrMessage);
			mySlide.slideIn();
		}
	},
	
	removeEditLinks4ReportResults: function() {
		// IE does not use the "baseURI" property....
		if (window.ie) {
			var baseURI = location.href.slice(0,location.href.search(/brweb.dll/));
		}
		
		$$('a').each(function(anElement){
			// First condition is for Firefox and Safari, second condition is for IE...
			if ((anElement.href == anElement.baseURI)|| (anElement.href == baseURI)) anElement.remove();
		});
	}
};

var CheckboxList = new Class({

	options: {
		boxesCount: 0,
		boxes: [],
		listeners: [],
		allboxes: Class.create()
	},
	
	initialize: function (options) {
		this.setOptions(options);
	
		this.options.boxesCount = this.options.boxes.length;
		this.options.boxes.each(function(item, index){
			item.checked = false;
			item.addEvent('click', function(){ this.onChange(); }.bind(this));
		}, this);
		this.options.allboxes.checked=false;
		this.options.allboxes.addEvent('click', this.toggleAll.bind(this));
		this.onChange();
	},
	
	onChange: function () {
		var lastCheck;
		this.checkedBoxesCount = 0;
		this.options.boxes.each(function(item, index){
			if (item.checked) { 
				this.checkedBoxesCount++;
				// Keep the lastCheck value to find the checked box if only one is checked
				lastCheck = index;
			}
		}, this);
		this.options.allboxes.checked = (this.checkedBoxesCount == this.options.boxesCount);
		this.options.listeners.each(function(item, index){
			item.fireEvent("listChange", [this.checkedBoxesCount]);
		}, this);
		
		// If only one checkbox is checked, we put the report name in the rename field
		if (this.checkedBoxesCount == 1) {
			// Get the box element that is checked
			var box = this.options.boxes[lastCheck];
			// Move to the next column to find the "display name" of the report.
			var report = box.getParent().getParent().cells[1]; // Fails in IE7 (does not know cells[1]?)
			$('report_rename').value = report.getText();
		};
	},
	
	toggleAll: function(){
		this.options.checkedCount = this.options.allboxes.checked?0:this.options.boxesCount;
		this.options.boxes.each(function(item, index) {
			item.checked = this.options.allboxes.checked;
		}, this);
		this.onChange();
	}
});

CheckboxList.implement(new Options);
CheckboxList.implement(new Events);

var ActionList = new Class({

	options: {
		actions: [],
		threshold:[],
		initiallyEnabled: [] 
	},


	initialize: function (options) {
		this.setOptions(options);
		this.addEvent('listChange', this.onListChange);
		this.options.actions.each( function(item, index) {
			this.options.initiallyEnabled[index] = !(item.disabled);
		}, this );
	},
	
	onListChange: function(aCount) {
		switch (aCount) {
			case 0:
				this.options.actions.each( function(item, index) {
					if (this.options.initiallyEnabled[index] && (this.options.threshold[index] == "0")) {
						this.enableItem(item);
					} else {
						this.disableItem(item);	
					}
				}, this );
				break    
			case 1:
				this.options.actions.each( function(item, index) {
						if (this.options.initiallyEnabled[index] && (this.options.threshold[index] == "1" || this.options.threshold[index] == "1+")) {
							this.enableItem(item);
						} else {
							this.disableItem(item);	
						}
				}, this );
				break
			default:
				this.options.actions.each( function(item, index) {
					if (this.options.initiallyEnabled[index] && this.options.threshold[index] == "1+") {
						this.enableItem(item);
					} else {
						this.disableItem(item);	
					}
				}, this );
		} 
	},
	
	enableItem: function(anItem) {
		anItem.setOpacity(1);
		anItem.disabled = false;
	},
	
	disableItem: function(anItem) {
		anItem.setOpacity(0.5);
		anItem.disabled = true;
	},
	disableAll: function() {
		this.options.actions.each( function(item, index) {
				this.disableItem(item);	
		}, this );
	}	
});
ActionList.implement(new Options);
ActionList.implement(new Events);


