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 preset_location_count=0;
var distance_active=false;
var intslider=0;
var actypes_status=0;
var dest_status=0;
var loc_status=0;
var allmaxy=1;
window.addEvent('domready', function(){ 
	alertbox = new SexyAlertBox();
	if ($defined($('actypeselect_txt')))$('actypeselect_txt').set('html',$("activity").options[$("activity").selectedIndex].text);
	if ($defined($('destselect_txt')) && $("property").selectedIndex>0)$('destselect_txt').set('html',$("do_"+$("property").selectedIndex).get("html"));
	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);
}	

  	intslider = new Slider($("intensity_slider"), $("intensity_arrow"), {
		steps: 111,	
		range: [0,110],	
		wheel: true,
		onChange: function(value){
			$('intensity').set('value', Math.round(value/10));
			setForm();
			update_form_look('intensity');
		}
	}).set($('intensity').get("value")*10);
	
	setDuration("");
	setForm();
//   $('intensity_div').addClass('px0');
//  $('duration_div').addClass('px0');
//   $('distance_div').addClass('px0');
		$('intensity_div').fade(0.4);
		$('duration_div').fade(0.4);
		$('distance_div').fade(0.4);
   showmorefilters($('morefilterspreset').get("html"));
   
   
   $("demo-remote").addEvent('focus',function(){this.value = (this.value=='Type keywords here')?'':this.value;});
   $("demo-remote").addEvent('blur',function(){this.value=(this.value=='')?'Type keywords here':this.value;});
   $("demo-remote").value=($("demo-remote").value=='')?'Type keywords here':$("demo-remote").value;
});
	

