jQuery(document).ready(function(){
	var viewportx = jQuery(window).height() - 30;	
	var viewporty = jQuery(window).width() -30;
	if(viewportx > '600'){
		viewportx = '600';
	}
	
	if(viewporty > '840'){
		viewporty = '840';
	}
	
	// Force equalheighted elements on the page
	if(jQuery('body').attr('id') == 'news'){
		// Sort out equal heights on news pages
		var colheight = jQuery(".left-content").height();
		var rightheight = jQuery(".wrap").height() + 155;
		var wrapheight = jQuery(".equalcolumn").height();
		//alert("Equal height = "+wrapheight);
		//alert("Wrap height = "+rightheight);
		//alert("Left col height = "+colheight);
		if(colheight){
			if(colheight < rightheight){
				jQuery(".equalouter").css({
					'height': rightheight
				});
				jQuery(".equalcolumn").css({
					'height': rightheight - 155
				});
			} else {
				jQuery(".equalcolumn").css({
					'height': colheight
				});
				jQuery(".equalcolumn").css({
					'height': colheight - 155
				});
			}
		}
	} else {
		// Treat these classes as equal heighted columns
		jQuery(".equalheight").equalHeights();
		jQuery(".equalcolumn").equalHeights();
	}	
	
	// Add first and last indicators to nav to ease styling		
	jQuery("#subNav li:last, #nav li:last").addClass("last");
	jQuery("#subNav li:first, #nav li:first").addClass("first");
	
	// Add last class to property listing on single pages
	jQuery(".posttown ul li:last-child").addClass("last");
	
	// Styling the property filter (current/completed)
	//jQuery(".posttown li.completed, .subposttown li.completed").hide();
	jQuery('.county-header p a#completed, .county-header p a#current').css({
		'text-decoration': 'none',
		'color': '#9dbfa9',
		'cursor': 'pointer'
	});
	jQuery('.county-header p a#all').css({
		'font-weight': 'normal',
		'text-decoration': 'none',
		'color': '#ed9500',
		'cursor': 'none'
	});
		
	// Function for nicer animated rolover states when no posts in category
	/*jQuery("ul li.single-prop").hover(
		function(event){
			var liid = getID(jQuery(this).attr('id'));
			//alert(liid);
			jQuery('#'+liid+' span').fadeIn('3000');
		},
		function(event){
			var liid = getID(jQuery(this).attr('id'));
			jQuery('#'+liid+' span').fadeOut('3000');
		}	
	);	
	
	jQuery(".posttown, .subposttown").each(function(){
		var townid = getID(jQuery(this).attr('id'));
		//alert(townid);
		var propcount = jQuery('li.townprop-'+townid).not('.completed').size();
		//alert(propcount);
		if(propcount == 0){
			jQuery('#town-'+townid).fadeOut();
		} else {
			jQuery('#town-'+townid).fadeIn();
		}
	});*/
	
	// Close function on subtown nav
	jQuery('.subposttown a.close').click(function(event){
		event.preventDefault();
		if(jQuery(this).hasClass('completed')){
			var notstatus = 'current';
		} else if(jQuery(this).hasClass('selectall')) {
			var notstatus = 'noclassplease';
		}  else {
			var notstatus = 'completed';
		}
		var subtownid = getID(jQuery(this).attr('id'));
		jQuery('#town-'+subtownid+' ul li').not('.'+notstatus).slideToggle();
		jQuery(this).slideToggle();
		jQuery('#town-'+subtownid).toggleClass('bgon');
	});
	
	// Show properties function on subtown nav
	jQuery('.subposttown ul li.showprops').click(function(event){
		event.preventDefault();
		if(jQuery(this).hasClass('completed')){
			var notstatus = 'current';
		} else if(jQuery(this).hasClass('selectall')) {
			var notstatus = 'noclassplease';
		}  else {
			var notstatus = 'completed';
		}
		var subtownid = getID(jQuery(this).attr('id'));
		jQuery('#town-'+subtownid+' ul li').not('.'+notstatus).slideToggle();
		jQuery('#town-'+subtownid+' ul a.close').slideToggle();
		jQuery('#town-'+subtownid).toggleClass('bgon');	
	});
	
	// Main property filter function
	jQuery(".county-header p a").click(function(event){
		event.preventDefault();
		var id = jQuery(this).attr('id');
		if(id == 'current'){
			jQuery(".posttown li.completed, .subposttown li.completed").slideUp();
			jQuery('.subposttown ul li.showprops').hide();
			jQuery('.subposttown').each(function(){
			
				if(jQuery(this).hasClass('bgon')){
				} else {
					jQuery('.subposttown ul a.close').slideToggle();
					jQuery(this).toggleClass('bgon');					
				}
				
			});
			jQuery('.subposttown ul a.close, .subposttown ul li.showprops').removeClass('completed');
			jQuery(".subposttown ul li.showprops, .subposttown a.close").removeClass('selectall');	
			jQuery(".posttown li.current, .subposttown li.current").slideDown();
			jQuery('.county-header p a#completed, .county-header p a#all').css({
				'text-decoration': 'none',
				'color': '#9dbfa9',
				'cursor': 'pointer'
			});
			jQuery(this).css({
				'font-weight': 'normal',
				'text-decoration': 'none',
				'color': '#ed9500',
				'cursor': 'none'
			});			
			
			houseCount(id);
			
			//Hide post town if empty on filter
			hideshowTown();
			
		} else if(id == 'completed') {
			jQuery(".posttown li.completed, .subposttown li.completed").slideDown();
			jQuery('.subposttown ul li.showprops').hide();
			jQuery('.subposttown').each(function(){
			
				if(jQuery(this).hasClass('bgon')){
				} else {
					jQuery('.subposttown ul a.close').slideToggle();
					jQuery(this).toggleClass('bgon');
				}

				
			});
			jQuery(".subposttown ul li.showprops, .subposttown a.close").removeClass('selectall');
			jQuery('.subposttown ul a.close, .subposttown ul li.showprops').addClass('completed');			
			jQuery(".posttown li.current, .subposttown li.current").slideUp();
			jQuery('.county-header p a#current, .county-header p a#all').css({
				'text-decoration': 'none',
				'color': '#9dbfa9',
				'cursor': 'pointer'
			});
			jQuery(this).css({
				'font-weight': 'normal',
				'text-decoration': 'none',
				'color': '#ed9500',
				'cursor': 'none'
			});
						
			houseCount(id);
			
			//Hide post town if empty on filter
			hideshowTown();
			
		} else {
			jQuery(".subposttown ul li.showprops, .subposttown a.close").addClass('selectall').removeClass('completed');
			jQuery(".posttown, .subposttown, .posttown li, .subposttown li").not('.showprops').slideDown();
				
			jQuery('.county-header p a#current, .county-header p a#completed').css({
				'text-decoration': 'none',
				'color': '#9dbfa9',
				'cursor': 'pointer'
			});
			jQuery(this).css({
				'font-weight': 'normal',
				'text-decoration': 'none',
				'color': '#ed9500',
				'cursor': 'none'
			});
			
			var whattocount = 'noclassplease';
			
			houseCount(whattocount);
			
		}
		
		
		
	}); //End main filter function
	
	//Hide post town if empty on page load
	//hideshowTown();
			
	var currentCat = jQuery('#subNav li.current-cat').text();
	
	var myTimer = {};

    // Set the timer for 0.1 seconds
	myTimer = jQuery.timer(100,function(){
		var flashnum = jQuery("#flash .post object").size();
		var homeflashnum = jQuery(".home-content object").size();
		if(flashnum > '0'){
			//alert('Object found');
			jQuery.clearTimer(myTimer);
			jQuery("#flash .post object").clone().appendTo("#flash");
			jQuery("#flash h1, #flash .post").hide();
		} else if(homeflashnum > '0'){
		
			jQuery.clearTimer(myTimer);
			var objectclone = jQuery(".home-content object").clone();
			var thumbclone = jQuery(".home-content  .home-thumb").clone();
			jQuery(".home-content").empty();
			jQuery(objectclone).prependTo(".home-content");		
			jQuery(thumbclone).appendTo(".home-content");		
			//alert('No objects here');
		} else {
		}
		jQuery('ul.toggle:has(li.active)').slideDown('fast', function(){
			jQuery.clearTimer(myTimer);
		});
		
	});
	
	jQuery("#google-wrap .google-inner").scrollFollow({
	   speed: 700,
	   offset: 25
	});
	
	var newsherocount = jQuery('#news .hero img').size();
	if(newsherocount > '1'){
		jQuery('#news .hero img:last').hide();
	}
	
	jQuery('.banner-ad').click(function(e){
		//e.preventDefault();
		jQuery('#banner-ad-backdrop').fadeIn();
		//var expwidth = jQuery('#banner-ad-expander').attr('rel') / 2;
		var expwidth = '929';
		jQuery('#banner-ad-expander').css('width', '188px');
		jQuery('#banner-ad-expander').animate({
			opacity: '1.0',
			width: expwidth
			}, 450, function() {
			// Animation complete.
			jQuery('#advert-close').fadeIn();
		});
	});
	
	jQuery('.banner-ad #advert-close').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		//jQuery(this).hide();
		jQuery('#advert-close').fadeOut();
		jQuery('#banner-ad-expander').animate({
			width:'188px'
			}, 450, function() {
			// Animation complete.
			jQuery(this).fadeOut();
			jQuery('#banner-ad-backdrop').fadeOut();
		});
	});

});

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

