var BDEW = (function (module) { 
   
   	/*################################################################
   	 	Stage
   	 #################################################################*/  
	
	function Stage(stageId,stageInterval,fadeDuration) {
		this.stage = $(stageId);
		this.stageInterval = stageInterval || 5000;
		this.fadeDuration = fadeDuration || 1000;
		this.preview = this.stage.find('div.preview');
		this.items = this.stage.find('div.item');
		this.zIndex = 10;
	
		this.previewLinks = this.stage.find('div.preview a');
	
		this.items.removeClass('active');
		this.previewLinks.removeClass('active');
		
		var self=this;
		
		this.previewLinks.mouseover(function() {
			var index = self.previewLinks.index(this);
			self.fadeItem(index);
		});
		
		this.preview.hover(
			function() {
				self.preview.removeClass('active');			
			},
			function() {
				self.preview.addClass('active');	
			}
		);
		
		this.stage.hover(
			function () {
				self.preview.addClass('active');
				self.stopShow();	
			},
			function () {
				self.preview.removeClass('active');	
				self.startShow();
			}
		);
		
		// show is running ?
		this.showTimerId = null;
		
		this.setActiveIndex(0);
		
		this.startShow();
		
		
	};


	Stage.prototype.startShow = function() {
		var self = this;
		if (!this.showTimerId) {
			this.showTimerId = window.setInterval(function(){
				self.nextItem();
			}, this.stageInterval);
		}
	};

	Stage.prototype.stopShow = function() {
		if (this.showTimerId) {
			window.clearTimeout(this.showTimerId);
			this.showTimerId = null;
		} 
	};

	Stage.prototype.setActiveIndex = function(activeIndex) {
		if (this.activeIndex != null && this.activeIndex != activeIndex) {
			$(this.items.get(this.activeIndex)).removeClass('active');
			$(this.previewLinks.get(this.activeIndex)).removeClass('active');	
		}
		$(this.items.get(activeIndex)).addClass('active');
		$(this.previewLinks.get(activeIndex)).addClass('active');	
		this.activeIndex = activeIndex;
	};

	Stage.prototype.fadeItem = function(to) {
		if (this.activeIndex != to) {
			var fromItem = $(this.items.get(this.activeIndex));
			this.setActiveIndex(to);
			var self = this;
			fromItem.addClass('fading');
			$(this.items.get(to)).css('z-index',++this.zIndex);
			$(this.items.get(to)).fadeIn(this.fadeDuration, function(){
				fromItem.removeClass('fading');
			});			
		}
	};

	Stage.prototype.nextItem = function() {
		var nextActiveIndex = (this.activeIndex + 1) % this.items.size();
		this.fadeItem(nextActiveIndex);
	};
	 
	 
	function createStage(selector) {
		new Stage(selector);
	} 

	/*################################################################
   	 	Social Bookmarks
   	 #################################################################*/  

	function createSocialBookmarks() {
		$('#navFooter li.last').removeClass('last').after('<li id="socialBookmarks"></li>');
		$('#socialBookmarks').bookmark({sites: [
			'delicious', 
			'digg',
			'google',
			'friendfeed',
			'linkarena',
			'misterwong',
			'webnews',
			'yigg',
			'wikio',
			'technorati',
			'twitter'
		]}).addClass('last');		
	}	

	/*################################################################
   	 	Module API
   	 #################################################################*/  

	module.createSocialBookmarks = createSocialBookmarks;
	module.createStage = createStage;
	
    return module; 
	
}(BDEW || {}));


// BUGFIX (Mantis Issue 7293): adds wmode=transparent parameter to embedded swfs in
// order to make lightboxes (overlays) appear above the flash file
if ($.browser.msie) {
	var originalswfobject = window.swfobject.embedSWF;
	window.swfobject.embedSWF = function(p1, p2, p3, p4, p5, p6, p7, params, p9, p10){
	
		if (params == null) {
			params = {};
		}
		params.wmode = 'transparent';
		originalswfobject(p1, p2, p3, p4, p5, p6, p7, params, p9, p10);
	};
}
	
