// check browsers
var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var webkit = /webkit/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
var ie6 = ie && /MSIE [6789]/.test(ua);
var ie7 = ie && /MSIE [789]/.test(ua);
var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
var moz = !opera && !webkit && /gecko/i.test(ua);
var nn6 = !opera && /netscape.*6\./i.test(ua);

var divAnimate = null;
var xh = 10;
var xw = 10;	
var yw = 550;
var yh = 400;
var right = 700;
var animateleft = false;
var top = 100;
var step = 15;

function Point(x, y) {
    this.x = x;
    this.y = y;
}


/* ANIMATION */

_aniwidth = 
_anistart = null;
_anistop = null;

function Animation() {
    //-- Private
    this._xh = 10;
    this._xw = 10;
    this._yh = 550;
    this._yw = 400;
    this._start = new Point(0,0);
    this._stop = new Point(0,0);
    this._div = null;
    this._animate = function () { 
	    this._xw += (this._yw / step);
    	this._xh += (this._yh / step);
        this._div.style.left = this._start.x + ((this._stop.x - this._start.x) * (this._xw / this._yw)) + 'px';
	    this._div.style.top = this._start.y + ((this._stop.y - this._start.y) * (this._xh / this._yh)) + 'px';
	    this._div.style.width = this._xw + 'px';
	    this._div.style.height = this._xh + 'px';
	    if (this._xw < this._yw)
		    window.setTimeout('Animator._animate()', this.speed);
    }    
    //-- Public
    this.step = 10;
    this.speed = 5;
    //-- Animation Effect
    this.aeScreenCenter = 0;this.aeRight = 1;this.aeLeft=2;
    this.effect = this.aeScreenCenter;
    this.Animate = function (div, width, height, start, stop) {
        this._start = (start?start:new Point((document.body.clientWidth / 2),(document.body.clientHeight / 2)));
        if (stop) {
            this._stop = stop;
        } else {
            switch (this.effect) {
                case this.aeRight:
                        this._stop = new Point(this._start.x, this._start.y  - (height / 2));
                        break;  
                case this.aeLeft:
                        this._stop = new Point(this._start.x - width, this._start.y  - (height / 2));
                        break;                
                default: // this.aeScreenCenter                        
                        this._stop = new Point((document.body.clientWidth / 2) - (width / 2), (document.body.clientHeight / 2) - (height / 2));                        
                        break;
            }
        }
        
        if (this._div) {
            this._div.style.display = 'none';
        }
        
        
	    this._div = div;
	    this._xw = 10;
	    this._xh = 10;
	    this._yw = width;
	    this._yh = height;
	    	    
	    div.style.display = 'block';        
        Animator._animate();
    }
}
var Animator = new Animation();
/* END OF ANIMATE */

function loadfloorplanex(obj, data) {
	document.getElementById('fpFlashCtrls').style.display = 'none';
	var flashdiv = document.getElementById('fpFlash');
    flashdiv.style.overflow = 'hidden';
	var image = null;
	eval(data);
	if (image) {
		flashdiv.innerHTML = '<img src="' + image + '"/>';
		flashdiv.style.overflow = 'hidden';
	}
}

function screenXtoclientX(ele, x) {
	var scrx = 0;
	ele = ele.offsetParent;
	while (ele) {
		scrx += ele.offsetLeft;
		ele = ele.offsetParent;
	}
	return (x - scrx) - (window.screenLeft);
}

function screenYtoclientY(ele, y) {
	var scry = 0;
	ele = ele.offsetParent;
	while (ele) {
		scry += ele.offsetTop;
		ele = ele.offsetParent;
	}
	return (y - scry) - (window.screenTop);
}

function showlogin(e, reqfeature) {
    var f =document.getElementById('lblusefeature');
    if (f) 
        f.style.display = (reqfeature?'block':'none');
    return loadDiv(e, 'saveSearchDiv')   
}

