// JavaScript Document

////////////////////////////////////////////////////////
// DPI Custom function

                
function dpi_ImageGalleryControl() {
    this.urls   = new Array();
    this.imgids = new Array();
    this.controls = new Array();
    this.imgshift = 0;
}

dpi_ImageGalleryControl.prototype.addImageId = function(imageId, ix) {
    if(! ix ) { ix = this.imgids.length; }
    
    this.imgids[ix] = imageId;
}

dpi_ImageGalleryControl.prototype.addImageUrl = function(imageUrl, ix) {
    if(! ix ) { ix = this.urls.length; }
    
    this.urls[ix] = imageUrl;
}

dpi_ImageGalleryControl.prototype.addControl = function(controlName) {
    this.controls[this.controls.length] = controlName;
}


dpi_ImageGalleryControl.prototype.selectControl = function(aControlName) {
    
    for(var cix = 0 ; cix < this.controls.length; cix++) {
	cname = this.controls[cix];
	ctl = document.getElementById(cname);
	if(ctl) {
	    ctl.style.color = "blue";
	    ctl.style.textDecoration = "underline";
	
	    if(cname == aControlName) {
		ctl.style.color = "black";
		ctl.style.textDecoration = "none";
	    }
	}//endif valid ctl
    }

}

dpi_ImageGalleryControl.prototype.shiftImgs = function(aShift, anOptionalControlName) {
    this.imgshift = aShift;

    for(var i = 0; i < this.imgids.length; i++) {
	var urlix = (i + this.imgshift) % this.urls.length;
	document.images[this.imgids[i]].src = this.urls[urlix];
	//	alert("Shifted " + this.urls[urlix] + "["+ urlix +"] to image " + this.imgids[i] + "["+ i + "]")
    }

    if(anOptionalControlName) this.selectControl(anOptionalControlName);

    return false;
}

dpi_ImageGalleryControl.prototype.shiftRelative = function(anAmount) {
    return this.shiftImgs(this.imgshift + anAmount);
}

dpi_ImageGalleryControl.prototype.loadKnownUrls = function() {
    this.preloads = new Array();
    
    for(var i = 0; i < this.urls.length; i++) {
	this.preloads[i] = new Image();
	this.preloads[i].src = this.urls[i];
    }
}

///////////////////////////////////////////////////////
//
// Agent directory support code

// cdh 12/12/06: added office code, but at the end so that it can
//  be optional
function getMyListings(formname, agentcode, firmcode, officecode ) {
	// requires a form someplace on the page with the appropriate
	// fields, etc.
	var searchform = document.forms[formname];
	searchform.agentcode.value = agentcode;

	// the officecode data used to be pretty bad, but it looks fairly
        // solid now, and the fact that MLS is duplicating agent codes between
        // offices means we pretty much have to start using office codes.
        
	if(officecode) {
          searchform.officecode.value = officecode;
	}

	searchform.firmcode.value = firmcode;

	searchform.submit();
}

////////////////////////////////////////////////////////
// Utility references

function GetHeightWidth(height, width){
    
    var xposition=0, yposition=0, text;

    xposition = (screen.width - width) / 2;
    yposition = (screen.height - height) / 2;
    
    text = "height=" + height + ",width=" + width + ",screenx=" + xposition + ",screeny=" + yposition + ",left=" + xposition + ",top=" + yposition;
                    
    return(text);
}
                

function stdpop(title, width, height, url) {
    var dim = GetHeightWidth(height, width);
    var props = dim + ',location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,hotkeys=0';
    window.open(url, title, props);
}

function mortgagecalc(price) {
    var url = '/dyn/propertysearch/mortcalc.php?price=' + price;
    stdpop('Calculator', 350, 395, url);
}

function emaillisting(mlsid) {
    var url = '/dyn/propertysearch/maillisting.php?id=' + mlsid;
    stdpop('Email', 400, 370, url);
}

///////////////////////////////////////////////
// Loading stuff

document._dpi_dynamicLoadQueue = new Array();


