
ccms.namespace('csocial');

ccms.event.onAvailable('page',function () { if (typeof setupZoom == 'function') { setupZoom(); } });
ccms.event.onAvailable('search-results', function () { new quickSearch('quick-search','quick-query','search-results','api/search'); });
ccms.event.onAvailable('quick-login', function () { new quickLogin('quick-login'); });

function editList (name, action) {
	var availableList = document.getElementById('fieldlist_' + name + '_fields');
	var selectedList = document.getElementById('fieldlist_' + name + '_selected');
	switch (action) {
		case 'add':
			var addIndex = availableList.selectedIndex;
			if(addIndex < 0)
				return;
			selectedList.appendChild(availableList.options.item(addIndex));
			availableList.selectedIndex = addIndex > 0 ? addIndex - 1 : addIndex;
			selectedList.selectedIndex = -1;
		break;
		
		case 'remove':
			var selIndex = selectedList.selectedIndex;
			if(selIndex < 0)
				return;
			if (selectedList[selectedList.selectedIndex].value == 'divide')
				return;
			availableList.appendChild(selectedList.options.item(selIndex))
			availableList.selectedIndex = -1;
			selectedList.selectedIndex = selIndex > 0 ? selIndex-1 : selIndex;
		break;
		
		case 'up':
			var selIndex = selectedList.selectedIndex;
			if(selIndex < 1)
				return;
			var opt = selectedList.options[selIndex];
			selectedList.removeChild(opt);
			selectedList.insertBefore(opt, selectedList.options[selIndex - 1]);
			availableList.selectedIndex = -1;
		break;
		
		case 'down':
			var selIndex = selectedList.selectedIndex;
			if(selIndex >= (selectedList.options.length - 1))
				return;
			var opt = selectedList.options[selIndex];
			selectedList.removeChild(opt);
			if (selIndex > selectedList.options.length - 2)
				selectedList.appendChild(opt);
			else			
				selectedList.insertBefore(opt, selectedList.options[selIndex+1]);
			availableList.selectedIndex = -1;
		break;
	}
	// Update the hidden field
	var listValue = '';
	var isDivide = false;
	for (i=0; i < selectedList.options.length; i++) {
		var newValue = selectedList.options[i].value;
		if (newValue == 'divide') {
			listValue += '|';
			isDivide = true;
		}
		else {
			listValue += ((listValue != '' && !isDivide) ? ',' : '') + newValue;
			isDivide = false;
		}
	}
	document.getElementById('fieldlist_' + name).value = listValue;
}

csocial.isInternalLink = function(url) {
	var domain = /[^.]+\.(com|net|info|org|uk|co\.uk|org\.uk|gov\.uk|me\.uk)$/.exec(window.location.host)[0];
	return !(/^http:/.test(url)) || (url.indexOf(domain) != -1);

	var host = new RegExp("^http://" + domain + "/");
	return !(url.match(/^http/) && !host.test(url));
}

