// TODO:
// - .addClass() / .removeClass() instead of hard-coded values


function show_search_tip() {
	var searchtip = $("#searchtip");
	$("#id_search_term").css("border-color", "yellow");
	searchtip.css("width", $("#id_search_term").width() -4);
	searchtip.css("left", $("#id_search_term").offset().left);
	searchtip.css("top", $("#id_search_term").offset().top + $("#id_search_term").height() + 8);
	searchtip.css("display", "block");
}


function hide_search_tip() {
	$("#searchtip").css("display", "none");
	$("#id_search_term").css("border-color", "#353535");
}


function search_for(query) {
	document.search_form.search_term.value = query;
	document.search_form.submit();
}


function highlight_tag(id) {
	$("#" + id).css("background-color", "yellow");
	$("#" + id + " > a").css("color", "black");
}


function de_highlight_tag(id, color) {
	//$("#" + id).css("background-color", "transparent");
	$("#" + id).css("background-color", color);
	if (id == "tag_all") {
		$("#" + id + " > a").css("color", "black");
	}
	else {
		$("#" + id + " > a").css("color", "white");
	}
}


function m_over(id, el) {
	//draw(el);
	if (!(id == "about")) {
	tags = project_tags[id];
		for (var i = 0; i < tags.length; i++) {
			if (! $("#tag_" + tags[i]).hasClass("tag_selected")) {
				highlight_tag("tag_" + tags[i]);
			}
		}
	}
	
	var project = $('#project_' + id);
	var label = $('#label_' + id);
	
	border = project.css("border-left-width");
	border = border.substring(0, border.length-2);
	border = parseInt(border);
	
	project.css("border-color", "yellow");
	/*project.css("margin-left", "30px");
	project.css("margin-right", "10px");*/
	
	// --- SHOW LABEL ---
	//label.css('top', (project.offset().top + (project.height() / 2)) - (label.height() / 2) + border + 'px');
	label.css('top', (project.offset().top + project.height()) - label.height() + 'px');
	label.css('left', (project.offset().left + border + (project.width() / 2)) - (label.width() / 2) - parseInt(label.css('padding-left').replace("px", "")) + 'px');
	if (label.offset().left < 5) {
		label.css('left', "5px");
	}
	else if ((label.offset().left + label.width()) > $(window).width()) {
		label.css('left', $(window).width() - label.width() - 5);
	}
	
	label.css('visibility', 'visible');
}


function m_out(id) {
	if (!(id == "about")) {
		tags = project_tags[id];
		for (var i = 0; i < tags.length; i++) {
			if (! $("#tag_" + tags[i]).hasClass("tag_selected")) {
				de_highlight_tag("tag_" + tags[i], tag_colors[tags[i]]);
			}
		}
	}
	
	$("#project_" + id).css("border-color", "white");
	$('#label_' + id).css('visibility', 'hidden');
}


function latest_resize() {
	var new_height = 270;
	var min_height = 80;
	
	var project_items = $(".projectitem");	
	var last_y = project_items.first().offset().top;
	project_items.css("clear", "none");
	
	//var row_counter = 1;
	//var item_counter = 0;
	
	$(".projectitem").each(
		function() {
			var imgs = $(this).children().filter("a").children().filter("img");
			if (imgs.length == 0) { // year
				if (! $(this).hasClass("textitem")) { // ist das überhaupt irgendwann der fall, wenn es sich um ein "year item" handelt?
					$(this).css("width", new_height + "px");
				}
				$(this).css("line-height", new_height + "px");
			}
			else {
				imgs.css("height", new_height + "px");
			}
			$(this).css("height", new_height + "px");
			
			// leave some space on the right
			if (($(this).offset().left + $(this).width()) > $(window).width()-25) {
				$(this).css("clear", "left");
				//alert($(this).offset().left + $(this).width());
			}
			
			var offset_top = $(this).offset().top;
			if (offset_top != last_y) {
				// neue zeile!
				new_height /= 1.5;
				//row_counter++;
				if (new_height < min_height)
					new_height = min_height;
				
				$(this).css("clear", "left");
				last_y = $(this).offset().top;
				
				if (imgs.length == 0) {
					if (! $(this).hasClass("textitem")) {
						$(this).css("width", new_height + "px");
					}
					$(this).css("line-height", new_height + "px");
				}
				else {
					imgs.css("height", new_height + "px");
				}
				$(this).css("height", new_height + "px");
			}
		}
	);
	
	if (first_time) {
		project_items.css("visibility", "visible");
		first_time = false;
	}
}