// Return Id of string //
function getID(strID){
 return strID.replace(/[^0-9]/gi, '');
}

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};

function floorPlans(selector){
	var viewportx = jQuery(window).height() - 120;	
	//alert(viewportx);
	var viewporty = '654';
	if(viewportx > '600'){
		var viewportx = '600';
	} else {
		var viewportx = viewportx;
		var viewporty = '660';
	}
	   	         	
	var elemarray = new Array();
	// Create an array to store the variable names			
	jQuery(selector).each(function() {
		var conid = 'content'+jQuery(selector).index(this);		
		elemarray.push(' '+conid);		
		var elemid = jQuery(this).attr('id');
    var thistitle = jQuery(this).attr('rel');
    var headcontent = jQuery('#'+elemid+' div.floortitle').html();
    var imgcontent = jQuery('#'+elemid+' div.floorimg').outerHTML();
   	var roomcontent = new String();
   	var roomcontent = jQuery('#'+elemid+' .room-details').outerHTML();
   	var footcontent = '<p class="footnote">All measurements are approximate and may change during construction.</p>';
   	var plancontent = new String(headcontent + imgcontent + roomcontent + footcontent); 
    var	plancontent = plancontent.replace(/(\r\n|\n|\r)/gm,"");
    //alert(plancontent);
    eval(conid + " = {player: 'html', content: '"+ plancontent + "' , title: '" + thistitle + " <span>~</span> Floor plans', width: '" + viewporty + "', height: '" + viewportx + "' }");	    
	});
	
	var mygallery = eval('['+jQuery.trim(elemarray.toString())+']');
	// Get the array as a string	
	
	Shadowbox.open(mygallery, {
	    continuous: true
	});
			  
};

