/********************************************************************************/
/***** CUFON FONTS *************************************************************/
/******************************************************************************/

Cufon.replace('#tabs > li:not(.view-all-products) > a');
Cufon.replace('#footer .left a', { hover: true });
Cufon.replace('#footer .right');
Cufon.replace('.btn a');
Cufon.replace('h1');
Cufon.replace('.product-detail h2');
Cufon.replace('.sub-menu h2');


/********************************************************************************/
/***** RELL EXTERNAL ***********************************************************/
/******************************************************************************/

$(function() {
	var anchors = $(document).find("a");
	$(anchors).each(function(i) {
		var anchor = $(anchors[i]);
		if($(anchor).attr("rel") == "external") { $(anchor).attr({'target':'_blank'}); }
	});
});


/********************************************************************************/
/***** OVERLAY *****************************************************************/
/******************************************************************************/

$(function() {
// Overlay Fade
	var overlayFade = $('<div></div>').hide().css({
		'width': '100%',
		'height': $(document).height() + 'px'
	}).addClass('overlay-fade').html('');
	$('body').append($(overlayFade));

// Overlay
	var overlay = $('<div></div>').addClass('overlay');
	$(overlayFade).append($(overlay));

// Header and footer
	var overlayHeader	= $('<div></div>').addClass('overlay-header').html('&nbsp;');
	var overlayContent	= $('<div></div>').addClass('overlay-content');
	var overlayFooter	= $('<div></div>').addClass('overlay-footer').html('&nbsp;');
	$(overlay).append(overlayHeader).append(overlayContent).append(overlayFooter);

// Fade out
/*
	$(overlayFade).mousedown(function(event) {
		if($(event.target).attr('class') == 'overlay-fade') {
			$(this).fadeOut(400, function() {
				
			});
		}
	});
*/
});

$(function() {
	var overlayContent	= $('.overlay-content');
	var iframeContent	= $('.iframe-overlay');
	$(overlayContent).html('').append($(iframeContent));
	$(iframeContent).show();
});

function requestOverlay() {
	var iframeContent	= $('.iframe-overlay');
	
	var iframe	= $(iframeContent).find('iframe');
	var src 	= $(iframe).attr('src');
	$(iframe).attr('src','');
	$(iframe).attr('src',src);
	
	$(iframe).load(function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		var overlayFade = $('.overlay-fade');
		$(overlayFade).css({
			'height': $(document).height() + 'px'
		}).fadeIn(400);
		this.height = this.contentWindow.document.body.offsetHeight + 'px';
	});
}


/********************************************************************************/
/***** NAVIGATION DROPDOWN *****************************************************/
/******************************************************************************/

$(function() {
	var LinksList = $('#tabs > li > a');
	var maxProductsVisible = 10;
	
	function runFunction(link) {
		var $this 	= $(link).parent();
		var $subNav	= $(link).next();
		$subNav.show();
		var linkOffset	= $this.offset();
		var popupOffset	= $subNav.offset();
		
		var width1		= $this.width();
		var height1		= $this.height();
		var left1		= linkOffset.left;
		var top1		= linkOffset.top;
		var right1		= left1 + width1;
		var bottom1		= top1 + height1;
		
		var width2		= $subNav.width();
		var height2		= $subNav.height();
		var left2		= popupOffset.left;
		var top2		= popupOffset.top;
		var right2		= left2 + width2;
		var bottom2		= top2 + height2;
		

		$(document).mousemove(function(e) {
			var xPos	= e.pageX;
			var yPos	= e.pageY;
			
			if(yPos < top1 ||
			   (xPos < left1 && yPos < top2) ||
			   (xPos < left2 && yPos > top2) ||
			   (xPos > right1 && yPos < top2)
			   || xPos > right2 || yPos > bottom2 || xPos < left2) {
				if($this.parent().hasClass('view-all-products')) {
					$subNav.fadeOut(100);
				} else {
					$subNav.hide();
				}
			}
		});
	}
	
	$(LinksList).each(function() {
		//$(this).mouseover(function() { $('#tabs .children').hide(); });
		if($(this).next().hasClass('children')) {
			var children = $(this).next();
			if($(this).parent().hasClass('view-all-products')) {
				var childList = $(children).find('li');
				if(childList.length > maxProductsVisible) {
					$(children).css({
						'height':'414px',
						'overflow-y':'scroll'
					});
				} else {
					$(children).css({
						'height':'auto',
						'overflow-y':'auto'
					});
				}
			}

			if($(this).parent().hasClass('view-all-products')) {
				$(this).click(function(e) { runFunction($(this)); e.preventDefault(); });
			} else {
				$(this).mouseover(function() { runFunction($(this)) });
			}
		}
	});
	
	
	
	var calbenchList = $('.sub-menu ul li a');
	
	$(calbenchList).each(function() {
		var $this 	= $(this);
		var $subNav	= $this.next();
		var state 	= 'closed';
		var delay;
		var fadeOutRun;
		if($subNav.length != 0) {
			
			$this.mouseover(function() {
				$subNav.stop().css('opacity',0.99).show();
				
				
				$(document).bind('mousemove', function(e) {
					var linkOffset	= $this.offset();
					var popupOffset	= $subNav.offset();
					
					var width1		= $this.width() + 30;
					var height1		= $this.height() + 5;
					var left1		= linkOffset.left;
					var top1		= linkOffset.top - 3;
					var right1		= left1 + width1;
					var bottom1		= top1 + height1 + 2;

					var width2		= $subNav.width();
					var height2		= $subNav.height();
					var left2		= popupOffset.left;
					var top2		= popupOffset.top;
					var right2		= left2 + width2;
					var bottom2		= top2 + height2;
					var xPos	= e.pageX;
					var yPos	= e.pageY;

					if((xPos > left1 && xPos < right1 && yPos > top1 && yPos < bottom1) || (xPos > left2 && xPos < right2 && yPos > top2 && yPos < bottom2)) {
						$subNav.stop().css('opacity',0.99).show();
					} else {
						$subNav.fadeOut(300, function() {
							width2		= '';
							height2		= '';
							left2		= '';
							top2		= '';
							right2		= '';
							bottom2		= '';
						});
					}
				});
			});
		}
	});
});


/********************************************************************************/
/***** PRESENTATIONS IMAGE CHANGER *********************************************/
/******************************************************************************/

$(function() {
	var sections = $('.image-section');
	$(sections).each(function() {
		var imageMain	= $(this).find('.image-top');
		var imageThumbs = $(this).find('.image-thumbs a');
		$(imageThumbs).each(function(i) {
			$(this).mouseover(function(e) {
				 $(imageMain).hide();
				 $(imageMain[i]).show();
				 
				 $(imageThumbs).removeClass('active');
				 $(this).addClass('active');
				 e.preventDefault();
			});
		});
	});
});

