
var URL = "query.php";
 
if (typeof(console) == "undefined") {
	console = {log: function(){}};
}

// ------------------

function AlertEdit($this, field, onchange) {
	var current = $("[name="+field+"]", $this).val(),
		$min = $("[name="+field+"_min]", $this),
		$max = $("[name="+field+"_max]", $this),
		$ul = $("ul", $this),
		$expl = $(".explanation", $this);
	
	$ul.children('li').each(function(idx, elem) {
		var $a = $ul.children('li').eq(0).children('div').eq(0).children('a').eq(0);
		if ($a) {
			initClickHandler($a, idx + 1);
		//} else { current = idx + 1;
		}
	});

	function initClickHandler($a, val) {
		$a.click(function() {
			if (val < current) {
				$min.val($min.val() <= val ? val + 1 : val);
			} else {
				$max.val($max.val() >= val ? val - 1 : val);
			}
			onchange();
			updateBounds();
			return false; 
		});
	}
	
	function updateBounds() {
		var classPrefix = ['alert', 'tolerance'],
			mi = $min.val(),
			ma = $max.val(),
			prev_alert = false,
			this_alert = false,
			next_alert = (mi <= 1 && 1 <= ma);
		$ul.children('li').each(function(idx, elem) {
			var $li = $(elem),
				$a = $li.children('div:first-child').children('a:first-child'),
				$a_class = '';
			prev_alert = this_alert;
			this_alert = next_alert;
			next_alert = (mi <= idx + 2 && idx + 2 <= ma);
			$li.removeClass().addClass(''
				+ classPrefix[this_alert ? 0 : 1] + '_'
				+ (prev_alert ? '1' : '0')
				+ (next_alert ? '1' : '0'));
			$a_class = classPrefix[this_alert ? 1 : 0] + '_' + (this.alert ^ (idx + 1 > current) ? 'right' : 'left');
			$a.removeClass().addClass($a_class);
		});
		$expl.html(createExplanation());
	}
	
	function createExplanation() {
		var mi = $min.val(),
			ma = $max.val(),
			txtBound = [],
			txt = "",
			pronoun = "";
		if (field == 'risk') {
			txt = "Die aktuelle Risikoklasse ist <b>"+current+"</b>. ";
			pronoun = "diese";
		} else {
			txt = "Das aktuelle Rating ist <b>"+current+"</b>. ";
			pronoun = "dieses";
		}
		if (mi > 1) {
			txtBound.push("fällt "+pronoun+" <b>unter "+mi+"</b>");
		}
		if (ma < 5) {
			txtBound.push("steigt "+pronoun+" <b>über "+ma+"</b>");
		}		
		if (txtBound.length == 0) {
			txt += "Da der Toleranzbereich alle Werte umfasst, erhalten Sie hier keine Mitteilungen.";
		} else {
			var txtBound0 = '' + txtBound[0];
			txt += txtBound0.charAt(0).toUpperCase() + txtBound0.substr(1);
			if (txtBound.length == 2)
				txt += " oder "+txtBound[1];
			txt += ", erhalten Sie eine Mitteilung.";
		}
		return txt;
	}
}

// ------------------

$(function()  {
	
	var loggedIn = $("#header_right .register").length == 0;
	
	$("fieldset.bound").each( function() {
		var field = $(this).hasClass("risk") ? "risk" : "rating";
		AlertEdit($(this), field, function() {
			if (!loggedIn) {
 
				$.get(URL+"?action=change&isin="+$("#alert [name=isin]").val()+
					"&risk_min="+$("#alert [name=risk_min]").val()+
					"&risk_max="+$("#alert [name=risk_max]").val()+
					"&rating_min="+$("#alert [name=rating_min]").val()+
					"&rating_max="+$("#alert [name=rating_max]").val()
					);
			}
		});
	});
	
	var $help_txt = $("#alert .help_txt");
	var $help_on = $("#alert a.help_on");
		
	$help_on.attr("href","#").click( function() {
		$help_txt.show();
		$help_on.hide();
		saveHelpStatus(1);
		return false;
	});
	$("#alert a.help_off").attr("href","#").click( function() {
		$help_txt.hide();
		$help_on.show();
		saveHelpStatus(0);
		return false;
	});
			
	$("#add_isin").autocomplete(
		"autocomplete.php",
		{
			delay:20,
			minChars:2,
			matchSubset:0,
			matchContains:1,
			cacheLength:1,
			autoFill:false
		}
	);	
	
	if (parseInt($("#register input[name=new_user]:checked").val())==0)
		$("#register .new").hide();
	
	$("#register input[name=new_user]").change( function() {
		var checked = parseInt($("#register input[name=new_user]:checked").val());

		if (checked) {
			$("#register .new").show();
		} else {
			$("#register .new").hide();
		}
	});
	
	function saveHelpStatus(val) {
		$.get(URL+"?action=set-help&new="+val);
	}
	
});

