function switchWidget(title)
{
	var content = title.next();
	
	// ID vergeben
	var jetzt = new Date();
	var id = 'switchwidget' + parseInt(jetzt.getTime());
	content.setAttribute('id', id);
	
	// Blenden
	
	Effect.toggle(id, 'blind', {duration: 0.8});
	
	// Pfeil austauschen.
	var img = title.firstDescendant().getAttribute('src');
	if (img.indexOf('_closed') >= 0) {
		img = img.replace(/_closed/, '_open');
	} else {
		img = img.replace(/_open/, '_closed');
	}
	title.firstDescendant().setAttribute('src', img);	
}

function scanWidgets()
{
	jQuery.noConflict();
	var tdivs = jQuery('.widgettitleDiv');
	var titles = new Array('facebook','twitter', 'flickr', 'YouTube', 'Kommentare', 'Blogger', 'Kategorien', 'Archiv', 'Blogroll', 'Links', 'Verschiedenes');
	var closed = new Array('Kommentare', 'Blogger', 'Kategorien', 'Archiv', 'Blogroll', 'Links', 'Verschiedenes');
	
	for (var i = 0; i < tdivs.length; ++i) {
		// Check if Widget should be foldable
		if (titles.indexOf(tdivs[i].firstDescendant().innerHTML) >= 0) {
			jQuery(tdivs[i]).bind('click',function(){switchWidget(this);});
			tdivs[i].style.cursor = 'pointer';
			var img = document.createElement('img');
			img.setAttribute('alt', 'Anzeigen');
			// Close widgets that are closed by default, otherwise keep open.
			if (closed.indexOf(tdivs[i].firstDescendant().innerHTML) >= 0) {
				img.setAttribute('src', window.location.protocol + '//' + window.location.host + '/wp-content/themes/opel_development/images/widget_closed.gif');
				tdivs[i].next().style.display = 'none';
			} else {
				img.setAttribute('src', window.location.protocol + '//' + window.location.host + '/wp-content/themes/opel_development/images/widget_open.gif');
			}
			tdivs[i].insertBefore(img, tdivs[i].firstDescendant());
		}
	}
}
