/**
 * EventLookupUSA
 *
 * By Kramer of SolutionSet.com
 * 070209
 *
 * See LookupBase documentation for more detail.
 *
 **/
var EventLookupUSA = LookupBase.extend({
	// Static properties

	settings: function(settings) {
		if (typeof(this.s) == 'undefined') {
			this.base();

			Object.extendProperties(this.s, {
				eventAttributes: [
					'id',
					'name',
					'type',
					'group_name',
					'start_date',
					'venue_city',
					'venue_state_region',
					'venue_country',
					'venue_postal_code',
					'detail-link',
					'distance'
				]
			});
			// Import cookie values
			Object.extendProperties(this.URLTokens, {
				//cookie_zip: getCookie('ADSKVISZIP'),
				//cookie_industry: getCookie('ADSKVISMKT')
				//cookie_country: getCookie('ADSKVISCNTRY')
			});
			if (this.URLTokens.cookie_zip) {
				this.s.formElementValues.zip = this.URLTokens.cookie_zip;
			}
			if (this.URLTokens.cookie_industry) {
				//this.s.formElementVisibility.industry = false;
				//this.s.formElementValues.industry = this.URLTokens.cookie_industry;
			}
			Object.extendProperties(this.s, settings);
		}
	},

	constructor: function(settings) {
		this.settings(settings);
		this.base();
	},

	ajaxIn: function(req) {
		Element.removeClassName(this.s.n.container, 'loading');
		this.base(req);
		// Process incoming XML
		var xml = req.responseXML;
		var events = xml.getElementsByTagName('event');
		this.displayItems.clear();
		for (var y=0; y<events.length&&y<this.displayItemsQtyLimit; y++) {
			var event = {};
			
			for (var x=0; x<this.s.eventAttributes.length; x++) {
				var nodes = events[y].getElementsByTagName(this.s.eventAttributes[x]);
				
				// switch added to override the city with the event type, when city is flase
				if(this.s.eventAttributes[x] == 'venue_city' && !(this.DOM_getElementText(nodes[0]))){
					event['venue_city'] = event['type'];
				} else{
					event[this.s.eventAttributes[x]] = (nodes.length) ? this.DOM_getElementText(nodes[0]) : '';
				}
			}
			event.isTooFarAway = (parseInt(event['distance']) > parseInt($F(this.s.n.form.elements['miles'])));
			if (event.isTooFarAway) break; // This works on the assumption that the returned list is sorted in ascending order of "miles away from given ZIP code." Events stop being processed when one is found outside the given radius. (Hence "break")
			this.displayItems.push(event);
		}
		this.displayItemsQtyOverflow((events.length > this.displayItemsQtyLimit && !event.isTooFarAway));
		this.display();
		
	},

	DOM_getElementText: function(el) {
		// Seeks out the first text element node contained in el and returns it
		if (typeof(el) != 'undefined' && typeof(el.childNodes) != 'undefined') {
			var nodeTypes = [3,4];                                         // Node types to check for data
			var nodes = {};
			for (var x=0; x<nodeTypes.length; x++) {
				nodes[nodeTypes[x]] = [];
			}
			for (var x=0; x<el.childNodes.length; x++) {
				if (nodeTypes.inArray(el.childNodes[x].nodeType) != -1) {
					nodes[el.childNodes[x].nodeType].push(el.childNodes[x]);
				}
			}
			if (nodes[4].length > 0) return nodes[4][0].nodeValue;
			if (nodes[3].length > 0) return nodes[3][0].nodeValue;
		}
		return false;
	},
	
	ajaxOut: function(args) {
		if(validateZIP(document.getElementsBySelector('#events form')[0].zip.value)){
			adskLinkClick("right sash","event search");
			Element.addClassName(this.s.n.container, 'loading');
			new Ajax.Request(this.s.ajaxUrl+'?'+args, {                      // AJAX out to the server using the given args
				method: 'get',
				//parameters: args,
				onSuccess: this.ajaxIn.bind(this)
			});
		}
	}
});
