/* COMMON jQuery INCLUDES */
	/* jquery easing code */
	jQuery.easing.jswing=jQuery.easing.swing,jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(a,b,c,d,e){return jQuery.easing[jQuery.easing.def](a,b,c,d,e)},easeInQuad:function(a,b,c,d,e){return d*(b/=e)*b+c},easeOutQuad:function(a,b,c,d,e){return-d*(b/=e)*(b-2)+c},easeInOutQuad:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b+c;return-d/2*(--b*(b-2)-1)+c},easeInCubic:function(a,b,c,d,e){return d*(b/=e)*b*b+c},easeOutCubic:function(a,b,c,d,e){return d*((b=b/e-1)*b*b+1)+c},easeInOutCubic:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b+c;return d/2*((b-=2)*b*b+2)+c},easeInQuart:function(a,b,c,d,e){return d*(b/=e)*b*b*b+c},easeOutQuart:function(a,b,c,d,e){return-d*((b=b/e-1)*b*b*b-1)+c},easeInOutQuart:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b*b+c;return-d/2*((b-=2)*b*b*b-2)+c},easeInQuint:function(a,b,c,d,e){return d*(b/=e)*b*b*b*b+c},easeOutQuint:function(a,b,c,d,e){return d*((b=b/e-1)*b*b*b*b+1)+c},easeInOutQuint:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b*b*b+c;return d/2*((b-=2)*b*b*b*b+2)+c},easeInSine:function(a,b,c,d,e){return-d*Math.cos(b/e*(Math.PI/2))+d+c},easeOutSine:function(a,b,c,d,e){return d*Math.sin(b/e*(Math.PI/2))+c},easeInOutSine:function(a,b,c,d,e){return-d/2*(Math.cos(Math.PI*b/e)-1)+c},easeInExpo:function(a,b,c,d,e){return b==0?c:d*Math.pow(2,10*(b/e-1))+c},easeOutExpo:function(a,b,c,d,e){return b==e?c+d:d*(-Math.pow(2,-10*b/e)+1)+c},easeInOutExpo:function(a,b,c,d,e){if(b==0)return c;if(b==e)return c+d;if((b/=e/2)<1)return d/2*Math.pow(2,10*(b-1))+c;return d/2*(-Math.pow(2,-10*--b)+2)+c},easeInCirc:function(a,b,c,d,e){return-d*(Math.sqrt(1-(b/=e)*b)-1)+c},easeOutCirc:function(a,b,c,d,e){return d*Math.sqrt(1-(b=b/e-1)*b)+c},easeInOutCirc:function(a,b,c,d,e){if((b/=e/2)<1)return-d/2*(Math.sqrt(1-b*b)-1)+c;return d/2*(Math.sqrt(1-(b-=2)*b)+1)+c},easeInElastic:function(a,b,c,d,e){var f=1.70158,g=0,h=d;if(b==0)return c;if((b/=e)==1)return c+d;g||(g=e*.3);if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);return-(h*Math.pow(2,10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g))+c},easeOutElastic:function(a,b,c,d,e){var f=1.70158,g=0,h=d;if(b==0)return c;if((b/=e)==1)return c+d;g||(g=e*.3);if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);return h*Math.pow(2,-10*b)*Math.sin((b*e-f)*2*Math.PI/g)+d+c},easeInOutElastic:function(a,b,c,d,e){var f=1.70158,g=0,h=d;if(b==0)return c;if((b/=e/2)==2)return c+d;g||(g=e*.3*1.5);if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);if(b<1)return-0.5*h*Math.pow(2,10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g)+c;return h*Math.pow(2,-10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g)*.5+d+c},easeInBack:function(a,b,c,d,e,f){f==undefined&&(f=1.70158);return d*(b/=e)*b*((f+1)*b-f)+c},easeOutBack:function(a,b,c,d,e,f){f==undefined&&(f=1.70158);return d*((b=b/e-1)*b*((f+1)*b+f)+1)+c},easeInOutBack:function(a,b,c,d,e,f){f==undefined&&(f=1.70158);if((b/=e/2)<1)return d/2*b*b*(((f*=1.525)+1)*b-f)+c;return d/2*((b-=2)*b*(((f*=1.525)+1)*b+f)+2)+c},easeInBounce:function(a,b,c,d,e){return d-jQuery.easing.easeOutBounce(a,e-b,0,d,e)+c},easeOutBounce:function(a,b,c,d,e){return(b/=e)<1/2.75?d*7.5625*b*b+c:b<2/2.75?d*(7.5625*(b-=1.5/2.75)*b+.75)+c:b<2.5/2.75?d*(7.5625*(b-=2.25/2.75)*b+.9375)+c:d*(7.5625*(b-=2.625/2.75)*b+.984375)+c},easeInOutBounce:function(a,b,c,d,e){if(b<e/2)return jQuery.easing.easeInBounce(a,b*2,0,d,e)*.5+c;return jQuery.easing.easeOutBounce(a,b*2-e,0,d,e)*.5+d*.5+c}});
	/* jquery background image animationg */
	(function(a){function d(a){a=a.replace(/left|top/g,"0px"),a=a.replace(/right|bottom/g,"100%"),a=a.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");var b=a.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);return[parseFloat(b[1],10),b[2],parseFloat(b[3],10),b[4]]}if(!document.defaultView||!document.defaultView.getComputedStyle){var b=jQuery.curCSS;jQuery.curCSS=function(a,c,d){c==="background-position"&&(c="backgroundPosition");if(c!=="backgroundPosition"||!a.currentStyle||a.currentStyle[c])return b.apply(this,arguments);var e=a.style;if(!d&&e&&e[c])return e[c];return b(a,"backgroundPositionX",d)+" "+b(a,"backgroundPositionY",d)}}var c=a.fn.animate;a.fn.animate=function(a){"background-position"in a&&(a.backgroundPosition=a["background-position"],delete a["background-position"]),"backgroundPosition"in a&&(a.backgroundPosition="("+a.backgroundPosition);return c.apply(this,arguments)},a.fx.step.backgroundPosition=function(b){if(!b.bgPosReady){var c=a.curCSS(b.elem,"backgroundPosition");c||(c="0px 0px"),c=d(c),b.start=[c[0],c[2]];var e=d(b.options.curAnim.backgroundPosition);b.end=[e[0],e[2]],b.unit=[e[1],e[3]],b.bgPosReady=!0}var f=[];f[0]=(b.end[0]-b.start[0])*b.pos+b.start[0]+b.unit[0],f[1]=(b.end[1]-b.start[1])*b.pos+b.start[1]+b.unit[1],b.elem.style.backgroundPosition=f[0]+" "+f[1]}})(jQuery);

	
$('a[rel=external]').live('click',function() { window.open(this.href); return false; });
$('a.pd').live('click',function(e) { e.preventDefault(); });
$('a,label,button').live('focus',function(e) { $(this).blur(); });

//used for feature detection
var testDiv_ = document.createElement('div');
var testInput_ = document.createElement('input');

//HTML5 placeholder support
if('placeholder'  in testInput_) { } else {
	$('input[type=text]').each(function() {
	
		if($(this).closest('#login').length || $(this).closest('#property_search').length) { return; }
		
		var placeholder_ = $(this).attr('placeholder') || $('label[for='+$(this).attr('id')+']').text().toLowerCase();
		$(this).attr('placeholder','');
		$(this).val(placeholder_);
		$(this).bind('focus',function() { if($.trim($(this).val()) == placeholder_) { $(this).val('').css('color','#000'); } })
		         .bind('blur',function() { if(!$.trim($(this).val())) { $(this).val(placeholder_).css('color','#d5b9b9'); } })
				 .css('color','#d5b9b9');
	});
}

//home slider
var HomeSlider = {
	_holder 	: false,
	_list			: false,
	_type		: false,	//vertical | horizontal | fade - fade is default
	_items		: false,
	_current	: 0,
	_total		: 0,
	_controls	: false,
	_paused	: false,
	_interval	: false,
	_speed		: 5000,


	init : function(holder) {
		var o = this;
		this._holder = holder;
		this._list = holder.children('ul.slider');
		this._items = this._list.children('li');
		this._total = this._items.length;
		
		this._controls = $('.gallery ul').find('a');
		this._items.css('position','absolute').not(':first').css('opacity',0);
		this._items.each(function(ind,item) {
			$(this).css('zIndex',o._total-ind);
		});
		
		this.addListeners();
		
		if(!this._paused) {
			this._interval = setTimeout("HomeSlider.moveTo("+(this._current-0+1)+")",this._speed);
		}
		
	},
	
	addListeners : function() {
		var scope = this;
		this._controls.bind('click',function(e) {
			e.preventDefault();
			scope.moveTo($(this).parent().index());
		});
	},
	
	moveTo : function(i) {
		var scope = this;
		clearInterval(scope._interval);
		if(i == this._current) { 
			this._interval = setTimeout("HomeSlider.moveTo("+(this._current-0+1)+")",this._speed);
			return; 
		}
		
		if(i>=this._total) { i=0; }
		
		//fade out the current li and fade in the "i"th li - both must have a .stop() on them
		if(i > this._current) {
			this._items.eq(this._current).stop().delay(900).animate({opacity:0},900,function() { $(this).css('display','none'); });
			this._items.eq(i).css('display','block').stop().animate({opacity:1},900,function() {
				if(!scope._paused) { scope._interval = setTimeout("HomeSlider.moveTo("+(scope._current-0+1)+")",scope._speed); }
			});		
		} else {
			this._items.eq(this._current).stop().animate({opacity:0},900,function() { $(this).css('display','none').css('z-index',0); });
			this._items.eq(i).css('display','block').stop().animate({opacity:1},900,function() {
				if(!scope._paused) { scope._interval = setTimeout("HomeSlider.moveTo("+(scope._current-0+1)+")",scope._speed); }
			});
		}
		
		this._controls.stop().animate({backgroundColor:'#fff'},900);
		this._controls.parent().eq(i).children().stop().animate({backgroundColor:'#ff3400'},900);
		this._current = i;
		
	}
}

if($('#mainslider').length) { HomeSlider.init($('#mainslider').parent()); }

//expand/collapse the login box, property_search box
$('#login a, #property_search a').bind('click',function(e) { 
	$(this).siblings('form').toggleClass('expanded');
});

//collapse the login box/property_search box if clicked elsewhere on the element
$(document).bind('click',function(e) {
	var loginEl = $('#login a');
	var propertyEl = $('#property_search a');
	var srcEl = (e.target) ? $(e.target) : $(e.srcElement);
	var closestDropdownesque = srcEl.closest('.dropdownesque')[0];
	
	if(loginEl.siblings('form').hasClass('expanded') && !srcEl.closest('#login form').length && srcEl[0] != loginEl[0]) { loginEl.siblings('form').toggleClass('expanded'); }
	if(propertyEl.siblings('form').hasClass('expanded') && !srcEl.closest('#property_search form').length && srcEl[0] != propertyEl[0]) { propertyEl.siblings('form').toggleClass('expanded'); }
	$('.dropdownesque.expanded').each(function(e) { if($(this)[0] != closestDropdownesque) { $(this).removeClass('expanded'); } });
});

//do the slideup
var SlideUp = {

	el			:	false,
	lis			:	false,
	as		:	false,
	close	:	false,
	opened	:	false,

	init					:	function(el) {
		this.el = el;
		this.lis = el.find('li');
		this.as = this.lis.children('a');
		
		this.close = el.children('a');
		
		this.addListeners();
	},
	
	addListeners		:	function() {
		var o = this;
		
		this.as.bind('click',function(e) {
			$(this)
				.parent()
				.removeClass('selected')
				.addClass('selected')
				.siblings()
				.removeClass('selected');
		});
		
		
		this.close.bind('click',function(e) {
			if(o.opened) {
				o.el.removeClass('opened');
				o.close.html('open');
				o.opened = false;
			} else {
				o.el.addClass('opened');
				o.close.html('close');
				o.opened = true;
			}
		});
	}
	
}

if($('#property_types').length) { SlideUp.init($('#property_types')); }

$('ul.dropdownesque').bind('click',function(e) {
	if($(this).hasClass('expanded')) {
		$(this).removeClass('expanded');
	} else {
		$(this).addClass('expanded');
	}
});

$('ul.dropdownesque li').bind('click',function(e) {
	if($(this).parent().hasClass('expanded')) {
		$(this).removeClass('selected').addClass('selected').siblings().removeClass('selected').end().parent().removeClass('expanded');
		return false;
	}
});

$('#associates .wrap').GenericSlider({numShowing:2,moveBy:1});
$('#news .wrap').GenericSlider({direction:'top',auto:5000});

if($('#property_search_form').length) {
	$('ul.dropdownesque li').bind('click',function(e) {
		console.log($(this));
		var 	theVal 		= 	($(this).attr('rel')) ? $(this).attr('rel') : (($(this).html() === 'Select') ? '' : $(this).html()),
				theTarget	=	$('#'+$(this).parent().attr('rel'));
				
				
				theTarget.val(theVal);
	});
}
