YAHOO.namespace("ps"); 
YAHOO.namespace("ps.menu");
YAHOO.namespace("ps.container");
YAHOO.namespace("ps.Realestate");
YAHOO.namespace("ps.Realestate.results");
YAHOO.namespace("ps.Realestate.criteria");

/////////////////////////
// Browsercheck (needed)
/////////////////////////
YAHOO.ps.UserAgent = function () { 
	this.agent = navigator.userAgent.toLowerCase();
	// Define browsers
	this.isOpera    = (this.agent.indexOf("opera")>-1 && window.opera) ? true : false;
  	this.isIE6 	    = (this.agent.indexOf("msie 6")>-1 && !this.op) ? true : false;
  	this.isIE7 	    = (this.agent.indexOf("msie 7")>-1 && !this.op) ? true : false;
  	this.isIE8 	    = (this.agent.indexOf("msie 8")>-1 && !this.op) ? true : false;
	this.isSafari   = (this.agent.indexOf("safari")>-1) ? true : false;
	this.isChrome   = (this.agent.indexOf("chrome")>-1) ? true : false;
	this.isFirefox  = (this.agent.indexOf("firefox")>-1) ? true : false;
	return this;
}
YAHOO.ps.ua = new YAHOO.ps.UserAgent();
YAHOO.ps.UserAgent = {
	printValue:function(el) {
		var agent = YAHOO.ps.ua.agent;	
		var value;
		if(YAHOO.ps.ua.isIE6) {
			  value = 'IE 6';
		} else if(YAHOO.ps.ua.isIE7) {
			  value = 'IE 7';
		} else if(YAHOO.ps.ua.isIE8) {
			  value = 'IE 8';
		} else if(YAHOO.ps.ua.isFirefox) {
			  value = 'Firefox';
		} else if(YAHOO.ps.ua.isSafari) {
			  value = 'Safari';
		} else if(YAHOO.ps.ua.isChrome) {
			  value = 'Chrome';
		} else {
			  value = 'Other';
		}
		var obj = document.getElementById(el);
		for (var x=0;x<obj.options.length;x++) { 
	        if (obj.options[x].value == value) { 
	            obj.selectedIndex = x; 
	            break; 
	        } 
	    } 
	}
}

