/*[development only scripts on|off ]*/
var doclix_devel = false; /*[load development scripts: true|false]*/
if (doclix_devel) {
	/*[replacement id for testing]*/
	doclix_pid = 9;
	/*[PRODUCTS on|off, if needed]*/
//	doclix_product_in_text		= true;
//	doclix_product_txt_blocks	= true;
//	doclix_product_cloud		= true;
//	doclix_product_links		= true;
	/*[icons off]*/
//	doclix_link_icon = false;
	/*[force sidebar, if needed]*/
//	doclix_show_sidebar = true;
	/*[additional tags to skip]*/
//	doclix_ignore_tags = 'h1,h2,h3';
}
/*[development replacement functions]*/
var doclix_debug;
if (typeof show == 'undefined') {show = function () {}}
if (typeof timeStamp 	== 'undefined') {timeStamp = function () {}}
/**********************************************/
/*[common scripts]*/
var supress_errors = false; /*[supress errors, if possible]*/
if (supress_errors) window.onerror = function () {return true;}

/*[shortcuts]*/
$ID =	function (sId)			{ return document.getElementById(sId);				}
$TN =	function (sTagName)	{ return document.getElementsByTagName(sTagName);	}
$dcE =	function (sTagName)	{ return document.createElement(sTagName);			}
$dcT =	function (sTxt)		{ return document.createTextNode(sTxt);			}
/*[check class name]*/
checkCN = function (obj,sCN) {
	var re = new RegExp('\\b'+sCN+'\\b'); return re.test(obj.className);
}
/*[get elements by class name]*/
$CN = function (sCN, sTN, obj) {
	var sTN = sTN || '*';
	var obj = obj || document;
	var els = obj.getElementsByTagName(sTN);
	var classEls = [];
	for (var i=0; i<els.length; i++) {
		if (checkCN(els[i],sCN)) {
			classEls.push(els[i]);
		}
	}
	return classEls;
}

/*[get elements by attribute name and value]*/
$AT = function (sTN, sAN, sAV, obj) {
	var sTN = sTN || '*';
	var obj = obj || document;
	var rAV = new RegExp('\\b'+sAV+'\\b')
	var els = obj.getElementsByTagName(sTN);
	var attEls = [];
	for (var i=0; i<els.length; i++) {
		if (sAN && sAV) {
			if (rAV.test(els[i].getAttribute(sAN))) {
				attEls.push(els[i]);
			}
		} else if (sAN && !sAV) {
			if (els[i].getAttribute(sAN)) {
				attEls.push(els[i]);
			}
		}
	}
	return attEls;
}

/*[clear intervals and timeouts]*/
xInt = function (x) { if (x > 0) {window.clearInterval(x);} }
xTmo = function (x) { if (x > 0) {window.clearTimeout(x);} }

/*[find this script's parent elements]*/
if (typeof doclix_tagtrail == 'undefined')
	doclix_tagtrail = '';

var	doclix_syndparent;

/*[recognize parent elements within the page structure]*/
traceParents = function (obj)	{
	if (obj.parentNode) {
		doclix_syndparent = obj.parentNode;
		while (obj.parentNode) {
			doclix_tagtrail += obj.parentNode.nodeName+'|';
			obj = obj.parentNode;
		}
	}
}

findPlacement = function (scr) {
	var doclix_allScripts = $TN('script');
	for (var i=0; i<doclix_allScripts.length; i++) {
		if (doclix_allScripts[i].src && doclix_allScripts[i].src.indexOf(scr)!=-1) {
			traceParents(doclix_allScripts[i]);
		}	
	}
}
findPlacement('doclix_synd.js');

var	doclix_cloudspot = doclix_syndparent;

//var doclix_bad_trail = /(TD|TH|TR|TBODY|TABLE|THEAD|TFOOT)/i; // not needed?
var doclix_head_tags = /(HTML|HEAD|BASE|META)/i;

/*[retrieve CSS property]*/
getCSSProp = function (obj, cssProp, cssPropIE) {
	if (window.getComputedStyle) {
		var objStyle = window.getComputedStyle(obj, null);
		return objStyle.getPropertyValue(cssProp);
	} else 	if (obj.currentStyle) {
		return obj.currentStyle[cssPropIE];
	}
}

/*[check environment]*/
/*[check platform]*/
var doclix_navPlatform;
checkPlatform = function () {
	if (navigator.platform.indexOf('Win')!=-1) {
		doclix_navPlatform = 'Windows';
	} else if (navigator.platform.indexOf('Mac')!=-1) {
		doclix_navPlatform = 'Mac';
	} else {
		doclix_navPlatform = 'Other';
	}
}
checkPlatform();

/*[check for quirks mode]*/
var doclix_quirksMode = false;
checkQuirksMode = function () {
if (document.compatMode && (document.compatMode == 'BackCompat')) {
		doclix_quirksMode = true;
	} 
}
checkQuirksMode();

