// JavaScript Document

// A self-executing anonymous function,
// Developed by Artemida.it jQuery plugin.

(function($){

	$.fn.splashScreen = function(settings){

		// Providing default options:

		settings = $.extend({
			textLayers		: [],
			textShowTime	: 3000,
			textTopOffset	: 10,
			div_h			: 100,
			div_w			: 500,
			line_Height 	: 36,
			font_size		: 28
		},settings);

		var promoIMG = this;

		// Creating the splashScreen div.
		// The rest of the styling is in splashscreen.css

		var textrotator = $('<div>',{
			id	: 'textrotator',
			css:{
				width				: settings.div_w,
				margin				: 'auto',
				'text-align'		: 'center',
				height				: settings.div_h
			}
		});

		$(this).append(textrotator);

		

		// Binding a custom event for changing the current visible text according
		// to the contents of the textLayers array (passed as a parameter)

		textrotator.bind('changeText',function(e,newID){

			// If the image that we want to show is
			// within the boundaries of the array:

			if(settings.textLayers[newID]){
				showText(newID);
			}
			
		});	

		textrotator.trigger('changeText',0);

		// Extracting the functionality into a
		// separate function for convenience.

		function showText(id){
			var text = $('<h2>',{
				html:settings.textLayers[id],
				css: {
					marginTop : settings.textTopOffset,
					lineHeight : settings.line_Height +'px',
					fontSize : settings.font_size +'px'
				}
			});

			
				text.fadeIn('slow').delay(settings.textShowTime,function(){
					textrotator.trigger('changeText',[id+1]);
				
				});
			

			textrotator.append(text);
		}

		return this;
	}


	$.fn.navigate = function(options) {
    
		  var defaults = {
			parent : 'ul.nav li' ,
			child : 'ul.subnav' ,
			master : 'li.selected',
			active : 1,
			activated:false
		  };
  
  		var options = $.extend(defaults, options);
		return this.each(function() {
								  
	$(defaults.parent +':nth-child('+ defaults.active +')').addClass('selected');
	
	$('ul.subnav li').removeClass('selected');
	if(defaults.activated == true){
	$(defaults.master).find(defaults.child).show();
	}
	$(defaults.parent).hover(function() { 
							 
		if ($(this).find(defaults.child).is(":empty") ) {
			$(this).parent().find(defaults.master).stop(true,true)
			} else if((defaults.activated == true) && $(this).is(defaults.master)) {
				$(this).parent().find(defaults.master).stop(true,true)}
			else if ( $(this).find(defaults.child).is(":hidden") ) {
				$(this).find(defaults.child).stop(true,true).slideDown('fast');
				if(defaults.activated == true){
				$(this).parent().find(defaults.master).find(defaults.child).stop(true,true).slideUp('fast');}
				}
				
			}, function() {
				if ( (defaults.activated == true) && $(this).is(defaults.master)) {
					$(this).parent().find(defaults.master).stop(true,true)
					}
			else if ( $(this).find(defaults.child).is(":visible") ) {
				$(this).find(defaults.child).stop(true,true).slideUp('fast');
				if(defaults.activated == true){
				$(this).parent().find(defaults.master).find(defaults.child).stop(true,true).slideDown('fast');}
				}
			});
		});
 	};

	$.fn.fader = function(settings){

		// Providing default options:

		defaults = $.extend({
			tofade : '.logos'
		},settings);
			
			$(defaults.tofade).find('li').bind({
			mouseenter : function(){
					var $this = $(this).find( '#mito a' );
					var $image = $this.find( 'img' )
					
					$('<img/>').bind( 'load', function(){
						var $self = $(this);
						var $currImage = $this.children('img:first');
						$self.insertBefore($currImage);
						
						$currImage.stop(true,true).fadeOut(500);
						
					}).attr('src',$image.attr('alt')).attr('alt',$image.attr('src'));
				},
				
				mouseleave: function(){
					var $this = $(this).find( '#mito a' );
					var $image = $this.find( 'img' )
					$this.children('img:nth-child(2)').remove();
					
					$('<img/>').load(function(){
						var $self = $(this);
						var $currImage = $this.children('img:first');
						$self.insertBefore($currImage);
						
						$currImage.stop(true,true).fadeOut(500,function(){
							$(this).remove();
						});
						}).attr('src',$image.attr('alt')).attr('alt',$image.attr('src'));
					}
				});
	}

})(jQuery);
