(function($) {
	$.khsb = $.khsb || {};

	$.extend($.khsb, {
		baseUrl: 'http://www.khsb-berlin.de/fileadmin/templates/',
		
		imageThumbs: function($el) {
			if($el.size() == 0){
				return false;
			}
			/*
			jQuery.get($.khsb.baseUrl+'images/images-thumb/list_content.php', null, function(data) {
				if(typeof data == 'object'){
					var randomImage = data[Math.floor(Math.random()*data.length)];
					$el
						.css('background-image', 'url('+$.khsb.baseUrl+'images/images-thumb/'+randomImage+')')
						.css('background-repeat', 'no-repeat')
						.css('border', '.1em solid #dddddd');
				}
			}, 'json');
			*/
		},
		imageHome: function() {
			var $el = jQuery('#content-holder.homepage');
			var countHomepageImages = homepageImages.length-1;
			if ($el.size() > 0 && countHomepageImages > 0) {
				var randno = Math.floor ( Math.random() * countHomepageImages+1 );
				if (!randno || randno == 0) {
					randno = 1;
				}
				var randomImage = homepageImages[randno];
				$el.css('background-image', 'url('+randomImage+')');
			}
		}	
	});
})( jQuery);

/**
 *
 */
jQuery(document).ready(function() {
	jQuery('input.tx-indexedsearch-searchbox-sword').focus(function() {
		var $this = $(this);
		if ($this.val().toLowerCase() == 'suche') {
			$this.val('');
		};
	});
	
	jQuery('input.tx-indexedsearch-searchbox-sword').blur(function() {
		var $this = $(this);
		if ($this.val().toLowerCase() == '') {
			$this.val('SUCHE');
		};
	});
	
	//Encode HTML Entities in code blocks
	jQuery('pre').each(function(index) {
		var $this = jQuery(this),
			htmlContent = $this.html();
		$this.text(htmlContent);
	});
	
	//Zebra Tables
	jQuery("tr:nth-child(odd)").addClass("odd");
	
	$.khsb.imageThumbs(jQuery('.image-thumb:first'));

	if (typeof(homepageImages) !== 'undefined') {
		$.khsb.imageHome();
	}
/*
	jQuery.get('fileadmin/templates/main-menu-holder.html', function(data) {
	  	var $menuHolder = jQuery(data);
		jQuery("ul.main-navigation li").each(function(index) {
			var $this = jQuery(this),
				currentSection = $this.find('a').attr('id');
			if (typeof currentSection != 'undefined') {
				var sectionParts = currentSection.split('_');
				$menuHolder.find('div.menu-'+sectionParts[1].toLowerCase()).appendTo($this);	
			};		
		});
	});
*/
	jQuery(".main-navigation li").hover(function() {
		var $this = jQuery(this),
			section = $this.attr('class');
		$this.addClass('active');
		$this.find('div:first').stop(true, true).addClass("menu-active");
		$this.find('div:first').slideDown('medium', function() {});

	}, function() {
		$(this).removeClass('active');
		$(this).find('div:first').stop().slideUp('fast', function() {
            var $this = $(this);
            $this.css('height', 'auto');
            $this.removeClass('menu-active');
        });
	});
	
	// jQuery('#content-holder pre, #content-holder p').each(function(index) {
	// 	var $this = jQuery(this);
	// 	if ($this.html() == '' || $this.html() == '&nbsp;') {
	// 		$this.remove();
	// 	};
	// });
	jQuery('.print').click(function() {
		jQuery('#container').printElement({
			leaveOpen:false,
			printMode:'popup',
			overrideElementCSS:[
			'/typo3conf/ext/khsb/res/css/print.css',
			//fileadmin/templates/css/print.css',
			//{ href:'fileadmin/templates/css/print.css',media:'print'}]
			{ href:'/typo3conf/ext/khsb/res/css/print.css',media:'print'}]
		});
		return false;
	});
});

