// Redeclare som Highslide functions to suit JCE popups
hs.isHsAnchor = function (a) {
	return a.href.match('option=com_jce&task=popup');
};
hs.getSrc = function (a) {
	return hs.getQueryVariable('img', a.onclick.toString());
};

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

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');
	var a;
	var match = 'img='+ hs.getQueryVariable('img', url);
	
	// identify caller
	for (i = 0; i < as.length; i++) {
		if (hs.isHsAnchor(as[i])) {
			if (as[i].onclick && as[i].onclick.toString().match(match)) {	
				a = as[i];
				break;				
			}
		}
	}
	
	if (a) {
		var captionText = hs.getQueryVariable('title', url);
		if (captionText) {
			captionText = captionText.replace(/_/g, ' ');
			var div = hs.createElement (
				'div',
				{
					innerHTML: captionText,
					className: 'highslide-caption',
					id: 'highslide-caption-'+ hs.expanders.length
				},
				null,
				document.body			
			);	
		}
		
		hs.expand(a, { captionId: 'highslide-caption-'+ hs.expanders.length } );
		
	} else {
		window.realOpen(url, name, attributes);
	}
};

window.open = hs.winOpen;

hs.graphicsDir = '/highslide/graphics/';
window.onload = function () {
	hs.styleImages();
	hs.preloadImages(5);
};
