window.addEvent('domready', function(){

	$$('div.callballoon').each( function(el){
		window.setTimeout(function(){
			var collapse = el.effects({ duration: 700 });
			collapse.start({'width': 76 });
		}, 1000);
	});

	if ($('mapcontainer')) {
		var disMap = new Map.AreaPropsAndLocalSearch($('mapcontainer'));
	}

	if ($('smallmap')) {
		var disMap = new Map.HomeLocation($('smallmap'), {showExtraControls: false});
	}

	if ($('LocationSelector')) {
		new LocationSelector($('LocationSelector'));
	}

	$$('div.result').each( function(el){
		new ResultClicker(el);
	});

	// Get Live Fors
	$$('.getlivefors').each( function(el){
		new Ajax('/livefor/recentthree', { method: 'get', update: el }).request();
	});
	$$('#breadcrumbs .bookmarks').each( function(el){ insertBookmarks(el); });

	// Property Track
	$$('meta').each( function(el){
		if (el.getProperty('name') == 'track'){
			PropertyTracking(el);
		}
	});
	
	// Google Analytics
	if(typeof urchinTracker == "function"){
		_uacct = "UA-356557-4";
		urchinTracker();
	}

	
	$$('.conversioncount').each( function(el){
	/*
		el.addEvent('click', ConversionCount );
	*/
	});


	$$('.googleme').each( function(el){
		el.addEvent('click', openGoogleMaps.bindWithEvent(this));
	});

});

function ConversionCount(){
	/* 
	if(typeof urchinTracker == "function"){
		_uacct = "UA-4427107-4";
		urchinTracker("/4204076685/goal");
	}
	*/
}

function PhotoCount() {
	digits = [ "15", "18", "1", "26", "11", "6", "31", "14", "12", "22", "10", "7", "3", "14", "16", "13" ]
	var digit = digits[Math.floor(Math.random()*digits.length)]
	document.write(digit) 
}

function insertBookmarks(el){
	var url = escape(window.location.href);
	var title = escape(document.title);
	new Ajax('/bookmarkme.html', {
		method: 'get',
		update: el,
		onComplete: function(){
			el.getElements('a').each( function(link){
				link.href = link.href.replace('[[[url]]]', url).replace('[[[title]]]', title);
			});
		}
	}).request();
}

function openGoogleMaps(e) {
	e.stop();
	var dis = $(e.target);
	var newWindow = window.open('http://maps.google.com/?q=' + encodeURIComponent(dis.getProperty('href')), '_blank');
	newWindow.focus();
}


function PropertyTracking(el){

	var params = {}

	// give it the base track type
	params['t'] = 'v';

	// add in the property id
	if (el.getProperty('content')) {
		params['p'] = el.getProperty('content');
	}

	// check for cookie tokens, and add them in
	if (Cookie.get('visitor')) {
		params['cv'] = Cookie.get('visitor');
	}

	// tack on the adwords tracking token
	if (queryString('ut') != 'false') {
		params['ut'] = queryString('ut');
	}

	// throw in the referrer
	if (document.referrer != '') {
		params['ref'] = document.referrer;
	}

	// send the POST request
	new Ajax(('/apartment_finder/track.aspx'), { method: 'post', data: params }).request();
}


function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q && this.q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

var ResultClicker = new Class({

	initialize: function(el){
		this.element = el;
		el.addEvent('click', this.figureAction.bindWithEvent(this))
		.addEvent('mouseover', this.addHover.bind(this))
		.addEvent('mouseout', this.removeHover.bind(this));
	},

	figureAction: function(e){
		var dis = $(e.target);
		if (dis.getTag() != 'a') {
			url = this.element.getElements('h2.fn a')[0].getProperty('href');
			window.location.href = url;
		}
	},
	
	addHover: function(){
		this.element.addClass('hover');
	},
	
	removeHover: function(){
		if (this.element.hasClass('hover')) {
			this.element.removeClass('hover');
		}
	}

});

