;(function(jQuery) {

	var defaults = {
		scrollableContentClass: '',
		slideElementID: '',
		currentPID: 0,
		rootLevelElementClass: '',
		loadingImage: '',
		slideWidth: 0,
		slideHeight: 0,
		ajaxCallTypeNum: 0,
		headerToReplace: '',
		descriptionToReplace: '',
		useSitetitleImage: 1,
		speed: 5000
	};

	
	//extend the fn for the methods
	
	jQuery.fn.ifwsMultiPageContentSlider = function(settings) {
		
		jQuery.extend(this,{
			preLinkList: new Array(), //links before the active one
			postLinkList: new Array(), //links after the active one
			linkList: new Array(), //for scrolling to find the right coordinates
			
			initialize: function() {
				var slider = this;
				var $slideContent=jQuery(jQuery('.'+this.slideElementID));
				var $slide=jQuery($slideContent.find('ul.ifwsmpageslider-slider'));
				var loading='<div class="ifwsmpageslider-loading"><p>Inhalt wird geladen...</p></div>';
				var $currentListElement=jQuery(this).find('a[rel='+this.currentPID+']').closest('li');
				
				//at this point there should be only one ifwsmpageslider-scrollable
				//add the id with the pid of the current page
				var $scrollable=jQuery('.ifwsmpageslider-scrollable');
				$scrollable.attr('id', 'ifwsmpageslider-scrollable'+this.currentPID).attr('rel',this.currentPID);
				
				//add the content elements pre or after the active element
				var postAdd=0;
				
				//get the current site title and put it into the corresponding menu entry
				if (this.useSitetitleImage) {
					$currentListElement.append('<div class="seitentitel-image" style="display: none;"></div>');
					jQuery('.seitentitel').find('img').clone().appendTo($currentListElement.find('div.seitentitel-text'));
				} else {
					$currentListElement.append('<div class="seitentitel-text" style="display: none;"></div>');
					jQuery('.seitentitel').find('h1').clone().appendTo($currentListElement.find('div.seitentitel-text'));
				}
				
				//get the current abstract-text and put it into the corresponding menu entry
				var $abstracttext=jQuery($slideContent).find('div.csc-frame-invisible');
				$abstracttext.remove();
				$currentListElement.find('.abstract-text').append($abstracttext);
				//also to the current description cause this should be empty
				jQuery('.'+this.descriptionToReplace).empty().append($abstracttext.clone());

				//find all links
				jQuery(this).find('a[rel]').each(function(i) {
					//add the link to linkList for the ajax getcontent
					var pageid=jQuery(this).attr('rel');
					if (pageid==slider.currentPID) {
						slider.postLinkList.push(pageid);
						postAdd=1;
					} else if (postAdd==0) {
						slider.preLinkList.push(pageid);
					} else {
						slider.postLinkList.push(pageid);
					}
					slider.linkList.push(pageid);
					
					//get the other contents per ajax except the current
					if (slider.currentPID!=jQuery(this).attr('rel')) slider.ajaxLoadContent(this);

					//add the image for menu fading
					//jQuery(this).wrapInner('<span></span>');
					//jQuery(this).append('<img src="fileadmin/template/pics/slide_tab_background.jpg" class="background-fade" />');
					//jQuery(this).find('img').css('width', jQuery(this).outerWidth());

					//add the sliding event
					jQuery(this).click(function(e) {
						slider.slideTo(e, this);
					});
					
					//add the hover effect
					//jQuery(this).hover(function(e) {
					//	slider.menuItemHoverIn(e, this);
					//}, function(e) {
					//	slider.menuItemHoverOut(e, this);
					//});
				});
							
				//reposition the slide so that the active content is still displayed
				$slide.css({
					'top': this.preLinkList.length*this.slideHeight*-1+'px',
					'height': (this.preLinkList.length+this.postLinkList.length)*this.slideHeight+'px'
					}); //move up
				
				//add the content containers 
				if (this.preLinkList.length>0) {
					for (var i=this.preLinkList.length-1;i>=0;i--) {
						$slide.prepend('<li id="ifwsmpageslider-scrollable'+this.preLinkList[i]+'" class="ifwsmpageslider-scrollable" rel="'+this.preLinkList[i]+'">'+loading+'</li>');
					};
				};
			
				if (this.postLinkList.length>1) { //1 cause the first is always the active one
					for (i=1;i<this.postLinkList.length;i++) {
						$slide.append('<li id="ifwsmpageslider-scrollable'+this.postLinkList[i]+'" class="ifwsmpageslider-scrollable" rel="'+this.postLinkList[i]+'">'+loading+'</li>');
					};
				};
				
				jQuery(this).children('.'+this.rootLevelElementClass).find('ul').each(function(i) {
					slider.initSubMenus(this);
				});
				
				jQuery(this).find('a').not('[rel]').each(function(i){
					jQuery(this).click(function(e) {
						slider.toggleSubMenu(e, this);
					});
				});
				
			},

			initSubMenus: function(item) {
				var activepage=jQuery(item).find('a[rel='+this.currentPID+']');
				if (!jQuery(activepage).attr('rel')) {
					//this is not the active submenue -> hide it
					jQuery(item).slideUp('fast');
				} else {
					jQuery(item).addClass('active-submenu');
				}
				//alert(jQuery(item).prev('span').children('a:first').attr('rel'));
			},
			
			ajaxLoadContent: function(item) {
				var slider = this;
				var rel = jQuery(item).attr('rel');
				jQuery.ajax({
					url: 'index.php?id='+rel+'&type='+this.ajaxCallTypeNum,
					success: function(result) {
						slider.insertAjaxContent(result, rel);
					}
				});
			},
			
			insertAjaxContent: function(result, rel) {
				//alert(result);
				if (result!='')	{
					//if there's content replace the loading... with the fetched
					jQuery('#ifwsmpageslider-scrollable'+rel).empty().append(result);
					//fetch the sitetitle (image or text)
					if (this.useSitetitleImage) {
						var sel=jQuery('#ifwsmpageslider-scrollable'+rel).find('div.seitentitel-image');
					} else {
						var sel=jQuery('#ifwsmpageslider-scrollable'+rel).find('div.seitentitel-text');
					}
					//remove the image from the content and add it to the link to find it later again
					jQuery(sel).remove();
					jQuery(this).find('a[rel='+rel+']').closest('li').append(sel);
					jQuery(sel).css('display','none');
					//fetch the abstract-text => frame-invisible within the content
					var $abstracttext=jQuery(jQuery('#ifwsmpageslider-scrollable'+rel).find('div.csc-frame-invisible'));
					$abstracttext.remove().removeClass('csc-frame-invisible');
					jQuery(this).find('a[rel='+rel+']').closest('li').find('.abstract-text').append($abstracttext);
				}
				//alert(result);
			},
			
			slideTo: function(e, link) {
				//slide to the correct position
				var $slideContent=jQuery(jQuery('.'+this.slideElementID));
				var $slide=jQuery($slideContent.find('ul.ifwsmpageslider-slider'));
				var cIndex=0;
				var rel = jQuery(link).attr('rel');
				while (cIndex<this.linkList.length && this.linkList[cIndex]!=rel) cIndex++;
				//var cIndex=this.linkList.indexOf();
				var topIndent = cIndex*this.slideHeight*-1;
				
				//set the current span wrapping the a-tag to the active;
				jQuery('.'+this.rootLevelElementClass).find('span.active').removeClass('active');
				jQuery(link).parent('span').addClass('active');
				$slide.animate({'top' : topIndent}, {queue: false, duration: 400});

				//replace document title
				document.title=jQuery(link).find('span').text()+' '+jQuery(link).closest('li').find('p:first').text()
				
				//replace header 
				if (this.headerToReplace!='') {
					if (this.useSitetitleImage) {
						var $el = jQuery(jQuery(link).closest('li').find('div.seitentitel-image').find('img')).clone();
						jQuery('.'+this.headerToReplace).find('img').remove();
					} else {
						var $el = jQuery(jQuery(link).closest('li').find('div.seitentitel-text').find('h1')).clone();
						jQuery('.'+this.headerToReplace).find('h1').remove();
					}
					$el.appendTo('.'+this.headerToReplace);
					$el.fadeIn('slow');
				}
				
				//replace description
				if (this.descriptionToReplace!='') {
					jQuery('.'+this.descriptionToReplace).empty().append(jQuery(link).closest('li').find('.abstract-text').find('div:first').clone());
				}
				
				e.preventDefault();
			},
			
			menuItemHoverIn: function(e, link) {
				jQuery(link).parent('span').addClass('hover');
				jQuery(link).closest('li').find('img.background-fade').css('display', 'block');
				e.preventDefault();
			},
			
			menuItemHoverOut: function(e, link) {
				jQuery(link).parent('span').removeClass('hover');
				jQuery(link).closest('li').find('img.background-fade').css('display', 'none');
				e.preventDefault();
			},
			
			toggleSubMenu: function(e, link) {
				//now check if the clicked item contains a menue itself and slidedown
				var $container=jQuery(jQuery(link).closest('li').find('ul'));
				if ($container.length) { //be sure there is a container
					if (!$container.hasClass('active-submenu')) {
						jQuery('ul.active-submenu').slideUp('fast', function() {
							$container.slideDown('fast').addClass('active-submenu');
						}).removeClass('active-submenu');
					};
				};
				
				e.preventDefault();
			}
						
		});

		jQuery.extend(this, defaults, settings);
		
		return this;
	};
})(jQuery);
