$(document).ready(function() {
	_initializeCpButtons();
	_intlDropdown();
});

function _initializeCpButtons() {
	_initializeCpCenter();
	_initializeCpButtonCorners();
	_initializeCpButtonAlignment();
	_initializeCpButtonTrademark();
}

function _initializeCpCenter() {
	var containerWidth = 0;
	$('.cpCenter').each(function() {
		var container = this;
		while (containerWidth == 0) {
			containerWidth = $(container).width();
			container = $(container).parent();
			if (! container) {
				return;
			}
		}
	});
	
	if (containerWidth == 0) {
		return;
	}
	
	$('.cpCenter > .cpCenterItem').each(function() {
		var width = $(this).width();
		var left = parseInt((containerWidth - width) / 2);
		$(this).wrapInner('<div style="margin-left: ' + left + 'px"></div>');
		$(this).after('<div class="cpCenterItemClear"></div>')
	});
}

/*
Usage:
<a id="myButton" class="cpButton">Foo</a>
<script>
	document.getElementById('myButton').update({text: 'New Text'});
</script>

config is an associative array with the following keys:
{ text: 'new button text' }

*/
function _updateCpButton(config) {
	if (! config) return;
	
	if (config.text) {
		$('.cpButtonText', $('#cpButton-' + this.id)).html(config.text.toString());
	}
}

function _initializeCpButtonCorners() {
	var i = 0;
	$('.cpButton').each(function() {
		var fn = function(buttonJq, index) {
			if (buttonJq.hasClass('cpButtonDisableJs') !== true) {
				buttonJq.hide();
				buttonJq.get(0).update = _updateCpButton;

				var secondaryClassName = buttonJq.attr('className').replace(/\bcpButton\b/, '');
				var text = buttonJq.attr('value') || buttonJq.html();
				var cpButtonId = buttonJq.attr('id');
				if (! cpButtonId) {
					cpButtonId = index.toString();
					buttonJq.attr('id', cpButtonId);
				}
				var content = [
					'<div id="cpButton-', cpButtonId ,'" class="cpButtonJs">',
						'<div class="', secondaryClassName, '">',
							'<div class="cpButtonRow cpButtonRowA"><span class="cpButtonText">', text, '</span></div>',
							'<div class="cpButtonRow cpButtonRowB"><span class="cpButtonText">', text, '</span></div>',
							'<div class="cpButtonRow cpButtonRowC"><span class="cpButtonText">', text, '</span></div>',
							'<a href="#" id="cpButtonAnchor-', cpButtonId,'" class="cpButtonAnchor cpButtonText">', text, '</a>',
							'<div class="cpButtonRow cpButtonRowX"><span class="cpButtonText">', text, '</span></div>',
							'<div class="cpButtonRow cpButtonRowY"><span class="cpButtonText">', text, '</span></div>',
							'<div class="cpButtonRow cpButtonRowZ"><span class="cpButtonText">', text, '</span></div>',
							'<div class="cpButtonRowOmega"></div>',
						'</div>',
					'</div>'
				].join('');
				
				buttonJq.after(content);
				
				var anchorJq = $('#cpButton-' + cpButtonId + ' a.cpButtonAnchor');
				var href = buttonJq.attr('href');
				if (href) {
					anchorJq.attr('href', href);
				}
				// click the hidden button
				anchorJq.click(function(e) {
					if (document.createEvent) {
						var evt = document.createEvent("MouseEvents");
						evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
						var bHref = buttonJq.attr('href');
						if (buttonJq.get(0).dispatchEvent(evt) && bHref) {
							window.location.href = bHref;
						}
					}
					else {
						buttonJq.get(0).click();
					}
					return false;
				});
			}
		}($(this), i);
		i++;
		
		return fn;
	});
}

function _initializeCpButtonTrademark() {
	var content = '<div class="cpButtonTrademarkJs">&nbsp;&#8482;</div>';
	$('.cpButtonJs .cpButtonTrademark .cpButtonText').append(content);
}

function _initializeCpButtonAlignment() {
	_initializeCpButtonAlignmentCenter();
}

function _initializeCpButtonAlignmentCenter() {
	$('.cpButtonContainerCenter').each(function() {
		var containerWidth = 0;
		var container = this;
		while (containerWidth == 0) {
			containerWidth = $(container).width();
			container = $(container).parent();
			if (! container) {
				return;
			}
		}
		
		var firstChild;
		var childWidth = 0;
		$(this).children().each(function() {
			if (! firstChild) {
				firstChild = this;
			}
			
			if (! $(this).hasClass('clear') && $(this).css('display') !== 'none') {
				childWidth += $(this).outerWidth();
			}
		});
		
		if (firstChild) {
			var left = parseInt((containerWidth - childWidth) / 2);
			$(this).wrapInner('<div style="margin-left: ' + left + 'px"></div>');
		}
	});
}

/* International Global Header Flyout */
function _intlDropdown() {
	var t;
	$("#international").hover(
		function () { t = setTimeout(_showIntlDropdown,400); }, 
		function () { clearTimeout(t); _hideIntlDropdown() }
	);
}

function _showIntlDropdown()
{
	$("#international").children("a").css('color','#0191c7');
	$("#international").children("a").css('padding-top','5px');
	$("#international").addClass("intlmenubg");
	$("#ausFlag").css('top','6px');
	$("#canFlag").css('top','6px');
	$("#ukFlag").css('top','6px');
	$("#usFlag").css('top','6px');
	$(".intlArrowRight").hide();
	$(".intlArrowDown").show();
	$("#intldropdown").fadeIn("fast");
}

function _hideIntlDropdown()
{
	$("#international").children("a").css('color','#4c4c4c');
	$("#international").children("a").css('padding-top','6px');
	$("#international").removeClass("intlmenubg");
	$("#ausFlag").css('top','7px');
	$("#canFlag").css('top','7px');
	$("#ukFlag").css('top','7px');
	$("#usFlag").css('top','7px');
	$(".intlArrowDown").hide();
	$(".intlArrowRight").show();
	$("#intldropdown").fadeOut("fast");  
}

