cursor = null;
var xmlHttp = null;
document.onmousemove = updatecursor;
var highlightColor = '#D6D7FF';
var stdColor = '#ffffff';
window.addEvent('domready', function() {
	// add Tooltip-Events
		activeElements = $$('*.activeTooltip');
		if (activeElements != null) {
			activeElements.each(function(item, index) {
				item.addEvent('mouseenter', function() {
					showTip(this.getProperty('title'));
				});
				item.addEvent('mouseleave', function() {
					slip();
				});
			});
		}

		// ajax-calls for input boxes starting with property "name"="amnt_..."
		$$('input.[name^=amnt_]').each(function(item, index) {
			item.addEvent('keyup', function() {
				setAmntItems(item);
			});
		});
		// highlight checkboxes on select
		$$('input.[name^=add_]').each(function(item, index) {
			item.addEvent('click', function() {
				colorElementOnCheckboxClick(this);
			});
		});
		$$('input.[name^=del_]').each(function(item, index) {
			item.addEvent('click', function() {
				colorElementOnCheckboxClick(this);
			});
		});

		// manufacturer view lines(highlight)
		$$('tr.manufacturerView').each(function(item, index) {

			var fadeIn = function fadeIn() {
				this.setStyle('background-color', highlightColor);
			};
			item.addEvent('mouseenter', fadeIn);

			item.addEvent('mouseleave', function() {
				new Fx.Tween(this).start('background-color', stdColor);
			});
		});
		
		// postprocess forms meant for deletion of items
		$$('form.deleteItems').each(function(item, index) {
			input=item.getElement('input[type=submit]');
			input.destroy();
			a=item.getElement('a');
			a.removeClass('invisible');
			a.addEvent('click',function(e){
				e.stop();
				this.getParent('form.deleteItems').submit();
			});
		});
		// postprocess forms meant for adding of items
		$$('form.addItems').each(function(item, index) {
			var button=item.getElement('input[class=button]');
			if(button!=null){
				button.destroy();
			}
		});
		
		$$('tr.tr_add_items_list').each(function(item, index) {
			
			var input=item.getElement('input[type=checkbox]');
			input.addClass('invisible');
			
			var a=item.getElement('a');
			a.removeClass('invisible');
			
			a.addEvent('click',function(e){
				e.stop();
				checkbox = a.getParent('td').getElement('input[type=checkbox]');
				if(checkbox!=null){
					checkbox.checked=true;
				}
				a.getParent('form').submit();
			});
		});
		
	});

function showTip(id) {
	cursor = $(id);
	if (cursor != null) {
		cursor.setStyle('opacity', 0);
		cursor.removeClass('invisible');
		cursor.fade('in');
		// cursor.style.display = "block";
	}
}

function updatecursor(Evt) {

	if (!Evt)
		Evt = window.event;
	p_left = Evt.clientX;
	p_top = Evt.clientY;

	if (window.document.compatMode
			&& window.document.compatMode == "CSS1Compat")
		ref = window.document.documentElement;
	else
		ref = window.document.body;

	if (ref) {
		p_left = ref.scrollLeft + p_left;
		p_top = ref.scrollTop + p_top;
	}
	if (cursor != null) {
		cursor.style.left = p_left + 20 + "px";
		cursor.style.top = p_top + 20 + "px";
	}
}

function slip() {
	// cursor.style.display = "none";
	if (cursor != null) {
		cursor.addClass('invisible');
	}
	cursor = null;
}

function colorElementOnCheckboxClick(source) {
	if (source.getProperty('name').search('add_') != -1)
		id = source.getProperty('name').replace('add_', '');
	if (source.getProperty('name').search('del_') != -1)
		id = source.getProperty('name').replace('del_', '');
	if (source.checked == true) {
		// darken
		new Fx.Tween($(id)).start('background-color', highlightColor);
	} else {
		// brighten
		new Fx.Tween($(id)).start('background-color', stdColor);
	}
	;
}

var global_g_index;
var globalSubGIndex;

function setAmntItems(source) {
	// supply g_index, params, subGIndex
	name = source.getProperty('name').replace("amnt_", "");
	if (source.getProperty('value') > 10)
		source.setProperty('value',10);

	indizes = name.split('->');

	global_g_index = indizes[0];
	globalSubGIndex = indizes[1];


	var param1 = "SetAmntItems";
	var param2 = "/" + global_g_index;
	var param3 = "/" + source.getProperty('value');
	var param4 = "/" + getStackId();
	var param5 = "/" + globalSubGIndex;
	var param = param1 + param2 + param3 + param4 + param5;

	var url = parent.document.URL+"/"+param;
	
	new Request.JSON( {
		method : 'post',
		url : url,
		onComplete : function(response) {

			if (globalSubGIndex != "" && globalSubGIndex != null)
				identSubGIndex = "->" + globalSubGIndex;
			else
				identSubGIndex = "";

			var price = $("price_" + global_g_index + identSubGIndex);
			var weight = $("weight_" + global_g_index + identSubGIndex);
			if (price != null)
				price.set("text", response.price);
			if (weight != null)
				weight.set("text", response.weight);
		}
	}).send();

}

function getStackId() {
	var stackid = window.document.getElementById("stackid").value;
	// alert(stackid);
	return stackid;
}