ccms.dom.updateExternalLinks = function(root) {
	if (typeof root == 'undefined') root = document;

	// Loop through all links; external links need updating
	ccms.arrayOf(root.getElementsByTagName('a')).foreach(function(node) {
		// rel="player"
		if (ccms.dom.hasRel(node, 'player')) {
			ccms.event.attach(node, 'click', function() {
				var player = window.open(node.href, node.target ? node.target : 'csoPlayer', 'height=300,width=300,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
				if (window.focus) { player.focus() }
				return false;
			});
		}
		// rel="external"
		else if (ccms.dom.hasRel(node, 'external')) {
			node.setAttribute('target', '_blank');
		}
		// HREF links in the forums that look external
		else if (window.location.pathname.startswith('/forums') && !csocial.isInternalLink(node.getAttribute('href'))) {
			node.setAttribute('target', '_blank');
		}
	});
}

ccms.widgets.addInstaller('updateExternalLinks', ccms.dom.updateExternalLinks);

// When we're quoting things focus on the editor and move the cursor to the end
if (window.location.search.match(/\bquote=/)) {
	ccms.async.when(function() { return tinymce && tinymce.EditorManager.activeEditor }, function() {
		var end, root, ed = tinymce.EditorManager.activeEditor;

		root = ed.dom.getRoot();
		end = root.childNodes.length ? root.childNodes[root.childNodes.length - 1] : root;

		if (tinymce.isGecko) { // Gecko collapses outside of block elements so we need to find something inline
			while (end.childNodes.length && window.getComputedStyle(end, null).display == 'block') {
				end = end.childNodes[end.childNodes.length - 1];
			}
		}

		ed.selection.select(end);
		ed.selection.collapse(false);
		ed.focus();
	});
}

csocial.thumbScroller = function(el) {
	var self = this;
	self.div = el;
	self.ul  = el.getElementsByTagName('ul')[0];
	
	ccms.dom.addClass(el, 'hasjs');
	
	var w = ccms.dom.getWidth(el);
	var li = el.getElementsByTagName('li');
	
	var ih = ccms.dom.getHeight(li[0]);
	el.style.position = 'relative';
	el.style.left     = '0px';
	el.style.right    = '0px';
	el.style.overflow = 'hidden';
	el.style.height   = ih + 'px';
	
	var iw = ccms.dom.getWidth(li[0]) || ih;
	var xw = Math.floor(w / iw) * iw;
	
	self.pages = Math.ceil(ccms.dom.getHeight(self.ul) / ih) - 1;
	
	self.wrap = document.createElement('div');
	ccms.dom.addClass(self.wrap, 'wrap');
	self.wrap.style.position = 'absolute';
	self.wrap.style.top = '0px';
	self.wrap.style.left = Math.floor((w - xw) / 2) + 'px';
	self.wrap.style.right = Math.floor((w - xw) / 2) + 'px';
	self.wrap.style.height   = ih + 'px';
	self.wrap.style.overflow = 'hidden';
	self.div.appendChild(self.wrap);
	
	self.wrap.appendChild(self.ul);
	self.ul.style.position = 'absolute';
	self.ul.style.top   = '0px';
	self.ul.style.width = (li.length * iw) + 'px';
	self.ul.style.left  = '0px';
	
	// transition for each browser
	self.ul.style.WebkitTransition = 'left .5s ease-in-out';
	self.ul.style.MozTransition = 'left .5s ease-in-out'; // Coming in Gecko 2 (Firefox 4)
	self.ul.style.OTransition = 'left .5s ease-in-out';
	self.ul.style.transition = 'left .5s ease-in-out';
	
	self.prev = document.createElement('div');
	ccms.dom.addClass(self.prev, 'prev');
	self.prev.style.position = 'absolute';
	self.prev.style.left = '0px';
	self.prev.style.top = '0px';
	self.prev.style.height = ih + 'px';
	self.prev.style.width = Math.floor((w - xw) / 2) + 'px';
	self.prev.style.lineHeight = self.prev.style.height;
	self.prev.style.textAlign = 'center';
	self.prev.innerHTML = '<a style="display: block;" href="javascript:;" title="Fewer Profiles">&lt;</a>';
	self.div.appendChild(self.prev);
	
	ccms.event.attach(self.prev, 'mousedown', self.goPrev = function() {
		if (self.current > 0) {
			self.current--;
			self.ul.style.left = (-self.current * ccms.dom.getWidth(self.wrap)) + 'px';
			
			self.slideTimer = window.setTimeout(self.goPrev, 1000);
		}
		else self.goStop();
	});
	ccms.event.attach(self.prev, 'mouseup', function() { self.goStop() });
	
	self.next = document.createElement('div');
	ccms.dom.addClass(self.next, 'next');
	self.next.style.position = 'absolute';
	self.next.style.right = '0px';
	self.next.style.top = '0px';
	self.next.style.height = ih + 'px';
	self.next.style.width = Math.floor((w - xw) / 2) + 'px';
	self.next.style.lineHeight = self.next.style.height;
	self.next.style.textAlign = 'center';
	self.next.innerHTML = '<a style="display: block;" href="javascript:;" title="More Profiles">&gt;</a>';
	self.div.appendChild(self.next);
	
	ccms.event.attach(self.next, 'mousedown', self.goNext = function() {
		if (self.current < self.pages) {
			self.current++;
			self.ul.style.left = (-self.current * ccms.dom.getWidth(self.wrap)) + 'px';
			
			self.slideTimer = window.setTimeout(self.goNext, 1000);
		}
		else self.goStop();
	});
	ccms.event.attach(self.next, 'mouseup', function() { self.goStop() });
	
	self.slideTimer = null;
	self.goStop = function() {
		window.clearTimeout(self.slideTimer);
		self.slideTimer = null;
	}
}
csocial.thumbScroller.prototype.current = 0;

csocial.thumbScroller.install = function(root) {
	if (typeof root == 'undefined') root = document;
	
	ccms.arrayOf(ccms.dom.getElementsByClassName('thumb-scroller', 'div', root)).foreach(function(node) {
		new csocial.thumbScroller(node);
	});
}

ccms.widgets.addInstaller('installThumbScroller', csocial.thumbScroller.install);

