var myCal = null;

function SwitchCalendar(value, id){
	if(!value && $('cal-frame').getStyle('opacity') == 1){		myCal.Disabled = true;		UpdateCalendar();
		new Fx.Style('cal-frame', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(0.3);
	} else if(value > 0) {		myCal.Disabled = false;
		GetAvailableDate(value);
		new Fx.Style('cal-frame', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(1);
	}
}

function LoadPrevMonth(){
	if (!myCal) { return; }
	if (myCal.month == 1){
	 	myCal.month = 12;
	 	myCal.year = (myCal.year - 1) 
	} else {
		myCal.month = (myCal.month - 1);
	} 
	UpdateCalendar();
}

function LoadNextMonth(){
	if (!myCal) { return; }
	if (myCal.month == 12){
	 	myCal.month = 1;
	 	myCal.year = (myCal.year + 1) 
	} else {
		myCal.month = (myCal.month + 1);
	} 
	UpdateCalendar();
}

function InitCalendar(){
	myCal = new CALENDAR();
	var opacity_level = 0.3;
	var curr_option = '';
	if($('region') != null){
		opacity_level = $('region').options[$('region').selectedIndex].value ? 1 : 0.3;
		curr_option = $('region').options[$('region').selectedIndex].value;
	}
	$('cal-frame').setStyle('opacity', opacity_level);
	UpdateCalendar();
	SwitchCalendar(curr_option, '');
}

function GetAvailableDate(id){
	$('loading').setStyle('display', 'block');
	axSend('/build-your-lot.html?S2XEvent=GetDates&id=' + id, 'get', DisplayResults);
}

function DisplayResults(){
	if(!axCheckResponse()){ return; }
	eval('myCal.AvailableDates = [' + axResponse() + '];');
	$('loading').setStyle('display', 'none');
	UpdateCalendar();
}

function UpdateCalendar(){
	document.getElementById('calendar-container').innerHTML = myCal.create();
}

function SwitchTo(name){
	if($('region') != null){
		name = name.replace('_', ' ');
		var sz = $('region').options.length;
		for(i = 0; i < sz; i++){
			if ($('region').options[i].innerHTML == name){
				$('region').options.selectedIndex = i;
				SwitchCalendar($('region').options[$('region').selectedIndex].value, '');
				break;
			}
		} 
	} else {
		SwitchCalendar(name, '');
	}
	
}

function ShowForm(val){
	$('axContainer').setStyle('opacity', 1);
	new Fx.Style('axContainer', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(1, 0).addEvent('onComplete', function(){
		axSend('/build-your-lot.html?S2XEvent=ShowForm&rand=' + Math.random() + '&date=' + val + '&com_id=' + ($('region') == null ? currCommunityId : $('region').options[$('region').selectedIndex].value), 'get', DisplayForm);
	});
	return false;
}

function DisplayForm(){
	if(!axCheckResponse()){ return; }
	axPut('axContainer', axResponse());
	new Fx.Style('axContainer', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(0, 1);
}

function SubmitForm(){
	axSubmitForm($('fmAppointment'), FormCallback, $('btnSubmit'));
	return false;
}

function FormCallback(){
	if(!axCheckResponse()){ return; }
	var arr = axResponse().split(':');
	if (arr[0] == 'ERROR') {
		$('error-msg').innerHTML = arr[2]; 
	}
	if (arr[0] == 'DONE'){
		new Fx.Style('appointment-block', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(1, 0).addEvent('onComplete', function(){
			$('appointment-block').setStyle('display', 'none');
			$('ThankYou').setStyle('opacity', 0);
			$('ThankYou').setStyle('display', 'block');
			new Fx.Style('ThankYou', 'opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(0, 1);
		});
	}
}

window.onload = DomReady;

function DomReady(){
	try{
		if(meet_team){
			var list = document.getElementsByTagName('a');
			for(i = 0; i < list.length; i++){
				if (list[i].rel == 'image'){
					list[i].onmouseover = function(){ 
						if(this.className != 'selected') {
							this.childNodes[0].src = this.childNodes[0].src.replace('small', 'small-hover');
						}	 
					}
					list[i].onmouseout = function(){ 
						if(this.className != 'selected') {
							this.childNodes[0].src = this.childNodes[0].src.replace('small-hover', 'small');
						}	 
					}
				}
			}
		}
	} catch(e){}
}

function ViewProperty(id){
	axSend('/active-adult.html?S2XEvent=View&id=' + id + '&rand=' + Math.random(), 'get', GetProperty);
	if($('view-info').style.display != 'block'){
		$('view-info').setStyle('display', 'block');
		new Fx.Style('view-info', 'opacity', {duration: 500, transition: Fx.Transitions.linear}).start(0, 1);
	}
	return false;
}

function ViewPrevProperty(){
	axSend('/active-adult.html?S2XEvent=ViewPrev&id=' + $('view-next').rel + '&rand=' + Math.random(), 'get', GetProperty);
	
	return false;
}

function ViewNextProperty(){
	axSend('/active-adult.html?S2XEvent=ViewNext&id=' + $('view-next').rel + '&rand=' + Math.random(), 'get', GetProperty);
	
	return false;
}

function GetProperty(){
	if(!axCheckResponse()) { return; }
	var answer = axResponse().split('###');
	$('property-photo').src = './public/files/properties/' + answer[0];
	$('property-photo').alt = answer[1];
	$('property-title').innerHTML = answer[1] + " (" + answer[5] + ")";
	$('view-prev').rel = answer[4];
	$('view-next').rel = answer[4];
	$('property-description').innerHTML = ('<p>' + answer[2] + '</p>');
	$('property-amenities').innerHTML = ('<p>' + answer[3] + '</p>');
}

function CloseViewer(){
	new Fx.Style('view-info', 'opacity', {duration: 500, transition: Fx.Transitions.linear}).start(1, 0).addEvent('onComplete', function(){
		$('view-info').setStyle('display', 'none');
	});
}

function DisableForm(){
	$$('.appointment-block input').each(function(el){
		el.disabled = "true";
	});
	$$('.appointment-block textarea').each(function(el){
		el.disabled = "true";
	});
}

window.addEvent('domready', function()
{
    $$('.main-menu A').each(function(el){
        var className = '.' + el.className;
        el.addEvent('mouseenter', function(e){
            new Event(e).stop();            
            $$(className).each(function(el){ el.addClass('hover'); });
        });
        el.addEvent('mouseleave', function(e){
            new Event(e).stop();
            $$(className).each(function(el){ el.removeClass('hover'); });
        });
    });
});