YAHOO.ps.Date = function() { };
YAHOO.ps.Date = {
	getDateName:function(val,short) {
		var i, d, dateOnly, currDate;
		var monthName = new Array('January', 'February', 'March', 'April', 'May', 'June', 
							      'July', 'August', 'September', 'October', 'November', 'December');
		var shortName = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
			      				  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
		if (val.indexOf(" ") != -1) {		
			var i = val.split(" ");
			dateOnly = i[0];
		} else {
			dateOnly = val;
			}
		currDate = dateOnly.split("-");
		d = new Date(currDate[0], parseInt(currDate[1],10)-1, currDate[2], 0, 0, 0); 
	
		if (short) {
			return shortName[d.getMonth()] + '. ' + d.getDate();
		} else {
			return monthName[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
		}
	},
	getDate:function(val) {
		var i, d, dateOnly, currDate;
		if (val.indexOf(" ") != -1) {		
			var i = val.split(" ");
			dateOnly = i[0];
		} else {
			dateOnly = val;
		}
		currDate = dateOnly.split("-");
		d = new Date(currDate[0], parseInt(currDate[1],10)-1, currDate[2], 0, 0, 0); 
		
		return d.getMonth()+1 + '/' + d.getDate() + '/' + d.getFullYear();
	},
	getDateDiff:function(val) {
		var i, d, dateOnly, currDate, today;
		var oneDay = 1000*60*60*24;
		if (val.indexOf(" ") != -1) {		
			var i = val.split(" ");
			dateOnly = i[0];
		} else {
			dateOnly = val;
		}
		currDate = dateOnly.split("-");
		d 	  = new Date(currDate[0], parseInt(currDate[1],10)-1, currDate[2], 0, 0, 0); 
		today = new Date();

		var diff = today.getTime()-d.getTime();
		return Math.floor(diff/oneDay);
	},
	getTime:function(val) {
		var i, t, timeOnly, currTime;
		if (val.indexOf(" ") != -1) {		
			var i = val.split(" ");
			timeOnly = i[1];
		} else {
			timeOnly = val;
		}
		currTime = timeOnly.split(":");
		t = new Date(0, 0, 0, currTime[0], currTime[1], currTime[2]); 
		
		var hour, min, ampm;
		if (t.getHours() > 11) {
			hour = t.getHours() - 12;
			ampm = 'PM';
		} else {
		 	hour = t.getHours();
			ampm = 'AM';
		}
		if (hour == 0) {
			hour = 12;
		}
		if (t.getMinutes() < 10) {
			min = '0' + t.getMinutes();
		} else {
			min = t.getMinutes();
		}
		
		return hour + ':' + min + ampm;
	},
	getDateTime:function(val) {
		var date = YAHOO.ps.Date.getDate(val);
		var time = YAHOO.ps.Date.getTime(val);
		return date + ' ' + time;
	}
};

YAHOO.ps.Form = function() { };
YAHOO.ps.Form = {
	// @function returns a 0 or 1 boolean value
	// @param {String} The name of the checkbox to collect
	// @param {String} The parent id if any
	// @returns {Integer} A 0 or 1 is returned based on the input being checked
	getCheckboxBool:function(name, parent) {	
		// IE can't tell the difference between checkbox and input elements. Look 
		// for specific class first and if the matching name element is checked.
		var elements = YAHOO.util.Dom.getElementsByClassName('check', 'input', parent);
		var val = 0;
		for (var x=0;x<elements.length;x++) { 
			if (elements[x].checked && elements[x].name == name) { 
				val = elements[x].value;
			}
		}
		return val;
	},
	// @function checks only when value equals 1 for true
	// @param {String} The name of the checkbox to mark
	// @param {Integer} 0 = false, 1 = true
	// @param {String} The parent id if any
	setCheckboxBool:function(name, value, parent) {
		var elements = YAHOO.util.Dom.getElementsByClassName('check', 'input', parent);
		for (var x=0;x<elements.length;x++) { 
			if (elements[x].name == name) { 
				if (value === 1) {
					elements[x].checked = true;
				} else {
					elements[x].checked = false;
				}
			}
		}
	},
	// @function returns a comma seperated string of selected values from the checkbox
	// @param {String} The name of the checkbox to collect
	// @param {Integer} The number of values that make the string full
	// @param {String} The parent id if any
	// @returns {Array} An array of selected values or an empty string if full
	getCheckboxCsv:function(name, full, parent) {
		// Get all checkbox elements by classname 'check'
		var elements = YAHOO.util.Dom.getElementsByClassName('check', 'input', parent); 

		var txt = "";
		var i   = 0;
		for (var x=0;x<elements.length;x++) { 
	        if (elements[x].checked && (elements[x].name == name || name == false)) { 
	        	i++;
	            txt += elements[x].value + ",";
	        } 
	    } 
		// If total values equal full return value is empty
	    if (i == full) {
	    	txt = "";
	    }
	    // Remove last comma
	    if (txt.length > 0) {
	    	txt = txt.substring(0,(txt.length-1));
	    }
	    return txt;
	},
	// @function checks only the boxes value is in the value string
	// @param {String} The name of the checkbox group to mark
	// @param {String} A comma separated string of checked values
	// @param {String} The parent id if any
	setCheckboxCsv:function(name, value, parent) {
		// Get all checkbox elements by classname 'check'
		var elements = YAHOO.util.Dom.getElementsByClassName('check', 'input', parent); 
		// If value is empty check all boxes
		// Otherwise check each value on its own
		if (value == "") {
			for (var x=0;x<elements.length;x++) { 
				if (elements[x].name == name || name == false) {
		        	elements[x].checked = true;
		        }
		    }
		} else {
			// First uncheck all
			for (var x=0;x<elements.length;x++) { 
				if (elements[x].name == name || name == false) {
		        	elements[x].checked = false;
		        }
		    }
		    // Check values only
		    for (var x=0;x<elements.length;x++) { 
		    	if ((elements[x].name == name || name == false) && value.indexOf(elements[x].value) >= 0) {
		    		elements[x].checked = true;
		    	}
		    }
		}
	},
	// @function a comma seperated list of checked values
	// @param {String} The name of the checkboxes
	// @returns {String} A comma separated list of items that are checked
	getMultiSelectCsv:function(name) {
		var element = document.getElementById(name);
		var txt = "";
		
		for (var x=0;x<element.options.length;x++) { 
		    if (element.options[x].selected) {
		    	txt += element.options[x].value + ",";
	    	}
	    }
	    // Remove last comma
	    if (txt.length > 0) {
	    	txt = txt.substring(0,(txt.length-1));
	    }
	    return txt;
	},
	// @function selects the value(s) of the named drop down menu
	// @param {String} The name of the drop down menu
	// @param {String} The value of the selected items separated by comma
	setMultiSelectCsv:function(name, values) {
		var element = document.getElementById(name);
		
		for (var x=0;x<element.options.length;x++) { 
			element.options[x].selected = false; 
	    }
	    if (!YAHOO.lang.isUndefined(values)) {
			values = values.replace('+', ' ');
			for (var x=0;x<element.options.length;x++) { 
	        	if (values.indexOf(element.options[x].value) >= 0 && element.options[x].value != '') { 
	        		element.options[x].selected = true; 
	        	} 
	    	} 
	    }
	},
	// @function selects the value of the named drop down menu
	// @param {String} The name of the drop down menu
	// @param {String} The value of the select
	setDropDown:function(name, value) {
		var obj = document.getElementById(name);
		for (var x=0;x<obj.options.length;x++) { 
	        if (obj.options[x].value == value) { 
	            obj.selectedIndex = x; 
	            break; 
	        } 
	    } 
	}
};

YAHOO.ps.Viewport = function() { };
YAHOO.ps.Viewport.captchaIndex = 0;
YAHOO.ps.Viewport = {
	setMainBg:function(el,bgImage,pad) {
		var bgTop = YAHOO.util.Dom.getY(el) + pad;
		// Remove 50 pixels from IE6 because it is off
		if (YAHOO.env.ua.ie == 6) {
			bgTop -= 50;
		}
		var bgTag = 'transparent url(' + bgImage + ') repeat-x left ' + bgTop + 'px';
		YAHOO.util.Dom.setStyle('main', 'background', bgTag);
	},
	clearMainBg:function() {
		YAHOO.util.Dom.setStyle('main', 'background-image', 'none');
	},
	popupWin:function(sUrl, target, w, h, scrollable) {
		x = Math.floor((screen.width - w) / 2);
		y = Math.floor((screen.height - h) / 2); 
		
		winContent = window.open(sUrl, target, "top="+y+",left="+x+",height="+h+",width="+w+",resizable=yes,menubar=no,toolbar=no,location=no,scrollbars="+scrollable);
		winContent.focus();
	},
	expandMap:function(el,pad,min) {	
		var vpHgt  = YAHOO.util.Dom.getViewportHeight(); 
		var mapHgt = vpHgt - (YAHOO.util.Dom.getY(el) + pad);
		if (min > mapHgt) {
			mapHgt = min;
		}
		var mapEl = document.getElementById(el);
		if (!YAHOO.util.Dom.hasClass(mapEl,'hide')) {
        	mapEl.style.height = mapHgt + 'px';
		}
	},
	setRemarks:function() {
		// Get element top and height positions
		var imgBoxHgt = document.getElementById('listingImages').offsetHeight;
		var imgBoxTop = document.getElementById('listingImages').offsetTop;
		var remBoxHgt = document.getElementById('innerRemarks').offsetHeight;
		var remBoxTop = document.getElementById('innerRemarks').offsetTop;
		// If remarks is larger than the image show the more link
		if ((remBoxTop + remBoxHgt) > (imgBoxTop + imgBoxHgt)) {
			var maxHgt = (imgBoxTop + imgBoxHgt) - (remBoxTop + 30);
			YAHOO.util.Dom.setStyle('companyRemarks', 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle('companyRemarks', 'height', maxHgt + 'px');
			YAHOO.util.Dom.removeClass('linkRemarks', 'hide')
		}
	},
	menuAction:function(el) {
		if (YAHOO.util.Dom.hasClass(el,'show')) {
			YAHOO.util.Dom.replaceClass(el,'show','hide');		
		} else {
			YAHOO.util.Dom.replaceClass(el,'hide','show');
		}
	},
	printPopup:function(el,show) {
		if (show) {
			YAHOO.util.Dom.addClass(el,'printShow');	
			YAHOO.util.Dom.addClass('container_main','printHide');
			window.print();
			window.setTimeout("YAHOO.ps.Viewport.printPopup('" + el + "',false);",5000);	
		} else {
			YAHOO.util.Dom.removeClass(el,'printShow');	
			YAHOO.util.Dom.removeClass('container_main','printHide');	
		}
	},
	resetCaptchaImage:function() {
	    var captchaImage = document.getElementById('captchaImage');
	    YAHOO.ps.Viewport.captchaIndex++;
	        	
	    captchaImage.innerHTML = '';
	    captchaImage.innerHTML = '<img src="/server/image/captcha/w/80/h/22/c/6/' + YAHOO.ps.Viewport.captchaIndex + '" />';
	}
};

function initContainers () {
	YAHOO.ps.container.wait = new YAHOO.widget.Panel("wait",  
													 { width:"160px", 
			 										   underlay:"none",
													   fixedcenter:true, 
													   close:false, 
													   draggable:false, 
													   modal:true,
													   visible:false,
													   effect:false
													 } 
													);

	YAHOO.ps.container.wait.setHeader('<div class="tl"></div>&nbsp;<div class="tr"></div>');	
	YAHOO.ps.container.wait.setFooter('<div class="bl"></div>&nbsp;<div class="br"></div>');
	YAHOO.ps.container.wait.setBody("<span class=\"wait\"><img src=\"/resources/images/busy.gif\"/>Please wait...</span>");
	YAHOO.ps.container.wait.render(document.body);

	// Define various event handlers for delete Dialog
	var handleDeleteYes = function() {
		YAHOO.ps.deleteItem.startRequest();
	};

	YAHOO.ps.container.deleteItem = new YAHOO.widget.SimpleDialog("delete",  
															   { width:"300px", 
		   														 underlay:"none",
															   	 fixedcenter:true, 
															   	 close:true, 
															     draggable:false, 
																 icon:YAHOO.widget.SimpleDialog.ICON_WARN,
															     modal:true,
															     visible:false,
													     		 effect:false
															   } 
															 );


	YAHOO.ps.container.deleteItem.setHeader('<div class="tl"></div>&nbsp;<div class="tr"></div>');	
	YAHOO.ps.container.deleteItem.setFooter('<div class="bl"></div>&nbsp;<div class="br"></div>');
	YAHOO.ps.container.deleteItem.setBody('<div id="delBody">This will permanently delete this item from the records.</div><div id="delFooter"></div>');
	YAHOO.ps.container.deleteItem.render(document.body);
	
	btnDelete = new YAHOO.widget.Button({ label:"Delete", id:"deleteButton", container:"delFooter", onclick: { fn: handleDeleteYes } });
}

YAHOO.namespace("ps.Ajax");
(function () {
    var Ajax = YAHOO.ps.Ajax;

	var evalText = function (o) {
		var response = false;
		// If the response text is set AJAX worked, evaluate
        if (o.responseText !== undefined){
            response = eval('(' + o.responseText + ')');
        }
        return response;
	};
	
    // Public functions
    Ajax.eval = evalText;
})();

//jQuery qTip defaults
$.fn.qtip.styles.help = {
    border: { 
		width: 3, 
		radius: 4, 
		color: '#355b8a' 
	},
	title: { 
		'background-color': '#355b8a', 
		'color': '#ffffff' 
	},
	tip: { 
		size: { 
			x: 8, 
			y: 8 
		} 
	},
	color: '#666666',
	background: '#ffffff'
};

//Label over used for main search input
$.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = $(this);
		var f = label.attr('for');
		if (f) {
			var input = $('#' + f);
				
			this.hide = function() {
				 label.css({ textIndent: -10000 });
			};
				
			this.show = function() {
				if (input.val() == '') label.css({ textIndent: 0 });
			};

			// handlers
			input.focus(this.hide);
			input.blur(this.show);
			label.addClass(overClass).click(function(){ input.focus(); });
				
			if (input.val() != '') this.hide(); 
		}
	});
};

