var CTV_indicator;
var CTV_win;
var CTV_path;
var CTV_alt;

function setCTV()
{
	var e = getElementsBySelector(".gallery")[0];
	if(e)
	{
		//alert("Found gallery");
	}
		else
	{
		return;
	}

	var areas = getElementsBySelector(".gallery a");
	for(var i=0; i<areas.length; i++){

		//EventUtils.addEventListener(areas[i],'mouseover',showCTVindicator);
		//EventUtils.addEventListener(areas[i],'mouseout',hideCTVindicator);
		EventUtils.addEventListener(areas[i],'click', doCTV);
		areas[i].imagePath = areas[i].href;
		areas[i].href = "javascript: void(0);";
		areas[i].target = "";
		
	}
}
/*
function showCTVindicator(){
	window.status = "Click to enlarge";
	if(CTV_indicator)
		CTV_indicator.style.visibility = "visible";
		
	return true;
}

function hideCTVindicator(){
	window.status = "";
	if(CTV_indicator)
		CTV_indicator.style.visibility = "hidden";
}*/

function doCTV(evt){
	var path;
	var alt;
	var img;
	if(evt.target)
	{
		el = evt.target;
	}
	else
	{
		el = evt.srcElement;
	}

	while(el.tagName != "A"){
		el = el.parentNode;
	}
	
	img = el.getElementsByTagName("img")[0];
	path = el.imagePath;
	alt = img.alt;
	
	CTV_win = window.open("image_viewer.htm",'CTV','status=yes,scrollbars=no,resizable=no,width=400,height=400');
	CTV_path = path;
	CTV_alt = alt;
	CTV_win.window.focus();
	
	return false;
}

/*
	Finds elements on page that match a given CSS selector rule. Some
	complicated rules are not compatible.
	Based on Simon Willison's excellent "getElementsBySelector" function.
	Original code (with comments and description):
	http://simon.incutio.com/archive/2003/03/25/getElementsBySelector
*/
function getElementsBySelector(selector)
{
	var tokens = selector.split(' ');
	var currentContext = new Array(document);
	for(var i=0;i<tokens.length;i++)
	{
		token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');
		if(token.indexOf('#') > -1)
		{
			var bits = token.split('#');
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if(tagName && element.nodeName.toLowerCase() != tagName)
				return new Array();
			currentContext = new Array(element);
			continue;
		}

		if(token.indexOf('.') > -1)
		{
			var bits = token.split('.');
			var tagName = bits[0];
			var className = bits[1];
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
				if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
	    }

		if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/))
		{
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
	        	if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction;
			switch(attrOperator)
			{
				case '=':
					checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
					break;
				case '~':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
					break;
				case '|':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
					break;
				case '^':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
					break;
				case '$':
					checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
					break;
				case '*':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
					break;
				default :
					checkFunction = function(e) { return e.getAttribute(attrName); };
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(checkFunction(found[k]))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
		}

		tagName = token;
		var found = new Array;
		var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				//alert(currentContext[h]);
				if(currentContext[h]){
					var elements = currentContext[h].getElementsByTagName(tagName);
					for(var j=0;j<elements.length; j++)
						found[foundCount++] = elements[j];
				}
			}

		currentContext = found;
	}

	return currentContext;
}


/* @constructor */
function EventUtils() {
	throw 'RuntimeException: EventUtils is a static utility class ' +
		' and may not be instantiated';
}

/**
 *	@access static
 *	@param HTMLElement target
 *	@param string type
 *	@param Function callback
 *	@param boolean captures
 */
EventUtils.addEventListener = function (target,type,callback,captures) {
	if (target.addEventListener) {
			// EOMB
		target.addEventListener(type,callback,captures);
	} else if (target.attachEvent) {
		// IE
		target.attachEvent('on'+type,callback,captures);
	} else {
		// IE 5 Mac and some others
		target['on'+type] = callback;
	}
}

EventUtils.addEventListener(window,'load',setCTV);
