var $j = jQuery.noConflict();
var STFDiv;
$j(document).ready(function() {

	if (joCorpBrandFooter && typeof (joCorpBrandFooter) == 'function') {
		var joBrandFooter = new joCorpBrandFooter({})
		joBrandFooter.write('JOFooter');
	}

	$j('.Popup').click(function(event) {
		event.preventDefault();
		window.open(this.href);
	});

	$j('.Content800').before('<div class="HeaderContent800"/>');
	$j('.Content800').after('<div class="FooterContent800"/>');

	$j('#AM-Item2').click(function(event) {

		// Cancel the default event.
		event.preventDefault();

		/*	XHR to get the text of the buttons and messages.  On the async callback, 
		setup the dialog with the text. */
		XHRURL = this.href + '/shared/cs/DynamicMessenger.aspx';

		$j.post(
			XHRURL,
			{ cmd: "SubscribeTerms" },
			function(Data) {

				// Build the dialog element and load it with the form using XHR
				NSFDiv =
				$j('<form id="NewsletterSignupForm"><input type="hidden" name="protocol" value="yes"/><input type="hidden" name="Brand" value="Subgear"/><input type="hidden" name="SourceType" value="Newsletter Signup"/><p><label>' +
				Data.EmailAddress +
				'</label><input type="text" name="EmailAddress" id="NewsletterEmail" /></p>' +
				'<p><label>' +
				Data.FirstName + '</label><input type="text" name="FirstName" id="NewsletterFirstName" /></p>' +
				'<p><label>' + Data.LastName +
				'</label><input type="text" name="LastName" id="NewsletterLastName" /></p>' +
				'<p><label>' + Data.OptIn + '</label><input type="checkbox" name="Optin" id="OptIn" value="Y"></p>' +
				'<p><a href="http://www.johnsonoutdoors.com/corporate/contact/PrivacyPolicy.aspx">' + Data.Privacy + '</a></p></form>');

				$j(document.body).append(NSFDiv);

				// Get the button text.  Since they are not literals, use array notation.
				var BTNs = {}
				BTNs[Data.OKButton] = function() { SubmitNewletterForm(XHRURL) };
				BTNs[Data.CancelButton] = function() { $j(this).dialog('close') };

				// Setup the dialog
				NSFDiv.dialog({
					buttons: BTNs,
					modal: true,
					title: Data.NewsletterSignup,
					width: 400
				});
			},
			'json'
		);

	});

	$j('.SendToFriend').click(function(event) {

		event.preventDefault();

		$j.post(
			'/shared/cs/send_friend_xhr.aspx',
			{ cmd: 'Translations' },
			function(Data) {


				var BTNs = {}
				BTNs[Data.Send] = function() { $j('#SendToFriendForm').submit() };
				BTNs[Data.Cancel] = function() { $j(this).dialog('close') };


				STFDiv = $j('<form id="SendToFriendForm"><p><label>' + Data.To + '</label><input type="text" name="To" id="STFTo" /></p>' +
								'<p><label>' + Data.Message + '</label><textarea name="Message" id="STFMessage"></textarea></p>' +
								'<p><label>' + Data.Signed + '</label><input type="text" name="Signed" id="STFSigned" /></p>' +
								'<p><label>' + Data.Email + '</label><input type="text" name="Email" id="STFEmail" />' +
								'<input type="hidden" name="URL" id="STFURL" value="' + document.baseURI + '" /></p></form>').dialog({
									buttons: BTNs,
									modal: true,
									title: Data.Title,
									width: 400
								});

				$j('#SendToFriendForm').submit(function(event) {
					event.preventDefault();
					SubmitSendToFriend(this);
				});

			},
			'json'
		);


	});

	$j('.PrintPage').click(function(event) {
		event.preventDefault();
		window.print();
	});

	$j('#RegionLink').click(function(event) {
		$j.cookie('Homepage', null, { expires: 90, path: '/' });
	});

});


function SubmitNewletterForm(SubmitURL) {
	$j.post(
		SubmitURL,
		$j('#NewsletterSignupForm').serialize() + '&cmd=Subscribe',
		function(Data) {
			if (Data.Success) {
				$j('#NewsletterSignupForm').dialog('close');
				ShowMessage('Success', Data.Message);
			} else {
				ShowMessage('Error', Data.Error);
			}
		},
		'json'
	);

}

	
function ShowMessage(Title, Message) {

	MessageBox = $j('<div>' + Message + '</div>');
	$j(document.body).append(MessageBox);
	MessageBox.dialog({
		title: Title,
		buttons: {
			Close: function() { $j(this).dialog('close')}
		},
		modal: true
	});

}

function SubmitSendToFriend(form){
	
	// Reset the background colors
		$j('#SendToFriendForm input[type="text"], #SendToFriendForm textarea').css('background-color', 'white');
	
	$j.post(
			'/shared/cs/send_friend_xhr.aspx',
			$j(form).serialize() + '&cmd=SubmitSTF',
			function(Data){
				
				Buttons = {};
				
				if (Data.Success) {
					STFDiv.dialog('close');
					Buttons[Data.OKButton] = function(){$j(this).dialog('close')};
								
					$j('<div>' + Data.Message + '</data>').dialog({
						title: Data.Title,
						modal: true,
						buttons: Buttons
					});
				}else{
					
					Buttons[Data.OKButton] = function(){
						$j(this).dialog('close');
						$j('#SendToFriendForm #' + Data.Error).css('background-color', '#E9F3FD').select();
					};
					
					$j('<div>' + $j('#SendToFriendForm #' + Data.Error).prev().text().replace(':','') + ' ' + Data.IsRequired + '</data>').dialog({
						modal:true,
						buttons: Buttons,
						title: 'Error'
					});
					
				}
				
				
				
				
				
			},
			'json'
	);
}



function showFlashPopup(fileName){
	
	var flashDiv = $j('<div>', {id:'Flash_' + fileName, className: 'FlashPopup'})
	.append($j('<div>',{className: 'innerFlash', id: 'Flash_' + fileName + '_inside'}))
	.append($j('<a>', {href: '#', className: 'FlashClose'}).click(function(event) { event.preventDefault(); flashDiv.remove(); }).text('X'))
	.appendTo($j('#SiteContent'));
	swfobject.embedSWF('/shared/flash/' + fileName, 'Flash_' + fileName + '_inside', '100%', '100%', '9.0.0', null, null, null, null);

}

