
$(function() { 

var Navigation = function() {
	var me = this;
	var args = arguments;
	
	var self = {
		c: {
			navItems: '#navigation .product, #navigation .howto, #navigation .faq, #navigation .testimonials, #navigation .mailing, #navigation .buy',
			
			navSpeed: ($.browser.safari ? 600 : 350),
			
			snOpeningSpeed: ($.browser.safari ? 400 : 250),
			snOpeningTimeout: 150,
			
			snClosingSpeed: function() {
				if (self.subnavHovered()) return 123450; return 150; 			
			},
			snClosingTimeout: 700 
		},
		
		init: function() {
			$('.level_1', this.c.navItems).css({
					'opacity': 0
				});
			
			this.initHoverFades();
			/*this.initSubmenus();*/
		},
		
		subnavHovered: function() {
			var hovered = false;
			$(self.c.navItems).each(function() {
				if (this.hovered /* && $('.level_1', this).length > 0*/) hovered = true;
			});
			return hovered;
		},
		
		initHoverFades: function() {
			$('#navigation .main').append('<span class="hover"></span>');
			
			$('#navigation .hover').css('opacity', 0);
			
			/*$('#navigation .product, #navigation .mailing,#logotype').hover(function() {*/
			$('#navigation .main,#subnavigation a').hover(function() {

				self.fadeNavIn.apply(this);
			}, function() {
				var el = this;
				
				setTimeout(function() {
					if (!el.open) self.fadeNavOut.apply(el);
				}, 10);
			});
		},
		
		fadeNavIn: function() {
			$('.hover', this).stop().animate({
				'opacity': 1
			}, self.c.navSpeed);
		},

		fadeSubnavIn: function() {
			var el = this;
			$('.level_1', this)
				.stop()
				.css('display', 'block')
				.animate({
					'top': '80px',
					'opacity': 1
				}, self.c.snOpeningSpeed, function() { el.open = true; });
		},

		fadeNavOut: function() {
			$('.hover', this).stop().animate({
				'opacity': 0
			}, self.c.navSpeed);
		
		},

		fadeSubnavOut: function() {
			$('.level_1', this)
				.stop()
				.animate({
					/*'top': '70px',*/
					'opacity': 0
				}, self.c.snClosingSpeed(), function() { $(this).css({'top': '70px', 'display': 'none'}); });
		},
		
		initSubmenus: function() {
			$(this.c.navItems).hover(function() {
				$(self.c.navItems).not(this).each(function() {
					self.fadeNavOut.apply(this);
					self.fadeSubnavOut.apply(this);
				});
				
				this.hovered = true;
				if ($('.level_1', this).length == 0) return;
		
				var el = this;
				
				self.fadeNavIn.apply(el);

				clearTimeout(el.level1);
				
				el.level1 = setTimeout(function() {						
					self.fadeSubnavIn.apply(el);
				}, self.c.snOpeningTimeout);
				
				
			}, function() {
				this.hovered = false;
				if ($('.level_1', this).length == 0) return;
				var el = this;
				
				clearTimeout(el.level1);
				if (!el.open) self.fadeNavOut.apply(el);

				el.level1 = setTimeout(function() {
					el.open = false;

					self.fadeSubnavOut.apply(el);
					self.fadeNavOut.apply(el);

				}, self.c.snClosingTimeout);


			});
		}
		
	};
	
	self.init.apply(self);
	return self;
};

function d(s) {
//	$('body').append	('<p>' + s + '</p>');
}

$(function(){
	new Navigation();
});/* Codename Rainbow */

function initGradients(s) {
	$(function() {
		$(s).each(function() {
			var el = this;
			// Parse the inputs
			var from = '#ffffff', to = '#000000';
			var
				fR = parseInt(from.substring(1, 3), 16),
				fG = parseInt(from.substring(3, 5), 16),
				fB = parseInt(from.substring(5, 7), 16),
				tR = parseInt(to.substring(1, 3), 16),
				tG = parseInt(to.substring(3, 5), 16),
				tB = parseInt(to.substring(5, 7), 16);

			
			var h = $(this).height() * 1.5;
			var html;
			
			if (this.initHTML)
				html = this.initHTML;
			else
				html = this.innerHTML;
			
			this.initHTML = html;
			this.innerHTML = '';
			
			for (var i = 0; i < h; i++) {
				var c = '#' +
					(Math.floor(fR * (h - i) / h + tR * (i / h))).toString(16) +
					(Math.floor(fG * (h - i) / h + tG * (i / h))).toString(16) +
					(Math.floor(fB * (h - i) / h + tB * (i / h))).toString(16);

				$('<span class="rainbow rainbow-' + i + '" style="color: ' + c + ';"><span style="top: ' + (-i - 1) + 'px;">' + html + '</span></span>').appendTo(this);
			}
			
			$('<span class="highlight">' + html + '</span>').appendTo(this);
			$('<span class="shadow">' + html + '</span>').appendTo(this);

		});
		
	});
}

initGradients('.rainbows');$(function() { 

	initTooltips({
		timeout: 3000
	}); 

});

function initTooltips(o) {
//alert($.browser.safari)

	var showTip = function() {
		var el = $('.tt', this).css('display', 'block')[0];
		
		var ttHeight = $(el).height();
		var ttOffset =  el.offsetHeight;
		var ttTop = ttOffset + ttHeight;

		$('.tt', this)
			.stop()
			.css({
				'opacity': 0,
				'top': 10 - ttOffset
			})
			.animate({
				'opacity': 1,
				'top': 5 - ttOffset
			}, 250);
	};

	var hideTip = function() {
	
		var self = this;
		var el = $('.tt', this).css('display', 'block')[0];

		var ttHeight = $(el).height();
		var ttOffset =  el.offsetHeight;
		var ttTop = ttOffset + ttHeight;
	//	alert(label.height());
	//	el.hiding = true;
		$('.tt', this)
			.stop()
			.animate({
				'opacity': 0,
				'top': 10 - ttOffset
			}, 250, function() {
				el.hiding = false;
				$(this).css('display', 'none');
			});
		
		
	};
	
	$('.tip .tt').hover(
		function() { return false; },
		function() { return true; }
	);
	
	$('.tip').hover(
		function(){
			var self = this;
			showTip.apply(this);
			if (o.timeout) this.tttimeout = setTimeout(function() { hideTip.apply(self) } , o.timeout);
		},
		function() {
			clearTimeout(this.tttimeout);
			hideTip.apply(this); 
		}
	);

}

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
};


});