var	doclix_isFirefox = doclix_isSafari = doclix_isNetscape = doclix_isOpera = doclix_isIE = doclix_isIEold = doclix_isIE5 = doclix_isIE6 = doclix_isIE7 = false;
checkBrowser = function () {
	var uA = navigator.userAgent;
	var ieV = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var re = new RegExp('MSIE ([0-9]{1,}[\.0-9]{0,})');
		if (re.exec(uA) != null) {
			ieV = parseFloat(RegExp.$1);
		} 
		if (ieV > -1) {
			doclix_isIE = true;
			if (ieV >= 7.0) {
				doclix_isIE7 = true;
			} else if (ieV >= 6.0 && ieV < 7.0) {
				doclix_isIE6 = true;
			} else if (ieV >= 5.0 && ieV < 6.0) {
				doclix_isIE5 = true;
			} else if (ieV < 5.0) {
				doclix_isIEold = true;
			}
		}
	} else {
		if (uA.indexOf('Firefox') > -1 && uA.indexOf('Navigator') < 0) {
			doclix_isFirefox = true;
		}
		if (uA.indexOf('Safari') > -1) {
			doclix_isSafari = true;
		}
		if (uA.indexOf('Navigator') > -1) {
			doclix_isNetscape = true;
		}
		if (navigator.appName == 'Opera') {
			doclix_isOpera = true;
		}
	}
}
checkBrowser();

/*[add/attach event]*/
addEvent = function (target, eventType, functionRef, capture) {
	if (typeof target.addEventListener != 'undefined') { 
		target.addEventListener(eventType, functionRef, capture); 
	} else if (typeof target.attachEvent != 'undefined') { 
		target.attachEvent('on' + eventType, functionRef); 
	} else { 
		eventType = 'on' + eventType;
		if (typeof target[eventType] == 'function') { 
			var oldListener = target[eventType]; 
			target[eventType] = function() { 
				oldListener(); return functionRef(); 
			} 
		} else { 
			target[eventType] = functionRef; 
		}
	}
}

/*[marker for the onload event]*/
if (typeof doclix_host_loaded == 'undefined') {
	doclix_host_loaded = false;
	addEvent(window,'load', function () {doclix_host_loaded = true;});
}

/*[load external files as needed][v1.6]*/
if (typeof doclix_extObjs == 'undefined') {
	doclix_extObjs = '';
}
loadExtFile = function (file, ftype, force) {
	var extFile = '';
	if (doclix_extObjs.indexOf(file) == -1 || force == true)	{ /*[check to see if this object has not already been added to page before proceeding, or force reload]*/
		if (ftype == 'script') {
			extFile = $dcE('script')
			extFile.setAttribute('type','text/javascript');
			extFile.setAttribute('src', file);
			extFile.setAttribute('defer', true);
		} else if (ftype == 'style')	{
			extFile = $dcE('link')
			extFile.setAttribute('rel', 'stylesheet');
			extFile.setAttribute('type', 'text/css');
			extFile.setAttribute('href', file);
		}
	}
	if (extFile != '')	{
		if ($TN('head').length > 0) {
			if (doclix_isIE && doclix_syndparent.nodeName.match(doclix_head_tags)) {
				doclix_syndparent.appendChild(extFile); //Operation Aborted bug for IE6 not recognizing BASE tags
			} else {
				$TN('head')[0].appendChild(extFile); //if there is no head, most browsers create one, except Safari and Opera, those will throw an error.				
			}
			doclix_extObjs += file + ' '; /*[remember this object as being already added to page]*/
		} else {
			try {
				
			} catch (x) {}
		}
	}
}
/*[load development scripts]*/
if (doclix_devel) {
	loadExtFile('http://cg.doclix.com/contextgain/serve/js/doclix_devel.js', 'script');
}

/*[temporary replacenment functions]*/
activatePop = function () { /*[placeholder]*/ }

/*[services]*/
if (typeof doclix_in_text_served == 'undefined')
	doclix_in_text_served = false;
if	(typeof doclix_product_txt_blocks_served == 'undefined')
	doclix_product_txt_blocks_served = false;
if	(typeof doclix_links_served == 'undefined')
	doclix_links_served = false;
if	(typeof doclix_cloud_served == 'undefined')
	doclix_cloud_served = false;


/*[side bar]*/
if (typeof doclix_show_sidebar == 'undefined')
	doclix_show_sidebar = false;

prepForIE = function () {
  if (doclix_isIE)	{
  		if (doclix_isIE7) {
			loadExtFile('http://cg.doclix.com/contextgain/serve/css/cg_remote_ie.css', 'style');
		} else if (doclix_isIE6) {
			loadExtFile('http://cg.doclix.com/contextgain/serve/css/cg_remote_ie_pre7.css', 'style');
		}
		if (doclix_quirksMode) {
			loadExtFile('http://cg.doclix.com/contextgain/serve/css/cg_remote_ie_qurks.css', 'style');
		}
		/*[strange bug fix, seems to improve popup responsiveness in IE]*/
		if (typeof ie_pop_wake == 'undefined' && !doclix_show_sidebar) {
			ie_pop_wake = $dcE('div');
			if (doclix_isIE6) {
				ie_pop_wake.innerHTML = '<!--[IE6 0px height bug fix]-->';
			}
			ie_pop_wake.style.height = '0px';
			document.body.appendChild(ie_pop_wake);
		}
  	}
}

