// ONLOAD FUNCTIONS 
window.onload = function()
{
	set_lang_switch();
	if(window.mapInit) mapInit();
}

function set_lang_switch()
{
	var cookies = document.cookie.split(';');
	for(var i = 0; i < cookies.length; i++)
	{
		var tmp_arr = cookies[i].split('=');
		
		if(tmp_arr[0].match(/wc_language/))
		{
			var lang = tmp_arr[1];
			getobj('set_language').style.backgroundImage = 'url(/_res/lang_' + lang + '.png)';
		}
	}
	return true;
}

function toggle_cat(obj, cid)
{
	// GET TRS
	var trs = getobj('company_list').getElementsByTagName('tr');
	var trs_length = trs.length;
	
	var pattern = '_' + cid + '_';

	if(cid == 'all')
	{
		pattern = '_[0-9]+_';
		
		// GET ALL CHECKBOXES
		var chboxes = getobj('category_list').getElementsByTagName('input');
		var chboxes_length = chboxes.length;
		for(var i = 0; i < chboxes_length; i++)
		{
			chboxes[i].checked = obj.checked;
		}
		
	}
	
	for(var i = 0; i < trs_length; i++)
	{
		if(trs[i].className.match(pattern))
		{
			if(obj.checked)
			{
				trs[i].className = trs[i].className.replace(' hide_row', '');
			}
			else
			{
				// REPLACE OLDER "hide_row"
				trs[i].className = trs[i].className.replace(' hide_row', '');
				trs[i].className = trs[i].className + ' hide_row';
			}			
		}
	}
}

function load_maps()
{
	var images = new Array();
	for(var i = 1; i < 3; i++)
	{
		var src = '/_res/maps/map_' + i + '.png';
		images[i] = new Image();
		images[i].src = src;
	}
}

/* ----------------------------------- SEARCH --------------------------------- */
function search_list(e)
{
	var keycode;
	
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	
	if (keycode == 13)
	{
		filterTable();
	}
}

function filterTable()
{
	var table = $('.company_list table')[0];
	var term  = $('#search_list_str').val();

	dehighlight(table);

	var terms = term.toLowerCase().split("NO-TERMS");

	for (var r = 0; r < table.rows.length; r++)
	{
		var display = '';
		for (var i = 0; i < terms.length; i++)
		{
			if(table.rows[r].innerHTML.match(/(<th|<TH)/) || table.rows[r].className.match(/hide_row/))
			{
				continue;
			}
			
			if (table.rows[r].innerHTML.replace(/<[^>]+>/g, "").replace(/(\.|\s+)/g, "").toLowerCase().indexOf(terms[i].replace(/(\.|\s+)/g, "")) < 0)
			{
				display = 'none';
			}
			else
			{
				if (terms[i].length) highlight(terms[i], table.rows[r]);
			}
			table.rows[r].style.display = display;
		}
	}
}

function dehighlight(container)
{
	for (var i = 0; i < container.childNodes.length; i++) {
		var node = container.childNodes[i];

		if (node.attributes && node.attributes['class']
			&& node.attributes['class'].value == 'highlighted') {
			node.parentNode.parentNode.replaceChild(
					document.createTextNode(
						node.parentNode.innerHTML.replace(/<[^>]+>/g, "")),
					node.parentNode);
			// Stop here and process next parent
			return;
		} else if (node.nodeType != 3) {
			// Keep going onto other elements
			dehighlight(node);
		}
	}
}

function highlight(term, container)
{
	for (var i = 0; i < container.childNodes.length; i++) {
		var node = container.childNodes[i];

		if (node.nodeType == 3) {
			// Text node
			var data = node.data;
			var data_low = data.toLowerCase();
			if (data_low.indexOf(term) >= 0) {
				//term found!
				var new_node = document.createElement('span');

				node.parentNode.replaceChild(new_node, node);

				var result;
				while ((result = data_low.indexOf(term)) != -1) {
					new_node.appendChild(document.createTextNode(
								data.substr(0, result)));
					new_node.appendChild(create_node(
								document.createTextNode(data.substr(
										result, term.length))));
					data = data.substr(result + term.length);
					data_low = data_low.substr(result + term.length);
				}
				new_node.appendChild(document.createTextNode(data));
			}
		} else {
			// Keep going onto other elements
			highlight(term, node);
		}
	}
}

function create_node(child)
{
	var node = document.createElement('span');
	node.setAttribute('class', 'highlighted');
	node.attributes['class'].value = 'highlighted';
	node.appendChild(child);
	return node;
}

/* ----------------------------------- CHANGE METRICO MAP STYLES --------------- */
function map_loaded()
{
	// load_new_map_zoom();
}


function load_new_map_zoom()
{
	if(getobj('OpenLayers.Control.PanZoomBar_4'))
	{
		getobj('OpenLayers.Control.PanZoomBar_4_panup').style.display = 'none';
		getobj('OpenLayers.Control.PanZoomBar_4_panleft').style.display = 'none';
		getobj('OpenLayers.Control.PanZoomBar_4_panright').style.display = 'none';
		getobj('OpenLayers.Control.PanZoomBar_4_pandown').style.display = 'none';
		
		getobj('OpenLayers.Control.PanZoomBar_4_zoomin_innerImage').src= '';
		getobj('OpenLayers.Control.PanZoomBar_4_OpenLayers.Map_5_innerImage').src= '';
		getobj('OpenLayers.Control.PanZoomBar_4_zoomout_innerImage').src= '';

		getobj('OpenLayers.Control.PanZoomBar_4_zoomin_innerImage').src= '/_res/metrico/zoom-plus-mini.png';
		getobj('OpenLayers.Control.PanZoomBar_4_OpenLayers.Map_5_innerImage').src= '/_res/metrico/slider.png';
		getobj('OpenLayers.Control.PanZoomBar_4_zoomout_innerImage').src= '/_res/metrico/zoom-minus-mini.png';

		getobj('OpenLayers.Control.PanZoomBar_4').style.visibility = 'visible';
	}	
}

/* ----------------------------------- PNG FIX --------------------------------- */

// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/



var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = (xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = (yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = xPos + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}

