/* For debugging: Remove when live */
/*$.getScript('/javascript/jquery/jquery.dump.js');*/

$(
	function() {
		var drop_shadow_options = {left: 1, top: 1, blur: 1, opacity: .3};

		/* Main menu - Drop shadow and rollover */
		default_z_index = {};
		$('#main_menu>li').each( function() { eval('default_z_index.' + $(this).attr('id') + ' = ' + $(this).css('zIndex')); } );

		function reset_main_menu() {
			$('#main_menu>li').each(
				function() {
					$('>a', this).removeClass('hover_active');
					$('>.sub_menu', this).hide();
					$(this).css('zIndex', default_z_index[$(this).attr('id')]);

					/* Remove drop shadow */
					$('.menu_tab, .sub_menu', this).removeShadow();
				}
			);
		}

		/* Main menu - Adjust sub-menu height of Companies tab */
		if ($('#main_menu').length > 0) {
			sub_menu_item_height = Number($('#main_menu .sub_menu a').css('height').replace(/[^\d]+/, '')) + Number($('#main_menu .sub_menu a').css('paddingTop').replace(/[^\d]+/, '')) + Number($('#main_menu .sub_menu a').css('paddingBottom').replace(/[^\d]+/, ''));
			company_items_per_column = $('#companies_tab .column_1').length;
			$('#companies_tab .sub_menu').css('height', company_items_per_column * sub_menu_item_height);
			$('#companies_tab .new_column').css('marginTop', company_items_per_column * sub_menu_item_height * -1);

			/* Expand height of last item to fill the rest of the column */
			last_item = $($('#companies_tab li')[$('#companies_tab li').length - 1]);
			remaining_blocks = company_items_per_column - $('#companies_tab .' + $(last_item).attr('class')).length;
			last_item.css('height', (remaining_blocks + 1) * sub_menu_item_height);
		}

		/* Launch mini-sites in external window */
		$('#companies_tab .vendor_level_3').attr('target', '_blank');

		/* Reset menu hack for Firefox */
		$('form').mouseenter( function() { reset_main_menu(); } );

		$('#main_menu>li').hover(
			function() {
				$('>a', this).addClass('hover_active');
				$('>.sub_menu', this).show();
				$(this).css('zIndex', 6);

				/* Drop shadow */
				$('.menu_tab, .sub_menu', this).dropShadow(drop_shadow_options);
				$('#' + $('.menu_tab', this).shadowId()).css('zIndex', 0);
				$('#' + $('.sub_menu', this).shadowId()).css('zIndex', 0);
				$('.sub_menu', this).css('zIndex', 1);
				$('.menu_tab', this).css('zIndex', 2);
			},
			function() { reset_main_menu(); }
		);

		/* Category selector - Drop shadow and toggle */
		category_selector_height = $('#category_selector').height();
		$('#current_category').addClass('off').toggle(
			function() {
				$(this).addClass('on');
				$('#category_selector').queue(
					function () {
						$(this).css('height', 'auto').fadeIn(0);
						$(this).dropShadow(drop_shadow_options);
						$(this).dequeue();
					}
				);
			 },
			function() {
				$(this).removeClass('on');
				$('#category_selector').css('height', category_selector_height + 'px').removeShadow();
			}
		);

		/* Log in */
		/* Now located in its own CSS file for company sites to use
		$('#login_form').submit(
			function() {
				var is_login_successful = false;
				var error_messages = {};

				$.ajax({
					async: false,
					url: '_system/controllers/user.cfc',
					data: { method: 'log_in', returnFormat: 'json', email_address: $('#login_email_address', this).val(), password: $('#login_password', this).val() },
					success: function(data) { is_login_successful = data; },
					dataType: 'json'
				});

				if (is_login_successful === true) {
					window.location.reload();
				} else {
					if ($('.error_messages', this).length == 0) $(this).prepend($('<ul />').addClass('error_messages'));
					error_messages = $('.error_messages', this).empty();
					error_messages.append($('<li />').html('Could not find this profile, please try again'));
					$('#login_password', this).val('');
				}
				return false;
			}
		);
		$('#login_form').addClass('jqmWindow').jqm({ trigger: '#login_form_link' });
		*/

		/* CRS Point of entry */
		$('#feed_verify_profile_link').click( function() { $('#feed_verify_profile').show(); return false; } );
		if ($('#feed_verify_profile_form .error_messages li').length > 0) $('#feed_verify_profile').show();
		$('#feed_verify_profile_form input, #feed_merge_profile_form input').keydown( function(event) { if (event.keyCode == 13) return false; } );
		$('#feed_merge_profile .form_control input[type=submit], #merge_submit input[type=submit]').click(
			function() {
				var fieldset_id = $(this).parents('fieldset').attr('id');
				switch(fieldset_id) {
					case 'feed_verify_user':
						//user_profile = { first_initial: $('#first_initial').val(), last_name: $('#last_name').val(), employee_id: $('#employee_id').val(), company_id: $('#company_id').val(), system_type: 'feed' }
					user_profile = { first_initial: $('#first_initial').val(), last_name: $('#last_name').val(), employee_id: $('#employee_id').val(), company_id: 29013, system_type: 'feed' }
						break;
					case 'merge_current_users':
						user_profile = { email_address: $('#merge_login_email_address').val(), password: $('#merge_login_password').val() }
						break;
					case 'merge_past_users':
						user_profile = { username: $('#merge_legacy_username').val(), system_type: 'legacy' }
						break;
				}
				$.ajax({
					async: false,
					url: '_system/controllers/user.cfc',
					data: $.extend({ method: 'is_existing_user', returnFormat: 'json' }, user_profile),
					success: function(data) { is_existing_user = data; },
					dataType: 'json'
				});

				if (is_existing_user === true) {
					if (fieldset_id == 'feed_verify_user') {
						$('#feed_merge_profile').jqmShow();
					} else {
						switch(fieldset_id) {
							case 'merge_current_users': $('#merge_type').val('current').removeAttr('disabled'); break;
							case 'merge_past_users': $('#merge_type').val('legacy').removeAttr('disabled'); break;
						}
						$('#feed_merge_profile').jqmHide();
						$('#feed_verify_profile_form').append($('.form_field input', $(this).parents('fieldset')).hide()).submit();
					}
				} else {
					this_section = $(this).parents('fieldset');
					if ($('.error_messages', this_section).length == 0) $(this_section).prepend($('<ul />').addClass('error_messages'));
					error_messages = $('.error_messages', this_section).empty();
					error_messages.append($('<li />').html('Cannot locate this account.'));
					$('#merge_login_password', this_section).val('');
				}
				return false;
			}
		);






		/* Modal */

		$('#feed_merge_profile').addClass('jqmWindow').jqm();

		/* Training module modal */
		/*
		training_module_modal = $('<div />').attr('id', 'training_module_modal').addClass('jqmWindow').appendTo('body');
		$('<iframe />').appendTo(training_module_modal);
		$('#training_module_modal').jqm({
			trigger: 'a.training_module_link',
			onShow: loadInIframeModal,
			toTop: true
		});
		*/

		$('body').append($('.jqmWindow'));

		/* Generic tooltip */
		$('.tooltip').bt({
			//contentSelector: "$(this).attr('title')",
			fill: 'white',
			cssStyles: { fontWeight: 'bold'},
			shrinkToFit: true,
			padding: 8,
			cornerRadius: 8,
			spikeLength: 15,
			spikeGirth: 5,
			positions: ['right']
		});
		
		/* Generic tooltip */
		$('.tooltip_left').bt({
			//contentSelector: "$(this).attr('title')",
			fill: 'white',
			cssStyles: { fontWeight: 'bold'},
			shrinkToFit: true,
			padding: 8,
			cornerRadius: 8,
			spikeLength: 15,
			spikeGirth: 5,
			positions: ['left'],
			width: 600
		});
	}
);

var loadInIframeModal = function(hash){
	var $trigger = $(hash.t);
	var $modal = $(hash.w);
	var myUrl = $trigger.attr('href');
	var $modalContent = $("iframe", $modal);

	$modalContent.html('').attr('src', myUrl);
	$modal.show();
}

//function launch_module(content_id,url){
//	ColdFusion.Window.create('training_module'+content_id, 'CyberScholar', url, {x:100,y:100,height:500,width:720,modal:true,closable:true, draggable:true,resizable:true,center:true,initshow:true,minheight:200,minwidth:200 });
//}