YAHOO.util.Event.addListener(window, "load", initContainers);

YAHOO.namespace("ps.Popup");
(function () {
    var Dom  = YAHOO.util.Dom,
    	Ev   = YAHOO.util.Event,
    	Pop  = YAHOO.ps.Popup;
	
	var popup,
		bdClass;
	
	var initPopup = function () {
		popup = new YAHOO.widget.Dialog("dynamic",  
				  { width:"700px", 
			  		underlay:"none",
					fixedcenter:true, 
					close:true, 
		 			draggable:false, 
		 			modal:true,
		 			visible:false,
		 			effect:false
	 			  });	
		popup.setHeader('<div class="tl"></div>&nbsp;<div class="tr"></div>');	
		popup.setFooter('<div class="bl"></div>&nbsp;<div class="br"></div>');	
		popup.setBody('<div id="dynHeader"></div><div id="dynBody">&nbsp;</div><div id="dynFooter"></div>');
		popup.render(document.body);		
	};
	
	var showFullscreen = function () {
		// Set height from viewport minus 180 pixels
		var vpHgt = Dom.getViewportHeight();
		document.getElementById('dynBody').style.height = (vpHgt - 140) + 'px';
		popup.cfg.setProperty('width', '700px');

		// Show help
		popup.show();	
		
	};
	
	var showSize = function (w, h) {
		document.getElementById('dynBody').style.height = h + 'px';
		popup.cfg.setProperty('width', w + 'px');
		
		// Show help
		popup.show();	
	};
	
	var setContent = function (o) {
		if (bdClass != '') {
			Dom.removeClass('dynamic', bdClass);
			bdClass = '';
		}
		document.getElementById('dynBody').innerHTML = o;
	};
	
	var setBodyClass = function (o) {
		bdClass = o;
		Dom.addClass('dynamic', bdClass);
	};
	
	var setHeader = function (o) {
		if (o == '') {
			Dom.addClass('dynHeader', 'hide');
		} else {
			Dom.removeClass('dynHeader', 'hide');
			document.getElementById('dynHeader').innerHTML = o;
		}
	};
	
	var setFooter = function (o) {
		if (o == '') {
			Dom.addClass('dynFooter', 'hide');
		} else {
			Dom.removeClass('dynFooter', 'hide');
			document.getElementById('dynFooter').innerHTML = o;
		}
	};
	
	var hide = function () {
		popup.hide();
	};
	
	var print = function () {
	};
	
	var email = function () {
	};

    // Get the DOM object for the results message and setup the map initializer
    Ev.onDOMReady(function () {
    	initPopup();
    });
    
    // Public functions
    Pop.hide 		   = hide;
    Pop.showFullscreen = showFullscreen;
    Pop.showSize	   = showSize;
    Pop.setHeader 	   = setHeader;
    Pop.setFooter 	   = setFooter;
    Pop.setContent 	   = setContent;
    Pop.setBodyClass   = setBodyClass;
})();


