var ratio = 1;
var objs = [];
var id_index = 0;

var ScalableObject = new Class({
	initialize: function(id, obj, compatible) {
		if(typeof(compatible) != 'Array')
			this.compatible = new Array('height', 'width', 'top', 'left', 'font-size',
				'margin-top', 'margin-bottom', 'margin-left', 'margin-right', 'padding-top','padding-left', 'padding-bottom',
				'padding-right', 'line-height', 'bottom', 'right');
		else
			this.compatible = compatible;
		this.obj = obj;
		$(obj).store('scaleID', id);
		this.styleList = [];
		this.id = id;
		for(var i=0; i<this.compatible.length; i++) {
			if($(obj).getStyle(this.compatible[i]).indexOf('px') == -1) {
				this.compatible.splice(i, 1);
				i--;
			} else
				this.styleList.push($(obj).getStyle(this.compatible[i]).split('px')[0]);
		}
	}
});
ScalableObject.implement({
	scale: function(r) {
		for(var i=0; i<this.compatible.length; i++)
			if($(this.obj).getStyle(this.compatible[i]).indexOf('%') == -1)
				$(this.obj).setStyle(this.compatible[i], this.styleList[i] * r +'px');
	},
	update: function(property) {
		for(var i=0; i<this.compatible.length; i++)
			if(this.compatible[i] == property) {
				this.styleList[i] = $(this.obj).getStyle(property).split('px')[0];
				return;
			}
		this.compatible.push(property);
		this.styleList.push($(this.obj).getStyle(property).split('px')[0]);
	},
	updateValue: function(property, value) {
		for(var i=0; i<this.compatible.length; i++)
			if(this.compatible[i] == property) {
				this.styleList[i] = value;
				return;
			}
		this.compatible.push(property);
		this.styleList.push($(this.obj).getStyle(property).split('px')[0]);
	},
	deleteCompatible: function(property) {
		for(var i=0; i<this.compatible.length; i++)
			if(this.compatible[i] == property) {
				this.compatible.splice(i, 1);
				this.styleList.splice(i, 1);
				return;
			}
	}
});

var isMacSafari = (navigator.vendor && navigator.vendor.indexOf('Apple') != -1 && navigator.platform && navigator.platform.indexOf('Mac') != -1);

window.addEvent(((isMacSafari) ? 'load' : 'domready'), function() {
	init_resize();
	resize();
	window.addEvent('resize', function() {
		resize();
	});
});

function scaleObject(obj, r) {
	for(var i=0; i<objs.length; i++)
		if(obj.nodeName != '#text' && (obj.nodeName != 'OBJECT' || !navigator.appName || navigator.appName != "Microsoft Internet Explorer") && obj.nodeName != 'param' && $(obj).retrieve('scaleID', -1) == objs[i].id) {
			objs[i].scale(r);
			break;
		}
}

function updateObject(obj, property) {
	for(var i=0; i<objs.length; i++)
		if(obj.nodeName != '#text' && obj.nodeName != 'param' && $(obj).retrieve('scaleID', -1) == objs[i].id) {
			objs[i].update(property);
			break;
		}
	
}
function updateObjectValue(obj, property, value) {
	for(var i=0; i<objs.length; i++)
		if(obj.nodeName != '#text' && obj.nodeName != 'param' && $(obj).retrieve('scaleID', -1) == objs[i].id) {
			objs[i].updateValue(property, value);
			break;
		}
}
function addObject(obj) {
	switch(obj.nodeName) {
		case '#text':
		case 'param':
		case 'br':
			return;
			break;
		case 'img':
			objs.push(new ScalableObject(id_index++, obj, new Array(
				'height',
				'width',
				'top',
				'left'
			)));
			objs[objs.length - 1].scale(ratio);
			break;
		default:
			if(obj.nodeName == 'OBJECT' && navigator.appName && navigator.appName == "Microsoft Internet Explorer")
				return;
			objs.push(new ScalableObject(id_index++, obj, 0));
			objs[objs.length - 1].scale(ratio);
			break;
	}
	for(var i=0; i<obj.childNodes.length; i++)
		addObject(obj.childNodes[i]);
}
function deleteCompatible(obj, property) {
	for(var i=0; i<objs.length; i++)
		if(obj.nodeName != '#text' && obj.nodeName != 'param' && $(obj).retrieve('scaleID', -1) == objs[i].id) {
			objs[i].deleteCompatible(property);
			break;
		}
}
function deleteObject(obj) {
	if(obj.nodeName == 'OBJECT' && navigator.appName && navigator.appName == "Microsoft Internet Explorer") return;
	if(obj != null && obj.nodeName != '#text' && obj.nodeName != 'param') {
		for(var i=0; i<objs.length; i++)
			if($(obj).retrieve('scaleID', -1) == objs[i].id) {
				objs.splice(i, 1);
				break;
			}
		for(var i=0; i<obj.childNodes.length; i++)
			deleteObject(obj.childNodes[i]);
	}
}
function init_resize() {
	var divs = $$('div');
	var images = $$('img');
	var text = $$('a', 'h1', 'h2', 'h3', 'p', 'input', 'textarea');
	for(var i=0; i<divs.length; i++)
		objs.push(new ScalableObject(id_index++, divs[i],0));
	for(var i=0; i<images.length; i++)
		objs.push(new ScalableObject(id_index++, images[i], new Array(
			'height',
			'width',
			'margin-bottom',
			'margin-right'
		)));
	for(var i=0; i<text.length; i++)
		objs.push(new ScalableObject(id_index++, text[i],0));
	if($('intro')) {
		$('intro').setStyle('width', window.getSize().x);
		$('intro').setStyle('height', window.getSize().y);
	}
}
function resize() {
	ratio = Math.min(window.getSize().y / 735, window.getSize().x / 1000);
	ratio = (ratio > 1) ? 1 : (ratio < .34) ? .34 : ratio;
	for(var i=0; i<objs.length; i++)
		objs[i].scale(ratio);
	if($('intro')) {
		$('intro').setStyle('width', window.getSize().x);
		$('intro').setStyle('height', window.getSize().y);
		$('intro_progress_bar').setStyle('height', 1);
	}
/*	if($$('div.static')[0])
		$('sidewrapper').setStyle('height', ($$('div.static')[0].getSize().y + 87 * ratio > window.getSize().y) ? $$('div.static')[0].getSize().y + 87 * ratio : window.getSize().y);*/
}