function loadDiv(e, div, effect) {
	e = (e?e:window.event);
	var div = document.getElementById(div);
	//-- Current Position of Mouse
	var start = new Point(screenXtoclientX(div, (e.screenX?e.screenX:e.pageX) - 10), screenYtoclientY(div, (e.screenY?e.screenY:e.pageY) - 25));
	
	if (effect)
        Animator.effect = effect;

	Animator.Animate(div, 400, 200, start);

	return false;
}

  function switchImg(obj) {
  if (obj.src.substring(obj.src.length-5, obj.src.length-4).toLowerCase() == 'a')
  obj.src = obj.src.substring(0, obj.src.length-5) + 'o.gif';
  else
  obj.src = obj.src.substring(0, obj.src.length-5) + 'a.gif';
  }

function showfloorplan(e, id, type) {		
	//-- Start Ajax Load of Content
	document.getElementById('fpFlash').innerHTML = '';
	loadpage('/p.dtx?c=fpurl&doctype=false&type=' + (type?type:'fp') + '&propertyid=' + id.substring(1, 37), loadfloorplanex, 'GET');

    
	e = (e?e:window.event);	
	//-- Current Position of Mouse
	var start = new Point(screenXtoclientX(document.getElementById('divFP'), (e.screenX?e.screenX:e.pageX) - 10), screenYtoclientY(document.getElementById('divFP'), (e.screenY?e.screenY:e.pageY) - 25));
	Animator.Animate(document.getElementById('divFP'), 550, 420, start);
	//animatediv(document.getElementById('divFP'), 550, 420);
}

function hidefloorplan() {
	document.getElementById('divFP').style.display = 'none';
}

var partaddr = '';
function docompare(address, part) {
	for (x=1; x<part.length; x++) {
		if ((x >= address.length) || (part.charAt(x) != address.charAt(x))) {
			return part.substring(0, x);
		}
	}
	return part;
}

function dolistload(obj) {
	eval(obj.ajax.responseText);
	if (rdata.length == 0) {
		document.getElementById('addrlist').innerHTML = 'No matching properties';
	} else {
		partaddr = rdata[0];
		for (i=1; i<rdata.length; i++) {
			partaddr = docompare(rdata[i], partaddr)
		}	
		var htmltext = '';
		for (i=0; i<rdata.length; i++) {
			htmltext += '<div onclick="performselectitem(event)" onmouseover="setHighLight(this)"><span>' + partaddr + '</span>' + rdata[i].substring(partaddr.length) + '</div>';
		}
		document.getElementById('addrlist').innerHTML = htmltext;
	}
}
					
function performkeydown (ele) {
    if ((event.keyCode||event.which) == 9) {
		ele.value = partaddr;
		return false;
	}
	return true;
}

function performselecitem(item) {
    document.getElementById('street').value = item.innerText;
    document.getElementById('addrlist').style.display = 'none';
}

var t = null;

function performloadcontent(url) {
    loadpage(url, dolistload);
}

function performlist(ele) {
    window.clearTimeout(t);
	if (ele.value.length > 0) {
		document.getElementById('addrlist').style.display = 'block';
		partaddr = ele.value;
		t = window.setTimeout('performloadcontent(\'/p.dtx?c=addrlist&doctype=false&address1=' + ele.value + '\')', 200);
	} else
		document.getElementById('addrlist').style.display = 'none';
}

/*------ Start lookup list component --------*/

function tlookuplist(element, div, url) {
    this.ele = element;
    this.div = div;
    this.url = url;
    this.partaddr = '';
    this.onkeydown = function () {
        if ((event.keyCode||event.which) == 9) {
		    this.ele.value = partaddr;
		    return false;
		}
		return true;
    }     
    this.onkeyup = function () {
        window.clearTimeout(t);
	    if (this.ele.value.length > 0) {
		    document.getElementById('addrlist').style.display = 'block';
		    partaddr = this.ele.value;
		    t = window.setTimeout('performloadcontent(\'' + this.url.replace('%value%', this.ele.value) + '\')', 200);
	    } else
		    document.getElementById('addrlist').style.display = 'none';    
    }  
}

/*------ Start slideshow component --------*/

