﻿
$(function() //initialize
{
	//~~~~~~~~|	load first content container, first selector
	eval($(".scg_content:eq(0) select:eq(0)").attr("reload"));
});

function DoSCGTab(id)
{
	//~~~~~~~~|	manage classes
	$(".scg_tab,.scg_content").removeClass('selected');
	$(String.Format("#scg_tab_{0},#scg_content_{0}", id)).addClass('selected');
	
	//~~~~~~~~|	reset all combos to empty
	//~~~~~~~~|	introduce default option
	$("#scg_content_" + id + " select").each(function()
	{
		ResetSelect($(this));
	});
	
	//~~~~~~~~|	load first combo
	eval($("#scg_content_" + id + " select:eq(0)").attr("reload"));
}

function ResetSelect(Obj)
{
	Obj.removeClass("empty").addClass("empty");
	var Ops = Obj[0].options;
	Ops.length = 0;
	Ops[0] = new Option(Obj.attr('default'),'');
}


function LoadSCG(source_seo, source_query, tab_id, target_id, select_id, position)
{
	//alert(position);
	// reset all selects above position
	$("#scg_content_" + tab_id + " select").each(function(index)
	{
		if (index >= position) // position xslt is 1 based, not zero based
		{
			ResetSelect($(this));
		}
	});
	
	
	
	var channel = "";// TODO: Option - custom !!!! - don't change ID of input !!!!
	if ( $("#channel_sales").attr("checked") == true )
	{
	    channel = "sales";
	}
	else
	{
	    channel = "rentals";
	}
	
	var new_target_id = "#scg_combo_" + tab_id + "_" + target_id;
	
	// load information into search
	var Ops = $("#scg_combo_" + tab_id + "_" + select_id)[0].options;
	if (Ops.selectedIndex > 0)
	{
		var name = Ops[Ops.selectedIndex].text;
			name = trim(name.replace(/\([0-9,]+\)/,''));
		$("#Term").val(name);
		$("#LocationSuggested").val(source_seo);
	}
	//alert(source_seo + "::" + source_query + "::" + id);
	
	if (source_seo=='' || target_id=='') return;
	
	var JsonURL = String.Format("/reno/search/service.aspx?Channel={0}&SearchType={1}&ReturnType=json&MaxReturns=350&Term={2}",
			channel, source_query, source_seo);
			
	//window.location.href=JsonURL;
	
	$.ajax({
          type: "GET",
          contentType: "application/json; charset=utf-8",
          url: JsonURL,
          data: "{}",
          dataType: "jsonp",
          error: function(XMLHttpRequest, textStatus, errorThrown)
          {
            $.log("Json Error Message: error(" + errorThrown + ") \"" + textStatus + "\""); 
          },
          success: function(response) 
          {
              //````````````````````````````````````````````````````````````````
              // Insert the returned HTML into the <div>.
              // This method is called ASYNCHRONOUSLY
              // so it is not aware of variables in it's scope.
              //````````````````````````````````````````````````````````````````                   
              var thishtml = "";
              
			if (response.group && 
				response.group.length>0 && 
				response.group[0].option &&
				response.group[0].option.length > 0)
				{
					ResetSelect($(new_target_id));
					for(var x=0;x<response.group[0].option.length;x++)
					{
						var obj = response.group[0].option[x];
						var Ops = $(new_target_id)[0].options;
						if (obj.title != null &&
							obj.title != '' &&
							obj.valueseo != null &&
							obj.valueseo != '' &&
							obj.count != null &&
							obj.count != '' &&
							!isNaN(obj.count) &&
							obj.count > 0)
							{
								//Ops[Ops.length] = new Option(String.Format("{0} ({1})", obj.title, FormatNumber(obj.count)), obj.valueseo);
								Ops[Ops.length] = new Option(obj.title, obj.valueseo);
							}
					}
					$(new_target_id).removeClass("empty");
				}
			}
        });
	
}