function myGallery(){
	    	         	
		var elemarray = new Array();
		// Create an array to store the variable names
						
		jQuery('#site-plan .wrap .content img').each(function() {
		// Perform a function on each a element within #large-gallery p
		
			var imgid = 'img'+jQuery('#site-plan .wrap .content img').index(this);
			// Define the image variable name using the index
			
			elemarray.push(' '+imgid);		
			// Push the name to the array
			
		    var href =  jQuery(this).attr('src');
		    var title =  jQuery(this).attr('alt');
		    // Get the image link
		    
		    eval(imgid + " = {player: 'img', title: '"+title+" <span>~</span> Site Plan', content: '"+href+"'}");
		    // Define the image shadowbox variables
		    
		});

		var mygallery = eval('['+jQuery.trim(elemarray.toString())+']');
		// Get the array as a string
		
		Shadowbox.open(mygallery, {
		    continuous: true,
		    title: 'Gallery'
		});
			  
};

function mySbOpen(){
	var c = Shadowbox.getCurrent();
	var newLink = jQuery('<a id="sb-nav-print" href="#" title="Print this information">> Print</a>');
	if(jQuery('a#sb-nav-print')!=null){
		jQuery('a#sb-nav-print').remove();
	}
	var html = "<html><head><title>Print this information</title>";
	html = html + "<style>body{font-family:verdana,sans-serif;}#sb-wrapper{height:auto;width:auto;}#sb-wrapper ol{background:none;font-family: verdana, helvetica, sans-serif; font-size: 11px;}#sb-nav{display:none;}#sb-nav-next{display:none;}#sb-wrapper-inner{height: 1200px !important;overflow:visible !important;}#sb-counter{display:none;}#sb-nav-previous{display:none;}#sb-overlay{display:none;}p.footnote{width: 400px;font-family: verdana, helvetica, sans-serif;}h3{font-family: verdana, helvetica, sans-serif;}div#sb-wrapper{left:20px !important;top:40px !important}#sb-info{height:50px;clear: both;}div#property-google-map{width:662px;height:313px;margin: 70px auto 0px auto;padding: 0px 20px;}div#property-google-map div {filter:none !important;background:transparent !important;}#sb-body h3{padding:4px 5px;background:  #4c7a4d;margin: 0px 0px 20px 0px;	color: #fff;font-size: 12px;font-weight:normal;text-transform: uppercase;}#sb-title-inner span{font-size:9px;}.room-details {font-size:10px;color:width: auto;padding: 11px 0;}.floor-plan-room {color:  #568544;}.roomnum {float: left;width: 14px;padding: 2px 0 2px 8px;background: #e5ece5;}.roomname {float: left;width: 199px;padding: 2px 0 2px 17px;background: #d1ddd1;}.roomdims {float: left;width: 330px;padding: 2px 0 2px 17px;background: #eaeeea;}p.footnote {color: #aebfaf;font-size: 10px;}#sb-body h2.press-release {color:#003300;font-weight:  bold;margin-top:  50px;font-size: 17px;}#sb-body h4 {color:  #7a7a7a;font-weight:  normal;font-size: 10px;margin-top:  4px;}#sb-body p.press-release {width: 472px;line-height: 16px;color:  #003300;font-weight:  bold;margin-top:15px;font-size: 13px;}p.press-link{margin-top:12px;padding-top:9px;border-top: 1px dashed #d9e0d9;}</style></head>";
	html = html + "<body><div id='myprint'>"+jQuery('#sb-container').clone().html()+"</div></body></html>";
	//alert(html);
	newLink.insertAfter(jQuery('a#sb-nav-close'));
	jQuery('a#sb-nav-print').click(function(){
		    var theWork =  window.open('','PrintWindow');
			theWork.document.open();
			theWork.document.write(html);
			theWork.document.close();
			theWork.print();
			theWork.close()
      return false;
	});
}

