
hs.acquire = function (a) {
	var acquired = false;
	var captionText = null;
	// JCE
	if (a.href.match('option=com_jce&task=popup')) {
		a.href = hs.getQueryVariable('img', a.onclick.toString());
		acquired = true;
		captionText = hs.getQueryVariable('title', a.onclick.toString());
		if (captionText) captionText = captionText.replace(/_/g, ' ');
	}
	// Virtuemart
	if (a.href.match('components/com_virtuemart/shop_image/product')) {
		a.href = a.href.replace(/^.*?window\.open\('(.*?\.jpg).*?$/, '$1');
		acquired = true;
	}
	
	if (acquired) {
		var params = new Array();
		
		// Caption
		if (captionText) {
			var caption = hs.createElement (
				'div',
				{
					innerHTML: captionText,
					className: 'highslide-caption',
					id: 'highslide-caption-'+ hs.expanders.length
				},
				null,
				document.body			
			);
			params['captionId'] = caption.id;
		}
		
		a.onclick = function () { return hs.expand(a, params); };
		a.className += ' highslide';
		a.rel = null;
		return true;
	} else {
		return false;
	}
}

// Redeclare some Highslide functions to suit JCE popups
hs.isHsAnchor = function (a) {
	if (a.className.match('highslide')) return true;
	else if (hs.acquire(a)) return true;	
	else return false;
};

// New HS funtions
hs.acquireAll = function () {
	var as = document.getElementsByTagName('a');
	
	// identify caller
	for (i = 0; i < as.length; i++) {
		hs.acquire(as[i]);
	}
};

hs.getQueryVariable = function(variable, query) {
	//var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
};

// override window.open
window.realOpen = window.open;

hs.winOpen = function(url, name, attributes) {
	var as = document.getElementsByTagName('a');
	
	// identify caller
	for (i = 0; i < as.length; i++) {
		if (as[i].onclick && as[i].onclick.toString().match(url)
				|| as[i].href && as[i].href.match(url)) {	
			if (hs.acquire(as[i])) {
			
				as[i].onclick();
				return;
			}
		}
	}
	// else
	window.realOpen(url, name, attributes);
};

window.open = hs.winOpen;

hs.graphicsDir = '/highslide/graphics/';
hs.outlineType = 'rounded-white';
window.onload = function () {
	hs.acquireAll();
	hs.preloadImages(5);
};