arr_slideshows = new Array();
function updateslideshow(divid) {
  var ss = null;   
  for (var i = 0; i < arr_slideshows.length; i++) {
    if ((arr_slideshows[i].div)&&(arr_slideshows[i].div.id == divid)) {
      ss = arr_slideshows[i];
    }
  }
  if (ss)
    ss.update();
}
tslideshow = function (divid, url, interval, fade, doload) {
  arr_slideshows[arr_slideshows.length] = this;
  this.divid = divid; 
  this.load = function (url, interval, fade, doload) {
      //alert(url);
      this.div = document.getElementById(divid);
      this.url = url;
      this.interval = interval;
      if (doload) 
        this.update();
      else
        this.start();
      this.fade = fade;
  }
  this.update = function () {
    var xmlhttp = GetDatogXmlHttp();
    if (xmlhttp) {
      xmlhttp.ajax.open('GET', this.url, true);
      xmlhttp.slideshow = this;
      eval('xmlhttp.ajax.onreadystatechange = function () { xmlhttpresponse(' + xmlhttp.ref + '); }');
      if (ie)
        xmlhttp.ajax.send();
      else
        xmlhttp.ajax.send(document);
    }
  }
  this.next = function () {
    window.clearTimeout(this.timer);
    updateslideshow(this.divid);
  }
  this.start = function () {
    if ((this.interval)&&(this.interval > 0))
        this.timer = window.setTimeout('updateslideshow(\'' + this.divid + '\')', this.interval);
    else   
        this.timer = null;
  }
  this.load(url, interval, fade, doload);
}
/*------ End slideshow component --------*/

function xmlhttpresponse(ref) {
    var xmlhttp = xmlhttpstack[ref];
    try {
        if (xmlhttp.ajax.readyState == 4) {
            //alert(xmlhttp.ref + ' - ' + xmlhttp.ajax.status);
            if (xmlhttp.ajax.status == 200) {
                try {
                    if (xmlhttp.slideshow.fade && !moz &&!nn6) xmlhttp.slideshow.div.filters[0].Apply();  
                } catch (e) {
                    // do nothing
                }
                xmlhttp.slideshow.div.innerHTML = xmlhttp.ajax.responseText;
                try {
                    if (xmlhttp.slideshow.fade && !moz &&!nn6) xmlhttp.slideshow.div.filters[0].Play();
                } catch (e) {
                    // do nothing
                }
            }
            xmlhttp.slideshow.start();
            xmlhttpstack[ref] = null;
        }
    } catch (ex) { 
        alert('exception: ' + ex.message);
        xmlhttp.slideshow.timer = null;
    }
  }	

var xmlhttpstack = new Array();

function GetDatogXmlHttp() {
    var xmlhttp = new Object();
    xmlhttp.ajax = GetXmlHttpEx();
    for (var i=0; i<xmlhttpstack.length; i++) {
        if (!xmlhttpstack[i]) {
            xmlhttp.ref = i;
            xmlhttpstack[i] = xmlhttp;
            return xmlhttp;
        }
    }
    xmlhttp.ref = xmlhttpstack.length;
    xmlhttpstack[xmlhttpstack.length] = xmlhttp;
    return xmlhttp;    
}

function GetXmlHttpEx() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest;
    } else if (window.ActiveXObject) {
        var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft XMLHttp"];
        for (var i = 0; i < aVersions.length; i++) {
            try {
                var x = new ActiveXObject(aVersions[i]);
                return x;
            } catch (oError) {
                //Do nothing
            }
        }
    }
    throw new Error("XMLHttp object could not be created");
}	

function loadpage(url, onevent, type, obj) { 
  if (!type) type = 'POST';
  var xmlhttp = GetDatogXmlHttp();
  if (xmlhttp) {
        xmlhttp.ajax.open(type, url + '&_enc=utf8', true);
        xmlhttp.onevent = onevent;
        xmlhttp.obj = obj;
        eval('xmlhttp.ajax.onreadystatechange = function () { loadpageresponse(' + xmlhttp.ref + '); }');
        if (ie)
   	        xmlhttp.ajax.send();
   	    else
   	        xmlhttp.ajax.send(xmlhttp.ajax.responseText);
   }	
}

function loadpageresponse (ref) {
    var xmlhttp = xmlhttpstack[ref];
    //alert(ref + ' - ' + xmlhttp.ajax.readyState);
	if ((xmlhttp.ajax.readyState == 4) && (xmlhttp.ajax.status == 200)) {
	    xmlhttp.onevent(xmlhttp);		        	
        xmlhttpstack[ref] = null;
    }
}
  