/*
	Class: Tips
		Display tips
		
	Usage:
		
	Parameters:
		When you initialize the class with addEvent, you can set some options.
		
	About:
		tips.js v.0.1 for mootools v1.2 - 02 / 2010
		
		Created by Sascha Boetzel, 
		Last modified by Sascha Boetzel
		
	Credits:
		Tips-Box is taken from FormCheck 1.5 (http://mootools.floor.ch/)
*/

var Tips = new Class({
	
	Implements: [Options, Events],

	options : {
		
		url	:	'/floor/cs?SRV=evok&L=fr&ACT=getAjaxItemDetail',
		
		tipsClass : 'fc-tbx',				//tips class
		errorClass : 'fc-error',			//div error class
		fieldErrorClass : 'fc-field-error',	//error class for elements

		display : {
			fixPngForIe : 1,
			replaceTipsEffect : 1,
			closeTipsButton : 1,
			tipsPosition : "right",
			tipsOffsetX : -45,
			tipsOffsetY : 0,
			listErrorsAtTop : false,
			fadeDuration : 300
		}
	},
	
	/*
	Constructor: initialize
		Constructor
	
		Add event on formular and perform some stuff, you now, like settings, ...
	*/
	initialize : function(container,options) {
		this.setOptions(options);
		
		if ($(container))	{
			this.container = $(container);
			this.container.getElements("*[class*=tips]").each(function(el) {
				if(el.getProperty("class").match(/tips\[(\d+)-(\d+)\]/)) {
					el.addEvent('click',function(ev)	{
						new Event(ev).stop();
						this.addTip(el);
					}.bind(this));
				}
			}, this);
			
			if(this.options.display.fixPngForIe)
				this.fixIeStuffs();
		}
	},
	
	/*
	Function: addTip
		Private method
		
		Add tip
	*/
	addTip : function(el)	{
		var url = this.options.url+'&CID='+el.getProperty("class").match(/tips\[(\d+)-(\d+)\]/)[1]+'&IID='+el.getProperty("class").match(/tips\[(\d+)-(\d+)\]/)[2]+'&layout=tips';
		new Request({
			url: url,
			method: 'get',
			onFailure: function(instance){},
			onSuccess: function(result){
				this.showTip(el,result);
			}.bind(this)
		}).send();
	},
	
	/*
	Function: showTip
		Private method
		
		show tip
	*/
	showTip : function(obj,result) {
		//determine position
		var coord = obj.getCoordinates();
		
		if(!obj.element) {
			var pos = (this.options.display.tipsPosition == 'left') ? coord.left : coord.right;
			var options = {
				'opacity' : 0,
				'position' : 'absolute',
				'float' : 'left',
				'left' : pos + this.options.display.tipsOffsetX
			}
			obj.element = new Element('div', {'class' : this.options.tipsClass, 'styles' : options}).injectInside(document.body);
			//this.addPositionEvent(obj);
		}		
		if (obj.element && obj.element != true) {
			//obj.element.empty();
			var tips = this.makeTips(result).injectInside(obj.element);
			if(this.options.display.closeTipsButton) {
				tips.getElements('a.close').addEvent('mouseup', function(){
					this.removeTip(obj);
				}.bind(this));
			}
			obj.element.setStyle('top', coord.top - tips.getCoordinates().height + this.options.display.tipsOffsetY);
			
			obj.element.setStyle('opacity', 1);
		}		
	},
	
	/*
	Function: addPositionEvent
		
		Update tips position after a browser resize
	*/
	addPositionEvent : function(obj) {
		obj.event = function(){
			var coord = obj.target ? $(obj.target).getCoordinates() : obj.getCoordinates();
			obj.element.setStyles({ 
				'left':coord.right + this.options.display.tipsOffsetX,
				'top':coord.top - obj.element.getCoordinates().height + this.options.display.tipsOffsetY
			});
		}.bind(this)
		window.addEvent('resize', obj.event);
	},
	
	/*
	Function: removeTip
		Private method
		
		Remove the Tip display
	*/
	removeTip : function(obj) {
		obj.element.destroy();
		obj.element = false;
	},
	
	/*
	Function: fixIeStuffs
		Private method
		
		Fix png for IE6
	*/
	fixIeStuffs : function () {
		if (Browser.Engine.trident4) {
			//We fix png stuffs
			var rpng = new RegExp('url\\(([\.a-zA-Z0-9_/:-]+\.png)\\)');
			var search = new RegExp('(.+)formcheck\.css');
			for (var i = 0; i < document.styleSheets.length; i++){
				if (document.styleSheets[i].href.match(/formcheck\.css$/)) {
					var root = document.styleSheets[i].href.replace(search, '$1');
					var count = document.styleSheets[i].rules.length;
					for (var j = 0; j < count; j++){
						var cssstyle = document.styleSheets[i].rules[j].style;
						var bgimage = root + cssstyle.backgroundImage.replace(rpng, '$1');
						if (bgimage && bgimage.match(/\.png/i)){
							var scale = (cssstyle.backgroundRepeat == 'no-repeat') ? 'crop' : 'scale';
							cssstyle.filter =  'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src=\'' + bgimage + '\', sizingMethod=\''+ scale +'\')';
							cssstyle.backgroundImage = "none";
						}
					}
				}
			}
		}
	},
	
	/*
	Function: makeTips
		Private method
		
		Create tips boxes
	*/
	makeTips : function(txt) {
		var table = new Element('table');
			table.cellPadding ='0';
			table.cellSpacing ='0';
			table.border ='0';
			var tbody = new Element('tbody').injectInside(table);
				var tr1 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'tl'}).injectInside(tr1);
					new Element('td', {'class' : 't'}).injectInside(tr1);
					new Element('td', {'class' : 'tr'}).injectInside(tr1);
				var tr2 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'l'}).injectInside(tr2);
					var cont = new Element('td', {'class' : 'c'}).injectInside(tr2);
						var divc = new Element('div', {'class' : 'err'}).injectInside(cont);
							new Element('p',{'html' : txt}).injectInside(divc);
						if (this.options.display.closeTipsButton) new Element('a',{'class' : 'close'}).injectInside(cont);
					new Element('td', {'class' : 'r'}).injectInside(tr2);
				var tr3 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'bl'}).injectInside(tr3);
					new Element('td', {'class' : 'b'}).injectInside(tr3);
					new Element('td', {'class' : 'br'}).injectInside(tr3);
		return table;
	}
});