//<!--
/*
 * Benaki Banner Rotator
 * http://web-scholar.com
 *
 * Copyright (c) 2010 Gabriele Petrioli
 *
 * Date: 2010-05-15 (Sat, 15 May 2010)
 */
 
 
 	function bannerRotator( options )
	{
		this.$group = $(options.element);
		this.effectDuration = options.effectDuration;
		this.rotateDelay = options.rotateDelay;
		this.startDelay = options.startDelay;
		this.uniqueID = options.uniqueID;
		this.enabled = false;
		this.start();
		return this;
	}
	
	bannerRotator.prototype.enable = function(){
			this.enabled = true;
			this.showIndex(0, false);
			this.createTimeout(this.startDelay);
		}
	
	bannerRotator.prototype.showHide = function($show, $hide, trigger){
			if (this.enabled)
			{
				var index = this.$group.children('.template-item').index($show);
				var $currenttip = $('.tooltip',$hide);
				this.slideOutTooltip($currenttip);
				//$currenttip.animate( {bottom:-$currenttip.outerHeight()}, {queue:false, duration:300});
				$hide.fadeOut(this.effectDuration);
				
				var $nexttip = $('.tooltip',$show);
				var $_rotator = this;
				if (trigger)
					{
					$show.fadeIn(this.effectDuration, function(){/*$_rotator.$group.trigger('mouseenter');*/});
					this.slideInTooltip($nexttip);
					}
				else
					{
						$show.fadeIn(this.effectDuration);
						this.slideOutTooltip($nexttip);
					}
				
				$('.tooltip-element-footer a.button_selected', this.$group).removeClass('button_selected').addClass('button');
				$('.tooltip-element-footer', this.$group).each(function(){
																		$(this).find('a:eq('+index+')').addClass('button_selected').removeClass('button');
																		});
			}
		}
	bannerRotator.prototype.slideInTooltip = function( $tooltip ) {
		if (this.enabled)
			{
				var $footer = $('.tooltip-element-footer', $tooltip);
				var $section = $footer.children('.section');
				var sectionpadding = $section.outerWidth() - $section.width();
				var groupelements = $('a',$footer).length ;
				if (groupelements > 1)
					{	
						$section.css('width',$footer.width() - sectionpadding - (groupelements*18) );
					}
				else
					{
						$section.css('width',$footer.width() - sectionpadding );
					}
				if ( $.browser.msie && $.browser.version.substr(0,1)=='7')
					{ $tooltip.animate({bottom:2},{queue:false, duration:100}); }
				else
					{ $tooltip.animate({bottom:0},{queue:false, duration:100}); }
			}
		}
	bannerRotator.prototype.slideOutTooltip = function( $tooltip ) {
			if ( this.enabled )
				$tooltip.animate({bottom:-$tooltip.outerHeight()-1},{queue:false, duration:100});
		}
		
	bannerRotator.prototype.showNext = function() {
		var $this = this.$group.children('.template-item:visible');
		var $next = $this.next();
		if ( $next.length == 0 )
			$next = $this.prevAll('.template-item:first-child');
			
		if ($next.length > 0) 
			{
				this.showHide($next, $this, false);
			}
												
		}
		
	bannerRotator.prototype.showIndex = function (idx, trigger) {
		var $this = this.$group.children('.template-item:visible');
		var $next = this.$group.children('.template-item').eq(idx);
			
		if ($next.length > 0) 
			{
				this.showHide($next, $this, trigger);
			}
		}
		
	bannerRotator.prototype.start = function(){
		var $_rotator = this;
		$('.tooltip', this.$group).each(function(){ 
						var $this = $(this);
						$this.css( 'bottom',-$this.outerHeight() );
						$this.children('div[class^="tooltip-element"]').each(function(){ 
																					  var $this = $(this);
																					  var mypadding = $this.outerWidth() - $this.width();
																					  
																					  $this.css('width',$this.parent().width()-mypadding);
																					  });
					});
		var groupelements = $('.template-item', this.$group).length;
		
		var $button = $('<a/>').html('&nbsp;').attr('href','#').addClass('button');

		$('.tooltip-element-footer', this.$group).each(function(){
																var $this = $(this);
																var $section = $this.children('.section');
																var sectionpadding = $section.outerWidth() - $section.width();
																
																if (groupelements > 1)
																	{
																		for (var i=0;i<groupelements;i++)
																			{
																				$button.clone(true).data('rotatorIndex',i).click(function(event){event.preventDefault();event.stopPropagation();this.blur();if ($(this).is('.button')) {$_rotator.showIndex($(this).data('rotatorIndex'), true);} return false;}).appendTo($this);
																			}
																		$section.css('width',$this.width() - sectionpadding - (groupelements*18) );
																	}
																else
																	{
																		$section.css('width',$this.width() - sectionpadding );
																	}
																});
		$button.remove();
			
		$('.template-item',this.$group).hover(
											  function(){
												  			$_rotator.slideInTooltip(  $('.tooltip',this) );
												  			
												  		},
											  function(){
												  			$_rotator.slideOutTooltip( $('.tooltip',this) );
														}
											  );
		
			this.$group.children('div').hide() ;

		}
	bannerRotator.prototype.createTimeout = function(extradelay){
			if (this.$group.children('div').length > 1) 
			{
				var _self = this;
				this.timeout = setTimeout( 
							  function()
							  	{ 
									_self.showNext(); 
									_self.createTimeout(0);
								}, 
								_self.rotateDelay + extradelay ); 
			}
		}
	bannerRotator.prototype.deleteTimeout = function(){ clearTimeout( this.timeout ); }
	$(window).load(
					  	function(){
								$('.template-group').each( function(index) { idx = index; $(this).data('banners', new bannerRotator(
																									{
																										element: this,
																										effectDuration: 500,
																										rotateDelay: 7000,
																										startDelay: 1000*index,
																										uniqueID: idx
																									}));
																				$(this).data('banners').enable();
																								   
																			} 
															).hover(function(){
																	$(this).data('banners').deleteTimeout();
																}, function(){
																	$(this).data('banners').createTimeout(0);
																	}
															);
								$('.controller').css('position','relative').css('left',0);
								$('.download a').attr('target','_blank');
	
							}	
					  );
	/*
	$(document).ready(
		function(){
			$('.template-group .tooltip').each(function(){ 
						$this = $(this);
						$this.css( 'bottom',-$this.outerHeight() ); 
						$this.css('width',$this.parent().width()-10);
					});
			$('.template-group').each(function(){ $(this).children('div:gt(0)').hide() });
			$('.template-item').hover(
				function(){
						var $tip = $('.tooltip',this);
						$tip.animate({bottom:0},{queue:false, duration:100});
					},
				function(){
						var $tip = $('.tooltip',this);
						$tip.animate(
							{bottom:-$tip.outerHeight()-1},
							{queue:false, duration:100, complete: 
								function(){
											$this = $(this).parents('.template-item');
											$next = $this.next();
											if ( $next.length == 0 )
												$next = $this.prevAll('.template-item:first-child');
												
											if ($next.length > 0) 
												{
													$next.fadeIn(500); 
													$this.fadeOut(500); 
												}
										}
									});
						
					}
				); // hover
		}
	);
	*/
//-->
