// cannot rely on jQuery being loaded here

var ccm_uiLoaded = false;
var ccm_siteActivated = true;
var ccm_animEffects = true;



/* check IE6 */
(function(){
	var ua = navigator.userAgent.toLowerCase();
	window.IE6 = (!window.opera && ua.indexOf('msie 6.0') > 0) ? true : false;
	if (IE6)
	{
		window.__ie6doc = document;
		window.__ie6stack = [];
		window.createScrollFollowFunc = function(target, flag)
		{
			var elm = target, flag = (flag) ? flag : false;
			var func = function(){
				var sc = {
					y: __ie6doc.body.scrollTop || __ie6doc.documentElement.scrollTop,
					x: __ie6doc.body.scrollLeft || __ie6doc.documentElement.scrollLeft
				}
				var info = elm.getBoundingClientRect();
				var height = info.bottom - info.top;
				var winSize = __ie6doc.documentElement.clientHeight || __ie6doc.body.clientHeight || __ie6doc.body.scrollHeight
				if (flag === false) {
					elm.style.top = (winSize - height) / 2 + sc.y + 'px';
				}
				else
				{
					elm.style.top = sc.y + 'px';
				}
			}
			__ie6stack.push(func);
			return func;
		}
		// for memory leak of IE6
		window.attachEvent('onunload', function(){
			for (var i = 0; i < __ie6stack.length; i++)
			{
				window.detachEvent('onscroll', __ie6stack[i]);
			}
		});
		if (CCM_ARRANGE_MODE === true)
		{
			window.attachEvent('onload', function()
			{
				var func = arguments.callee;
				$(".ccm-block-arrange").each(function()
				{
					var elm = $(this);
					elm.mouseover(function()
					{
						elm.css('cursor', 'move');
						elm.css('border', 'dotted 2px #000');
					});
					elm.mouseout(function()
					{
						elm.css('cursor', 'normal');
						elm.css('border', 'dotted 2px #f00');
					});

				});
				window.attachEvent('onunload', function()
				{
					$(".ccm-block-arrange").each(function()
					{
						$(this).unbind('mouseover');
						$(this).unbind('mouseout');
					});
				})
			});
		}
	}
})();

ccm_parseJSON = function(resp, onNoError) {
	if (resp.error) {
		alert(resp.message);
	} else {
		onNoError();
	}
}

ccm_deactivateSite = function(onDone) {
	if (ccm_siteActivated == false) {
		return false;
	}

	if ($("#ccm-overlay").length < 1) {
		$(document.body).append('<div id="ccm-overlay"></div>');
	}
	var layer = document.getElementById('ccm-overlay');
	if (ccm_animEffects) {
		$("#ccm-overlay").fadeIn(100, function() {
			ccm_siteActivated = false;

			if (IE6)
			{
				var size = {
					width: document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth,
					height: document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight
				}
				layer.style.width = size.width + 'px';
				layer.style.height = size.height + 'px';

				var sc = {
					y : document.body.scrollTop || document.documentElement.scrollTop,
					x : document.body.scrollLeft || document.documentElement.scrollLeft
				};
				layer.style.position = 'absolute';
				layer.style.top = sc.y + 'px';
				window.attachEvent('onscroll', createScrollFollowFunc(layer));
			}
			if (typeof onDone == 'function') {
				onDone();
			}
		});

	} else {
		$("#ccm-overlay").show();
			var size = {
				width: document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth,
				height: document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight
			}
			layer.style.width = size.width + 'px';
			layer.style.height = size.height + 'px';
			if (IE6)
			{
				var sc = {
					y : document.body.scrollTop || document.documentElement.scrollTop,
					x : document.body.scrollLeft || document.documentElement.scrollLeft
				};
				layer.style.position = 'absolute';
				layer.style.top = sc.y + 'px';
				window.attachEvent('onscroll', createScrollFollowFunc(layer));
				var ds = __ie6doc.getElementById('ccm-dashboard-page');
				if (ds !== null)
				{
					$("#ccm-dashboard-page select").each(function(){
						$(this).css('visibility', 'hidden');
					})
				}
			}
		ccm_siteActivated = false;
		if (typeof onDone == 'function') {
			onDone();
		}
	}
}

ccm_activateSite = function() {
	if (ccm_animEffects) {
		$("#ccm-overlay").fadeOut(100);
	} else {
		$("#ccm-overlay").hide();
	}
	if (IE6)
	{
		var ds = __ie6doc.getElementById('ccm-dashboard-page');
		if (ds !== null)
		{
			$("#ccm-dashboard-page select").each(function(){
				$(this).css('visibility', 'visible');
			})
		}
	}
	ccm_siteActivated = true;
	ccm_topPaneDeactivated = false;
}


// called in versions popup
ccm_disableLinks = function() {
	td = document.createElement("DIV");
	td.style.position = "absolute";
	td.style.top = "0px";
	td.style.left = "0px";
	td.style.width = "100%";
	td.style.height = "100%";
	td.style.zIndex = "1000";
	document.body.appendChild(td);
}