// here lies the code for the dynamic table zebra stripes

  // this function is need to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

	    // avoid rows that have a class attribute
        // or backgroundColor style
	    if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
 
         // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
	        if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
        
		      mytd.style.backgroundColor = even ? evenColor : oddColor;

              
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }

Event.observe(window, 'load', function() {
    if ($('buyTab') && $('rentTab')) {
        var buy     = $$('#tabs .buyACT a')[0];
        var rent    = $$('#tabs .rentACT a')[0];

        buy.setAttribute('href', 'javascript:;');
        rent.setAttribute('href', 'javascript:;');

        Event.observe(buy, 'click', function() {
            var buy     = $$('#tabs .buyACT')[0];
            var rent    = $$('#tabs .rentACT')[0];

            if (!buy.hasClassName('sel')) {
                buy.addClassName('sel');
            }

            rent.removeClassName('sel');

            $('buyTab').style.display   = 'block';
            $('rentTab').style.display  = 'none';
            $('gmapz').setStyle({
                backgroundImage: $('gmapz').getStyle('backgroundImage').replace('renting', 'buying')
            });

            return false;
        });

        Event.observe(rent, 'click', function() {
            var buy     = $$('#tabs .buyACT')[0];
            var rent    = $$('#tabs .rentACT')[0];

            if (!rent.hasClassName('sel')) {
                rent.addClassName('sel');
            }

            buy.removeClassName('sel');

            $('rentTab').style.display  = 'block';
            $('buyTab').style.display   = 'none';
            $('gmapz').setStyle({
                backgroundImage: $('gmapz').getStyle('backgroundImage').replace('buying', 'renting')
            });

            return false;
        });
    }

    if ($('durPlace')) {
        Event.observe('durPlace', 'focus', function() {
            if ('City or ZIP or MLS #...' == $F('durPlace')) {
                $('durPlace').value = '';
            }
        }, false);
        Event.observe('durPlace', 'blur', function() {
            if ('' == $F('durPlace')) {
                $('durPlace').value = 'City or ZIP or MLS #...';
            }
        }, false);
    }
    
    if ($('modify-search')) {
        $('search').style.display = 'none';
        $('top-results').style.display = 'block';

        Event.observe('modify-search', 'click', function() {
            $('top-results').style.display = 'none';
            $('search').style.display = 'block';
        }, false);
    } else {
        if ($('search')) {
            $('search').style.display = 'block';
        }
    }
    
    if ($('mnp')) {
        Event.observe('mnp', 'focus', function() {
            if ('min' == $F('mnp')) {
                $('mnp').value = '';
            }
        }, false);
        Event.observe('mnp', 'blur', function() {
            if ('' == $F('mnp')) {
                $('mnp').value = 'min';
            }
        }, false);
    }

    if ($('mxp')) {
        Event.observe('mxp', 'focus', function() {
            if ('max' == $F('mxp')) {
                $('mxp').value = '';
            }
        }, false);
        Event.observe('mxp', 'blur', function() {
            if ('' == $F('mxp')) {
                $('mxp').value = 'max';
            }
        }, false);
    }
    
    if ($('mnrp')) {
        Event.observe('mnrp', 'focus', function() {
            if ('min' == $F('mnrp')) {
                $('mnrp').value = '';
            }
        }, false);
        Event.observe('mnrp', 'blur', function() {
            if ('' == $F('mnrp')) {
                $('mnrp').value = 'min';
            }
        }, false);
    }

    if ($('mxrp')) {
        Event.observe('mxrp', 'focus', function() {
            if ('max' == $F('mxrp')) {
                $('mxrp').value = '';
            }
        }, false);
        Event.observe('mxrp', 'blur', function() {
            if ('' == $F('mxrp')) {
                $('mxrp').value = 'max';
            }
        }, false);
    }

    if ($('photoReel')) {
        if ($('prevPhoto')) {
            var prevPhoto = $('prevPhoto');
            
            Event.observe(prevPhoto, 'click', function() {
                var iLinks = $('photoReel').getElementsByTagName('a');
            
                for (var i = 1; i < iLinks.length; i++) {
                    if ('sel' == iLinks[i].className) {
                        iLinks[i - 1].className = 'sel';
                        iLinks[i].className     = '';
                        $('propPhoto').src      = $('propPhoto').src.replace(/n=[0-9]{1,2}/, 'n=' + (Number(iLinks[i - 1].innerHTML) - 1));
                        
                        break;
                    }
                }
            }, false);
        }

        if ($('nextPhoto')) {
            var nextPhoto = $('nextPhoto');
            
            Event.observe(nextPhoto, 'click', function() {
                var iLinks = $('photoReel').getElementsByTagName('a');

                for (var i = 0; i < (iLinks.length - 1); i++) {
                    if ('sel' == iLinks[i].className) {
                        iLinks[i + 1].className = 'sel';
                        iLinks[i].className     = '';
                        $('propPhoto').src      = $('propPhoto').src.replace(/n=[0-9]{1,2}/, 'n=' + (Number(iLinks[i + 1].innerHTML) - 1));
                        
                        break;
                    }
                }
            }, false);
        }

        var iLinks = $('photoReel').getElementsByTagName('a');

        for (var i = 0; i < iLinks.length; i++) {
            Event.observe(iLinks[i], 'click', function(e) {
                var link;

                if (!e) var e = window.event;

                if (e.target) {
                    link = e.target;
                } else if (e.srcElement) {
                    link = e.srcElement;
                }

                if (link.nodeType == 3) { // defeat Safari bug
                    link = link.parentNode;
                }
                
                $('propPhoto').src  = $('propPhoto').src.replace(/n=[0-9]{1,2}/, 'n=' + (Number(link.innerHTML) - 1));
                var iLinks          = $('photoReel').getElementsByTagName('a');

                for (var i = 0; i < iLinks.length; i++) {
                    iLinks[i].className = '';
                }

                link.className = 'sel';
            }, false);
        }
    }

    if ($('addFav')) {
        Event.observe($('addFav'), 'click', function() {
            var mlsln   = Number($('mlsln').innerHTML);
            var request = '/find-a-home/buy-a-home/add-favorite/' + mlsln;

            new Ajax.Request(request, {
                method: 'get',
                onSuccess: function(transport) {
                    var root            = transport.responseXML.getElementsByTagName('response')[0];
                    var status          = root.getElementsByTagName('status')[0];
                    var statusCode      = status.getElementsByTagName('code')[0].firstChild.nodeValue;
                    var statusMessage   = status.getElementsByTagName('message')[0];

                    if (statusMessage.hasChildNodes()) {
                        statusMessage = statusMessage.firstChild.nodeValue;
                    } else {
                        statusMessage = '';
                    }

                    if (200 == statusCode) {
                        window.alert('#' + mlsln + ' has been added to your favorites!');
                        
                        $('addFav').parentNode.parentNode.removeChild($('addFav').parentNode);
                        
                        if (!$('favs')) {
                            var favs        = document.createElement('p');
                            var favsLink    = document.createElement('a');
                            
                            favs.setAttribute('id', 'favs');
                            favs.appendChild(favsLink);
                            
                            favsLink.setAttribute('href', '/find-a-home/buy-a-home/favorites');
                            favsLink.appendChild(document.createTextNode('view favorites')); 

                            document.body.insertBefore(favs, document.body.firstChild);
                        }
                    } else {
                        window.alert(statusMessage);
                    }
                },
                onFailure: function() {
                    window.alert('An error has occurred.')
                }
            });
        }, false);
    }
}, false);

// EXTERNAL LINK NEW WINDOW CODE add: rel="external"

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}




 //WINDOW ONLOAD FUNCTIONS 

window.onload = function () {
	externalLinks();
	stripe('zeb', '#F4F4F4', '#fff' );
	stripe('zeb2', '#F4F4F4', '#fff' );
	stripe('more', '#F4F4F4', '#fff' );
	//menuOnload()
}

var hadFocus = false;

function clearSearch(obj) {
	if(!hadFocus) {
		obj.value='';
		hadFocus=true;
	}
}

function switchImage(filename) {
    var imageList = document.getElementById("imageList").getElementsByTagName("IMG");
    for(x = 0; x <= imageList.length-1; ++x) {
        imageList[x].className = "";
    }
    document.getElementById(filename).className = "imageSelected";
    document.getElementById("mainImage").src = "/data/img/rentals/"+filename+".jpg";
}  



