var page_url = domain+'smiley/';

function FetchElement(id) {
	if (document.getElementById) {
		elem = document.getElementById(id);
	}
	else if (document.all) {
		elem = document.all[id];
	}
	else if (document.layers) {
		elem = document.layers[id];
	}
	return elem;
}
function changeBGColor(nr) {
	field = FetchElement('cb'+nr);
	field.className = 'colorbox_selected';
	for(i=1;i<5;i++) {
		if (i != nr) {
			unselect = FetchElement('cb'+i);
			unselect.className = 'colorbox';
		}
	}
	img = FetchElement('smiley');
	img.style.backgroundColor = field.style.backgroundColor;
}
function url(datei) {
	var url = page_url+datei;
	FetchElement('code_url').value = url;
	update();
}
function update() {
	var datei = FetchElement('code_url').value;
	code_art_1 = FetchElement('code_art_1');
	code_art_2 = FetchElement('code_art_2');
	code_ausgabe = FetchElement('code_ausgabe');
	if (datei != '') {
		if (code_art_1.checked == true) {
			code_ausgabe.value ="[img]"+datei+"[/img]";
		}
		else if(code_art_2.checked == true) {
			code_ausgabe.value ="<img src=\""+datei+"\" />";
		}
		else {
			code_ausgabe.value = datei;
		}
		code_ausgabe.select();
		code_ausgabe.focus();
	}
	else {
		code_ausgabe.value = 'Bitte Grafik anklicken!';
	}
}
function face_gen(save){
	var body = FetchElement('body').value;
	var mouth = FetchElement('mouth').value;
	var eye = FetchElement('eye').value;
	var stuff = FetchElement('stuff').value;
	var img = FetchElement('preview');
	if (body == 0 && mouth == 0 && eye == 0 && stuff == 0) {
		img.src = 'generator/gesichter/nodata.gif';
	}
	else {
		img.src = 'index.php?a=gesichter&job=1&body='+body+'&mouth='+mouth+'&eye='+eye+'&stuff='+stuff+'&save='+save;
		if (save == 1) {
			var url = domain+"gesichter/"+body+"-"+eye+"-"+mouth+"-"+stuff+".png";
			FetchElement('code_url').value = url;
			update();
		}
	}
}
function face_preview(sel){
	var img = FetchElement(sel.id+'_preview');
	img.src = 'generator/gesichter/'+sel.id+'/'+sel.value+'.gif';
}
function ResizeImg(img) {
	var maxwidth = 180;
	if(img.width >= maxwidth && maxwidth != 0) {
		var owidth = img.width;
		var oheight = img.height;
		img.width = maxwidth;
		img.height = Math.round(oheight/(owidth/maxwidth));
		img.title = 'Dieses Bild wurde verkleinert. Klicke hier um es zu vergrößern';

		try {
		    img.style.cursor = "pointer";
	    }
	    catch(e) {
	    	img.style.cursor = "hand";
	    }

		img.onclick = function() {
			var width = screen.width-30;
			if (width > owidth) {
				width = owidth+30;
			}
			var height = screen.height-50;
			if (height > oheight) {
				height = oheight+30;
			}
			window.open(img.src,"","scrollbars=yes,status=no,toolbar=no,location=yes,directories=no,resizable=no,menubar=no,width="+width+",height="+height)
		}
	}
}

function initImg() {
	for(var i =0; i < document.images.length; i++) {
		if (document.className = 'smiley') {
			ResizeImg(document.images[i]);
		}
	}
	var box = FetchElement('smileyurl');
	if (typeof box != undefined && box != null) {
		box.style.display = 'block';
	}
}