$(window).ready(function(){
	
	
           
	
	BDEW.createStage('#contentStage');
	BDEW.createSocialBookmarks();
	
	
	// mark last table row in each table
	$('table.contenttable tr').find('th:last, td:last').addClass('last');
	
	$('ul#navLeft li.level1').hover(function() {
		$(this).addClass('hover').find('div.level1').addClass('hover');
		
	},function() {
		$(this).removeClass('hover').find('div.level1').removeClass('hover');
	});
	
	// set target=_blank for links with rel="external"
	$('a[rel=external]').each(function(){
		$(this).attr('target','_blank');
	});
	
	$('#accordion').accordion();
	
	// glossary links
	$('a.glossaryMain').click(function(event) {
		event.preventDefault();
		$('#iframeGlossary').attr('src',$(this).attr('rel'));
	})
	
	/*
	
	//stage	
	if($('div.cntStage').length!=0){
		stageArray = new Array();
		currentIdx = 0;
		stageTimeoutTimer = 10000;
		$('div.cntStage').each(function(){
			stageArray[stageArray.length] = $(this);
		})
		stageArray.shuffle();
		for(var i=1;i< stageArray.length;i++){
			stageArray[i].hide();
		}
		window.setInterval('switchStages()',stageTimeoutTimer);
	}
	
	*/
	
	if($('img.movieImage').length!=0){
		movieImageArray = new Array();
		movieImageCurrentIdx = 0;
		movieImageTimeoutTimer = 6000;
		$('img.movieImage').each(function(){
			movieImageArray[movieImageArray.length] = $(this);
		});
		movieImageArray.shuffle();
		for(var i=1;i< movieImageArray.length;i++){
			movieImageArray[i].hide();
		}
		window.setInterval('switchMovieImage()',movieImageTimeoutTimer);
	}
	
	
});


function switchMovieImage(){
	movieImageArray[movieImageCurrentIdx].fadeOut(2000);
	movieImageArray[(movieImageCurrentIdx < movieImageArray.length-1? movieImageCurrentIdx+1:0)].fadeIn(2000);
	movieImageCurrentIdx = (movieImageCurrentIdx < movieImageArray.length-1)? movieImageCurrentIdx+1:0;
}

/*
function switchStages() {
	stageArray[currentIdx].fadeOut(2000);
	stageArray[(currentIdx < stageArray.length-1? currentIdx+1:0)].fadeIn(2000);
	currentIdx = (currentIdx < stageArray.length-1)? currentIdx+1:0;
}

*/

// arrayShuffle
function arrayShuffle() {
	var tmp, rand;
	for(var i=0;i<this.length;i++){
		rand = Math.floor(Math.random() * this.length);
		tmp = this[i]; 
		this[i] = this[rand]; 
		this[rand] =tmp;
	}
}

Array.prototype.shuffle =arrayShuffle;

/************************************************************
* TopUp-Settings
************************************************************/
TopUp.images_path = "/fileadmin/images/top_up/";



TopUp.addPresets({
    "a.gallery": {
      readAltText: 1,
      layout: "quicklook",
      shaded: 1,
	  overlayClose : 1,
	     ondisplay: function() 
		 {
		 },
		 
		 onclose: function() {
		 	($('.te_controls').hide());
		 }
    }
  });
  
TopUp.addPresets({
    'a[href*="/microsite_animation/"]': {
      readAltText: 1,
      layout: "quicklook",
      shaded: 1,
	  type: "iframe", 
	  effect:"fade",
	  width: 962, 
	  height: 542, 
	  overlayClose: 1,

	     ondisplay: function() 
		 {
		 },
		 
		 onclose: function() {
		 	($('.te_controls').hide());
		 }
    }
  });
  
TopUp.addPresets({
    'a[href*="/flipviewer/"]': {
      readAltText: 1,
      layout: "quicklook",
      shaded: 1,
	  type: "iframe", 
	  effect:"fade",
	  width: $(window).width()-($(window).width()/100*20), 
	  height: $(window).height()-($(window).height()/100*20), 
	  overlayClose: 1,

	     ondisplay: function() 
		 {
		 },
		 
		 onclose: function() {
		 	($('.te_controls').hide());
		 }
    }
  }); 
  
$(function() {
	$('.te_content').click(function() {
		TopUp.close();
	});
	
	// enable topup image click
	$('a.gallery').each(function(){
		var obj = $(this).closest('div');
		$('img',obj).css('cursor','pointer');
		$('img',obj).click(function(event){
			$('a',obj).click();
		});
	});
});

/************************************************************
* DISPLAY DEFAULT VALUE IN INPUT FIELD
************************************************************/
function dV(obj,val){obj.value=(obj.value==val)?'':(obj.value=='')?val:obj.value;}
