var show = function(url, target) {
	if (typeof(target)!="string") { target = "_blank"; };
	var w = window.open(url, target);
	w.focus();
}

var $$$ = MochiKit.DOM.getElementsByTagAndClassName;

addLoadEvent(function() {
	fixform('login');
	connectOver($$$(null, 'area_p'), efectoAreaP);
	connectOver($$$(null, 'opcion_p'));
	connectClick($$$(null, 'opcion_p'), goLink);
	connectOver($$$(null, 'area_d'), efectoAreaD);
	connectOver($$$(null, 'opcion_d'));
	connectClick($$$(null, 'opcion_d'), goDigest);
	var a = $('digest0');
	if (a != null) { a.href = "javascript:show(\""+a.href+"\");"; }
	anim();
});

var connectOver = function(elems, fcn) {
	var overFcn = function(ver) {
		if (ver != hasElementClass(this, 'over')) {
			toggleElementClass('over', this);
			this.overFcn(ver);
		}
	}
	if (typeof(fcn) == "undefined") {fcn = function() {} };
	forEach(elems, function(elem) {
		elem.overFcn = fcn;
		connect(elem, 'onmouseenter', bind(overFcn, elem, true));
		connect(elem, 'onmouseleave', bind(overFcn, elem, false));
	});
}

var connectClick = function(elems, fcn) {
	forEach(elems, function(elem) {
		connect(elem, 'onclick', partial(fcn, elem));
	});
}

var efectoAreaP = function(ver) {
	Morph(this, {duration :0.3, style: {"height": (ver ? "124" : "56")+"px"}});
}

var efectoAreaD = function(ver) {
	Morph(this, {duration :0.16, style: {
			"height": (ver ? "55" : "28")+"px",
			"width":  (ver ? "410" : "200")+"px"}
	});
}

var efectoOpcionD = function(ver) {
	var act = 'digest' + (ver ? this.id.substring(1) : '0');
	var remove = function(n) {
		var x = $('digest'+n);
		if (x != null) { addElementClass(x, 'invisible'); }
	};
	map(remove, [0,1,2,3]);
	removeElementClass(act, 'invisible');
}

var goLink = function(btn) {
	var u=btn.id+'.php';
	if (btn.id == 'pedidos') { u = "https://ssl.acens.com/firmainf.es/pedidos.php"; }
	document.location = u;
}

var goCatg = function(n) {
	var names = ['infbancos', 'infnewsletters', 'infdiversos', 'infcomerc'];
	goLink($(names[n-1]));
}

var goDigest = function(btn) {
	var n = btn.id.substring(1);
	var url = 'boletines.php';
	if (n==0) {
		document.location = url;
	} else {
		url = $('digest'+btn.id.substring(1)).href;
		show(url, 'digest');
	}
}

var anim = function() {
	var x = [[0,95],[0.75,192],[1.5,289],[2.25,386], [3,483]];
	imgs = $$$('img', null, 'fotos');
	var w = getElementDimensions('fotos').w;
	var i = 0;
	forEach(imgs, function(img) {
		var xx = x[i++];
		Morph(img, {duration:4,delay:xx[0],style:{left: (w-xx[1])+'px'}});
	});
	Morph('conozca', {duration:1.5,delay:5.5,style:{color:'#3975b1'}});
}

var fixform = function(s) {
	if ($(s) == null) return;
	var ls = $$$('label',null,s);
	forEach(ls, function(l) {
		setStyle(l, {display: ((navigator.userAgent.indexOf('Firefox') != -1) ? '-moz-inline-box' : 'inline-block')});
		w = getStyle(l, 'width');
		t = scrapeText(l);
		e = {'style':{'display':'block','text-align':'right','width':w}};
		replaceChildNodes(l, SPAN(e,t));
	});
}
