﻿$(function() {

	var f = true;
	var rightsList = [];
	var params = {
		"objects": [],
		"actions": [
			'update',
			'delete',
			'publish',
			'unpublish'
		]
	};

	$('[sdfobjid]').each(function() {
		params['objects'].push(
			parseInt($(this).attr('sdfobjid'))
		);
	});

	$(document).bind('keydown', 'ctrl+shift+e', function() {
		if ($('.sdf-obj-act').length == 0 && f) {
			f = false;
			$.ajax({
				type: "POST",
				contentType: "application/json; charset=utf-8",
				url: "/cpanel/c/CommonJS.asmx/CheckRights",
				data: $.toJSON(params),
				dataType: "json",
				success: function(r) {
					var rights = [];
					var objId, cpLink, el;
					var res = r.d;

					if (res.IsSuccess) {
						rights = res.Object;

						$('[sdfobjid]').each(function() {
							var el = $(this);
							var c = '<div class="sdf-obj-act">';
							objId = parseInt(el.attr('sdfobjid'));
							cpLink = el.attr('sdfcpanellink');
							for (var i = 0; i < rights.length; i++) {
								if (objId == rights[i][0]) {
									c += '<div class="' + rights[i][1] + '" action="' + rights[i][1] + '" sdfobjid="' + objId + '" sdfcpanellink="' + cpLink + '"/>';
								}
							}
							c += '</div>';
							el.before(c);
						});

						$('.sdf-obj-act > div').click(function() {
							var id = $(this).attr('sdfobjid');
							var link = $(this).attr('sdfcpanellink');
							var action = $(this).attr('action');

							location.href = link + 'index.sdf?action=' + action + '&id=' + id;
						});

						f = true;
					}
				}
			});
		} else if ($('.sdf-obj-act').length > 0) {
			$('.sdf-obj-act').each(function() {
				$(this).remove();
			});
		}
	});

});