/* compat mode wegen prototype und so... */
jQuery.noConflict();

var initNavigation = function(el){
	var myOffset = 0;
	jQuery(el + ' .main-1').each( function(i){
		jQuery(this).css('background-position','0px -'+(myOffset)+'px');
		myOffset += (jQuery(this).children('a').height()+16);
	});
};

var initAccordion = function(el){
	jQuery(el).accordion();
			// pseudo links dazubauen
			jQuery(el + ' h3').hover( function(){
				jQuery(this).addClass('focus');
				},
				function(){
				jQuery(this).removeClass('focus');
			});
};

var initBoxes = function(el,open){
	var open = (open)?open:0;
	jQuery(el + ' .box h2')
				.hover(function() {
					jQuery(this).addClass('focus');
					},
					function(){
					jQuery(this).removeClass('focus');
			
				})
			
				.click(function() {
					jQuery(this)
						.blur()
						.toggleClass('active')
						.next().slideToggle();
					jQuery('#right .box h2.active').not(this)
						.removeClass('active')
						.next().slideUp();
				})
				
				.dblclick(function(){
					jQuery('#right .box h2')
						.addClass('active')
						.next().slideDown();
					jQuery(this).blur();
				})
				.next().hide();
	if (jQuery('#empty_right').length != 0) {
		jQuery(el + ' .box:eq('+open+') h2')
				.addClass('active')
				.next().show();
	};
	// newsletterbox sonderfall
	jQuery('#right .box').each( function(){
		// wenn es die NL box ist...
		if( jQuery(this).find('.tx-danewslettersubscription-pi1').length != 0 ){
			
			// ... und etwas anderes als das inputfeld gezeigt wird...
			if( jQuery(this).find('.tx-danewslettersubscription-pi1-mustlogin').length == 0 ){
				// ... dann die NL Box ausklappen:
				jQuery(this).children('h2').addClass('active').next().show();
			}
		}
	});
};

var initSearchbox = function(el_field,el_button){
	jQuery(el_button)
		.addClass('icon')
		.attr('value','')
		.click( function(){ jQuery(this).blur(); });
	jQuery(el_field).addClass('icon');
	jQuery(el_field).focus( function(){ jQuery(this).attr('value','') });
};

//hilfsfunktion zum array shufflen
(function(jQuery){

jQuery.fn.shuffle = function() {
return this.each(function(){
var items = jQuery(this).children().clone(true);
return (items.length) ? jQuery(this).html(jQuery.shuffle(items)) : this;
});
}

jQuery.shuffle = function(arr) {
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
return arr;
}

})(jQuery);



// header animation
var initHeader = function(el,count,max,timeout,random){
	/* statische variante
	/* TO DO: dynamisch mit ajax machen
	*/
	// optionen default:
	var count = (count)?count:2;
	var max = (max)?max:2;
	var timeout = (timeout)?timeout:5000;
	var random = (random)?random:0;
	// let there be bilders:
	var images = new Array();
	for( var i=0;i<count;i++){
		images[i] = 'header-'+(i+1)+'.jpg';
	}

	jQuery.shuffle(images);
	//var out = '';
	var imgs_to_load = max;
	jQuery(el + ' p').html('Loading: ');
	for( var j=0; j<max; j++){
		var myImg = new Image();
		jQuery(myImg)
    		// once the image has loaded, execute this code
    		.load(function() {
      			// set the image hidden by default    
      			jQuery(this).hide();
				jQuery(el).append(this);
				jQuery(el + ' p').append(' '+imgs_to_load);
				imgs_to_load--;
				if(imgs_to_load == 0){
					//jQuery(this).fadeIn('slow');
					bannerAnimate(el,timeout,random);
				}
			})
			.attr('src','/fileadmin/includes/ilf/images/header/'+images[j])
			.attr('alt','ILF Frankfurt')
			.attr('id','bnr'+j);
		//out += '<img src="/fileadmin/includes/ilf/images/header/'+images[j]+'" alt="" title="" style="display:none;" />';
	}
	
	// ab auf die seite damit:
	// und animation:
	//bannerAnimate(el,timeout,random);
};
var bannerAnimate = function(el,timeout,random){	
	setTimeout( function(){
	jQuery(el + ' p').remove();
	jQuery(el).cycle({
		fx:"fade",
		random: random,
		timeout: timeout,
		speed: 2000
	});	
	}, 200);
};

jQuery('document').ready( function(){			
			// ---------------------------------------
			// navigation
			initNavigation('#left');
			// ---------------------------------------
			// searchbox
			initSearchbox('#si','#sb');
			// ---------------------------------------
			// accordion intro
			initAccordion('#accordion');
			// ---------------------------------------
			// boxen rechts
			// initBoxes([element],[open(key)]);
			initBoxes('#right');
			// ---------------------------------------
			// header grafik animation
			initHeader('#banner',23,5,7500,1);
});

