// JavaScript Document
$(document).ready(function(){

 
		/**
		 * Create a cookie with the given name and value and other optional parameters.
		 *
		 * @example $.cookie('the_cookie', 'the_value');
		 * @desc Set the value of a cookie.
		 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
		 * @desc Create a cookie with all available options.
		 * @example $.cookie('the_cookie', 'the_value');
		 * @desc Create a session cookie.
		 * @example $.cookie('the_cookie', null);
		 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
		 *       used when the cookie was set.
		 *
		 * @param String name The name of the cookie.
		 * @param String value The value of the cookie.
		 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
		 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
		 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
		 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
		 *                             when the the browser exits.
		 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
		 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
		 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
		 *                        require a secure protocol (like HTTPS).
		 * @type undefined
		 *
		
		 */
		
		/**
		 * Get the value of a cookie with the given name.
		 *
		 * @example $.cookie('the_cookie');
		 * @desc Get the value of a cookie.
		 *
		 * @param String name The name of the cookie.
		 * @return The value of the cookie.
		 * @type String
		 *
		 */
		jQuery.cookie = function(name, value, options) {
				if (typeof value != 'undefined') { // name and value given, set cookie
						options = options || {};
						if (value === null) {
								value = '';
								options.expires = -1;
						}
						var expires = '';
						if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
								var date;
								if (typeof options.expires == 'number') {
										date = new Date();
										date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
								} else {
										date = options.expires;
								}
								expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
						}
						// CAUTION: Needed to parenthesize options.path and options.domain
						// in the following expressions, otherwise they evaluate to undefined
						// in the packed version for some reason...
						var path = options.path ? '; path=' + (options.path) : '';
						var domain = options.domain ? '; domain=' + (options.domain) : '';
						var secure = options.secure ? '; secure' : '';
						document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
				} else { // only name given, get cookie
						var cookieValue = null;
						if (document.cookie && document.cookie != '') {
								var cookies = document.cookie.split(';');
								for (var i = 0; i < cookies.length; i++) {
										var cookie = jQuery.trim(cookies[i]);
										// Does this cookie string begin with the name we want?
										if (cookie.substring(0, name.length + 1) == (name + '=')) {
												cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
												break;
										}
								}
						}
						return cookieValue;
				}
		};
		jQuery.fn.extend({
	everyTime: function(interval, label, fn, times) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval < 0)
				return;

			if (typeof times != 'number' || isNaN(times) || times < 0) 
				times = 0;
			
			times = times || 0;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});


 
 
	
	
	function get_random_listing(){
		var set_city =  $.cookie('city');
		if(set_city=="" || set_city=="null"){
		  set_city = "Seattle";
		}
			  
		$.ajax({
				type: 'GET',
				url: '/json_funcs.php',
											data: {'random_listing': 1,
											       'city' : set_city
											       },
											dataType: 'json',
											cache: false,
											success: function(data){
												update_random_listing(data);
											}
				});
		
		//alert("ok");
		//$.timer(5000, get_random_listing());
		
	}
	function update_random_listing(JSONDATA){
		$("#feat_box").fadeOut(500,function () {
		  $("#_city").html("<b>City:</b> " + JSONDATA.city);
      $("#_beds").html("<b>Beds:</b> " + JSONDATA.bedrooms);
      $("#_baths").html("<b>Baths:</b> " + JSONDATA.bathrooms);
      $("#_sqft").html("<b>Square Feet:</b> "+ JSONDATA.sql_feet);
      $("#_addy").html("<b>Address:</b><br />"+ JSONDATA.address);
      $("#_st").html("<b>Status:</b> " + JSONDATA.rental_status + "<br><a href='/content/rental_details.php?rental_id="+ JSONDATA.rental_id +"'>Click here to view</a>");
			$("#house_img").error(function(){
			                $("#house_img").attr("src","/images/feat_house.jpg");															 
																		 });
			$("#house_img").attr("src","/thumb.php?image=rental_rid_" + JSONDATA.rental_id + "_1.jpg&rental=1");
		  $("#feat_box").show();
																					});
		
		
		//rental_id 	
		
	}
	get_random_listing();
	
	 $("#feat_box").everyTime(5000, function() {
    get_random_listing();
																					 });
	 

});