function noSBprint(){
	return true;
}

function mySbGmOpen(){
	var c = Shadowbox.getCurrent();
	var newLink = jQuery('<a id="sb-nav-print" href="#" title="Print this information">> Print</a>');
	if(jQuery('a#sb-nav-print')!=null){
		jQuery('a#sb-nav-print').remove();
	}
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address}, function(results, status) {
	 window.staticMap = "http://maps.google.com/maps/api/staticmap?center="+address+"&zoom=13&size=662x332&maptype=roadmap&markers=icon:"+gmicon+"|"+results[0].geometry.location+"&sensor=false";
	});
	newLink.insertAfter(jQuery('a#sb-nav-close'));
	jQuery('a#sb-nav-print').click(function(){
			var html = "<html><head><script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.min.js'></script><script type='text/javascript'>jQuery(document).ready(function(){jQuery('#myprint img').load(function(){window.print();window.close();});});</script><title>Print this information</title>";
			html = html + "<style>body{font-family:verdana,sans-serif;}h3{padding:4px 5px;background:#4c7a4d;margin:0px 0px 20px 0px;color:  #fff;font-size:12px;font-weight:normal;text-transform:uppercase;width:632px;}p{padding:4px 5px;margin:0px 0px 15px 0px;color:#666;font-size:11px;font-weight:normal;width:632px;}</style></head>";
			html = html + "<body><div id='myprint'><h3>"+proptitle+" <span>~</span> "+propaddress1+", "+propcounty+", "+proppostcode+"</h3><p>"+propexcerpt+"</p><img src='"+staticMap+"' /></div></body></html>";
		    var theWork =  window.open('','PrintWindow');
			theWork.document.open();
			theWork.document.write(html);
			theWork.document.close();
			//theWork.print();
      return false;
	});
}

