//-- Image Functions ---------------------------------------------------------//
function scaleImage(i,m) {
	if(!m) m = 320;
	var d = i.getDimensions();
	if(d.width>m) {
		d.height = Math.floor(m*(d.height/d.width));
		d.width = m;
		$(i).setStyle({ 'height': d.height+'px', 'width': d.width+'px' });
	}
	if(d.height>m) {
		d.width = Math.floor(m*(d.width/d.height));
		d.height = m;
		$(i).setStyle({ 'height': d.height+'px', 'width': d.width+'px' });
	}
}

//-- Form Functions ----------------------------------------------------------//

function checkLogin(main) {
	var allow = true;
	p = (main) ? 'loginpass' : 'smallpass';
	n = (main) ? 'loginname' : 'smallname';
	if($F(p).strip() == '') allow = false;
	if($F(n).strip() == '') allow = false;
	return allow;
}

function setLength(x,l) {
	if(!x || !l) return;
	var counter = new Element('div', { 'class': 'small' });
	var span = new Element('span', { 'class': ''}).update('0');
	x.up().insert(counter.insert("(").insert(span).insert('&nbsp;of '+l+' character limit)'));
	x.onkeyup = x.onchange = x.onblur = function() {
		span.className = (x.value.length > l) ? 'error' : '';
		span.update(x.value.length);
	}
	x.onkeyup();
}

function init_fieldTabs() {
	$$('.tabs').each(function(c){
		var t = c.select('.tab');
		var l = new Element('ul', {'class':'tablist'});
		t.each(function(tab){
			var li = new Element('li');
			li.update(tab.down('.title').innerHTML);
			li.onclick = function() {
				l.select('li').invoke('removeClassName', 'active');
				li.addClassName('active');
				t.invoke('removeClassName', 'active');
				tab.addClassName('active');
			}
			l.insert({bottom:li});
		});
		c.addClassName('enhanced');
		c.insert({top:l});
		t.first().addClassName('active');
		l.down('li').addClassName('active');
	});
}

function openExternal(a) {
	if(a.readAttribute('rel')=='external') {
		if(!a.readAttribute('title')) a.writeAttribute('title','Opens in a new window');
		a.onclick = function(e) {
			if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
				return true;
			} else {
				var oWin = window.open(this.getAttribute('href'), '_blank');
				if (oWin) {
					if (oWin.focus) oWin.focus();
					return false;
				}
				oWin = null;
				return true;
			}
		}
	}
	return a;
}

function init_menus() {
	$$('ul.menu').each(function(u) {
		u.childElements().each(function(i) {
			i.addClassName("over").removeClassName("over");
			i.onmouseover = function() { this.addClassName("over"); }
			i.onmouseout = function() { this.removeClassName("over"); }
		});
	});
}

//-- Global onload -----------------------------------------------------------//
document.observe("dom:loaded", function() {
	$$('.collapse').each(function(d) { d.hide(); });
	$$('input[type=text]','input[type=password]').each(function(n) {
		n.onfocus = function() { n.removeClassName('elementBlur').addClassName('elementFocus'); }
		n.onblur = function() { n.removeClassName('elementFocus').addClassName('elementBlur'); }
	});
	$$('input.readonly').each(function(n) { n.onclick = function() { this.activate(); }});
	if(document.location.hash) {
		jumpObj = $(document.location.hash.sub("#",""));
		if(jumpObj.match('fieldset')) jumpObj.addClassName('jumpTarget').select('input,select,textarea').first().activate();
		if(jumpObj.match('textarea') || jumpObj.match('input') || jumpObj.match('select')) jumpObj.activate();
	}
	init_menus('domainmenu');
	$$('a').each(function(a) { openExternal(a); });
});