if (typeof doclix_icon_img == 'undefined')
	doclix_icon_img = new Image();
	
preloadIcons = function () {
	(doclix_isIE && !doclix_isIE7) ? doclix_icon_img.src = 'http://cg.doclix.com/contextgain/serve/img/cg_hit.gif' : doclix_icon_img.src = 'http://cg.doclix.com/contextgain/serve/img/cg_hit.png';
}

initContextGain = function () {
	/*[load CSS]*/
	loadExtFile('http://cg.doclix.com/contextgain/serve/css/cg_remote.css', 'style');
	prepForIE();
	var doclix_params = 'pid='+doclix_pid;
	if (typeof doclix_cxg_exclude != 'undefined')
		doclix_params += '&channels='+doclix_cxg_exclude;
	/*[default service, neither in_text, nor links or cloud specified, load in_text]*/
	if (	typeof doclix_product_in_text		== 'undefined'	&&
			typeof doclix_product_txt_blocks	== 'undefined'	&&
			typeof doclix_product_links		== 'undefined'	&&
			typeof doclix_product_cloud		== 'undefined'	) {
		if (typeof doclix_auto_keywords != 'undefined' && doclix_auto_keywords == false)
			doclix_params += '&auto_keywords=0';
		if (typeof doclix_max_links != 'undefined')
			doclix_params += '&maxlinks='+doclix_max_links;			
		loadExtFile('http://cg.doclix.com/contextgain/serve/js/match_in_text.jsp?'+doclix_params, 'script');
		preloadIcons();
	}
	/*[new services]*/
	if (typeof doclix_product_in_text != 'undefined') {
		if (doclix_product_in_text) {
			if (typeof doclix_auto_keywords != 'undefined' && doclix_auto_keywords == false)
				doclix_params += '&auto_keywords=0';
			if (typeof doclix_max_links != 'undefined')
				doclix_params += '&maxlinks='+doclix_max_links;			
			loadExtFile('http://cg.doclix.com/contextgain/serve/js/match_in_text.jsp?'+doclix_params+'&prod=in_text', 'script');
			/*file loads again if parameters change*/
			preloadIcons();	
		}
	}
	if (typeof doclix_product_txt_blocks != 'undefined') {
		if (doclix_product_txt_blocks) {
			loadExtFile('http://cg.doclix.com/contextgain/serve/js/match_txt_blocks.jsp?'+doclix_params+'&prod=txt_blocks', 'script');
			/*file loads again if parameters change*/	
		}
	}
	if (typeof doclix_product_links != 'undefined') {
		if (doclix_product_links) {
			loadExtFile('http://cg.doclix.com/contextgain/serve/js/match_in_text.jsp?'+doclix_params+'&prod=links', 'script');
			/*file loads again if parameters change*/
			preloadIcons();	
		}
	}
	if (typeof doclix_product_cloud != 'undefined') {
		if (doclix_product_cloud) {
			if (typeof doclix_max_cloud_keywords != 'undefined')
				doclix_params += '&maxCloudKeywords='+doclix_max_cloud_keywords;			
			if (typeof doclix_preview_url != 'undefined') // for preview from account setup wizard
				doclix_params += '&previewUrl='+doclix_preview_url;  
			loadExtFile('http://cg.doclix.com/contextgain/serve/js/match_kw_cloud.jsp?'+doclix_params, 'script');	
		}
	}
}

/*[IE DOM ready - not reliable, if there are many external scripts hooked up]*/
initOnIE_DOMContentLoaded = function () {
	document.write('<scr'+'ipt type="text/javascript" id="doclix_domready" defer="defer" src="javascript:void(0)"></scr'+'ipt>');
	var domloadtag = $ID('doclix_domready');
	domloadtag.onreadystatechange = function () {
		if (this.readyState == 'complete') {
			initContextGain();
		}
	}
}

/*[safe mode switch]*/
var doclix_safe_mode = true; /*[make IE wait for the onload event to load: true|false]*/

/*[start services]*/
if (doclix_host_loaded) {
	initContextGain();
} else {
	if (doclix_isIE) {
		if (doclix_safe_mode) {
			addEvent(window, 'load', initContextGain);
		} else {
			initOnIE_DOMContentLoaded();
		}
	} else if (document.addEventListener) {
		document.addEventListener('DOMContentLoaded', initContextGain, false);
	} else {
		addEvent(window, 'load', initContextGain);
	}
}