function dpi_onLoad() {
    // Please add onLoad code to this
    // function, rather than directly
    // to the onLoad() handler in the body tag

    // (or, if you wnat to be really cool,
    // use the dynamic loader where you are,
    // and keep your damn code out of this header :)
    
    dyn = document._dpi_dynamicLoadQueue;
    for(var di = 0; di < dyn.length; di++) {
       dyn[di]();
    }
}


///////////////////////////////////////////////////
// Public names

function dpi_AddDynamicLoader(aFn) {
    q = document._dpi_dynamicLoadQueue;
    q[q.length] = aFn;
}



document.dpi_detailGallery = new dpi_ImageGalleryControl();

dpi_AddDynamicLoader(function () { document.dpi_detailGallery.loadKnownUrls() });

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function createObjectCallback(obj, fn)
{
  return function() { fn.apply(obj, arguments); };
} 

/* AJAX stuff */
/* credit to Raj Shekhar for the basics of the AJAX code below
*/
function xml_req() {
   if(navigator.appName == "Microsoft Internet Explorer"){
       var http = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
       var http = new XMLHttpRequest();
   }
   return http;
}

function replace_innerHTML_handleResponse () {
  if( this.http && this.http.readyState == 4 ) {
    if ( this.http.status == 200 ) {
      this.innerHTML = this.http.responseText;
    } else {
      alert('ERROR: status code is ' + this.http.status );
    }
  }
}


// XmlHttpReq call to replace a tag's content with the result of an HTTP
// request
function replace_innerHTML(elt,url) {
  //alert("Call to replace text on " + elt.id);
  elt.http = xml_req();
  
 
  elt.http.open('get',url);
  elt.http.onreadystatechange = createObjectCallback(elt,replace_innerHTML_handleResponse);
  elt.http.send(null);
}

function repeat(thunk,delay) {
  var obj = new Object();
  obj.repeat = function () {
    thunk();
    setTimeout(createObjectCallback(obj,obj.repeat),delay*1000);
  }
  obj.repeat();
}

function insert_option_sorted(dst,opt) {
  for (var i=0;i<dst.options.length;i++) {
    if ( dst.options[i].id > opt.id ) {
      dst.insertBefore(opt,dst.options[i]);
      return;
    }
  }
  dst.appendChild(opt);
}

function move_selected(src_id,dst_id,callback) {
  var src = document.getElementById(src_id);
  var dst = document.getElementById(dst_id);

  //  alert("selectedIndex is " + src.selectedIndex );
  
  /* at least in firefox, we can get multiple selections by removing
     one at a time and FF will update selectedIndex to the next of a
     multiple selection */
  while ( src.selectedIndex >= 0 ) {
    var opt = src.options[src.selectedIndex];
    opt.parentNode.removeChild(opt);
    insert_option_sorted(dst,opt);
    if ( callback!=null ) {
       callback(opt.value);
    }
  }
}

function select_all(sel_id) {
  var sel = document.getElementById(sel_id);
  for(var i=0;i<sel.options.length;i++) {
    sel.options[i].selected = true;
  }
}

function check_times(start,end,msg) {
 var sel_start = $(start);
 var sel_end = $(end);

 
 if ( sel_start.options[sel_start.selectedIndex].value > sel_end.options[sel_end.selectedIndex].value ) {
   $(msg).show();
 } else {
   $(msg).hide();
 }
}

function updateCellEmail(obj,method) {
  var field = obj + '_' + method;
  var sel = $( field + '_provider');
  
  $(field).value = $(field + '_digits').value + sel.options[sel.selectedIndex].value;
}

function disable_children(container,disabled) {
  var clist = $A(container.childNodes);

  clist.each(function (child) {
    child.disabled=disabled;
     });
}

function limit_length(elt,max_len,status_id) {
  var remaining = max_len - elt.value.length;
  if ( remaining < 0 ) {
    $(status_id).style.color = "red";
    $(status_id).innerHTML = "Maximum length exceeded by " + (0-remaining) + " characters";
  } else {
    $(status_id).style.color = "black";
    $(status_id).innerHTML = remaining + " characters remaining";
  }
}

function select_radio_button(elem) {
  var radio = document.getElementById(elem);
  radio.checked = true;
}
