// Copyright (c) 2009-2010 Daniel Ifrim

if(!Decorix) var Decorix = {};
Decorix.TooltipProduct = Class.create();
Decorix.TooltipProduct.prototype = {
    initialize: function(ids, tooltip_id, options) {
    	this.els = [];
    	for (var i=0; i< ids.length; i++) {
    		if ($(ids[i])) {
    			this.els[i] = $(ids[i]);
    		}
    	}
    	this.tooltip = $(tooltip_id);
    	if (!$(this.tooltip)) return;

    	this.is_visible = false;
    	this.runs = false;
		this.pipe_last = 0;
		this.pipe_current = this.pipe_last;
    	
    	this.options = options || {};
    	this.showClass = (this.options.delay || 0.1);
    	this.showClass = (this.options.show_class || "tooltip-prod-show");
    	this.hideClass = (this.options.hide_class || "tooltip-prod-hide");
        
    	for (var i=0; i< ids.length; i++) {
    		if (this.els[i]) {
    			Event.observe(this.els[i],'mouseover',this.elMouseOver.bindAsEventListener(this));
				Event.observe(this.els[i],'mouseout',this.elMouseOut.bindAsEventListener(this));
    		}
    	}
		
		Event.observe(this.tooltip,'mouseover',this.elMouseOver.bindAsEventListener(this));
		Event.observe(this.tooltip,'mouseout',this.elMouseOut.bindAsEventListener(this));
		
		this.basicLogger = (this.options.basic_logger || "my_logger");
		this.logEnable = (this.options.log_enable || false);
    },
    
    getNewPipeId: function() {
		this.pipe_last = this.pipe_last + 1;
		
		return this.pipe_last;
	},
	runNewPipe: function() {
		return this.pipe_current = this.getNewPipeId();
	},
    
	elMouseOver: function() {
		this.doMouseOver.delay(this.delay, this, this.runNewPipe());
	},
	
    doMouseOver: function(obj, pipe_local) {
    	if (obj.runs == false && obj.pipe_current == pipe_local && obj.is_visible == false) {
    		obj.runs = true;
    		obj.tooltip.addClassName(obj.showClass);
    		obj.tooltip.removeClassName(obj.hideClass);

			obj.is_visible = true;
			obj.my_log("show");
			obj.runs = false;;
    	}
    },
    elMouseOut: function() {
		this.doMouseOut.delay(this.delay, this, this.runNewPipe());
	},
    doMouseOut: function(obj, pipe_local) {
    	if (obj.runs == false && obj.pipe_current == pipe_local && obj.is_visible == true) {
    		obj.runs == true;
    		obj.tooltip.addClassName(obj.hideClass);
    		obj.tooltip.removeClassName(obj.showClass);

    		obj.is_visible = false;
    		
    		obj.my_log("hide");
    		obj.runs == false;
    	}
    },
	my_log: function(msg) {
		if (this.logEnable == true)
			$(this.basicLogger).innerHTML += "<br />" + msg;
	}
}

Decorix.Redirect = Class.create();
Decorix.Redirect.prototype = {
    initialize: function(path, location) {
    	this.els = $$(path);
    	this.location = location;
    	var redirecter = this;
    	redirecter.els.each( function(el,i) {
			Event.observe(el, "click", redirecter.click.bind(redirecter, redirecter.location));
		}); 
    },
    
    click: function(location) {
    	window.location = location;
    }
}

Decorix.AdresaTippers = Class.create();
Decorix.AdresaTippers.prototype = {
    initialize: function(options) {
    	this.options = options || {};
    	this.tippers = ($(this.options.tippers) || null);
    	this.sufix = (this.options.sufix || null);
    	this.company = ($(this.options.company) || null);
    	this.company_container = ($(this.options.company + this.sufix) || null);
    	this.cui = ($(this.options.cui) || null);
    	this.serienrbuletin = ($(this.options.serienrbuletin) || null);
    	this.cui_container = ($(this.options.cui + this.sufix) || null);
    	this.serienrbuletin_container = ($(this.options.serienrbuletin + this.sufix) || null);
    	this.regcom = ($(this.options.regcom) || null);
    	this.banca = ($(this.options.banca) || null);
    	this.banca_container = ($(this.options.banca + this.sufix) || null);
    	this.myiban = ($(this.options.myiban) || null);
    	
    	Event.observe(this.tippers,'change',this.eventChangeTippers.bindAsEventListener(this, this));
    	
    	this.initView(this);
    },
    initView: function() {
    	var adresa = this;
    	//adresa.changeView(adresa);
    },
    eventChangeTippers: function(event, adresa) {
    	adresa.changeView(adresa);
    },
    changeView: function(adresa) {
    	if (adresa.tippers.value == 1) {
    		adresa.company.value = "";
    		adresa.cui.value = "";
    		adresa.regcom.value = "";
    		adresa.banca.value = "";
    		adresa.myiban.value = "";
    		
    		adresa.company_container.hide();
    		adresa.cui_container.hide();
    		adresa.banca_container.hide();
    	} else {
    		adresa.company_container.show();
    		adresa.cui_container.show();
    		adresa.banca_container.show();
    	}
    }
}
