addOnloadEvent(fixModTops);

function toggleDiv(e_mod, alt_less, alt_more) {
	if(document.getElementById) {
		var id_div = document.getElementById('mod_'+e_mod);
		var id_top = document.getElementById('mod_'+e_mod+'_top');
		var id_link = document.getElementById('details_'+e_mod );
		var id_img = document.getElementById('arrow_'+e_mod);

		if(css_tweak('check', id_div, 'detail_collapse') == false) {
			css_tweak('add', id_div, 'detail_collapse');
			id_link.firstChild.nodeValue = alt_more;
			id_img.src = "/images/general-images/arrow_down.gif";
			id_img.alt = alt_more;
			css_tweak('add', id_top, 'mod_collapse')
		} else {
			css_tweak('remove', id_div, 'detail_collapse');
			id_link.firstChild.nodeValue = alt_less;
			id_img.src = "/images/general-images/arrow_up.gif";
			id_img.alt = alt_less;
			css_tweak('remove', id_top, 'mod_collapse')
		}
		clearSelection();
		return false;
	} else {
		return true;
	}
}

function fixModTops() {
	var tops = getElementByClassName('mod_top', document.body);
	var details = getElementByClassName('mod_detail', document.body);
	var no_scripts = getElementByClassName('no_script', document.body);
	for(var i=0;i<tops.length;i++) {
		css_tweak('add', tops[i], 'mod_collapse');
		css_tweak('add', tops[i], 'mod_top_script');
	}
	for(var i=0;i<details.length;i++) {
		css_tweak('add', details[i], 'detail_collapse');
	}
	for(var i=0;i<no_scripts.length;i++) {
		css_tweak('remove', no_scripts[i], 'no_script');
	}
}

//	http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
function css_tweak(a,o,c1,c2) {
  switch (a) {
    case 'swap':
      o.className=!css_tweak('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
    break;
    case 'add':
      if(!css_tweak('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

function getElementByClassName( strClassName, obj ) {
	var ar = arguments[2] || new Array();
	var re = new RegExp("\\b" + strClassName + "\\b", "g");

	if ( re.test(obj.className) ) {
		ar.push( obj );
	}
	for ( var i = 0; i < obj.childNodes.length; i++ ) {
		getElementByClassName( strClassName, obj.childNodes[i], ar );
	}
	return ar;
}

function clearSelection() {
	if(document.selection && document.selection.empty){
		document.selection.empty() ;
	} else if(window.getSelection) {
		var sel=window.getSelection();
		if(sel && sel.removeAllRanges) {
			sel.removeAllRanges() ;
		}
	}
}

