var dh1=0;
var dh2=0;
var dm1=0;
var dm2=0;
var preset_duration=0;
var preset_intensity=0;
var preset_distance=0;
var preset_location=0;
var distance_active=false;
window.addEvent('domready', function(){ 
	alertbox = new SexyAlertBox();
	var s1 = new CustomSelect($('activity'), {
    	theme : 'aqua',    	
    	onSelect: function(el) {
    	},
    	onChange: function(el) {
		 setForm();
    	},
    	onShow: function(el) {
    	},
    	onHide: function(el) {
    	}		
    });    
    var s2 = new CustomSelect($('location'), {
    	theme : 'aqua',    	
    	onSelect: function(el) {
    	},
    	onChange: function(el) {
    	},
    	onShow: function(el) {
    	},
    	onHide: function(el) {
    	}		
    }); 		
    var s3 = new CustomSelect($('property'), {
    	theme : 'aqua',    	
    	onSelect: function(el) {
    	},
    	onChange: function(el) {
		   SetLocations($("property").options[$("property").selectedIndex].value,$("select_location"));
    	},
    	onShow: function(el) {
    	},
    	onHide: function(el) {
    	}		
    }); 
	if ($("property").selectedIndex>0)SetLocations($("property").options[$("property").selectedIndex].value,$("select_location"));

	if (preset_distance != '') {
		distance_active=true;		
	}			
	
	
	
	if (preset_intensity>0)$('intensity').set("value",preset_intensity);	
	
if ($defined($("dist_from"))){
	   var dist_from=$('dist_from').get("value").toInt();
	   var dist_to=$('dist_to').get("value").toInt();
	   if (dist_to==100)dist_to=31;
	   mySlideA = new DSlider($('slider_minmax_gutter_m'), $('slider_minmax_minKnobA'),$('slider_bkg_img'), {
		start: 0,
		end: 31,
		offset:10,
		snap:false, 
		onChange: function(pos){ 
			SetDistanceNew(pos.minpos,pos.maxpos);
		},
		onComplete: function(pos){ 
			if (distance_active==false && ($('dist_from').get("value")>0 || $('dist_to').get("value")<31)){ 
					this.setMin(0);
					this.setMax(31);		 		
			}
			else SetDistanceNew(pos.minpos,pos.maxpos); 
		}
	}, $('slider_minmax_maxKnobA')).setMin(dist_from).setMax(dist_to);
	SetDistanceNew(dist_from,dist_to);
}	





	
	
  	new Slider($("intensity_slider"), $("intensity_arrow"), {
		steps: 111,	
		range: [0,110],	
		wheel: true,
		onChange: function(value){
			$('intensity').set('value', Math.round(value/10));
			setForm();
		}
	}).set($('intensity').get("value")*10);
	
	setDuration("");
	setForm();
  
});
	

	
	
	
	
	






function setDuration(val){
	if (preset_duration!=''){
		$('duration').set("value",preset_duration);
		preset_duration=0;
	}
	var step=30;
	var minutes=$('duration').get("value").toInt();
    if (val=='+')minutes=minutes+step;
	if (val=='-')minutes=minutes-step;
	if (minutes<0)minutes=0;
	$('duration').set("value",minutes);
	var hours=minutes/60;
	dh1=Math.floor(hours/10);
	dh2=Math.floor(Math.floor(hours)-dh1*10);
	var mins=Math.floor((hours-Math.floor(hours))*60);
	dm1=Math.floor(mins/10);
	dm2=Math.floor(mins-dm1*10);
	$('dh1').set("src","/tpl/img/act/activities-duration-"+dh1+".gif");
	$('dh2').set("src","/tpl/img/act/activities-duration-"+dh2+".gif");
	$('dm1').set("src","/tpl/img/act/activities-duration-"+dm1+".gif");
	$('dm2').set("src","/tpl/img/act/activities-duration-"+dm2+".gif");
	
	
	if (mins/60>0)half=".5";
	else half="";
	if (Math.floor(hours)==0 && half=="")$('duration_show').set("html","Any");
	else $('duration_show').set("html","About "+Math.floor(hours)+half+" hour(s)");
}



