var dh1=0;
var dh2=0;
var dm1=0;
var dm2=0;
var distance_active=false;
var mySlideA='';

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 ($defined($("dist_from"))){
	   mySlideA = new DSlider($('slider_minmax_gutter_m'), $('slider_minmax_minKnobA'),$('slider_bkg_img'), {
		start: 0,
		end: 100,
		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")<100)){ 
					this.setMin(0);
					this.setMax(100);		 		
			}
		}
	}, $('slider_minmax_maxKnobA')).setMin($('dist_from').get("value")).setMax($('dist_to').get("value"));
	SetDistanceNew($('dist_from').get("value"),$('dist_to').get("value"));
	}
	
	
  	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){
    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;
	   if ($defined($("dist_from")) &&  ($('dist_from').get("value")>0 || $('dist_to').get("value")<100)) {
	      		mySlideA.setMin(0);
				mySlideA.setMax(100);		
	   }
	}	
	
}







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;
		}
	});
	TrainRequest.get({'projectid': val});
	return false;
}

function SetDistance(){
	if (distance_active==false)return false;
	alertbox.prompt('Please, set distance range between <b>1</b> and <b>100</b> miles. <br>Or press "Cancel" button to display all distances<br><br>Example: <b>20-50</b>','',
	  { onComplete: 
	 	function(returnvalue) {
			vals=returnvalue.split('-');
			if (returnvalue.contains("-") && $defined(vals[0]) && $defined(vals[1])){
				d1=vals[0].toInt();
			    d2=vals[1].toInt();
				if (d2>d1 && d1>=0 && d2<=100){
					$("distance").set("value",returnvalue);
					$("distance_show").set("html",returnvalue+" miles");				
				}				
				else {return false;}
			}
			else { return false; }
	    }
	  }
	); 
}

function SetDistanceNew(d1,d2){
	if (d2>d1 && d1>=0 && d2<=100){
			$("distance").set("value",d1+'-'+d2);
			$("distance_show").set("html",d1+'-'+d2+" miles");			
			if (d1==0 && d2==100)$("distance_show").set("html","Any");
		    $("dist_from").set("value",d1);	
			$("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_activities.php?rand='+$time(),
		method: 'get',
		onSuccess: function(tree,elements,html){
        	resultdiv.set("text","");
			$("searchresults_container").removeClass('ajax-loading');
			resultdiv.adopt(tree);
			resultdiv.fade(1);
		},
		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');
}




