function show_nth(coll, n) {
	coll.each(function(e, index) {
		(n == index) ? e.show() : e.hide();
	})
}
function check_form(form) {
	var errors = 0;
	$A(Form.getInputs(form)).each(function(e) {
		if(e.name.match(/_$/)) {
			if(e.value.match(/\S/))
				Element.removeClassName(e, "err");
			else {
				Element.addClassName(e, "err");
				errors++;
			}
		}
	})
	//if(errors)
		//alert("?")
	return errors == 0
}

function check_form_results() {
	if(location.search.match(/\berror\b/))
		alert("Daten wurden NICHT gesendet!")
	if(location.search.match(/\ok\b/))
		alert("Ihre Daten wurden erfolgreich gesendet")
}

function doOpen(url,name,width,height,center,scrollbars){
	var x=0,y=0;
	if(center){
		x=(screen.availWidth-width)/2-12;
		y=(screen.availHeight-height)/2+23;
	}
	if(!width)
		width=screen.availWidth;
	if(!height)
		height=screen.availHeight;
	if(!scrollbars)
		scrollbars=0;
	var w=window.open(url,name,'width='+width+',height='+height+',left='+x+',top='+y+',scrollbars='+scrollbars+',resizable=1');
	if(w)
		w.focus();
}

function popup(url, w, h) {
	doOpen(url, 'popup', w || 410, h || 410, 1, 1);
	return false;
}

function flash(swf, width, height) {
	doOpen('fileadmin/flash.php?swf=' + swf, 'popup', 400, 400, 1, 0);
	return false;
}

//

Portfolio = {
	ajax : 0,
	init_sort : [],
	curr_id : 0,

	sort_list : function(col) {
		function cmp(a, b) {
			return a > b ? 1 : a < b ? -1 : 0;
		}
		var rows = $('P_SCROLLBOX').innerHTML.match(/<tr[\s\S]+?<\/tr>/gi)
		rows.sort(function(a, b) {
			if(col < 0) {
				var x = a.match(/\d+(?=\.html)/)
				var y = b.match(/\d+(?=\.html)/)
				return Portfolio.init_sort[x] - Portfolio.init_sort[y]
			}
			var x = a.match(/[^<>]+(?=<\/a)/gi)
			var y = b.match(/[^<>]+(?=<\/a)/gi)
			return cmp(x[col], y[col]) || cmp(x[1 - col], y[1 - col])
		})
		var m = $('P_SCROLLBOX').innerHTML.match(/<table.*?>/i)
		$('P_SCROLLBOX').innerHTML = m[0] + rows.join("\n") + "</table>";
		Portfolio.setup_list();
	},
	init_list : function() {
		var m = location.href.match(/\d+(?=\.html)/)
		if(m)
			Portfolio.curr_id = "r_" + m[0]
		$$('#P_SCROLLBOX table tr').each(function(e, index) {
			var id = e.innerHTML.match(/\d+(?=\.html)/)
			Portfolio.init_sort[id] = index
		})
		Event.observe($('P_SCROLLBOX'), 'click', function(evt) {
			var tr = Event.findElement(evt, "TR")
			Portfolio.row_click(tr)
		})
	},
	row_click : function(tr) {
		if(tr)
			Portfolio.load_details(tr.cells[0].firstChild.href, tr.id)
	},
	browse : function(n) {
		var tr = $(Portfolio.curr_id)
		if(!tr)
			return
		var rows = tr.parentNode.rows
		n += tr.rowIndex;
		if(n >= rows.length)
			n = 0
		if(n < 0)
			n = rows.length - 1;
		Portfolio.row_click(rows[n])
	},
	setup_list : function() {
		$$('#P_SCROLLBOX table tr').each(function(e, index) {
			e.className = "r" + (index % 2);
			if(e.id == Portfolio.curr_id)
				e.className = "rc";
			if(Portfolio.ajax) {
				e.cells[0].firstChild.onclick =
				e.cells[1].firstChild.onclick = function() {
					return false;
				}
			}
		})
		Portfolio.scroll_to_curr()
	},
	scroll_to_curr : function() {
		if(!Portfolio.curr_id)
			return
		var tr = $(Portfolio.curr_id)
		var sb = $('P_SCROLLBOX')
		if(tr.offsetTop < sb.scrollTop || tr.offsetTop > sb.scrollTop + sb.offsetHeight)
			sb.scrollTop = tr.offsetTop - sb.offsetHeight / 2
	},
	setup_details : function() {
		$$('.p_thumbs .thumb img').each(function(e, index) {
			show_nth($$('.p_fullsize p'), 0);
			Event.observe(e, 'click', function() {
				show_nth($$('.p_fullsize p'), index);
			})
			Event.observe(e, 'mouseover', function() {
				try {
					var mo =  $$('.p_thumbs_MO .thumb img')[index];
					if(e.src != mo.src) {
						e.old_src = e.src;
						e.src = mo.src;
					}
				} catch(err) {}
			})
			Event.observe(e, 'mouseout', function() {
				try {
					var mo =  $$('.p_thumbs_MO .thumb img')[index];
					if(e.src == mo.src) {
						e.src = e.old_src;
					}
				} catch(err) {}
			})
		})
	},
	close_details : function() {
		if(location.href.match(/portfolio/)) {
			$('P_DETAILS').hide();
			$('P_DETAILS_CONTROLS').hide();
			$('TOPBOX').show();
			$('P_LOADING').hide();
			Portfolio.curr_id = 0;
			Portfolio.setup_list();
			return false;
		}
		return true;
	},
	load_details : function(url, id) {
		if(!Portfolio.ajax)
			return location.href = url
		Portfolio.curr_id = id
		$('P_DETAILS').hide();
		$('P_DETAILS_CONTROLS').hide();
		$('TOPBOX').hide();
		$('P_LOADING').show();
		
		if(window.urchinTracker) {
			window.urchinTracker(url)
		}
		
		new Ajax.Updater('P_DETAILS', 'fileadmin/portfolio.php', {
			method: 'get',
			parameters: 'url=' + url,
			onFailure : function() { location.href = url },
			onException : function() { location.href = url },
			onComplete : function() {
				Portfolio.setup_details()
				Portfolio.setup_list()
				Effect.Appear('P_DETAILS', { duration: 0.2, afterFinish : function() {
					$('P_DETAILS_CONTROLS').show()
				}})
			}
		})
	}
}

//

yo = {}

yo.flashHTML = function(movie, width, height, color) {
	var html = 	[
		'<embed src="~movie~" quality="high" bgcolor="~color~" width="~width~" height="~height~"'
		+ ' align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"'
		+ ' pluginspage="http://www.macromedia.com/go/getflashplayer">'
		,
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="~width~" height="~height~" >'
		+ ' <param name="movie" value="~movie~"><param name="quality" value="high"><param name="bgcolor" value="~color~">'
		+ ' </object>'
	];
	if(!movie.match(/\.swf$/)) movie += ".swf";
	color = color || "#ffffff";
	var h = (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) ? html[0] : html[1];
	return h.replace(/~(\w+)~/g, function($0, $1) { return eval($1); });
}

yo.flash = function(movie, width, height, color) {
	document.write(yo.flashHTML(movie, width, height, color));
}
