if (typeof PD == 'undefined') { PD = {} };
if (typeof PD.DFW == 'undefined') { PD.DFW = {} };

PD.DFW.viewAll = function(what) {
	// capture the existing URL params
	params = $H(window.location.search.parseQuery());
	params.unset("address");
	params.unset("aff");
	params.unset("key");
	params.unset("sort");
	params.unset("test");
	params.unset("distance");
	params.unset("viewAll");
	params.unset("sNum");
	params = params.merge({ viewAll: what });
	// create the container, or use the existing one if it's already there
	div = $('view-all-container') || new Element('div', { id: "view-all-container" });
	div.addClassName(what);
	// showing in case we're recycling the previously hidden div
	div.show();
	$('browse-by-category').insert(div);
	new Ajax.Updater(div, '/dfw/modules/listings/view_all_widget.jsp', { parameters: params });
}

PD.DFW.closeViewAll = function() {
	$('view-all-container').hide();
	$('view-all-container').removeClassName("neighborhoods");
	$('view-all-container').removeClassName("cuisines");
}

// Accepts a DOM object and an optional array of class names as required elements to appear within the DOM object.  Will iterate through each 'var' element within it and capture their class names and title values as key/value pairs.  If any of the class names marked required aren't present, opens an alert with the list of missing class names.
// Usage examples: 
//     var pluckdata = EmbeddedData($('pluck'));
//     var pin = EmbeddedData($$$('.map-pin'), ['name', 'address', 'lat', 'long']);
// Once created, includes the Enumerable functions and the get(key) and set(key, value) functions from Prototype's Hash implementation.
//     PDPluckComments(pluckdata.get('pluck-id'));
// todo: allow multiple class names in an 'var' element and capture each as a key with the same value
EmbeddedData = Class.create({
  initialize: function(o, requiredFields) {
    this.container = o;
    this.properties = new Hash();
    this.errors = new Array();

    if (requiredFields && requiredFields.size() > 0) {
      this._checkFor(requiredFields, this.container);
    };
    
    // If the object has no errors, pull in the values of the child 'var' elements, otherwise display the errors. 
    if (this.errors.size() == 0) {
      this._pullInValues();
    } else {
      this._displayErrors();
    };
  },
  
  // Accepts an array of class names and the object to look within for var elements with those class names.  If any of the class names do not return an element, add the class name to the errors array.
  _checkFor: function(requiredFields, o) {
    // using a placeholder array, as we don't have access to this.errors from within the Enumerable.each()
    var errors = new Array();
    $A(requiredFields).each(function(e) {
      if (!o.down("var.#{className}".interpolate({ className: e}))) {
        errors.push(e);
      };
    });
    this.errors = errors;
  },
  
  // Looks within this.container for each 'var' element, and extracts the class names and title attributes as key/value pairs
  _pullInValues: function() {
    // using a placeholder hash, as we don't have access to this.properties from within the Enumerable.each()
    var properties = new Hash();
    this.container.select('var').each(function(e) {
      var key = $A(e.classNames()).first();
      var value = e.title;
      properties.set(key, value);
    });
    this.properties = properties;
  },
  
  // Display the errors
  _displayErrors: function() {
    alert("The following required fields are missing: #{fields}".interpolate({ fields: this.errors.join(', ')}));
  },
  
  // Implementing _each and extending with Enumerable grants the object all the properties of Prototype Enumerable objects, like each, inspect, et al: Object.extend(YourObject.prototype, Enumerable);
  _each: function(iterator) {
    this.properties.each(iterator);
  },
  
  set: function(key, value) {
    return this.properites.set(key, value);
  },
  
  get: function(key) {
    return this.properties.get(key);
  }
});

Object.extend(EmbeddedData.prototype, Enumerable);

// Collect the map pins embedded in HTML code
function  processEmbeddedPins () {
  $$('.map-pin').each(function(e) {
    var data = new EmbeddedData(e, ['pin-title', 'pin-address', 'pin-lat', 'pin-long', 'pin-image', 'pin-icon','pin-phone', 'pin-resultnum']);
    addPin(
      data.get('pin-title'),
      data.get('pin-address')+', '+data.get('pin-city') + ', '+ data.get('pin-state') + ' ' + data.get('pin-zip'),
      data.get('pin-lat'),
      data.get('pin-long'),
      data.get('pin-image'),
	  data.get('pin-icon'),
      data.get('pin-phone'),
      data.get('pin-resultnum'),
	  data.get('pin-siteid')  
    );
  });
}

function  processTheaterEmbeddedPins () {
	  $$('.show_theater').each(function(e) {
		  if(e.down('.map-pin')){
			    var data = new EmbeddedData(e.down('.map-pin'),['pin-title']);
			    addPin(
			      data.get('pin-title'),
			      data.get('pin-address')+', '+data.get('pin-city') + ', '+ data.get('pin-state') + ' ' + data.get('pin-zip'),
			      data.get('pin-lat'),
			      data.get('pin-long'),
			      data.get('pin-image'),
				  data.get('pin-type'),
			      data.get('pin-phone'),
			      data.get('pin-resultnum'),
				  data.get('pin-siteid')  
			    );
		  }
	  });
	}


//handles the Results Per page dropdowns on several of the search pages -nab
function changeNumberResults()
{
	var numResults = document.getElementById("select_results").value;
	var redirectUrl = "/sp?";
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		//alert(vars[i]);
		redirectUrl = redirectUrl + vars[i];
		if(!(i==vars.length-1)) {
			redirectUrl = redirectUrl + "&";
		 }
 	}
 	redirectUrl = redirectUrl +"&select_results="+numResults
 	//alert(redirectUrl);
	window.location = redirectUrl;
}

function validateZip (zip){
	if(zip.value=""){
		alert("Please enter a zip code!")
	}
}

function selectMovie(){
	var myForm =$('movies_filter');
	if(myForm.select_movie.value != ""){
		window.location = "/sp?aff=10011&movieId=" + myForm.select_movie.value;
		return false;
	}						
}

function selectTheater(){
	var myForm =$('movies_filter');
	if(myForm.select_theater.value != ""){
		window.location = "/sp?aff=10012&theaterId=" + myForm.select_theater.value;
		return false;
	}								
}

function selectNeighborhood(){
	var myForm =$('movies_filter');
	if(myForm.select_neighborhood.value != ""){
		window.location = "/sp?aff=10018&neighborhood=" + myForm.select_neighborhood.value;
		return false;
	}								
}