(function () {
	var Dom  = YAHOO.util.Dom,
		Ev   = YAHOO.util.Event,
    	Pop	 = YAHOO.ps.Popup;
	
	var html  = '<div id="ehoPopup"><img src="/resources/images/eho-lg.gif" />';
		html += 'McEnearney Associates, Inc. is pledged to the letter and the spirit of the United States ';
		html += 'policy for the achievement of equal housing opportunity throughout the Nation. We encourage ';
		html += 'and support an affirmative advertising and marketing program in which there are no barriers ';
		html += 'to obtaining housing because of race, color, religion, sex, handicap, familial status or ';
		html += 'national origin.  In addition, McEnearney Associates, Inc. is committed to full compliance ';
		html += 'with all additional state and local fair housing laws, as applicable, throughout our network. ';
		html += '<br /><br />';
		html += 'For More Information, please refer to the US Department of Housing and Urban Development. ';
		html += '<a target="eho" href="http://www.hud.gov/offices/fheo">http://www.hud.gov/offices/fheo</a></div>';
	
	Ev.onDOMReady(function () {	
		Ev.addListener('link_eho_footer', 'click', function () {
			Pop.setHeader('Equal Housing Opportunity');
			Pop.setFooter('');
			Pop.setContent(html);
			Pop.showSize(468, 210);
		});
	});
})();