// print event
Event.observe(window,'load',function(){
	$$('a.print_property').each(function(el){Event.observe(el,'click',function(event){event.stop();printProperty(el)})})
});

// toggle display of the what to do next layer
function whatLayer(){
	if($('what_overlay').visible()){
		Effect.SlideUp('what_overlay',{duration:0.2});		
	}else{
		Effect.SlideDown('what_overlay',{duration:0.2});
	}
}

// AJAX call to the tabbed page
function homePage(url){
	new Ajax.Updater('body_home',url,{parameters:{'ajax':1},onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3)},evalScripts:true});
}
// AJAX call to the photo page
function photoPage(url,home_room_id){
	new Ajax.Updater('body_home',url,{parameters:{'ajax':1,'home_room_id':home_room_id},onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3)},evalScripts:true});
}

// Image gallery slideshow
// elm: the id of the div to style with the image
// home_id: the id of home for the image
// query: the json query of image (NEEDS .DATA.FILENAME but can include others) 
// dir: 'next' or 'prev' - obvious!
// elm_summary: the id of the summary div (if not specified, it's ignored) 
function imageSlide(elm,home_id,query,dir,elm_summary){
	var image=0,
			prev_id='slide_prev', // the id of the prev href 
			next_id='slide_next', // the id of the next href 
			disable_slide=0,
			disable_prev=0,
			disable_next=0,
			output_summary='',
			query_rows=query.ROWCOUNT-1,
			host_name='http://'+window.location.hostname,
			current_image=$(elm).style.backgroundImage.replace(host_name,'').replace('"','').replace('"','').replace('url(/assets/images/home/home'+home_id+'/large/','').replace(')','');
			current_image_index=query.DATA.FILENAME.indexOf(current_image);
	// setup the var for simple call to tell to go next or prev
	if(dir=='next'){var go_prev=0,go_next=1}
	else{var go_prev=1,go_next=0};
	// work out the image index now
	if(go_next){image=current_image_index+1;}else if(go_prev){image=current_image_index-1}else{disable_slide=1};
	// work out if the current image is the first or last image in the set 
	if(current_image_index==0){disable_prev=1;}
	else if(current_image_index==query_rows){disable_next=1;}
	// do we need to disable the previous button
	if(image==0){$(prev_id).addClassName('slide_disabled');}
	else if(image>=0){$(prev_id).removeClassName('slide_disabled');}
	// do we need to disable the next button
	if(image==query_rows){$(next_id).addClassName('slide_disabled');}
	else if(image<=query_rows){$(next_id).removeClassName('slide_disabled');}
	// add a hack to prevent ANY change if theres only one record 
	if(!query_rows){
		disable_slide=1;
		disable_prev=1;
		disable_next=1;
		$(prev_id).addClassName('slide_disabled');
		$(next_id).addClassName('slide_disabled');
	}
	// can they do the dir they want?
	if(go_next && disable_next){disable_slide=1}
	else if(go_prev && disable_prev){disable_slide=1}
	if(!disable_slide){
		// not disabled, go to the selected image
		$(elm).setStyle({backgroundImage:'url(/assets/images/home/home'+home_id+'/large/'+query.DATA.FILENAME[image]+')'});
		// if we got a summary elm, update the innerHTML of it
		if(elm_summary){
			if(query.DATA.NAME_ROOM[image] != null){output_summary='<h3>'+query.DATA.NAME_ROOM[image]+'</h3>'}else{output_summary=''}
			if(query.DATA.SUMMARY[image] != null){output_summary=output_summary+'<span class=\"alt_font\">'+query.DATA.SUMMARY[image]+'</span>'}
			if(output_summary.length){$(elm_summary).show();$(elm_summary).innerHTML=output_summary;}else{$(elm_summary).hide()}
		}
	}
}

// photos hover state
function photoHover(photoid){
	if(!$('photo_summary_'+photoid).visible()){
		$('photo_href_'+photoid).addClassName('photo_href_bg');
		$('photo_summary_'+photoid).show()
	}else{
		$('photo_href_'+photoid).removeClassName('photo_href_bg');
		$('photo_summary_'+photoid).hide()
	}
}

// AJAX watch property
function watchProperty(home_id){
	new Ajax.Updater('watchproperty','/property/'+home_id+'/watching/watch/',{parameters:{'ajax':1},evalScripts:true,onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3)}});	
}

// print property window
function printProperty(el){
	window.open(el.href,'printable_property','width=790,height=700,menubar=1,resizable=0,scrollbars=1,toolbar=0,status=0')
}