/**
 * @author u1005stulz
 */
 
// expand the jQuery framework with needed functions
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

// functions

// changeBackgroundImage is used from entb_adventures
function changeBackgroundImage (newImageSource) {
	//if its the same image, do nothing
	if (newImageSource == jQuery('#background img').attr('src')) return false;
	objImage = jQuery('#background img');
	//preload the image
	var image1 = jQuery('<img />').attr('src', newImageSource);
	//change the images
	jQuery('#background').animate({opacity:0},500, '', function(){
		objImage.attr('src', newImageSource);
		jQuery('#background').animate({opacity:1},500);
	});
}

// setLayout is used for set the right height on #centerLayout.
// this fix is used because the html-setup works with absolute positioned divs
function setLayout() {
	jQuery(document).ready(function(){
		//position of #timeNav: should continue on right-position of quickNav
		var position = jQuery('#quickNav').position();
		var intLeft = parseInt(jQuery('#quickNav').css('margin-left')) + parseInt(jQuery('#quickNav').width());
		jQuery('#timeNav').css('left',intLeft);
		
		// height of #centerLayout
		if (jQuery('#bg1').length == 0) jQuery('#centerLayout').height(jQuery('#website').height());
			// höhe bei fahrplan (probleme mit dem slider)
		if(jQuery('.tx-kiwislider-pi1').length > 0) setTimeout(" jQuery('#centerLayout').height(jQuery('#wrapper').height() + parseInt(jQuery('#wrapper').css('padding-top')));", 200);
			// height at kiwi-accordion
		if(jQuery('.ka-panel').length > 0) setTimeout(" jQuery('#centerLayout').height(jQuery('#website').height());", 600);
			// height at entb_railway
		if(jQuery('.tx-entbrailway-pi1').length > 0) setTimeout(" jQuery('#centerLayout').height(jQuery('#website').height());", 600);
			// height at toggling comments
		if(jQuery('.tx-comments-pi1').length > 0) setTimeout(" jQuery('#centerLayout').height(jQuery('#website').height());", 300); 
	});
}

// on document ready

jQuery(document).ready(function(){
	// ajax loader at entb_adventures click
	jQuery('.tx-entbadventures-pi1 .item').click(function() {
		jQuery('#ajax_cube h1').before('<img src="fileadmin/images/basics/loading.gif" id="loader" />');
		setLayout();
	});
	//layout
	setLayout();
	//if click on a accordion, fire setLayout
	jQuery('.ka-panel').click(function() {
		setLayout();
	});
	
	// shadows
	var opts = {
          left: 0.2,
          top: 0.2,
          blur: 2,
          opacity: 0.5,
          color: 'black',
          swap: 0
        };	

	//jQuery('.csc-textpic-imagewrap img, #list .thumb img, .news-list-item img').dropShadow(opts);
	//jQuery('.csc-textpic-imagecolumn img, .meldungen img').removeShadow();

    //set cursor at logo's over
	jQuery('#logo').mouseover(function(){
		jQuery(this).css({
	  		'cursor': 'pointer'
	  	});
	});
	
	// initialise navigation
	initQuickNav();
	
	// showBackground
	jQuery('#showBackground').click(function(){
		tmpHeight = jQuery('#wrapper').height();
		jQuery('#content').fadeToggle(500);
		jQuery('#teaser').fadeToggle(500);
		jQuery('#searchNav').fadeToggle(500);
		jQuery('#wrapper').height(tmpHeight)
	});
	jQuery('#showBackground').mouseover(function(){
		jQuery(this).css({
			'cursor': 'pointer'
		});
	});
	// initialize scrollable
	if (jQuery(".itemList").length > 0) {
		jQuery(".itemList").scrollable({mousewheel:true,history:true,speed:400}).navigator().mousewheel();
		if (typeof(defaultScrollablePage)!='undefined' && defaultScrollablePage > 0) {
			jQuery(".itemList").scrollable().seekTo(defaultScrollablePage, 400);
		}
		jQuery(".pane").hide();
	}
});

// init the quicknav (code is taken from niels weber)
var masterWidth = 0;
var activeQuickNav = null;
var QuickNavTimeout = null;

function initQuickNav() {
	
	jQuery('#timeNavSummer').css({'margin-left' : jQuery('#quickNav').width()+220});
	jQuery('#timeNavWinter').css({'margin-left' : jQuery('#quickNav').width()+220});
	jQuery('.quickNavParent').hover(function() {
		masterWidth = jQuery(this.parent).outerWidth();
		jQuery('.quickNavItems').css({'width' : masterWidth});
		jQuery('.quickNavItems').slideUp(400);
		activeQuickNav = this;
		clearTimeout(QuickNavTimeout);
		QuickNavTimeout = setTimeout('slideQuickNav()', 200);
	},function() {
		clearTimeout(QuickNavTimeout);
		jQuery('.quickNavItems', this).slideUp(400)
	});
}
function slideQuickNav() {
	jQuery('.quickNavItems', activeQuickNav).slideDown(500);
}