function setForm(){
	
	// Intensity
	if ($('intensity').get("value")>0){
		$('intensity_show').set("html",$('intensity').get("value"));
	}
	else $('intensity_show').set("html","All");
	
	// Locaitions
	if ($("property").options[$("property").selectedIndex].value!=''){
	   $("select_location").fade(1);

	}
	else {
	   $("property").selectedIndex=0;
	   $("select_location").set('text', '');
	}
	
	
	// Distance
	if ($("activity").options[$("activity").selectedIndex].value.contains("checked")){
   	   $("act_distance").fade(1);
	   distance_active=true;
	}
	else {
	   $("act_distance").fade(0.4);
	   distance_active=false;
	   $("distance").set("value","0");	
	   if ($defined($("dist_from")) &&  ($('dist_from').get("value")>0 || $('dist_to').get("value")<31)) {
	      		mySlideA.setMin(0);
				mySlideA.setMax(31);		
	   }	   
	   
	}	
}







function SetLocations(val,resultdiv){
	var TrainRequest = new Request.HTML({
		url: '/arequests/select_locations.php?projectid='+val+'&rand='+$time(),
		method: 'get',
		onSuccess: function(tree,elements,html){
			resultdiv.set('text', '');
			resultdiv.adopt(tree);
			    var s2 = new CustomSelect($('location'), {
			    	theme : 'aqua',    	
			    	onSelect: function(el) {},
    				onChange: function(el) {},
			    	onShow: function(el) {},
    				onHide: function(el) {}	
			    }); 
			setForm();
		},
		onFailure: function(){
			return false;
		}
	});
	if (preset_location>0){ TrainRequest.get({'projectid': val, 'preset': preset_location}); preset_location=0; }
	else TrainRequest.get({'projectid': val});
	return false;
}




function SetDistanceNew(d1,d2){
	if (d2==100)d2=31;
	
	if (d2>d1 && d1>=0 && d2<=31){
			$("distance").set("value",d1+'-'+d2);
			if (d2<31)$("distance_show").set("html",d1+'-'+d2+" miles");			
			else $("distance_show").set("html",d1+'-100 miles');
			if (d1==0 && d2==31)$("distance_show").set("html","Any");
		    $("dist_from").set("value",d1);	
			if (d2==31)d2=100;
			$("dist_to").set("value",d2);	
	}			
}



function searchActivity(val){
	if ($("property").options[$("property").selectedIndex].value.contains('http')){
		document.location.href=$("property").options[$("property").selectedIndex].value;
		return false;
	}
	
	var resultdiv = $("searchresults");
	resultdiv.fade(0);
	$("searchresults_container").addClass('ajax-loading');
	var TrainRequest = new Request.HTML({
		url: '/arequests/search_activitiesNew.php?rand='+$time(),
		method: 'get',
		onSuccess: function(tree,elements,html){
        	document.location.href=html;
			//resultdiv.set("html",html);
			//resultdiv.fade(1);
			return false;
		},
		onFailure: function(){
			return false;
		}
	});
	disable_tabs();
	TrainRequest.post($("sa_form"));
	return false;
}


function hide_edges(){
	$('tab_edge').removeClass('edge_overview');
	$('tab_edge').removeClass('edge_details');
	$('tab_edge').removeClass('edge_packages');
	$('tab_edge').removeClass('edge_photos');
}
function disable_tabs(){
	if ($defined($('tab_edge'))) {
		hide_edges();
		$$('#tabs div').setStyle('display', 'none');
	}
}



function ChangeTab(tabid){
	$$('#tabs div').removeClass('tab_selected');
	$('tab_'+tabid).addClass('tab_selected');
	hide_edges();
	
	$('tab_edge').setStyle('margin-top','0px');	
	if (tabid=='overview')$('tab_edge').setStyle('margin-top','0px');
	if (tabid=='details')$('tab_edge').setStyle('margin-top','104px');
	if (tabid=='packages')$('tab_edge').setStyle('margin-top','187px');
	if (tabid=='photos' && $defined($("tab_packages")))$('tab_edge').setStyle('margin-top','349px');
	if (tabid=='photos' && !$defined($("tab_packages")))$('tab_edge').setStyle('margin-top','187px');
	$('tab_edge').addClass('edge_'+tabid);
	
	$('div_overview').addClass('nodisplay');
	$('div_details').addClass('nodisplay');
	$('div_packages').addClass('nodisplay');
	$('div_photos').addClass('nodisplay');
	$('div_'+tabid).removeClass('nodisplay');
}



