// General Functions

////////////////////
// addLoadEvent() //
////////////////////

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
oldonload();
func();
}
}
}

////////////////
// in_array() //
////////////////

function in_array(needle,haystack) {
var found = false;
for (var i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
found = true;
break;
} else {
found = false;
}
}
return found;
}

////////////////////////
// getClassElements() //
////////////////////////

function getClassElements(className, tag, elm) {
var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
var tag = tag || "*";
var elm = elm || document;
var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
var returnElements = [];
var current;
var elementLength = elements.length;
for (var i = 0; i < elementLength; i++) {
current = elements[i];
if (testClass.test(current.className)) {
returnElements.push(current);
}
}
return returnElements;
}

// Image

/////////////////
// viewImage() //
/////////////////

function viewImage(imagelink,folder,urlwidth,urlheight,url) {
var url = url || '';
var imagelink = imagelink;
var folder = folder || '';
var imagesrc = folder+'/'+imagelink;
var urlwidth = urlwidth || 640;
var urlheight = urlheight || 480;
var screenwidth = screen.width;
var screenheight = screen.height;
if (urlwidth > screenwidth || urlheight > screenheight){ // url is larger
var showscroll = 'yes';
} else {
var showscroll = 'no';
}
if (urlwidth > screenwidth){ // width is larger
var leftpos = 0;
var winwidth = screenwidth;
} else {
var leftpos = (screenwidth - urlwidth)/2;
var winwidth = urlwidth;
}
if (urlheight > screenheight){ // height is larger
var toppos = 0;
var winheight = screenheight;
} else {
var toppos = (screenheight - urlheight)/2;
var winheight = urlheight;
}
var win=window.open(url+'?image='+imagesrc,'new','toolbar=no,status=no,scrollbars='+ showscroll +',location=no,menubar=no,directories=no,resizable=yes,left=' + leftpos + ',top=' + toppos + ',width=' + winwidth + ',height=' + winheight + '');
//var win=window.open(url+'?'+imagesrc,'new','toolbar=no,status=no,scrollbars='+ showscroll +',location=no,menubar=no,directories=no,resizable=no,left=' + leftpos + ',top=' + toppos + '');
win.focus()
}

///////////////////
// windowSizer() //
///////////////////

function windowSizer(){
var imgWidth =  document.images[0].width;
var imgHeight =  document.images[0].height;
window.resizeTo(imgWidth, imgHeight);
var left = (screen.width - imgWidth) / 2;
var top = (screen.height - imgHeight) / 2;
self.moveTo(left, top);
//alert(imgWidth+'x'+imgHeight);
var clientWidth = document.body.parentNode.clientWidth;
var clientHeight = document.body.parentNode.clientHeight;

var wDif = clientWidth-imgWidth;
var hDif = clientHeight-imgHeight;

if( wDif != 0 || hDif != 0 ) {
//alert(wDif+' x '+hDif);
if(wDif > 0 || wDif < 0){
wDif = wDif*(-1);
};
if(hDif > 0 || hDif < 0){
hDif = hDif*(-1);
};
window.resizeBy(wDif, hDif);
var left = (screen.width - imgWidth) / 2;
var top = (screen.height - imgHeight) / 2;
self.moveTo(left, top);
//alert('client: '+clientWidth+'x'+clientHeight+'\nimg: '+imgWidth+'x'+imgHeight+'\ndif: '+wDif+'x'+hDif);
}	
self.focus();
}

// Email

///////////////
// encrypt() //
///////////////

function encrypt(username,hostname,linktext,subject,message){
var username = username || 'sball';
var hostname = hostname || 'redcliffrealty.com';
var address = username + '&#64;' + hostname;
var linktext = linktext || address;
var subject = subject || '';
if (subject != '') {
subject = '?subject=' + subject;
}
var message = message || '';
if (message != '') {
message = '?body=' + message;
}
var output = '<a href="&#109;&#97;&#105;&#108;' + '&#116;&#111;&#58;'+ address + subject + message +'" class="email">'+ linktext +'</a>';
document.write(output);
}

// Tree

//////////////////
// treeToggle() //
//////////////////

function treeToggle(aTag) {
if (aTag.parentNode.tagName == 'LI') { // valid LI
if (aTag.className == 'on') { // turn off
ulDisplay(aTag,'none');
aTag.className = 'off';
} else { // turn off
ulDisplay(aTag,'block');
aTag.className = 'on';
} // end turn on
var aTags = document.getElementsByTagName('A');
for (var i = 0; i < aTags.length; i++) { // for each A tag in the document
childATags = aTags[i].parentNode.getElementsByTagName('A');
aTagFound = false;
for (var j = 0; j < childATags.length; j++) { // for each A tag within the parent LI
if (childATags[j] == aTag) { // aTag found within parent LI
aTagFound = true;
break;
} // end aTag found within parent LI
} // for each A tag within the parent LI
if (!aTagFound) { // aTag not found, set display to 'none'
ulDisplay(aTags[i],'none');
if (aTags[i].className == 'on') { // set className to 'off'
aTags[i].className = 'off';
} // end set className to 'off'
} // end aTag not found, set display to 'none'
} // end for each A tag in the document
} // end valid LI
}

/////////////////
// ulDisplay() //
/////////////////

function ulDisplay(aTag,displayValue) {
var parentLI = aTag.parentNode;
var ulTags = parentLI.getElementsByTagName('UL');
var i = ulTags.length;
while (i--) { // for each ul descending
immediateChild = false;
for (var j = 0; j < parentLI.childNodes.length; j++) { // for each child node
if (parentLI.childNodes[j] == ulTags[i]) { // ulTags[i] is immediate child of parentLI
immediateChild = true;
break;
} // end ulTags[i] is immediate child of parentLI
} // end for each child node
if (immediateChild) { // toggle display
ulTags[i].style.display = displayValue;
} // end toggle display
} // end for each ul descending
}

// hide branches
document.write('<link href="/template/shared/css/tree.css" rel="stylesheet" type="text/css" media="screen" />');