(function($){
	//Style Fixes on the nav
	var height = 'auto';
	$('#nav-links .subnav').each(function() {
		$(this).css('display', 'block');
		var products_row = $(this).find('.products-row');
		if ( products_row.length > 0 && products_row.height() != null ) {
			height = products_row.height();
		}
		$(this).css('display', 'none');	
	});
	$('.products-row > li').css('height', height);
	
	$('#nav-links li').hover(
		function() {
			$('.subnav', this).css({'display':'block'});
			/*
			if ($('.subnav-shadow', this).length == 0) {
				var subNavPosition = $('.subnav', this).position();
				var subNavWidth = $('.subnav', this).outerWidth();
				var subNavHeight = $('.subnav', this).outerHeight();
				$('.subnav', this).after($('<div class="subnav-shadow">').css({ position: 'absolute', top: '34px' }).width(subNavWidth).height(subNavHeight));
			}
			*/
		},
		function() {
			$('.subnav', this).css({'display':'none'});
		}
	);
	$('#nav-links ul li ul.products-row > li:last').css({'border-right':'0px'});
	
	
	// Define the height of the tab content area to prevent browser jumpiness. Fade in the first tab.
	var tallestTabContentHeight = 0;
	$('#home-tabs-content')
		.find('div.tab-text')
			.each(function(){
				tallestTabContentHeight = $(this).height() > tallestTabContentHeight ? $(this).height() : tallestTabContentHeight;
			})
			.filter(':first')
				.fadeIn(200)
				.addClass('active-tab')
			.end()
		.end()
		.css('height', tallestTabContentHeight * 1.5);
	
	
	/**
	 * country dropdown
	 *
	 * this loads the languages for that country
	 */
	$('#country').change(function() {
		if ($(this).val().match('^https?://')) {
			var url = $(this).val();
		} else if ($(this).val() == '') {
			var url = parent.location.protocol + '//' + DOMAIN_ROOT + URI_SITE;
		} else {
			var url = parent.location.protocol + '//' + $(this).val() + '.' + DOMAIN_ROOT + URI_SITE;
		}
		window.location = url;
	});
	
	/**
	 * language dropdown
	 *
	 * this redirects the user to the new language
	 */
	$('#language').change(function() {
		if ($(this).val().match('^https?://')) {
			var url = $(this).val();
		} else {
			var url = URI_SITE + '/' + $(this).val();
		}

		window.location = url;
	});
	
	/**
	 * Axis41 Tabs
	 * Simple tabs for your everyday needs. Requires jQuery History for hashing.
	 * TODO Check if jquery.history methods are avaialble and don't use them if not.
	 */
	$.fn.tabs41 = function(opts) {
		var conf = {
			activeClass: 'active'
		};
		if (opts) { $.extend(conf, opts); }
		
		return this.each(function() {
			var contentsSelector = '';
			var tabs = this;
			$('a', this).each(function() {
				var link = '.' + $(this).attr('href').replace(/^.*#/, '');
				contentsSelector += link + ', ';
			}).click(function(e) {
				$(contentsSelector).hide();
				var url = $(this).attr('href').replace(/^.*#/, '');
				$('.' + url).show();
				$(this).parent().siblings().andSelf().removeClass(conf.activeClass).end().end().addClass(conf.activeClass);
				if ($.history != undefined) {
					$.history.load(url);
				}
			    $(tabs).trigger('tabClicked', [e.currentTarget]);
				return false;
			});
			
			$(contentsSelector).hide();
			if ($.history != undefined) {
				$.history.init(function(hash) {
					if (hash != '') {
						$('a[href="#' + hash + '"]', tabs).click();
					} else {
						$('a', tabs).first().parent().addClass(conf.activeClass);
						$(contentsSelector).first().show();
						// $('a:first', tabs).click();
					}
				});
			} else {
				$('a', tabs).first().parent().addClass(conf.activeClass);
				$(contentsSelector).first().show();
			}
		});
	};
	
	/**
	 * Reliably get width from element(s), regardless of css display state.
	 * @returns Returns total width of all items found.
	 * @author Remington Reed
	 */
	$.fn.width41 = function(opts) {
		var conf = {
			hiddenTarget: null,
			outer: false,
			includeMargin: false
		};
		if (opts) { $.extend(conf, opts); }
		
		var combinedWidth = 0;
		this.each(function() {
			if (conf.hiddenTarget === null) conf.hiddenTarget = this;
			
			var originalCSS = {
				position: $(conf.hiddenTarget).css('position'),
				visibility: $(conf.hiddenTarget).css('visibility'),
				display: $(conf.hiddenTarget).css('display')
			};
			
			$(conf.hiddenTarget).css({
				position: 'absolute',
				visibility: 'hidden',
				display: 'block'
			});
			
			var elemWidth;
			if (conf.outer == true) {
				elemWidth = $(this).outerWidth(conf.includeMargin);
			} else {
				elemWidth = $(this).width();
			}
			combinedWidth += elemWidth;
			$(conf.hiddenTarget).css(originalCSS);
		});
		return combinedWidth;
	};
	
	/**
	 * Reliably get height from element(s), regardless of css display state.
	 * @returns Returns total height of all items found.
	 * @author Remington Reed
	 */
	$.fn.height41 = function(opts) {
		var conf = {
			hiddenTarget: null,
			outer: false,
			includeMargin: false
		};
		if (opts) { $.extend(conf, opts); }
		
		var combinedHeight = 0;
		this.each(function() {
			if (conf.hiddenTarget === null) conf.hiddenTarget = this;
			
			var originalCSS = {
				position: $(conf.hiddenTarget).css('position'),
				visibility: $(conf.hiddenTarget).css('visibility'),
				display: $(conf.hiddenTarget).css('display')
			};
			
			$(conf.hiddenTarget).css({
				position: 'absolute',
				visibility: 'hidden',
				display: 'block'
			});
			
			var elemHeight;
			if (conf.outer == true) {
				elemHeight = $(this).outerHeight(conf.includeMargin);
			} else {
				elemHeight = $(this).height();
			}
			combinedHeight += elemHeight;
			
			$(conf.hiddenTarget).css(originalCSS);
		});
		return combinedHeight;
	};
	
	$('.products-row').width($('.products-row > li').width41({ outer: true, includeMargin: true, hiddenTarget: '.subnav' }) + 35);

	
	/**
	 * Localization Dropdowns
	 * @author Remington Reed
	 */
	$.fn.localeDropdown = function(opts) {
		return this.each(function() {
			$(this).click(function() {
				$('ul', this).show();
			});
			$(this).mouseleave(function() {
				$('ul', this).hide();
			});
		});
	};
	
	$('.locale-dropdown').localeDropdown();
	
	/* Safari Style Fixes */
	if ( $.browser.webkit ) {
		$('#nav-links li a').css({'padding-top':'0px'});
		$('#top-nav-links li a, #footer-nav li a, #footer-nav li p').css({'padding-top':'0px','padding-bottom':'1px'});
		$('#footer-nav li p').css({'margin-top':'-1px'});
		$('#home-tabs-nav li').css({'margin-top':'1px'});
	}
	
})(window.jQuery);

Shadowbox.init({ skipSetup: true });
Shadowbox.init({ skipSetup: true });


/**
 * Manually open shadowbox window for video
 */
function fancyVideo(url, width, height) {
	Shadowbox.init({ skipSetup: true });
    // open a welcome message as soon as the window loads
    Shadowbox.open({
        content:    url,
        player:     'iframe',
        width:      width,
        height:     height
    });
}
