var AddToMyProfile = function(clientId) {

	this.id = clientId;
	this.isUser = false;

	this._initialize = function(bUserIsAuth) {
		this.isUser = bUserIsAuth;
	};

	this.highliteButton = function(bOn) {
		var btnsholder = $get(this.id + "_buttons_holder");
		if (!btnsholder)
			return;

		btnsholder.parentNode.className = ((bOn) ? "holder_hover" : "holder");
	};

	this.AddGContentToFeed = function(contentUcid, useCallBack) {
		var callBackFn = ((useCallBack) ? SucceededCallback.bind(this) : function() { });
		Flux.Site.WebService.ContentActionService.AddContentToUserFeed('', contentUcid, callBackFn);
	};

	function SucceededCallback(result, eventArgs) {
		if (!result) return;
		if (!result.StatusCode) return;
		if (result.StatusCode == Flux.Site.WebService.StatusCode.SystemError) {
			alert(result.ErrorDescription);
			return;
		}
		if (result.StatusCode != Flux.Site.WebService.StatusCode.OK) return;

		var okConfirm = $get(this.id + "_addConfirm");
		var defaultBtn = $get(this.id + "_addButton");
		if (!defaultBtn || !okConfirm) {
			return;
		}
		var w = defaultBtn.clientWidth;
		defaultBtn.style.display = "none";
		okConfirm.style.display = "";
		okConfirm.className += " favorites_grayed";
		if (w <= 0)
			return;
		okConfirm.style.width = w + "px";
		if (okConfirm.clientWidth != w)
			w = (2 * w) - okConfirm.clientWidth;
		if (w < 0)
			return;
		okConfirm.style.width = w + "px";
	};

	(function() {
		AddToMyProfile.inst[this.id] = this;
		this.preinited = true;
	}).call(this);

}

AddToMyProfile.inst = {};



////////////////////////////  ContentActionControl

var VoteControl = function(clientId, fbPostWidget) {
	this.id = clientId;
	this.updatedCounter = false;
	
	this._disableVote = function() {
		$(this.id+"_spanDisabled").className = 'disabled';
		$(this.id+"_VoiceUp").onclick = null;
	};
	
	this._enableVote = function() {
		$(this.id+"_spanDisabled").className = '';
		alert('was error. enable control');
	};
	
	this._updateCount = function() {
		if (!this.updatedCounter) {
			var countHolder = $(this.id+"_ratingView");
			countHolder.innerHTML = (parseInt(countHolder.innerHTML) +1).toString();
			this.updatedCounter = true;
		}
	};
	
	this.leaveVote = function(contentUcid) { // castVote
		if ($(this.id+"_spanDisabled").className != 'disabled') {
			this._disableVote();
			Flux.Site.WebService.ContentActionService.LeaveVote(contentUcid, this._updateCount.bind(this), this._enableVote.bind(this));
			if (typeof fbPostWidget != undefined)
				fbPostWidget.sendVote(contentUcid, 1, 2 /*RateContentThumbUp*/);
		}
	};
	
	(function() {
		VoteControl.inst[this.id]	= this;
		this.preinited	= true;
	}).call(this);	
};

VoteControl.inst	= {};

///////////////////////////////////////
// Flagging

