$(document).ready(function() {


	//// fix WYSIWYG images
	$('#content-primary img[align]').addClass('floated');
	$('#content-primary img[align=right]').addClass('alt');



	$('#content-primary .options .view a').click(function() {

		//$(this).addClass('current').siblings('a').removeClass('current');
		var viewclass = $(this).attr('class').replace('current','');

		$('#content-primary .rewidx-results').attr('class','rewidx-results');
		$('#content-primary .rewidx-results').addClass(viewclass);

		$('#content-primary .options .view a.' + viewclass).addClass('current').siblings('a').removeClass('current');

		$.cookie('listingsview', viewclass);
		return false;
	});

	$('#content hr').wrap('<div class="rule"></div>');

	// featured listings on homepage

	$('#featured-listings').after('<div id="nav-slides"><a href="" id="nav-prev" title="Previous Listing">Previous</a><a href="" title="Next Listing" id="nav-next">Next</a></div>').cycle({
		speed:  'slow',
		timeout: 10000,
		prev: '#nav-prev',
		next: '#nav-next',
		pause:   1
	});

	function onAfter(curr, next, opts) {
		var index = opts.currSlide;
		$('#nav-prev')[index == 0 ? 'hide' : 'show']();
		$('#nav-next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
	}

	if(listingsview = $.cookie('listingsview')) {
		$('.rewidx-results').attr('class','rewidx-results');
		$("." + listingsview.replace(' current','')).addClass('current');
		$('.rewidx-results').addClass(listingsview);
	} else {
		//$('.options a').attr('class','');
		$('.options a:first').addClass('current');
		$('.rewidx-results').addClass('viewas-grid');
	}

	initForms($('body'));

	$('.idx-results .listing').each(function(){
		var listing = $(this);
		var url = $('a:first',listing).attr('href');
		var img = $('.imgset img', listing);
		var imgH = img.outerHeight();
		var imgW = img.outerWidth();
		var imgHe = img.outerHeight() *1.5;
		var imgWe = img.outerWidth() *1.5;
		var mLft = -(imgHe - imgH) / 2;
		var mTop = -(imgWe - imgW) / 2;

		listing.click(function() {
			document.location = url;
		})

		.css({cursor:'pointer'});

	});


	/* QUICK SEARCH */

			$('#content-secondary .quick-search').each(function() {
					var qSearch = $(this);

					$('.nav a', qSearch).hover(
						function() {
							var mPos = '';
							var pLink = $(this).parent('li').attr('class').replace('navi-qs-','');
							if(pLink=='YT') {
								mPos = '-294px 0';
							} else if(pLink=='NT') {
								mPos = '-588px 0px';
							} else if(pLink=='NV') {
								mPos = '-882px 0px';
							} else if(pLink=='PE') {
								mPos = '0 -252px';
							} else if(pLink=='NF') {
								mPos = '-294px -252px';
							} else if(pLink=='QC') {
								mPos = '-588px -252px';
							} else if(pLink=='ON') {
								mPos = '-882px -252px';
							} else if(pLink=='SK') {
								mPos = '-294px -504px';
							} else if(pLink=='AB') {
								mPos = '-588px -504px';
							} else if(pLink=='BC') {
								mPos = '-882px -504px';
							} else if(pLink=='NS') {
								mPos = '-294px -758px';
							} else if(pLink=='NB') {
								mPos = '0 -758px';
							} else if(pLink=='MB') {
								mPos = '0 -504px';
							}

							$('.quick-search .map').css('background-position', mPos);
						},
						function() { $('.quick-search .map').css('background-position','0 0'); }
					);
			});

	$('#content-primary .bio .rewidx-results').attr('class','rewidx-results viewas-list');
	$('body.home .rewidx-results').attr('class','rewidx-results viewas-grid');

});




/* FUNCTIONS */



function initForms(parent) {
	$('form input[type=text], form textarea, form input[type=password], select.multi', parent).not(':hidden').wrap('<span class="txtfield"></span>');
	$('form select', parent).not('.multi').wrap('<span class="optfield"></span>');
	$('.toggleset', parent).wrap('<div class="toggleset-w"></div>')
	$('form legend', parent).each(function() {
		var content = $(this).html();
		$(this).after('<h4 class="legend">'+ content +'</h4>');
	});
}


function numAddCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}