window.addEvent('load', function(){
   	 
}); 
	
	
	
	






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)");
	update_form_look('duration');
}



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', '');
	}
	if ($defined($('locselect_txt')) && $("location").selectedIndex>0)$('locselect_txt').set('html',$("location").options[$("location").selectedIndex].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_locationNew.php?projectid='+val+'&activity='+$("activity").options[$("activity").selectedIndex].value+'&rand='+$time(),
		method: 'get',
		onSuccess: function(tree,elements,html){
			resultdiv.set('text', '');
			resultdiv.adopt(tree);
			setForm();
		},
		onFailure: function(){
			return false;
		}
	});
	//if ($("location").selectedIndex > 0) {
	//	preset_location = $("location").options[$("location").selectedIndex].value;
	//}
	
	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 (trim($("demo-remote").get("value"))=='Type keywords here')$("demo-remote").set("value",'');
	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 searchActivityKeyword(val){
	if (trim($("demo-remote").get("value"))=='Type keywords here')$("demo-remote").set("value",'');
	if (trim($("demo-remote").get("value"))=='' || 'Type keywords here')return false;
	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 searchActivityKeyword2(val){
	if (trim($("demo-remote").get("value"))=='Type keywords here')$("demo-remote").set("value",'');
	if (trim($("demo-remote").get("value"))=='')return false;
	var resultdiv = $("searchresults");
	$("searchresults_container").addClass('ajax-loading');
	resultdiv.fade(0);
	var TrainRequest = new Request.HTML({
		url: '/arequests/search_activitiesKeyword.php?rand='+$time(),
		method: 'get',
		onSuccess: function(tree,elements,html){
			resultdiv.set("html",html);
			resultdiv.fade(1);
			$("searchresults_container").removeClass('ajax-loading');
			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');
}


function update_form_look(utype){
	if (utype=='activity'){
		if ($("activity").selectedIndex==0){}
		else {
			$('destination_div').removeClass('px0');
			if ($('destination_select').getStyle('display')=='none'){
				$('location_div').removeClass('px0');
				if ($("property").selectedIndex>0)SetLocations($("property").options[$("property").selectedIndex].value,$("select_location"));
				update_form_look('location');
			}
			else update_form_look('property');
		}
	}
	if (utype=='property'){
		if ($("property").selectedIndex==0){}
		else {
			SetLocations($("property").options[$("property").selectedIndex].value,$("select_location"));
		}
	}
}

function showmorefilters(el){
	if (el=='checked'){
		$('intensity_div').fade(1);
		$('duration_div').fade(1);
		$('distance_div').fade(1);
	}
	else if (el.checked==true){
		$('intensity_div').fade(1);
		$('duration_div').fade(1);
		$('distance_div').fade(1);
	}
	else {
		$('intensity_div').fade(0.4);
		$('duration_div').fade(0.4);
		$('distance_div').fade(0.4);
	}
}


function showactypes(){
	if (dest_status==1) showdestypes();
	if (loc_status==1)showloctypes();
	var pos = $('actypeselect').getPosition();
	if (actypes_status==1) {
		actypes_status=0;
		$("dcats_all").morph({
			height: 0,
			width: 540,
			left: pos.x - 555,
			display:'none'
		});	
	}
	else {
		actypes_status=1;
		$("dcats_all").setStyle('height', '0px');
		$("dcats_all").setStyle('width', '540px');
		$("dcats_all").setStyle('top', pos.y - 460);
		$("dcats_all").setStyle('left', pos.x - 555);	
	    
		var myChain3 = new Chain();
	    myChain3.chain(function(){$("dcats_all").morph({duration:50,height: 1,width: 540,left: pos.x - 555,display:'block'});});
	    myChain3.chain(function(){CleanActRows();});
  	    var runChain3 = function() {if (myChain3.callChain()==false){ runChain3 = $clear(timer3);}} 
        var timer3 = runChain3.periodical(25);  
	}
}

function showdestypes(){
	if (loc_status==1)showloctypes();
	var pos = $('destselect').getPosition();
	if (dest_status==1) {
		dest_status=0;
		$("dest_all").morph({
			height: 0,
			width: 540,
			left: pos.x - 555,
			display:'none'
		});	
	}
	else {
		dest_status=1;
		$("dest_all").setStyle('height', '0px');
		$("dest_all").setStyle('width', '360px');
		$("dest_all").setStyle('top', pos.y - 460);
		$("dest_all").setStyle('left', pos.x - 1072);	
	    $("dest_all").morph({duration:50,height: 230,width: 360,left: pos.x - 1072,display:'block'});  
	}
}

function showloctypes(){
	var pos = $('locselect').getPosition();
	if (loc_status==1) {
		loc_status=0;
		$("loc_all").morph({
			height: 0,
			width: 540,
			left: pos.x - 555,
			display:'none'
		});	
	}
	else {
		loc_status=1;
		$("loc_all").setStyle('height', '0px');
		$("loc_all").setStyle('width', '350px');
		$("loc_all").setStyle('top', pos.y - 460);
		$("loc_all").setStyle('left', pos.x - 1063);	
		if ($("property").selectedIndex>0)$("propname").set("html",$("property").options[$("property").selectedIndex].text);
		var myChain3 = new Chain();
	    myChain3.chain(function(){$("loc_all").morph({height: 1,width: 350,left: pos.x - 1063,display:'block'});});
	    myChain3.chain(function(){
			size=$('loclistul').getSize();
			$("loc_all").morph({duration:50,height: size.y+50});
		});
  	    var runChain3 = function() {if (myChain3.callChain()==false){ runChain3 = $clear(timer3);}} 
        var timer3 = runChain3.periodical(25);  
	}
}




function setAcat(acval,actxt){
	$('actypeselect_txt').set('html',actxt);
	if ($defined($("activity"))) {
		$("activity").selectedIndex = acval;
		setForm();
		preset_location = 0;
		if ($defined($("location")))$("location").selectedIndex = 0;
		update_form_look('activity');
	}
	showactypes();
}
function setDcat(acval,actxt){
	$('destselect_txt').set('html',actxt);
	if ($defined($("property"))) {
		$("property").selectedIndex = acval;
		   SetLocations($("property").options[$("property").selectedIndex].value,$("select_location"));
		   update_form_look('property');	
	}
	showdestypes();
}
function setLcat(acval,actxt){
	$('locselect_txt').set('html',actxt);
	if ($defined($("location"))) {
		$("location").selectedIndex = acval;
		   update_form_look('location');	
	}
	showloctypes();
}












function CleanActRows(){
	allmaxy=50;
	var crows=$('crows').value;
	if (crows>0){
		for (i = 1; i <= crows+1; i++) {
			maxy=0;
			$$('.row_'+i).each(function(item, index){
   				 size=item.getSize();
				 if (size.y>maxy) maxy=size.y-5;
			});
			if (maxy>0){
				$$('.row_'+i).morph({height: maxy});
				allmaxy=allmaxy+maxy+5;
			}
		}
		$("dcats_all").morph({height: allmaxy});
	}
}






