/*
	Written by Bborie Park
	Copyright (c) 2008 - 2010 California Vectorborne Disease Surveillance System
	Licensed under the GNU GPL
*/

var idxColor = 0;
var COLOR = [
	'#800080',
	'#000080',
	'#008000',
	'#ff8800',
	'#ff0000'
];
/*
// Color symbols: ColorBrewer.org
var COLOR = [
	'#A6CEE3',
	'#1F78B4',
	'#B2DF8A',
	'#33A02C',
	'#FB9A99',
	'#E31A1C',
	'#FDBF6F',
	'#FF7F00',
	'#CAB2D6',
	'#6A3D9A',
	'#FFFF99'
];
*/
/*
// Color symbols: ColorBrewer.org
var COLOR = [
	'#E41A1C',
	'#377EB8',
	'#4DAF4A',
	'#984EA3',
	'#FF7F00',
	'#FFFF33',
	'#A65628',
	'#F781BF'
];
*/
/*
// Color symbols: ColorBrewer.org
var COLOR = [
	'#1B9E77',
	'#D95F02',
	'#7570B3',
	'#E7298A',
	'#66A61E',
	'#E6AB02',
	'#A6761D',
	'#666666'
];
*/

getColor = function() {
	return COLOR[(idxColor++) % COLOR.length];
}

function resizeThickbox(buffer) {
	var $tb = $('#TB_window');
	if (!$tb.length) return;
	var $tbC = $tb.children('div:last');

	resizeParent($tbC, buffer);
	TB_HEIGHT = $tbC.outerHeight() + 45;
	tb_position();

	if (!$tb[0].redo) {
		$tb[0].redo = true;
		setTimeout('resizeThickbox(0)', 100);
	}
	else {
		delete $tb[0].redo;
	}
}

function resizeParent($parent, buffer) {
	$parent = ($parent instanceof jQuery) ? $parent : $(parent);
	buffer = (typeof buffer == 'number') ? buffer : 10; 

	var childrenHeight = $.Util.childrenHeight($parent);
	if ((($parent.height() + buffer) < childrenHeight) || ((childrenHeight + buffer) < $parent.height())) {
		$parent.height(childrenHeight + buffer + 'px');
	}
}

function moveOptions(set, d, doSort) {
	var o, i, dsort = [];
	doSort = (typeof doSort == 'undefined') ? true : doSort;

	// move set of sites to destination
	// remove selected flag
	for (var i = set.length - 1; i >= 0; i--) set[i].selected = false;
	$(set).appendTo(d);

	if (!doSort) return;

	// sort destination options
	// only if there are more than one option
	o = d.options;
	if (o.length < 2) return;
	for (i = 0; i < o.length; i++)
		dsort.push([o[i].text, o[i].value]);

	dsort.sort(sortOptions);

	// this is a fix around IE
	setTimeout(function() {
		for (i = 0; i < dsort.length; i++) {
			o[i].value = dsort[i][1];
			o[i].text = dsort[i][0];
		}
		dsort = null;
	}, 0);
}

function sortOptions(a, b) {
	var x = a[0], y = b[0];
	if (isNaN(x) || isNaN(y))
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	else {
		x = x * 1;
		y = y * 1;
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}
}

// move options around
function swapOptions(e, i, j) {
	var tmp;
	var keys = ['value', 'text', 'selected'];

	for (var x in keys) {
		// save the option that is getting swapped
		tmp = e.options[j][keys[x]];
		// overwrite the swapped option
		e.options[j][keys[x]] = e.options[i][keys[x]];
		// restore swapped option into new place
		e.options[i][keys[x]] = tmp;
	}
}

// move option in list by the increment
// up => -1
// down => 1
function shiftOptions(e, by) {
	// do nothing if either only 1 option or no option
	if (e.options.length < 2) return;

	var i, j;
	// go down, positive increment value
	if (by > 0) {
		for (i = (e.options.length - 1); i >= 0; i--) {
			j = i + by;
			// option is selected
			if (e.options[i].selected && (j < e.options.length)) {
				swapOptions(e, i, j);
			}
		}
	}
	// go up, negative increment value
	else {
		for (i = 0; i < e.options.length; i++) {
			j = i + by;
			// option is selected
			if (e.options[i].selected && (j >= 0)) {
				swapOptions(e, i, j);
			}
		}
	}
}

