 jQuery.noConflict();
 jQuery(document).ready(function()
		{
	        var cityLocationUrl="/assets/php/search/processLocationRequest.php";
			//adjustZindex();
			fixOverlay();
			autoComplete();
			toolNavBar();
			homeIRotate();
			homeVideo();
			setExpDivC();
			expandMinDiv();
			//tourApartment();
			findApartment();
			propertyTab();
			propertyPageGallery();
			validateForm();
			suggestResults();
			dealHeight();
			resEngineBack();
			propertyDescriptionReadMore();
			
			
			function adjustZindex(){
				var zIndexNumber = 1000;
				jQuery('div').each(function() {
					jQuery(this).css('zIndex', zIndexNumber);
					zIndexNumber -= 10;
				});
			}
			
			function fixOverlay(){
				//ff hack
				jQuery("#find-apart .card").css("display", "none");
				jQuery("#find-apart .card").css("display", "block");
			}
			
			function autoComplete(){
				jQuery("#find-apart #findApt").attr( "autocomplete", "off" );
				jQuery("#findApt-s").attr( "autocomplete", "off" );
			}
			
			function toolNavBar(){
				jQuery("#global-tool-nav li.headlink").each(function (i) {
					var nw = jQuery(this).children("a").width() + 4;
					jQuery(this).css("width",nw+"px");
				});
				jQuery("#global-tool-nav li.headlink").hover(
					function(){
						jQuery(this).children("a").css("color","#9D102D");
						jQuery(this).children("ul").show();
					},
					function(){
						jQuery(this).children("a").css("color","#333333");
						jQuery(this).children("ul").hide();
					}
				);
				jQuery("#global-tool-nav li.headlink").eq(0).hover(
					function(){
						jQuery('#findApt #state').css("visibility", "hidden");
					},
					function(){
						jQuery('#findApt #state').css("visibility", "visible");
					}
				);
				jQuery("#global-tool-nav li.headlink ul li a").hover(
					function(){
						var sib = jQuery(this).siblings("span.nav-arrw");
						if(sib.get(0) != null){
							sib.show();
						}
						else{
							jQuery(this).before('<span class="nav-arrw">></span>');
						}
						jQuery(this).parent().css("margin-left","0px");
					},
					function(){
						
						jQuery(this).siblings("span.nav-arrw").hide();
						jQuery(this).parent().css("margin-left","10px");
					}
				);
			}
			
			function homeIRotate(){
				if(jQuery("#top-content-img .video-link").size()>0){
					//stop the slideshow on the video after one loop
					//count number of slides
					var numSlides = jQuery('#top-content-img > a > img') ? jQuery('#top-content-img > a > img').size() : 0 ;
					
					//top-content-img
					 jQuery('#top-content-img').cycle({
						 	timeout: 5000,
						 	autostop: true,
						 	autostopCount:(numSlides+1)
						 });
				}
				else{
					 jQuery('#top-content-img').cycle({
						 	timeout: 5000
						 });
				}
			} 
			
			function setExpDivC(){
				var len = jQuery(".b-column .inner-column").length;
				var nlen = len/3;
				jQuery(".b-column .inner-column").each(function (i) {
					if(i%nlen == 0){
						jQuery(this).children().filter(".inner-envelop").show();
						jQuery(this).children().filter(".tab").children().filter(".tab-head").css("color","#333333");
					}
				});
			}
			
			//Controls the collapsing and expanding div in minor section
			function expandMinDiv(){
				jQuery(".b-column .inner-column .tab").click(function () {
					var paren = jQuery(this).parent();
					paren.siblings(".inner-column").each(function (i) {
						jQuery(this).children(".inner-envelop").hide();
						jQuery(this).children(".tab").children(".tab-img").attr("src","/assets/new/images/arrw-click.png");
						jQuery(this).children(".tab").children(".tab-head").css("color","#666666");
					});	
					var head = jQuery(this).children().filter(".tab-img");
					if(jQuery(this).siblings().filter(".inner-envelop").get(0).style.display == "block"){
						jQuery(this).children().filter(".tab-img").attr("src", "/assets/new/images/arrw-click.png");
						head.siblings().filter(".tab-head").css("color","#666666");
					}
					else{
						var chk = jQuery(this).children().filter(".tab-img").attr("src");
						if( chk == "/assets/new/images/arrw-click.png"){
							jQuery(this).children().filter(".tab-img").attr("src","/assets/new/images/arrw.png");
							head.siblings().filter(".tab-head").css("color","#333333");
						}else{
							jQuery(this).children().filter(".tab-img").attr("src","/assets/new/images/arrw-click.png");
							head.siblings().filter(".tab-head").css("color","#666666");
							}
					}
					jQuery(this).siblings().filter(".inner-envelop").slideToggle(1000);
				});
			}
			
			function tourApartment(){
				jQuery("img.tor-img2").hover(
					function(){
						jQuery(this).hide();
						jQuery("#tour-sect").show();
						jQuery("#tour-sect-lnk").show();
					},
					function(){

					}
				);
				jQuery("#tour-sect").hover(
					function(){

					},
					function(){
						jQuery("#tour-sect").hide();
						jQuery("#tour-sect-lnk").hide();
						jQuery("img.tor-img2").show();
					}
				);

			}
			
			function findApartment(){
				jQuery("#content .findApt-lnk").click(function () {
					jQuery(this).hide();
					jQuery("#content #find-apart").show();
				});	
				
				jQuery("#find-apart .close-find-apart").click(function () {
					jQuery("#find-apart").hide();
					jQuery("#content .findApt-lnk").show();
				});	
			}
			
			function propertyTab(){
				
				jQuery("#content-inner-bottom #top-bar span").click(function () {	
					var ind = jQuery("#content-inner-bottom #top-bar span").index(this);
					var p = "point"+ind;
					if( jQuery(this).hasClass("select") ){
						return false;
					}
					jQuery(this).addClass("select");
					jQuery("#content-inner-bottom #top-bar img").attr("class",p);
					jQuery(this).siblings().filter("span").each(function (i) {
						jQuery(this).removeClass("select");
					});
					jQuery("#content-left-col #content-inner-bottom .tabs").each(function (i) {
						jQuery(this).hide();
					});
					jQuery("#content-left-col #content-inner-bottom .tabs").eq(ind).show();
				});
				jQuery("#map-this-property").click(function () { jQuery("#content-inner-bottom #top-bar span:eq(2)").click();	});
			}
			
			function propertyPageGallery(){
				//Set up the Gallery navigation
				
				var len = jQuery("#content-left-col .content-inner-right-col img.gallery").length;
				var nwidth = (len*9)+79;
				
				jQuery("#content-left-col .content-inner-right-col img.gallery").each(function (i) {
					
						// define a new image
						
						var _img   = jQuery(this);
						var mh = 277;
						var mw = 368;
						
						//jQuery(this).replaceWith(_img);
						
						//scale image
						//var _main = jQuery('#galleria_wrapper');
						var realw = _img.width();
						var realh = _img.height();
						var w = Math.ceil( _img.width() / _img.height() * mh );
						var h = Math.ceil( _img.height() / _img.width() * mw );

						if (w > h) {
							var mt, ht;
							if(h >= mh){
								mt = 0; 
								ht = h;
							}
							else{
								mt = -(h-mh)/2;
								if(mt > 20 || mt < 0)
									mt = 0;
								ht = 'auto';
							}
							if(realh > mh){ ht = h; }
							jQuery(this).css({ height: ht, width: mw, marginTop: mt });
						} else {
							var mt, wt;
							if(w >= mw){
								mt = 0; 
								wt = w;
							}
							else{
								mt = -(w-mw)/2;
								if(mt > 0){
									if((mt+w) > mw)
										mt = 0;
								}
								else{mt = 0}
								wt = 'auto';
							}
							if(realw > mw){ wt = w; }
							jQuery(this).css({ width: wt, height: mh, marginLeft: mt });
						}
												
				});
				jQuery("#content-left-col .content-inner-right-col img.gallery").each(function (i) {
					var nsrc = jQuery(this).attr("src");
					jQuery(this)[0].src = "";
					jQuery(this)[0].src = nsrc;
				});
				
				jQuery("#content-left-col .content-inner-right-col #slide-control").width(nwidth);
				jQuery("#content-left-col .content-inner-right-col #slide-control").html('<img src="/assets/new/images/button-bck.png" alt="previous" class="prev"/><img src="/assets/new/images/button-fwd.png" alt="next" class="next"/>');
				
				jQuery("#content-left-col .content-inner-right-col img.gallery").each(function (i) {
						if(i == 0){
							jQuery("#content-left-col .content-inner-right-col #slide-control .next").before('<img src="/assets/new/images/dot-select.png" alt="" class="dot select"/>');
							}
						else{
							jQuery("#content-left-col .content-inner-right-col #slide-control .next").before('<img src="/assets/new/images/dot-nselect.png" alt="" class="dot"/>');
						}
				});
				//handle Next click event
				jQuery("#content-left-col .content-inner-right-col #slide-control .next").click(function () {

					jQuery("#content-left-col .content-inner-right-col #slide-control .dot").each(function (i) {
						if(jQuery(this).hasClass("select")){
							if( i == (len-1) ){  i = -1; }
							jQuery("#content-left-col .content-inner-right-col img.gallery").hide();
							jQuery("#content-left-col .content-inner-right-col div.caption").hide();
							var tt = jQuery("#content-left-col .content-inner-right-col img.gallery");
							tt.eq(i+1).show();								
							if(tt.eq(i+1).parent().next().hasClass('caption')){tt.eq(i+1).parent().next().show()}
							jQuery(this).attr("src","/assets/new/images/dot-nselect.png");
							jQuery(this).removeClass("select");
							if( i == -1 ){
								jQuery("#content-left-col .content-inner-right-col #slide-control .dot:eq(0)").addClass("select");
								jQuery("#content-left-col .content-inner-right-col #slide-control .dot:eq(0)").attr("src","/assets/new/images/dot-select.png");								
							}
							else{
								jQuery(this).next(".dot").addClass("select");
								jQuery(this).next(".dot").attr("src","/assets/new/images/dot-select.png");
							}
							return false;
						}
					});
				});
				
				//handle Previous click event
				jQuery("#content-left-col .content-inner-right-col #slide-control .prev").click(function () {

					jQuery("#content-left-col .content-inner-right-col #slide-control .dot").each(function (i) {
						if(jQuery(this).hasClass("select")){
							if( i == 0 ){ i = len; }
							jQuery("#content-left-col .content-inner-right-col img.gallery").hide();
							jQuery("#content-left-col .content-inner-right-col div.caption").hide();
							var tt = jQuery("#content-left-col .content-inner-right-col img.gallery");
							tt.eq(i-1).show();								
							if(tt.eq(i-1).parent().next().hasClass('caption')){tt.eq(i-1).parent().next().show()}
							jQuery(this).attr("src","/assets/new/images/dot-nselect.png");
							jQuery(this).removeClass("select");
							if( i == len ){
								jQuery("#content-left-col .content-inner-right-col #slide-control .dot").eq(i-1).addClass("select");
								jQuery("#content-left-col .content-inner-right-col #slide-control .dot").eq(i-1).attr("src","/assets/new/images/dot-select.png");								
							}
							else{
								jQuery(this).prev(".dot").addClass("select");
								jQuery(this).prev(".dot").attr("src","/assets/new/images/dot-select.png");
							}
							return false;
						}
					});
				});

			}
			
			function validateForm(){
				jQuery("#find-apart #findApt").submit(function() {
					var cityTrimmedValue = jQuery.trim(jQuery("#find-apart #findApt input:eq(0)").val());
					var stateTrimmedValue = jQuery.trim(jQuery("#find-apart #findApt #state").val());
					if(cityTrimmedValue== "" || stateTrimmedValue == ""){
						var ncity = jQuery("#find-apart #findApt input:eq(0)").val();
						if(ncity.toLowerCase() == "washington dc"){
							jQuery("#find-apart #findApt input:eq(0)").val("Washington");
							jQuery("#find-apart #findApt #state").val("District of Columbia");
							return true;
						}
						if (cityTrimmedValue == "") {
							if(jQuery("#find-apart #findApt .label:eq(0) .req-field").get(0) == null){
								jQuery("#find-apart #findApt .label:eq(0)").append("<span class='req-field' style='color: red;'>&#160;&#160;&#160;This field is required</span>");
							}
						}
						else{
							jQuery("#find-apart #findApt .label:eq(0) > .req-field").remove();
						}
						
						if (stateTrimmedValue== "") {
							if(jQuery("#find-apart #findApt .label:eq(1) .req-field").get(0) == null){
								jQuery("#find-apart #findApt .label:eq(1)").append("<span class='req-field' style='color: red;'>&#160;&#160;&#160;This field is required</span>");		
							}					
						}
						else{
							jQuery("#find-apart #findApt .label:eq(1) > .req-field").remove();
						}
						return false;
					}	
					return true;	
				});
				
				jQuery("#findApt-s").submit(function() {
					var cityTrimmedValue = jQuery.trim(jQuery("#findApt-s input:eq(0)").val());
					var stateTrimmedValue = jQuery.trim(jQuery("#findApt-s #state2").val());
					if(cityTrimmedValue== "" || stateTrimmedValue== ""){
						var ncity = jQuery("#findApt-s input:eq(0)").val();
						if(ncity.toLowerCase() == "washington dc"){
							jQuery("#findApt-s input:eq(0)").val("Washington");
							jQuery("#findApt-s #state2").val("District of Columbia");
							return true;
						}
						if (cityTrimmedValue == "") {
							if(jQuery("#findApt-s .label:eq(0) .req-field").get(0) == null){
								jQuery("#findApt-s .label:eq(0)").append("<span class='req-field' style='color: red;'>&#160;&#160;&#160;This field is required</span>");
							}
						}
						else{
							jQuery("#findApt-s .label:eq(0) > .req-field").remove();
						}

						if (stateTrimmedValue == "") {
							if(jQuery("#findApt-s .label:eq(1) .req-field").get(0) == null){
								jQuery("#findApt-s .label:eq(1)").append("<span class='req-field' style='color: red;'>&#160;&#160;&#160;This field is required</span>");		
							}
						}
						else{
							jQuery("#findApt-s .label:eq(1) > .req-field").remove();
						}

						return false;
					}	
					return true;	
				});


			}
			function suggestResults(){
				if(jQuery("#state").get(0) != null || jQuery("#state2").get(0) != null){
					jQuery("#city,#city2").autocomplete(cityLocationUrl, {
						dataType: "json",
						minChars: 3,
						width: 135,
						matchContains: "word",
						max: 30,
						autoFill: false,
						delay: 50,
						parse: function(data){
						   var parsed=[];
						   if(data!=null && jQuery.isArray(data)){
							   for(var i =0; i< data.length; i++){
								   parsed[parsed.length] = {
									  data: data[i],
								      value: data[i].city,
								      result: data[i].city
								   };
							   }
						   }
						   return parsed;
					    },
						formatItem: function(row) {
							return row.city + ", " + row.abbrv;
						},
						formatResult: function(row) {
							return row.city;
						}
					});
					
					jQuery("#city2").result(function(event, data) {
						if (data){
							jQuery("#state2").val(data['state']);
						}
					});
					jQuery("#city").result(function(event, data) {
						if (data){
							jQuery("#state").val(data['state']);
						}
					});
				}
			}

			function dealHeight(){
				if(jQuery("#content .dealpgrewards-back").get(0) != null){
					var nimg = jQuery("#content .content-left-col-dealpg:eq(0) img").eq(0);
					var dpic;
					nimg.load(function() {
						dpic = jQuery(this).height();	
						jQuery("#content .dealpgrewards-back").height(dpic);
					});
					var src = nimg.get(0).src;
					nimg.get(0).src = "";
					nimg.get(0).src = src;										
				}
			}
			
			function resEngineBack(){
				resEngineBackToSearch();
				resEngineBackToProperty();
			}
			function resEngineBackToSearch(){
				var city = getURLParam("city");
				var state = getURLParam("state");
				var hrf = jQuery("#iframe_container #reserveback a#back-to-search").attr("href");
				jQuery("#iframe_container #reserveback a#back-to-search").attr("href", hrf+"?city="+city+"&state="+state);
			}
			function resEngineBackToProperty(){
				var pathParam = getURLParam("property-path");
				var backToProperty = jQuery("#iframe_container #reserveback a#back-to-property");
				if(backToProperty !=null){
					
					if(pathParam!=null && pathParam.length>0){
						var pathname = rewritelink(pathParam);
						//Manually specify host to avoid injection-style attacks
						var url = window.location.protocol + "//"+ window.location.host + pathname;
						backToProperty.attr("href", url);
					}
					else{ //eliminate link if last page was not in the furnished-apartments folder
						var referrer = document.referrer;
						var fromProperty = referrer.indexOf("/furnished-apartments/");
						if(fromProperty>-1){
							backToProperty.attr("href", referrer);
						}
						else{
							backToProperty.remove();
						}
					}
				}
				
			}
			function rewritelink(path){
				var st = path.indexOf("/", 1);
				var nlink = path.substr(st)+".php";
				return nlink; 
			}
			function getURLParam(strParamName){
				var strReturn = "";
				var strHref = window.location.href;
				if ( strHref.indexOf("?") > -1 ){
					var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
					var aQueryString = strQueryString.split("&");
					for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
						if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
							var aParam = aQueryString[iParam].split("=");
							strReturn = unescape(aParam[1]).replace(/\+/g, " ");
							break;
						}
					}
				}
				return strReturn;
			}
			function propertyDescriptionReadMore(){
				var propertyDiv = jQuery("#property-full-description");
				
				if( propertyDiv != null) {	
					//save expanded height for later toggle
					var originalHeight = propertyDiv.height();
					propertyDiv.data("expanded-height",originalHeight );
					if(originalHeight > 80){
						propertyDiv.addClass("collapsed");
						jQuery("<a id='expand-property-description' onClick='return expandPropertyDescription();'>Read More</a>").insertAfter("#property-full-description");
					}
					
				}
			}
			function homeVideo(){
				jQuery("#top-content-img .video-link").click(loadVideo);
			}
			function loadVideo(){
				jQuery('#top-content-img').cycle('destroy');
				jQuery("#top-content-img").addClass("video");
				jQuery("#tour-sect-lnk").hide();
				
				jQuery("#top-content-img").html('<div id="top-video" style="display:none;"><object width="560" height="340" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" ><param name="src" value="http://www.youtube.com/v/prnk2VaNy3M&amp;hl=en_US&amp;fs=1&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="transparent" /><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/prnk2VaNy3M&amp;hl=en_US&amp;fs=1&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" allowfullscreen="true" width="560" height="340" ></embed></object></div>');

				jQuery("#top-content-img > #top-video").append('<div id="video-links-wrapper"><div id="video-links" style="display:inline;"></div></div>');
				jQuery("#tour-sect-lnk > a").clone().appendTo("#video-links-wrapper > #video-links");
				jQuery("#addthis-widget").insertAfter("#video-links-wrapper > #video-links");
				//jQuery("#addthis-widget iframe").width(55);
				jQuery("#addthis-widget iframe").removeAttr("style");
				jQuery("#addthis-widget").show();
				
				jQuery("#top-content-img > #top-video").fadeIn(100, function(){
					jQuery("#top-content-img > #top-video").show();
					
				});

			}
		}
 );
  //functions called directly via ahref to avoid compatibility problems with background in IE6 for read more functionality
	function expandPropertyDescription(){
		var propertyDiv = jQuery("#property-full-description");
		if(propertyDiv !=null){
			propertyDiv.animate({"height": propertyDiv.data("expanded-height")},"200");
			propertyDiv.removeClass("collapsed");
			jQuery("#expand-property-description").remove();
			jQuery("<a id='collapse-property-description' onClick='return collapsePropertyDescription();'>Close</a>").insertAfter("#property-full-description");
		}
	}
	function collapsePropertyDescription(){
		var propertyDiv = jQuery("#property-full-description");
		if(propertyDiv !=null){
			propertyDiv.animate({"height": "80px"},"200");
			propertyDiv.addClass("collapsed");
			jQuery("#collapse-property-description").remove();
			jQuery("<a id='expand-property-description' onClick='return expandPropertyDescription();'>Read More</a>").insertAfter("#property-full-description");
		}
	}