XD.loadModule('main', function (that) {

	/**
	 * Private area here
	 */


	/**
	 * Public methods
	 */
	return {

		/**
		 * This one will be called first
		 */
		init: function () {

			/**
			* jAlert global Configurations
			*/
			$.alerts.okButton = that.getGlobal('dialogMessages').ok;
			$.alerts.cancelButton = that.getGlobal('dialogMessages').cancel;

			/**
			* Create tabs pannels
			*/
			$('.tabs').tabs({
				cookie: $('#dashboard-updates-list').length > 0 ? false : {expires: 30},
                                cache: true,
				show: function(event, ui) {
                                    setTimeout(function () {$(ui.tab).find('.new').fadeOut(200, function(){$(this).remove()});}, 1500);
                                    if($(ui.tab).attr('data-update'))
                                    {
                                            $.get($(ui.tab).attr('data-update'));
                                    }
				}
			});

			/**
			* Set up feedback box
			*/
			if ($('#feedback-badge').length) {
				$('#feedback-badge').feedbackBadge({
					css3Safe: $.browser.safari ? true : false,
					onClick: function () {
						var self = $(this);
						$('<div id="feedback-box">' + that.getAjaxLoading() + '</div>').dialog(
							that.getDialogOptions({
								dialogClass: 'popup-feedback-box',
								open: function (ev, ui) {
									$('#feedback-box').load(self.attr('href'), function(){
										$('.popup-feedback-box').removeClass('ajax-loading');
										$('#feedbackForm').ajaxForm({
											dataType: 'json',
											beforeSend: function () {
												that.doAjaxFormBeforeSend({formId:'#feedbackForm'});
											},
											success: function (data) {
												that.doAjaxFormSuccess({formId:'#feedbackForm',
													data:data,
													errorsPrefix:'FeedbackForm_',
													inputPrefix:'FeedbackForm[',
													inputPostfix:']',
													statusSuccess: function (response) {
														$('#feedback-box').dialog('destroy');
														$('#feedback-box').remove();
													},
													statusEmpty: function (response) {
														$('#feedback-box').dialog('destroy');
														$('#feedback-box').remove();
													}
												});
											}
										});
										$('.cancel-window').click(function(ev){
											$('#feedback-box').dialog('destroy').remove();
										});
										that.focus({cnt:'#feedback-box'});
									});
								},
								close: function(){
									$('#feedback-box').dialog('destroy').remove();
								}
							}
						));
						return false;
					}
				});
			}

			/**
			* Handle Terms & COnditions url click on register box
			*/
			$('#tos-link').live('click',
				function () {
					that.popupWindow({url:$(this).attr('href'),
									 title:$(this).attr('title'),
									 width:1024,
									 height:550,
									 top:100,
									 left:100
									});
					return false;
				}
			);

			/**
			 * Need a register box when click on register button
			 * Also show the register box by default to guests on landing (Signup Step 1)
			 */
			if (that.checkUserParam('profileViewWithoutPopup', false)) {
				that.enableRegisterBox({
					autoShow: true,
					canClose: false,
					dialogClass: 'popup-register-login landing-search-login',
					width:450
				});
			} else {
				that.enableRegisterBox();
				$('#sign-in-button').click(function () {
					that.enableRegisterBox({tabSelected:1});
					$('#sign-up-button').trigger('click');
					return false;
				});
			}

			/**
			 * Call the action for logging a PM visit. We do it like this to filter out bogus visits by crawlers and
			 * avoid problems if PM is not available. Include the current URL as the HTTP_REFERER is blocked in a lot of cases.
			 */
			// Temp disable the calls for registered users until we get the script to run that will add visits to older users
			if (!that.isRegisteredUser() || typeof that.getGlobal('userInfo')['pmId'] == 'undefined' || that.getGlobal('userInfo')['pmId'].length == 0)
			{
				$.cookie('test_cookies', 'test');
				if (that.getGlobal('pmIsRootTraffic') == 1 && $.cookie('test_cookies') != null)
				{
					$.get(that.getGlobal('logPMVisitUrl'));
				}
				$.cookie('test_cookies', null)

				var pmData = that.getGlobal('pmData');
				if (pmData && pmData.length != 0)
				{
					var GAParams = that.getGlobal('GAParams');
					if (GAParams) {
						GAParams['_setCampContentKey'] = pmData['campaignId'] ? pmData['campaignId'] : GAParams['_setCampContentKey'];
						GAParams['_setCampSourceKey'] = pmData['affiliateId'] ? pmData['affiliateId'] : GAParams['_setCampSourceKey'];
						GAParams['_setCampTermKey'] = pmData['keyword'] ? pmData['keyword'] : GAParams['_setCampTermKey'];
						var actualParams = that.getGlobal('GAParams');
						if (GAParams['_setCampContentKey'] != actualParams['_setCampContentKey'] ||
							GAParams['_setCampSourceKey'] != actualParams['_setCampSourceKey'] ||
							GAParams['_setCampTermKey'] != actualParams['_setCampTermKey'])
						{
							that.setGlobal('GAParams', GAParams);
							that.trackGAURL();
						}
					}
				}
			}

			/**
			 * Show photo upload notice for new users
			 * first time - show it slow
			 * after that - show fast
			 */
			if (that.getGlobal('showUploadNotice') && !that.getGlobal('showSafeModeNotice')) {
				$.doomNotice({
					message:that.getGlobal('uploadNoticeMessage'),
					animationDuration: that.getGlobal('showUploadNotice') < 2 ? 800 : 0,
					delayBeforeShow: that.getGlobal('showUploadNotice') < 2 ? 1000 : 0,
					beforeClose: function () {$.get(that.getGlobal('removeUploadNoticeUrl'))}
				});
			}

			/**
			 * Safe mode switch from header event handler
			 */
			$('#header-safe-mode-switch-cnt').delegate('#safe-mode-switch,.text-on,.text-off', 'click', function (ev) {
				if ($(ev.target).hasClass('text-on') && !$(ev.target).parent().hasClass('mode-off') ||
					$(ev.target).hasClass('text-off') && $(ev.target).parent().hasClass('mode-off')) {
					return false; //disable OFF text click when safe mode OFF and ON text click when safe mode on
				}
				var $editSafeMode = $('#safeMode'); //click the edit profile switcher if present to prevent double requests
				($editSafeMode.length > 0 && $editSafeMode.click()) || that.setSafeMode();
			});

            /**
             * Handle cams link for non-logged in users
             */
            if (!that.isRegisteredUser()) {
                that.indexPageTriggers();
            }

			/**
			* General Events when window resize
			* - reposition UIDialogs on resize
			*/
			if (that.getGlobal('isMobile') === 0) {
				$(window).resize(
					function () {
						$('.ui-dialog-content').each(
							function (index, dialog) {
								if ($(dialog).dialog('isOpen')) {
									$(dialog).dialog({position:'center'});
								}
							}
						);
					}
				);
			}

			return this;
		}
	};
});
