﻿function submitForm(str){
	try {
		jQuery(str).submit();
	}catch(e){ jQuery(str).submit(); }
}
function goToUrl(str){
	window.location.href = str;
}
function icTip(_this, a, b){
	jQuery( _this ).addClass('tip').attr( 'title', '<strong>' + a + '</strong><br />' + b );
}

/* jquery notice */
jNotice = function(text, type){
	require(['cmnotice'], function(CMNotice){
		(jNotice = function(text, type){
			var arg = {};
			arg[type.toLowerCase().replace(/^ok$/i, 'success')] = text;
			CMNotice(arg)
		})(text, type);
	});
}

showLastSeen = function (stUpdates) {
	var span = '<span title="%s" class="tip cm-updatecount" >%d</span>';
	var sectionKey = '';
	var elm = null;

	if (typeof jQuery.sprintf != 'undefined') {
		for (sectionKey in stUpdates) {
			if (Object.hasOwnProperty.call(stUpdates, sectionKey)) {
				const element = stUpdates[sectionKey];

				if (parseInt(element.count, 10) > 0) {
					elm = jQuery.sprintf(span, element.text, element.count);
					elm = jQuery(elm).effect('pulsate', {times: 2}, 1500);
					jQuery('#menu-' + sectionKey.toLowerCase())
						.find('a')
						.append(jQuery(elm));
				}
			}
		}
	}
};
function cbsel(item)
{
    fe = jQuery(item);
	if (fe.disabled == false) {
	    if (fe.checked) { fe.checked=false; }
	    else { fe.checked = true; }
	}
}
function toggleTopHelp(){
	var t = document.getElementById('helpTop');
	var i = document.getElementById('helpTopDelImg');
	var d = t.style.display=='block'?'none':'block';
	t.style.display=d;
	i.style.visibility=d=='block'?'visible':'hidden';
}
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

function CMParseURL(url) {
	url || (url = window.location.href);

	var a = document.createElement('a');

	a.href = url;

	return {
		source: url,
		protocol: a.protocol.replace(':', ''),
		hostname: a.hostname,
		port: a.port,
		query: a.search.replace(/^\?/, ''),
		params: (function(){
			var segment,
				params = {},
				parts = a.search.replace(/^\?/, '').split('&'),
				i = 0,
				l = parts.length;

			for(; i < l; i++) {
				if(!parts[i]) continue;

				segment = parts[i].split('=');

				params[segment[0]] = segment[1];
			}

			return params;
		}()),
		page: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
		hash: a.hash.replace('#',''),
		path: a.pathname.replace(/^([^\/])/,'/$1'),
		relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
		segments: a.pathname.replace(/^\//,'').split('/')
	};
};

var CMThrottle = function(fn, delay, context) {
	var timeout = 0;

	delay || (delay = 500);
	context || (context = this);

	return function() {
		var args = arguments;

		clearTimeout(timeout);
		timeout = setTimeout(function() {
			fn.apply(context, args);
		}, delay);
	};
};

var CMSpread = function(fn, delay, context) {
	var spreader, startTimeout,
		lastRun = (new Date()).getTime() - delay,
		queue = [],
		timeout = 0;

	delay || (delay = 500);

	spreader = function() {
		var args = arguments,
			_this = this,
			now = (new Date()).getTime();

		if(queue.length || (now - lastRun) < delay) {
			queue.push({
				args: args,
				context: this
			});

			if(!timeout) {
				startTimeout(now);
			}
		} else {
			lastRun = now;
			fn.apply(context || _this, args);
		}
	};

	startTimeout = function(now) {
		timeout = setTimeout(function() {
			var item = queue.shift();

			if(item) {
				lastRun = (new Date()).getTime();
				fn.apply(context || item.context, item.args);

				if(queue.length) {
					startTimeout(lastRun);
				} else {
					timeout = 0;
				}
			}
		}, Math.max(Math.abs(delay - now + lastRun), 13));
	};

	spreader.flush = function() {
		clearTimeout(timeout);
		timeout = 0;
		lastRun = (new Date()).getTime() - delay;

		while(queue.length > 0) {
			queue.pop();
		}
	};

	return spreader;
};

var CMLicense = function(settings){

	this.dialogUpgrade = jQuery('<div />');
	this.ajax = cmAjax({
		cfc: '/cfc/conference.cfc'
	});

	this.settings = jQuery.extend({}, settings);
};

CMLicense.prototype = {
	showUpgrade: function(){
		var _this = this,
			tabs = [];

		_this.ajax({
			method: 'templateUpgrade'
		}).done(function(obj){
			_this.dialogUpgrade.html(obj.template).dialog({
				width: 500,
				modal: true,
				resizable: false
			});

			jQuery('#button-payment').on('click', function(event) {
				event.preventDefault();
				document.location.href = "/events?key="+_this.settings.conferenceID+"&doPayment"
			});

			jQuery('#button-conferencemanager').on('click', function(event) {
				event.preventDefault();
				window.open("//conferencemanager.dk");
			});

// 			tabs = jQuery('#upgrade-license-tabs div[data-tab]');
// 			tabs.on('click', function(){
// 				var index = jQuery(this).data('tab');

// 				jQuery.each(tabs, function(i, tab){
// 					var content = jQuery('#upgrade-license-' + i);

// 					jQuery(tab).removeClass('page_tab_wrap_active');
// 					content.hide();

// 					if(i == index){
// 						content.show();
// 						jQuery(tab).addClass('page_tab_wrap_active');						
// 					}
// 				});

// 			});
// ;
// 			if(_this.settings.hasOwnProperty('defaultTab')){
// 				jQuery(tabs[_this.settings.defaultTab]).trigger('click');
// 			}

// 			if(_this.settings.hasOwnProperty('featureLevel') && _this.settings.featureLevel === 10){
// 				tabs[0].remove();
// 				jQuery('#upgrade-license-' + 0);
// 			}

		});


	}
}