var tanx = null;

window.addEvents({
	'domready': function() {
		if (document.getElementById('bio-place') == null) return;
		document.getElement('div.preload').removeClass('preload');
		tanx = new TanxBody();
		tanx.corners.push(new Bio());
		tanx.corners.push(new Audio());
		tanx.corners.push(new News());
		tanx.corners.push(new Xtras());
		tanx.resize();
		
		tanx.introChain();

		tanx.moodal = new Moodal();
	},
	'resize': function() {
		if (document.getElementById('bio-place') == null) return;
		tanx.resize();
	}
});

var TanxBody = new Class ({
	initialize: function() {
		this.startCorners();
	},
	corners : [],
	resize : function () {
		this.calculateLayout();
		this.corners.each(function(c) {
			c.resize();
		});
	},
	introChain : function () {
		this.corners.each(function(c) {
			c.introChain();
		});
	},
	startCorners : function () {
		return;
		$$('div.cornhl').each(function(corner) {
			corner.addEvent('mouseenter', function(e){
				var el = $(e.target);
				if (!el.hasClass('cornhl')) {
					el = el.getParent('div.cornhl');
				}
				el.setStyle('opacity', 1);
			});
		 	corner.addEvent('mouseleave', function(e){
				var el = $(e.target);
				if (!el.hasClass('cornhl')) {
					el = el.getParent('div.cornhl');
				}
				el.setStyle('opacity', 0.85);
			});
		});
	},
	calculateLayout: function() {
		this.calcs = [];
		$$('div.wxpan').each( function(lay) {
			//alert (lay.get('wxw-pc')+"."+lay.get('wxw-px')+"."+lay.get('wxh-pc')+"."+lay.get('wxh-px'));
			var par = lay.getParent();
			var parC = par.getCoordinates();
			var pans = par.getElements('div.wxpan');
			if (!this.calcs.contains(par)) {
				this.calcs.extend([par]);
				var parInnerW = parC.width-par.getStyle('padding-left').toInt()-par.getStyle('padding-right').toInt();
				var parInnerH = parC.height-par.getStyle('padding-top').toInt()-par.getStyle('padding-bottom').toInt();
				var tot = 0;
				for (var i = 0; i < pans.length; i++) {
					var b = pans[i];
					if (b.getParent() != par) continue;
					if (b.get('wxh-px') != null) {
					
						tot += b.get('wxh-px')*1;
						b.setStyle('height', b.get('wxh-px')+"px");
						b.setStyle('width', parInnerW+"px");
					}
				}
				tot = parInnerH-tot;
				var h = tot;
				for (var i = 0; i < pans.length; i++) {
					var b = pans[i];
					if (b.getParent() != par) continue;
					if (b.get('wxh-pc') != null) {
						var h2 = Math.ceil(tot/100*b.get('wxh-pc'));
						if (h2 > h) h2 = h;
						b.setStyle('height', h2+"px");
						b.setStyle('width', parInnerW+"px");
						h -= h2;
					}
				}

				tot = 0;
				for (var i = 0; i < pans.length; i++) {
					var b = pans[i];
					if (b.getParent() != par) continue;
					if (b.get('wxw-px') != null) {
						tot += b.get('wxw-px')*1;
						
						b.setStyle('width', b.get('wxw-px')+"px");
						b.setStyle('height', parInnerH+"px");
					}
				}
				tot = parInnerW-tot;
				h = tot;
				for (var i = 0; i < pans.length; i++) {
					var b = pans[i];
					if (b.getParent() != par) continue;
					if (b.get('wxw-pc') != null) {
						var h2 = Math.ceil(tot/100*b.get('wxw-pc'));
						if (h2 > h) h2 = h;
						if (h2 < 0) h2 = 0;
						b.setStyle('width', h2+"px");
						b.setStyle('height', parInnerH+"px");
						h -= h2;
					}
				}
			}
			
		}.bind(this));
	}
	
});

