window.addEvent('domready', function() {
	initPhotos();
	$ES('a.toggleProperty').each(function(el) {
		el.addEvent('click', toggleProperty);
	});
	$ES('a.tab').each(function(el) {
		el.addEvent('click', switchTab);
	});
});

Focus = {
	find: function(el) {
		try {
			$ES(el).getLast().focus();
		} catch(ex) {}
	}
}

function initPhotos() {
	if (window.photos && $('prev-photo')) {
		$('prev-photo').addEvent('click', prevPhoto);
		$('next-photo').addEvent('click', nextPhoto);

		var tmp = new Array();
		for (key in photos) {
			tmp.push('thumb.regular.' + photos[key]);
		}
		photos = tmp;
	}
}

function prevPhoto(e) {
	switchPhoto(e, -1);
}

function nextPhoto(e) {
	switchPhoto(e, 1);
}

function switchPhoto(e, step) {	
	e = new Event(e).stop();
	
	var src = $('photo').src.split('/').pop();	
	var idx = photos.indexOf(src);
	idx = idx + step;
	
	if (!photos[idx]) {
		idx = (step < 0) ? photos.length - 1 : 0;
	}
	
//	var original_source = $('photo').src;	
	$('photo').src = '/img/properties/' + photos[idx];
	
//	if (original_source == $('photo').src)
//	{
//		if (step == 1)
//		{
			// next
//			$('photo').src = '/img/properties/thumb.regular.' + photos[photos.length-1];
//		}
		
//		else
//		{
			// previous
//			$('photo').src = '/img/properties/thumb.regular.' + photos[0];
//		}
//	}
	
}

function initAjaxForm() {
	if (!$('ajax-form'))
		return false;

	$('ajax-form').addEvent('submit', submitAjaxForm);
}

function submitAjaxForm(e) {
	e = new Event(e).stop();
	$('ajax-form').send({onComplete: submitComplete, evalScripts: true});
}

function submitComplete(resp) {
	if (resp.substring(0, 4) == 'URL:') {
		window.location = resp.substring(4);
	} else {
		Lightbox.loadContentComplete(resp);
	}
}

function initAjaxForm2() {
	if (!$('ajax-form-2'))
		return false;

	$('ajax-form-2').addEvent('submit', submitAjaxForm2);
}

function submitAjaxForm2(e) {
	e = new Event(e).stop();
	$('ajax-form-2').send({onComplete: submitComplete2, evalScripts: true});
}

function submitComplete2(resp) {
	if (resp.substring(0, 4) == 'URL:') {
		window.location = resp.substring(4);
	} else {
		Lightbox.loadContentComplete(resp);
	}
}

function initRegisterForm() {
	['UserRoleUser', 'UserRoleCompany'].each(function(el) {
		if (!$(el)) return;
		$(el).addEvent('click', toggleRole);
	});
}

function toggleRole(e) {
	e = new Event(e);
	var els = $ES('#ajax-form .company');
	if (e.target.value == 'company') {
		els.each(function(el) { el.removeClass('hidden') });
	} else {
		els.each(function(el) { el.addClass('hidden') });
	}
	
	els = $ES('#ajax-form .user');
	if (e.target.value == 'user') {
		els.each(function(el) { el.removeClass('hidden') });
	} else {
		els.each(function(el) { el.addClass('hidden') });
	}
}

function toggleProperty(e) {
	e = new Event(e).stop();
	var el = e.target.getParent().getParent().getParent();
	el.getElements('.toggle').each(function(toggle) {;
		toggle.toggleClass('hidden');
	});
}

function switchTab(e) {
	e = new Event(e).stop();
	var tabs = $ES('a.tab');
	tabs.each(function(tab) {
		tab.removeClass('selected');
	});
	e.target.addClass('selected');

	$ES('.tab-content').each(function(tab) {
		tab.addClass('hidden');
	});
	var id = e.target.href.split('#').getLast();
	$(id).removeClass('hidden');

	if (id == 'tab3') {
		map.checkResize();
		updateMap();
	}
}

LoginFunctions = {
	showForgotPasswordForm: function() {
		var loginDiv = $('login_div') ;
		var forgottenDiv = $('forgotten_div') ;
		var errorMsg = $$('#forgotten_div .error-message') ;
		
		loginDiv.setStyle('display','none') ;
		forgottenDiv.setStyle('display','block') ;
		if (errorMsg[0]) errorMsg[0].remove() ;
	},
	
	showLoginForm: function() {
		var loginDiv = $('login_div') ;
		var forgottenDiv = $('forgotten_div') ;
		var errorMsg = $$('#login_div .error-message') ;
		
		loginDiv.setStyle('display','block') ;
		forgottenDiv.setStyle('display','none') ;
		if (errorMsg[0]) errorMsg[0].remove() ;
	}
	
}