addLoading = function(id, style, append) {
	id = id || 'loading';
	style = style || 'vertical-align: middle; margin-left: 5px;';
	append = (typeof append == 'undefined') ? false : append

	var html = '<img id="' + id + '" src="' + APP_BASE_PATH + 'image/loading-small.gif" alt="" style="' + style + '" class="loading-image"></img>';
	if ($(this).parent().hasClass('button') || append)
		$(this).append(html);
	else
		$(this).after(html);
}

removeLoading = function(id) {
	id = id || 'loading';
	$('#' + id).remove();
}

openWindow = function(href) {
	if (typeof href != 'string' || !href.length) return false;

	var name = $.Util.urlValue('name', href);
	var features = {
		height: $.Util.urlValue('height', href) || false,
		width: $.Util.urlValue('width', href) || false,
		location: $.Util.urlValue('location', href) ? true : false,
		menubar: $.Util.urlValue('menubar', href) ? true : false,
		resizable: $.Util.urlValue('resizable', href) ? true : false,
		scrollbars: $.Util.urlValue('scrollbars', href) ? true : false,
		status: $.Util.urlValue('status', href) ? true : false,
		toolbar: $.Util.urlValue('toolbar', href) ? true : false
	};
	var replace = false;

	var tmp = [];
	$.each(features, function(k, v) {
		if (v === false) return;

		if (v === true)
			tmp.push(k);
		else
			tmp.push(k + '=' + v);
	});
	features = tmp.join(',');

	window.open(
		href,
		name,
		features,
		replace
	);

	return true;
}

searchSelect = function(evt) {
	var i, e, $e, $q, q, slt, re, filtered = [], match = [], $msg, o, tmp;
	e = this;
	$e = $(e);
	$q = $e.parent().find('.filter_text:first');
	q = $q.val();
	slt = evt.data.slt;
	if (!$e.parent().find('span.search_message').length)
		$msg = $('<span class="search_message"></span>').appendTo($e.parent());
	else
		$msg = $e.parent().find('span.search_message:first');

	if (!q.length) {
		clearSelect.call(e, evt);
		return;
	}
	else if ($.browser.msie) {
		clearSelect.call(e, evt, q);
	}

	re = new RegExp(q, 'gi');
	for (i = 0; i < slt.options.length; i++) {
		if (slt.options[i].text.search(re) != -1)
			match.push(slt.options[i]);
		else
			filtered.push(slt.options[i]);
	}

	// matches found
	if (match.length) {
		if ($.browser.mozilla) {
			$(filtered).hide().is(':hidden');
			$(match).show();
		}
		else {
			for (i = 0; i < filtered.length; i++) {
				o = {
					'text': filtered[i].text,
					'value': filtered[i].value
				};
				tmp = document.createComment($.toJSON(o));
				slt.replaceChild(tmp, filtered[i]);
			}
		}

		$msg.text(match.length + ' matches');
	}
	// no matches
	else {
		if ($.browser.mozilla) {
			$(slt.options).show();
		}
		else {
			filtered = [];
			for (i = slt.childNodes.length - 1; i >= 0; i--) {
				if (slt.childNodes[i].nodeType != 8) continue;
				filtered.push(slt.childNodes[i]);
			}

			for (i = 0; i < filtered.length; i++) {
				tmp = $.evalJSON(filtered[i].nodeValue);
				o = document.createElement('option');
				o.innerHTML = tmp.text;
				o.value = tmp.value;
				slt.replaceChild(o, filtered[i]);
			}
		}

		$msg.text('0 matches');
	}
}

clearSelect = function(evt, filter) {
	var slt, $p = $(this).parent();
	filter = (filter == null || filter == undefined) ? '' : filter
	$p.find('.filter_text:first').val(filter);
	slt = evt.data.slt;
	$('option', slt).show();

	if (!$.browser.mozilla) {
		var filtered = [], tmp, o;
		for (i = slt.childNodes.length - 1; i >= 0; i--) {
			if (slt.childNodes[i].nodeType != 8) continue;
			filtered.push(slt.childNodes[i]);
		}

		for (i = 0; i < filtered.length; i++) {
			tmp = $.evalJSON(filtered[i].nodeValue);
			o = document.createElement('option');
			o.innerHTML = tmp.text;
			o.value = tmp.value;
			slt.replaceChild(o, filtered[i]);
		}
	}

	$p.find('span.search_message:first').empty();
}