var Flagging = function(clientId) {
	this.id = clientId;
	this.editmode = false;
	this.dd = null;
	this.isUser = false;

	this._initialize = function(bUserIsAuth) {
		var btnsholder = $get(this.id + "_ddholder");
		var button = $get(this.id + "_ddbutton");
		var popup = $get(this.id + "_dd");
		var mate = $get(this.id + "_ddmate");
		this.isUser = bUserIsAuth;

		if (!btnsholder || !button || !popup || !mate)
			return;

		//$addHandler(btnsholder.parentNode, 'mouseover', highliteWidget.bind(this, true));
		//$addHandler(btnsholder.parentNode, 'mouseout', highliteWidget.bind(this, false));

		if (bUserIsAuth) {
			this.dd = new WidgetDropDown(this.id);
			this.dd.parentObject = this.id + "_ddholder";
			this.dd.onShow = this.onDDShow.bind(this);
			this.dd.cancelEventsSet = true;
			this.dd.init();
		}
	};

	this.keepAlive = function() {
		if (this.dd)
			this.dd.keepAlive();
	}

	this.hidePopup = function() {
		if (this.dd)
			this.dd.hidePopup();
	}

	this.onDDShow = function(bView) {
		var button = $get(this.id + "_ddbutton");
		var popup = $get(this.id + "_dd");
		var mate = $get(this.id + "_ddmate");
		if (!button || !popup || !mate)
			return;

		button.className = bView ? "ddArrow_active" : "ddArrow";
		var btnsholder = $get(this.id + "_buttons_holder");
		popup.style.width = mate.style.left = "63px";
		popup.style.height = "";
		Position.clone(popup, mate);
	};


	this.showFlags = function(e) {
		if (this.dd)
			this.dd.showPopup(true, e);

		return !this.isUser;
	};

	this.highliteWidget = function(bOn) {
		var btnsholder = $get(this.id + "_buttons_holder");
		if (!btnsholder)
			return;

		bOn = this.editmode ? false : bOn;

		btnsholder.parentNode.className = bOn ? "holder_hover" : "holder";
	};

	this.Flag4Content = function(contentUcid, flag, useCallBack) {
		var callBackFn = useCallBack ? SucceededCallback.bind(this) : function() { };
		Flux.Site.WebService.ContentActionService.Flag4Content(contentUcid, flag, callBackFn);
	};

	this.Flag4User = function(userUcid, flag, useCallBack) {
		var callBackFn = useCallBack ? SucceededCallback.bind(this) : function() { };
		Flux.Site.WebService.ContentActionService.Flag4User(userUcid, flag, callBackFn);
	};

	this.Flag4Message = function(umid, flag, useCallBack) {
		var callBackFn = useCallBack ? SucceededCallback.bind(this) : function() { };
		Flux.Site.WebService.ShareMessageService.Flag4Message(umid, flag, callBackFn);
	};

	this.Flag4Comment = function(contentUcid, commentId, flag, useCallBack) {
		var callBackFn = useCallBack ? SucceededCallback.bind(this) : function() { };
		Flux.Site.WebService.ContentActionService.Flag4Comment(contentUcid, commentId, flag, callBackFn);
	};

	this.Flag4Activity = function(activityId, flag, useCallBack) {
		var callBackFn = useCallBack ? SucceededCallback.bind(this) : function() { };
		Flux.Site.WebService.ContentActionService.Flag4Activity(activityId, flag, callBackFn);
	};

	this.ShowCopyrightWindow = function(button, copyRightControl) {
		var copyCtrl = $(copyRightControl);
		var root = $(document.body.parentNode);
		var bodySize = { width: Math.max(root.clientWidth, root.offsetWidth), height: Math.max(root.clientHeight, root.offsetHeight) };
		var btn = $(button);
		var ctrlPos = { left: 0, top: 0 };

		$(copyCtrl.parentNode).show();
		copyCtrl.show();

		copyCtrl.parentNode.style.position = "relative";

		var ctrlSize = { width: Math.max(copyCtrl.clientWidth, copyCtrl.offsetWidth), height: Math.max(copyCtrl.clientHeight, copyCtrl.offsetHeight) };
		
		var parentNode = $(button);
		var left = 0;
		while (parentNode && parentNode.nodeName.toLowerCase() != "body" && parentNode.nodeName.toLowerCase() != 'html') {
			left += Math.max(parentNode.offsetLeft, parentNode.clientLeft);
			parentNode = parentNode.offsetParent;
		}

		copyCtrl.setStyle(ctrlPos);

		if (bodySize.width < (left + ctrlSize.width))
			ctrlPos.left += bodySize.width - (left + ctrlSize.width + 50);

		ctrlPos.left += "px";
		ctrlPos.top += "px";
		copyCtrl.setStyle(ctrlPos);
	};


	function SucceededCallback(result, eventArgs) {

		if (!result) {
			return;
		}
		if (!result.StatusCode) {
			return;
		}
		if (result.StatusCode != Flux.Site.WebService.StatusCode.OK) {
			return;
		}

		var okConfirm = $get(this.id + "_addConfirm");
		var defaultBtn = $get(this.id + "_addButton");
		if (!defaultBtn || !okConfirm) {
			return;
		}
		var w = defaultBtn.clientWidth;
		defaultBtn.style.display = "none";
		okConfirm.style.display = "";
		okConfirm.parentNode.className += " flagging_grayed";

		if (w <= 0)
			return;
		okConfirm.style.width = w + "px";
		if (okConfirm.clientWidth != w)
			w = (2 * w) - okConfirm.clientWidth;
		if (w < 0)
			return;
		okConfirm.style.width = w + "px";
	};

	(function() {
		Flagging.inst[this.id] = this;
		this.preinited = true;
	}).call(this);

}

Flagging.arrangeInsts = function() {
	var insts = Flagging.inst;
	for (var instance in insts) {
		if (insts[instance] instanceof Flagging) {
			insts[instance].moveBackwards();
			insts[instance].showFlags(false);
		}
	}
};

Flagging.dataChanged = function() {
	var insts = Flagging.inst;
	
	for (var instance in insts) {
		if (insts[instance] instanceof Flagging)
			insts[instance].forceRefresh();
	}
};

Flagging.inst				= {};
Flagging.flagsList			= null;


// [AtlasScript]

if (typeof(Sys)!="undefined")
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]
