// JavaScript Document

if(typeof logicbox == "undefined") var logicbox = {};

(function($) {
		  
	logicbox.newWin = function() {
		var w = window.open($(this).attr('href'),'_blank');
		w.focus();
		return false;
	}
 
	$(function() {
			   
		var countriesDB, citiesDB;
		
		//load country and city datasets
		$.getJSON('/ajax/getlocationdata',function (json) {
			countriesDB = new TAFFY(json[0]);
			citiesDB = new TAFFY(json[1]);
		});
		
		var updateSelect = function(name,obj) {
			
			var selectName = 'select[name=' + name + ']';
			var defaultVal = $(selectName + ' :first-child').html();
			$(selectName).empty().addOption('',defaultVal,false);
			
			$.each(obj, function(i,row) {
				$(selectName).addOption(row.idx,row.name,false);
			});
			
		
		};
		
		$('select[name=region]').change(function() {
			var regionId = $(this).val();
			updateSelect('country',countriesDB.get({region_id:regionId}));
			updateSelect('city',citiesDB.get({region_id:regionId}));
		});
		
		$('select[name=country]').change(function() {
			var countryId = $(this).val();
			updateSelect('city',citiesDB.get({country_id:countryId}));
		});
		
		$("a[rel=external],.newWin").click(logicbox.newWin);
		
	});

/**
 * Adds (single/multiple) options to a select box (or series of select boxes)
 *
 * @name     addOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @example  $("#myselect").addOption("Value", "Text"); // add single value (will be selected)
 * @example  $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
 * @example  $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
 *
 */
$.fn.addOption = function()
{
	var add = function(el, v, t, sO)
	{
		var option = document.createElement("option");
		option.value = v, option.text = t;
		// get options
		var o = el.options;
		// get number of options
		var oL = o.length;
		if(!el.cache)
		{
			el.cache = {};
			// loop through existing options, adding to cache
			for(var i = 0; i < oL; i++)
			{
				el.cache[o[i].value] = i;
			}
		}
		// add to cache if it isn't already
		if(typeof el.cache[v] == "undefined") el.cache[v] = oL;
		el.options[el.cache[v]] = option;
		if(sO)
		{
			option.selected = true;
		}
	};
	
	var a = arguments;
	if(a.length == 0) return this;
	// select option when added? default is true
	var sO = true;
	// multiple items
	var m = false;
	// other variables
	var items, v, t;
	if(typeof(a[0]) == "object")
	{
		m = true;
		items = a[0];
	}
	if(a.length >= 2)
	{
		if(typeof(a[1]) == "boolean") sO = a[1];
		else if(typeof(a[2]) == "boolean") sO = a[2];
		if(!m)
		{
			v = a[0];
			t = a[1];
		}
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			if(m)
			{
				for(var item in items)
				{
					add(this, item, items[item], sO);
				}
			}
			else
			{
				add(this, v, t, sO);
			}
		}
	);
	return this;
};

})(jQuery);