/** MAC OS Specific CSS Fixes
 *******************************************************/
$(function(){
	if (navigator.userAgent.indexOf('Mac OS X') == -1) return;
	$('button:not(.sm) span, .button:not(.sm) span').css('line-height', '26px');
});

/** homepage box heights
 *******************************************************/
$(function(){
	if ( !$('#homePage')[0] ) return;
	var boxes = $('.box:not(#products-box)');
	var diff = $(boxes[0]).height() - $(boxes[0]).find('.contents').height();
	var max = 0;
	boxes.each(function(){
		var c = $(this).find('.contents');
		c.css('height', 'auto');
		var h = c.height();
		if ( h > max ) max = h;
		c.removeAttr('style');
	});
	boxes.css('height', max+diff);
	boxes.find('.contents').css('height', max);
});

/** suite-features panel functionality
 *******************************************************/
$(function(){
	if ( !$('#suite-features')[0] ) return;
	$('#suite-features li.closed').click(function(){
		$(this).attr('class', $(this).hasClass('open') ? 'closed' : 'open');
	});
});

/** showcase snippet functionality
 *******************************************************/
$(window).load(function() {
	if ( !$('body#homePage')[0] ) return;
	$('#showcase div:eq(0)').nivoSlider({
		effect:       'sliceDown', // Specify sets like: 'fold,fade,sliceDown'
		boxCols:      8,      // For box animations
		boxRows:      8,      // For box animations
		animSpeed:    500,    // Slide transition speed
		pauseTime:    5000,   // How long each slide will show
		controlNav:   false,  // 1,2,3... navigation
		directionNav: false,  // Next & Prev navigation
		pauseOnHover: false   // Stop animation while hovering
	});
});
$(function(){
	return;
	var showcase = $('#showcase');
	if ( !showcase[0] ) return;
	if ( showcase.find('a').length <= 1) return;
	setInterval(function(){
		var c = showcase.find('a:visible');
		var n = c.next();
		if ( !n[0] ) n = showcase.find('a:eq(0)');
		c.fadeOut(800);
		n.fadeIn(800);
	}, 5000);
});

/** fancybox zoom functionality
 *******************************************************/
$(function(){
	if ( !$('a.zoom')[0] ) return;
	$('a.zoom').fancybox({
		'titleShow'     : false,
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn'       : 500,
		'speedOut'      : 200
	});
});

/** ledssuite features
 *******************************************************/
$(function(){
	if ( !$('body[id^=productsLedssuite]')[0] ) return;
	if ( $('#showcase .holder div').length <= 1 ) return;

	var
		cycle,                            // interval timer
		switching = false,                // true when animation is running. prevents double switching
		clicked = false,                  // stops cycle after mouseout if navigation buttons were clicked
		holder = $('#showcase .holder');  // wrapper for the cycling features

	var prev = function(){
		if (switching) return false;
		switching = true;
		var c = $('#showcase .holder div:eq(0)');
		var p = $('#showcase .holder div:last');
		var w = c.outerWidth();
		c.css('left', 0);
		p.css('left', w*-1);
		holder.prepend(p);
		c.animate({'left': w}, 1000, function(){c.removeAttr('style');});
		p.animate({'left': 0}, 1000, function(){switching = false;});
	}

	var next = function(){
		if (switching) return false;
		switching = true;
		var c = $('#showcase .holder div:eq(0)');
		var n = c.next();
		var w = c.outerWidth();
		c.animate({'left': w*-1}, 1000, function(){
			holder.append(c);
			c.removeAttr('style');
		});
		n.animate({'left': 0}, 1000, function(){switching = false;});
	};

	$('#showcase .holder-prev, #showcase .holder-next').click(function(){
		clearInterval(cycle);
		clicked = true;
		if ( $(this).hasClass('holder-prev') )
			prev();
		else next();
	});

	holder.mouseover(function(){if (!clicked) clearInterval(cycle);});
	holder.mouseout(function(){if (!clicked) cycle = setInterval(next, 4000);});
	cycle = setInterval(next, 4000);
});

/** division showcase
 *******************************************************/
$(function(){
	if ( !$('#division-showcase')[0] ) return;
	var
		showcase   = $('#division-showcase'),
		nav        = showcase.find('ul a'),
		img_holder = showcase.find('div'),
		cycle      = false,
		switching  = false,
		clicked    = false,
		w          = img_holder.outerWidth();

	var next = function(){
		if (switching) return false;
		switching = true;
		var c = img_holder.find('a:visible');
		var n = c.next();
		if ( !n[0] ) n = img_holder.find('a:eq(0)');

		var i = n.index();
		showcase.find('.active').removeClass('active');
		nav.eq(i).parent().addClass('active');

		c.fadeOut(400, function(){
			c.attr('style', 'display: none;');
		});
		n.fadeIn(750, function(){
			switching = false;
		});
		/*
		c.animate({'left': w}, 750, function(){
			c.attr('style', 'display: none;');
		});
		n.css({'left': w*-1, 'display': 'block'}).animate({'left': 0}, 750, function(){
			switching = false;
		});
		*/
	};

	nav.click(function(){
		clicked = true;
		clearInterval(cycle);
		if (switching) return false;
		if ( $(this).parent().hasClass('active') ) return false;

		switching = true;
		showcase.find('.active').removeClass('active');
		$(this).parent().addClass('active');

		var i = nav.index(this);
		var c = img_holder.find('a:visible');
		var n = img_holder.find('a').eq(i)

		c.fadeOut(400, function(){
			c.attr('style', 'display: none;');
		});
		n.fadeIn(750, function(){
			switching = false;
		});
		/*
		c.animate({'left': w}, 750, function(){
			c.attr('style', 'display: none;');
		});
		n.css({'left': w*-1, 'display': 'block'}).animate({'left': 0}, 750, function(){
			switching = false;
		});
		*/
		return false;
	});

	img_holder.mouseover(function(){if (!clicked) clearInterval(cycle);});
	img_holder.mouseout(function(){if (!clicked) cycle = setInterval(next, 6000);});
	cycle = setInterval(next, 6000);
});