function myOfGmOpen(address, gmicon, proptitle, propaddress, latlng){
	var c = Shadowbox.getCurrent();
	var newLink = jQuery('<a id="sb-nav-print" href="#" title="Print this information">> Print</a>');
	if(jQuery('a#sb-nav-print')!=null){
		jQuery('a#sb-nav-print').remove();
	}
	if(latlng){
		window.staticMap = "http://maps.google.com/maps/api/staticmap?center="+address+"&zoom=13&size=800x600&maptype=roadmap&markers=icon:"+gmicon+"|"+latlng+"&sensor=false";
	} else {
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status) {
		window.staticMap = "http://maps.google.com/maps/api/staticmap?center="+address+"&zoom=13&size=800x600&maptype=roadmap&markers=icon:"+gmicon+"|"+results[0].geometry.location+"&sensor=false";
		});
	}
	newLink.insertAfter(jQuery('a#sb-nav-close'));
	jQuery('a#sb-nav-print').click(function(){
			var html = "<html><head><script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.min.js'></script><script type='text/javascript'>jQuery(document).ready(function(){jQuery('#myprint img').load(function(){window.print();window.close();});});</script><title>Print this information</title>";
			html = html + "<style>body{font-family:verdana,sans-serif;}h3{padding:4px 5px;background:#4c7a4d;margin:0px 0px 20px 0px;color:  #fff;font-size:12px;font-weight:normal;text-transform:uppercase;width:632px;}p{padding:4px 5px;margin:0px 0px 15px 0px;color:#666;font-size:11px;font-weight:normal;width:632px;}</style></head>";
			html = html + "<body><div id='myprint'><h3>"+proptitle+"</h3><p>"+propaddress+"</p><img src='"+staticMap+"' /></div></body></html>";
		    var theWork =  window.open('','PrintWindow');
			theWork.document.open();
			theWork.document.write(html);
			theWork.document.close();
			//theWork.print();
      return false;
	});
}

function hideshowTown(){
	jQuery(".posttown, .subposttown").each(function(){
			if(jQuery(".county-header p a#completed").css("cursor") == "pointer"){
				var alterid = 'completed';
			} else {
				var alterid = 'current';
			}
			//alert(alterid);
			var townid = getID(jQuery(this).attr('id'));
			var propcount = jQuery('li.townprop-'+townid).not('.'+alterid).size();
			//alert(propcount);
			if(propcount < 1){
				jQuery('#town-'+townid).slideUp();
				jQuery('#town-'+townid).slideUp(function(){
				
					if(jQuery(this).hasClass('subposttown')){
						jQuery('#town-'+townid).prev('.posttown').slideUp(); 
					} else {
					}	
				}); 
			} else {
				jQuery('#town-'+townid).delay('600').slideDown(function(){
					if(jQuery(this).hasClass('subposttown')){
						jQuery('#town-'+townid).prev('.posttown').slideDown(); 
					} else {
					}	
				});	
			}
		});	
}

function clearOverlays(markersArray) {
  if (markersArray) {
	   for(var i=0; i<markersArray.length; i++){
	       markersArray[i].setMap(null);
	   }
   };

}

function clearinfoWindow(infoWindowArray) {
  if (infoWindowArray) {
    for(var i=0; i<infoWindowArray.length; i++){
        infoWindowArray[i].close();
    }
  }
}

function showOverlays(markersArray) {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(mapall);
    }
  }
}

function setprintableMap(){
	singlemap();
	mySbGmOpen();
}

function houseCount(whattocount){
	if(whattocount == 'noclassplease'){
		jQuery(".subposttown").each(function(){
			var subtownid = jQuery(this).attr('id');
			var propcount = jQuery('#'+subtownid+' ul li').not('.showprops').size();
			if(propcount > '1'){			
				var housevar = 'houses';		
			} else {
				var housevar = 'house';
			}
			jQuery('#'+subtownid+' ul li.showprops a').text(propcount+' '+housevar);
		});
	} else {
		jQuery(".subposttown").each(function(){
			var subtownid = jQuery(this).attr('id');
			var propcount = jQuery('#'+subtownid+' ul li.'+whattocount).not('.showprops').size();
			if(propcount > '1'){			
				var housevar = 'houses';		
			} else {
				var housevar = 'house';
			}
			jQuery('#'+subtownid+' ul li.showprops a').text(propcount+' '+housevar);
		});	
	}
}

