var xPos;
var yPos;
var color;
var type;
var text;
var textcolor;
var textsize;
var width;
var addwidth;

var docEl = (
             typeof document.compatMode != "undefined" && 
             document.compatMode        != "BackCompat"
            )? "documentElement" : "body";

function init_mousemove() {
    if(document.layers) document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove =	dpl_mouse_pos;
}

function dpl_mouse_pos(e) {

    xPos    =  e? e.pageX : window.event.x;
    yPos    =  e? e.pageY : window.event.y;

	if (document.all && !document.captureEvents) {
	    xPos    += document[docEl].scrollLeft;
	    yPos    += document[docEl].scrollTop;
    }
    
    if (document.layers) routeEvent(e);
}

function showColorDiv(color) {

	if( ! document.getElementById('color_div')) {

		var div = document.createElement('div');
		div.setAttribute("id", "color_div");

		document.body.appendChild(div);
	}

	var div = document.getElementById('color_div');

	div.style.backgroundColor = color;
	div.style.top = yPos + 10;
	div.style.left = xPos -50;
	div.style.display = '';
}

function hideColorDiv() {

	var div = document.getElementById('color_div');
	div.style.display = 'none';
}

function init_button() {

	color = 'FFFF00';
	type = 'button';
	text = 'Home';
	textcolor = '000000';
	textsize = 14;
	addwidth = 0;

	width = document.preview.width;
	document.getElementById('addwidth').value = width;

	change();
}

function setType(t) {

	if(t.substring(0, 4) == "star") {

		$("#box_width").slideUp("slow");	
	} else {

		$("#box_width").slideDown("slow");	
	}

	type = t;
}

function setColor(c) {

	color = c;
}

function setText() {

	var t = document.getElementById('text').value;
	text = t;
}

function setTextColor(tc) {

	textcolor = tc;
}

function setTextSize() {

	textsize = document.getElementById('textsize').value;
}

function setAddWidth() {

	addwidth = document.getElementById('addwidth').value - width;
}

function change() {

	$('#pic').attr('src', 'images/load.gif');
	$('#pic').attr('src', 'create.php?type=' + type + '&c=' + color + '&t=' + text + '&tc=' + textcolor + '&fs=' + textsize + '&w=' + addwidth);
}

function download() {

	location.href = 'create.php?type=' + type + '&c=' + color + '&t=' + text + '&tc=' + textcolor + '&fs=' + textsize + '&download' + '&w=' + addwidth;
}

$(document).ready(function() {
	
	init_button(); 
	init_mousemove(); 

	$("#slider").slider({
		max: 400,	
		step: 10,
		value: width,
		change: function(event, ui) {

			$("#addwidth").attr('value', ui.value);
			return false;
		}
	});

	$('a[href*="#"]').click(function() {

		if (location.pathname == this.pathname && location.host == this.host) {

			var target = $(this.hash);
			target = target.size() && target || $("[name=" + this.hash.slice(1) +']');
			
			if (target.size() == 1) {

				$('html, body').animate({
					scrollTop: target.offset().top
				}, 1000);
				return false;
			}
		};
	});

	$('#generator_nojs').hide();
	$('#generator').show();
});

function impressum(id) {
	var elem_id = "impressum_" + id;

	$("#" + elem_id).slideDown("slow");
}

