﻿window.contentJumpOverlay = {
	_initialize: function() {
		this.currentEl = null;
		this.messageEl = null;
		this.confirmJump = true;
		this.urlJump = '';
		this.idExclusionUpdatePanel = '';
		this.referrerContextInfo = null;

		Event.observe(window, 'load', this._onLoad.bind(this));
	},

	_onLoad: function() {
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(this._updatePanelLoadedHandler.bind(this));
	},

	initParams: function(referrerContextInfo) {
		this.referrerContextInfo = referrerContextInfo;
	},

	initLinks: function(areaId) {
		if (this.disableLinkBind) return;
		var t = this;
		areaSelector = areaId ? '#' + areaId + ' A' : 'A';

		var res = $$(areaSelector).select(function(elem) { return (/ContentJumpPage/i).test(elem.href); }).each(function(el) {
			//var res = $$(areaSelector).select(function(elem){ return (/http/i).test(elem.href); }).each(function(el){
			Event.observe(el, 'click', t._linkClickHeader.bindAsEventListener(t, el));
			if (el.onclick) {
				el.removeAttribute('onclick');
				el.onclick = null;
			}
		});
	},

	_linkClickHeader: function(e, el) {
		if (this.confirmJump) {
			e.stop();
			this.currentEl = el;
			this.dataBind();
		}
	},

	dataBind: function() {
		if (!this.currentEl) return;
		var url = this._urlParser(this.currentEl.href);
		Flux.Site.WebService.ContentJumpService.GetContentJumpData(url.contentUcid, this.referrerContextInfo, this._dataBindHandler.bind(this));
	},

	_dataBindHandler: function(result) {
		if (result.StatusCode == Flux.Site.WebService.StatusCode.OK) {
			this._parseResult(result);
		} else {
			Flux.Debug.traceServiceError('ContentJump', result);
		}
	},

	_parseResult: function(result) {
		this.urlJump = result.ContentLink;
		this._showMessage(result);
		$('contentJumpOverlayTextTitle').update(result.ContentTitle);
		$('contentJumpOverlayTextOwner').update(result.SiteOwnerName);
		$('contentJumpOverlayImage').src = result.ContentThumbnailUrl;
	},

	_showMessage: function(result) {
		var t = this;
		if (!this.messageEl) {
			this.messageEl = $('contentJumpOverlayArea');
			//Event.observe(this.messageEl, 'mouseout', this._messageMouseoutHandler.bindAsEventListener(this));
		}
		Position.clone(this.currentEl, this.messageEl, { 'setWidth': false, 'setHeight': false });
		this.messageEl.show();
	},

	_urlParser: function(url) {
		var rg = new RegExp('\/0([0-9,A-F]{28})\/', 'i');
		var results = rg.exec(url);
		return {
			communityId: this._getUrlParam(url, 'source_community'),
			userId: this._getUrlParam(url, 'source_community'),
			contentUcid: (results && results.length > 1) ? results[1] : ''
		}
	},

	_getUrlParam: function(url, name) {
		var name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(url);
		return results ? results[1] : "";
	},

	_updatePanelLoadedHandler: function(sender, args) {
		var updatedPanels = args.get_panelsUpdated();
		for (i = 0; i < updatedPanels.length; i++) {
			if (this.idExclusionUpdatePanel && updatedPanels[i].id.indexOf(this.idExclusionUpdatePanel) != -1) {
				this.idExclusionUpdatePanel = '';
				continue;
			}
			this.initLinks(updatedPanels[i].id);
		}
	},

	_okButtonClickHandler: function() {
		var jumpConfirm = $('contentJumpOverlayConfirmJump');
		if (jumpConfirm && jumpConfirm.checked) this._disableConfirmJumpState();
		this._linkJump();
	},

	_linkJump: function() {
		var el = this.currentEl;
		var redirectUrl = this.urlJump;
		if (!el.target || el.target == '_top' || el.target == '_self') {
			window.setTimeout(function() {
				window.location.href = redirectUrl;
			}, 1);
		} else {
			window.open(redirectUrl, el.target);
		}
		this.messageEl.hide();
	},

	_cancelButtonClickHandler: function() {
		this.messageEl.hide();
	},

	_disableConfirmJumpState: function() {
		var url = this._urlParser(this.currentEl.href);

		Flux.Site.WebService.ContentJumpService.SaveIsDisplayCrossCommunityPage(
			url.contentUcid, false, this._disableConfirmJumpStateHandler.bind(this)
		);
	},

	_disableConfirmJumpStateHandler: function(result) {
		this.confirmJump = false;
		if (result.StatusCode != Flux.Site.WebService.StatusCode.OK) {
			Flux.Debug.traceServiceError('ContentJump', result);
		}
	},

	setExclusionUpdatePanel: function(id) {
		this.idExclusionUpdatePanel = id;
	}
}

window.contentJumpOverlay._initialize();


// [AtlasScript]

if(typeof(Sys) != 'undefined')
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]
