(function($){
  
  var site = "abcenter.no";
  var tempext = "";
  var xmlfilename = "searchdata.snippet.xml";
  var xmlurl = "/site/" +  site + "/design/snippets/" + xmlfilename;


  $(function(){ $("body").finnfilter(); });

  $.fn.finnfilter = function(opts)
  {
  	$this = $(this);
    var	options = {
      selType:      { id: "ad_type", defaultValue: "realestate/common"},
      selLocation:  { id: "location", defaultValue: "20011"},
      selArea :     { id: "areaId", defaultValue: "0"},
      selPlace:     { id: "place", defaultValue: "alle"}
    }
   
    $.extend(options, opts);
    
    _init();
    

    
    function _init()
    {
      var $type = $("#" + options.selType.id);
    	var	$location = $("#" + options.selLocation.id);
      var	$area = $("#" + options.selArea.id);
        
      $type.initType(options);      
      $location.initLocation(options);
      $area.initArea(options);
      
      //$type.change(function(){ $(this).initType(options); });      
      $location.change(function(){ $area.initArea(options); });
      //$area.change(function(){ $(this).initArea(options); });

      
      //initPlace(options);    
    };
  }
  
	// Henter xml-data
	function _getXmlData()
	{
		var retstr = "";
		$.ajax
		({
				url: xmlurl,
				context: $this,
				dataType: "xml",
				async:false,
				success: function(xml)
				{
					retstr = xml;
				},
				statusCode: { 404: function() {}}
		});
		
		return retstr;
	}	  
  

  $.fn.initType = function(opts) {
    var xmldata = _getXmlData();
    $this = $(this);
    
		// Tømmer lista
		$this.find("option").remove().end();
  				
		// Leser xml og fyller lista på nytt
		$(xmldata).find("type").each(function(ix){ $(this).addOptions($this); });

    return $(this);
  }

/* LOCATION */
  
  $.fn.initLocation = function(opts){
    var xmldata = _getXmlData();
    $this = $(this);
    
		// Tømmer lista
		$this.find("option").remove().end();
  				
		// Leser xml og fyller lista på nytt
		$(xmldata).find("fylke").each(function(ix){ $(this).addOptions($this); });
    
   return $(this); 
  }
  
  
  
  $.fn.initArea = function(opts)
  {   
    var xmldata = _getXmlData();
    $this = $(this);
    
		// Tømmer lista
		$this.find("option").remove().end();
  						
    $currentlocation = $("#" + opts.selLocation.id + " option:selected").first();
    var $parent = $(xmldata).find("fylke[id=" + $currentlocation.val() + "]");
		
    // Leser xml og fyller lista på nytt
		$parent.find("kommune").each(function(ix){ $(this).addOptions($this); });
    
    return $(this); 
  }
  
  
  //$.fn.initPlace() = function(opts){ return $(this);

  
  $.fn.addOptions = function(parent)
  {
			$parent = parent;
      var cName = $(this).attr("name");
			var cId = $(this).attr("id");
			
			var qsLocation  = getParameterByName("loc");
			var qsArea  = getParameterByName("omraade");
			var qsType  = getParameterByName("type");

			var isSelected = ((qsLocation == cId || qsLocation == cName) || (qsArea == cId || qsArea == cName) || (qsType == cId || qsType == cName)) ? true : false;
			
			// Setter riktig som valgt
			if(isSelected)
			{
				$("<option>").val(cId).text(cName).attr("selected", "selected").appendTo($parent);						
			}
			else
			{
				$("<option>").val(cId).text(cName).appendTo($parent);			
			}
    
  }		
})(jQuery)

