/* --------------------------     Bildergalerie Bildlaufleiste ------------------------------ */

if($('right_gallery_handle')) {
	// horizontal slider control
	var slider4 = new Control.Slider('right_gallery_handle', 'right_gallery_track', {
		onSlide: function(v) { scrollHorizontal(v, $('right_gallery'), slider4);  },
		onChange: function(v) { scrollHorizontal(v, $('right_gallery'), slider4); }
	});
	
	// scroll the element horizontally based on its width and the slider maximum value
	function scrollHorizontal(value, element, slider) {
		element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
	}
}

/* --------------------------    Suchfunktion ------------------------------ */

function searchForText( toFind ) {
	if( !toFind ) { return; }
	if( window.find ) {
		//Netscape compatible browsers provide the window.find method
		if( document.layers ) {
			//Against the JS spec, Netscape 4 will produce errors if too many arguments are given
			var ifFound = window.find( toFind, false, false );
		} else {
			var ifFound = window.find( toFind, false, false, true, false, true, false );
		}
	} else if( window.document.body && window.document.body.createTextRange ) {
		//IE or compatible use various TextRange features
		if( window.document.selection && window.document.selection.type != 'None' ) {
			//If some text is selected already (previous search or if they have selected it)
			//make that the text range. Then move to the end of it to search beyond it
			var theRange = window.document.selection.createRange();
			theRange.collapse( false );
		} else {
			//If no text is selected, start from the start of the document
			var theRange = window.document.body.createTextRange();
		}
		//find the next occurrence of the chosen string
		var ifFound = theRange.findText( toFind );
		if( ifFound ) { theRange.select(); }
	}
}

/* --------------------------  Magazin austauschen ------------------------------ */

function change_megazine(number) {

	var now = new Date();
	$('polo-magazin_full_container').update('');
	var full = document.createElement('div');
	full.id = 'polo-magazin_full';
	$('polo-magazin_full_container').appendChild(full);
	swfobject.embedSWF("/megazine/megazine.swf", "polo-magazin_full", "562", "540", "9.0.115", "/megazin/expressInstall.swf", {xmlFile:"megazine.php?megazine="+number}, {bgcolor : "#eeeeee", allowFullscreen : "true"}, {id : "megazine"+now.getTime()});

	return false;

}

/* --------------------------     Blog Bewertungsfunktion ------------------------------ */

old_value = '';
glob_container = null;

function in_array(haystack, needle) {
	for(n = 0; n < haystack.length; n++)
		if(haystack[n] == needle)
			return true;
			
	return false;
}

var has_perm = false;
function rate_mouseover(rating, container) {
	id = container;
	container = $(container);
	container = $("article"+container);
	container = container.getElementsByClassName("rating")[0];
	new Ajax.Request("/script/ajax.php?check_perm&dataset="+id, {
					 method: 'get',
					 onSuccess: function(transport) {
						 if (transport.responseText.length == 0)
						 	has_perm = true;
						 else
						 	container.update(transport.responseText);
					}
	});

	if(has_perm == true) {
		imgs = container.getElementsByTagName("img");
		for(n = rating; n < 5; n++)
			imgs[n].src = "/images/public/bewertung_inaktiv.jpg";
		for(n = 0; n < rating; n++)
			imgs[n].src = "/images/public/bewertung_aktiv.jpg";
	}
}

function resetValue() {
	glob_container.className = "rating";
	glob_container.innerHTML = old_value;
	imgs = glob_container.getElementsByTagName("img");
	for(n = 0; n < imgs.length; n++) {
		imgs[n].onmouseover = '';
		imgs[n].onclick = '';
	}
}
function rate_click(rating, dataset) {
	container = $("article"+dataset);
	container = container.getElementsByClassName("rating")[0];
	new Ajax.Request("/script/ajax.php?rating="+rating+"&dataset="+dataset, {
					 method: 'get',
					 onSuccess: function(transport) {
						 if (transport.responseText.length == 0)
						 	container.update("Erfolgreich!");
						 else
						 	container.update(transport.responseText);
					}
	});
	glob_container = container;	
	old_value = container.innerHTML;
	container.className = "rating2";
	container.innerHTML = "Erfolgreich!";
	window.setTimeout("resetValue()", 10000);
}

function rate_reset(container, rating, number) {
	imgs = container.getElementsByTagName("img");
	if(imgs.length == 0) {
		$(container).update('');
		for(n = 1; n <= 5; n++) {
			$(container).appendChild(new Element('img', { src: '/images/public/bewertung_aktiv.jpg', alt: '', onmouseover: 'rate_mouseover('+n+', '+number+', false)'}));
		}
		has_perm = false;
	}
	for(n = 0; n < imgs.length; n++) {
		if(n < rating)
			imgs[n].src = "/images/public/bewertung_aktiv.jpg";
		else
			imgs[n].src = "/images/public/bewertung_inaktiv.jpg";
	}
}

/*	Article Gallerys auf Breite testen	*/
/*
function check_galleries() {
	if($('gal'+article_gallery).scrollWidth <= 541)
		$('wrap'+article_gallery).style.display = "none";
}
window.setTimeout("check_galleries();", 1000);
*/

jsapi = $;

function doCreatePermalink(input, output, prefix, suffix) {
	var content = jsapi(input).value.toLowerCase();
	
	if (content != '') {
	
		var from = new Array('ä', 'á', 'à', 'â', 'å', 'ö', 'ó', 'ò', 'ô', 'õ', 'ü', 'ú', 'ù', 'û', 'é', 'è', 'ê', 'í', 'ì', 'î', 'ß', 'ç', 'Ç', 'ñ', 'ý', '&', '.', '{', '}', ' ');
		var to = new Array('ae', 'a', 'a', 'a', 'a', 'oe', 'o', 'o', 'o', 'o', 'ue', 'u', 'u', 'u', 'e', 'e', 'e', 'i', 'i', 'i', 'ss', 'c', 'c', 'n', 'y', 'und', '-', '', '', '-');
	
		for (i=0; i<from.length; i++) {
			content = content.replace(from[i], to[i]);
		}
	
		content = content.replace(/[^a-z0-9-_]/g, ' ');
		content = content.replace(/[ ]{2,}/g, ' ');
		content = content.replace(/ /g, '-');
		jsapi(output).value = prefix + content + suffix;
		
		if (jsapi('ajax_check_permalink')) {
			new Ajax.Updater('ajax_check_permalink', '/admin.php?file=options&mode=check_permalink&clean=yes&ajax=yes&ajax_type=check_permalink&ajax_vars[permalink]=' + jsapi(output).value);
		}
	}
}
// Erstellt die HTML-basierten Tooltips / Layer in verschiedenen Bereichen.

tooltip = null;

function doUpdateTooltip(e) {
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (tooltip != null) {
		tooltip.style.left = (x + 20) + 'px';
		tooltip.style.top 	= (y + 20) + 'px';
	}
}

function doShowTooltip(name) {
	tooltip = jsapi(name);
	tooltip.style.display = 'block'
}

function doHideTooltip(name) {
	tooltip = jsapi(name);
	tooltip.style.display = 'none';
}
