﻿var $mdp;

if ($mdp == undefined) {
	$mdp = {

		// OPTIONs
		options: {
			baseSvcUrl: "/svc/"
			, currentSiteId: null
			, currentSiteBaseUrl: "/"
		}

		// FFM (FORM FIELD MESSAGE)
		, _ffm: function(el, msg, cssClass) {
			var _p = $($(el).parents('div.formBlock').get(0));
			var _i;
			_p.find('label.loginAlert').remove();
			_i = $('<label/>').addClass(cssClass);
			if (msg == null) {
				// LOADING
				_i.append($('<img/>').attr('src', '/css/img/inputLoader.gif').css('width', '16px').css('height', '16px'));
			} else {
				// NORMAL MSG
				_i.text(msg);
			}
			_p.append(_i);
		}

		// FORM FIELD LOADING
		, formFieldLoading: function(el) {
			this._ffm(el, null, 'loginAlertLoading');
		}

		// FORM FIELD ERROR
		, formFieldError: function(el, msg) {
			this._ffm(el, msg, 'loginAlertWrong');
		}

		// FORM FIELD OK
		, formFieldOk: function(el, msg) {
			this._ffm(el, msg, 'loginAlertCorrect');
		}

		// IS USER LOGGED OUT
		, isUserLogged: function() {
			return $mdp.getCurrentUserName() != undefined;
		}

		// GET CURRENT USER NAME
		, getCurrentUserName: function() {
			return $.cookie('kun');
		}

		// GET CURRENT USER ID
		//		, getCurrentUserId : function() {
		//			return null;
		//		}

		// DO LOGOUT
		, doLogout: function(callback) {
			$.ajax(
				$mdp.options.baseSvcUrl + 'logout.ashx'
				, {
					dataType: 'json'
					, data: { dummy: '1' }
					, type: 'POST'
					, success: function() {
						$.cookie('kun', null);
						if (callback != null) {
							callback();
							return;
						}

						window.location.href = window.location.href + ' ';
					}
				}
			);
		}

		// ANIMATE POLL CHART
		, animatePollChart: function(container) {
			$(container).find("ul li span[data-tl-perc]").css("width", "0%").each(function(i, item) {
				$(this).animate({ width: $(this).attr("data-tl-perc") + "%" }, 1000);
			});
		}

	};
}
