// generic close and open
function closeElm(elm){$(elm).hide()}
function openElm(elm){$(elm).show()}

// bind popup windows
Event.observe(window,'load',function(){
	$$('a.popup_window').each(function(i){
		Event.observe(i,'click',function(event){
			event.stop();
			addPopup(i)
		});
	})
});
function addPopup(el){
	window.open(el.href,'popup_tep','width=600,height=800,location=0,menubar=0,scrollbars=1,resizable=1,status=0,toolbar=0')
}

// clear/show contents on a form focus/blur
function fieldLabel(elm,label){
	if($(elm).value==label){$(elm).value=''}
	else if($(elm).value==''){$(elm).value=label}
}

// toggle display of the share layer
function toggleShare(){
	if($('share_tabs').visible()){Effect.BlindUp('share_tabs',{duration:0.2})}
	else{Effect.BlindDown('share_tabs',{duration:0.2});Effect.ScrollTo('shareanchor');}
}

// Limit textareas
function textareaLimit(field,limit){
	if (field.value.length>limit){
		field.value=field.value.substr(0,limit)
	}
}

// toggle header overlay menu 
function toggleMenu(elm){
	// elements all over the shop, just just predefine them in an array now.
	var menu=new Array();
	menu[0]="my_account";
	//menu[1]="messages";
	//menu[2]="properties";
	//menu[3]="searches";
	if($('menu_'+elm).visible()){
		$('li_'+elm).removeClassName('selected');
		Effect.SlideUp('menu_'+elm,{duration:0.2})
	}else{
		// sliding a menu down, so slide all others up first (if they are visible) 
		for (i=0;i<menu.length;i++){
			if($('menu_'+menu[i]).visible()){
				Effect.SlideUp('menu_'+menu[i],{duration:0.1});
				$('li_'+menu[i]).removeClassName('selected');
			}
		}
		$('li_'+elm).addClassName('selected');
		Effect.SlideDown('menu_'+elm,{duration:0.2});
		// make sure the speech bubble is closed regardless.
		$('overlay_speech_top').hide();
	}
}

// toggle display of indicators
function showIndicator(val){openElm('indicator'+val)}
function hideIndicator(val){closeElm('indicator'+val)}

// display window overlay, and define what to populate it with
function toggleWindow(elm){
	// Before anything, loop thru the win_c children and hide regardless... don't want dups
	var children=$('win_c').childElements(); 
	for (i=0;i<children.length;i++){
	$(children[i]).hide();}
	// show the wanted view
	$(elm).show()
	// now the overlay holder
	$('win').show();
}

// AJAX call to a blank page
function blank(elm){new Ajax.Updater(elm,'/site/blank/');}

// simple login AJAX request
function login(){new Ajax.Updater('win_c','/sessions/login/',{parameters:$('login_form').serialize(true),onLoading:function(request){showIndicator(2)},onComplete:function(request){hideIndicator(2)},evalScripts:true});}

// disable a submit button
function disableButton(elm){
	$(elm).hide();
	$(elm+'_wait').show();
	$(elm+'_indicator').style.backgroundImage='url(/images/icons/ajax_arrows_4.gif)'
}

// Search cat selection
function searchCat(category_id,size){
	// basic, reset all tabs first
	$('searchtab_'+size+'_1').removeClassName('selected');
	$('searchtab_'+size+'_2').removeClassName('selected');
	$('searchtab_'+size+'_3').removeClassName('selected');
	$('searchform_'+size).target="_self"
	// now set the correct tab and fill the field
	$('searchtab_'+size+'_'+category_id).addClassName('selected');
	$('searchcatid_'+size).value=category_id;
	if(category_id==3){$('searchform_'+size).target="_blank"}
	}



// show/hide the feedback tab
function feedbackToggle(){
	var xPos=0;
	if($('feedback_c').getStyle('left')=='0px'){xPos=-250}else{xPos=0};
	new Effect.Parallel([new Effect.Move('feedback_c', { sync: true, x: xPos, y: 130, mode: 'absolute' })],{duration: 0.3});
}
// AJAX feedback form
function feedbackSend(){
	// default setup
	var formhash = $('feedbackform').serialize(true);
	new Ajax.Updater('feedback_c','/ask/',{parameters:formhash,onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3)},evalScripts:true});	
}




// AJAX call to the top grey menu for refresh of values
function refreshStats(params){new Ajax.Updater('header_logged_r','/sessions/refresh_stats/',{parameters:params,evalScripts:true});}

// hover toggle tabs - over
function tabOver(value){
	var sep2_value=value+1;
	$('sep'+value).addClassName('sep_hover');
	$('sep_l'+value).addClassName('sep_hover');
	$('nav'+value).addClassName('nav_hover');
	$('sep_r'+value).addClassName('sep_hover');
	$('sep'+sep2_value).addClassName('sep_hover');
}
// hover toggle tabs - out
function tabOut(value){
	var sep2_value=value+1;
	$('sep'+value).removeClassName('sep_hover');
	$('sep_l'+value).removeClassName('sep_hover');
	$('nav'+value).removeClassName('nav_hover');
	$('sep_r'+value).removeClassName('sep_hover');
	$('sep'+sep2_value).removeClassName('sep_hover');	
}
// click toggle tabs - click
function tabClick(value){
	var tabs=$('tabs_home').childElements();
	var sep2_value=value+1;
	// deselect all tabs first
	for (i=0;i<tabs.length;i++){
		tabs[i].removeClassName('sep_selected');
		tabs[i].removeClassName('sep_l_selected');
		tabs[i].removeClassName('nav_selected');
		tabs[i].removeClassName('sep_r_selected');
	}
	// set the selected tab to on
	$('sep'+value).addClassName('sep_selected');
	$('sep_l'+value).addClassName('sep_l_selected');
	$('nav'+value).addClassName('nav_selected');
	$('sep_r'+value).addClassName('sep_r_selected');
	$('sep'+sep2_value).addClassName('sep_selected');
}

// Add bookmark, courtesy of http://www.dynamicdrive.com/ 
function addBookmark(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all){// ie
	window.external.AddFavorite(url, title);
	}else{
		// safari doesn't allow add bookmark from js... to my knowledge
		alert('Press cmd+d to bookmark this property');
	}
}

// call a homes image slideshow
function homeSlideshow(home_id,home_room_id,photo_id,empty_div){
	// AJAX call the images
	new Ajax.Updater('photo_slideshow','/property/'+home_id+'/slides/photo_slideshow/',{parameters:{'ajax':1,'home_room_id':home_room_id,'photo_id':photo_id,'empty_div':empty_div},onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3);$('win_slide').show();},evalScripts:true});
}
function homeSlideshowClose(){
	blank('photo_slideshow');
	$('win_slide').hide();
}

// call the cropper
function callCropper(source_filename,source_path,ratiox,ratioy,minwidth,minheight,image_id){
	// AJAX call the images
	new Ajax.Updater('cropper_window','/image_tool/fe_cropper/',{parameters:{'ajax':1,'source_filename':source_filename,'source_path':source_path,'ratiox':ratiox,'ratioy':ratioy,'minwidth':minwidth,'minheight':minheight,'image_id':image_id},onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3);$('win_slide').show();},evalScripts:true});
}
function callCropperClose(){
	blank('cropper_window');
	$('win_slide').hide();
}

// AJAX ask a question form
function askQuestion(){
	// default setup
	var formhash = $('ask_form').serialize(true); 
	new Ajax.Updater('askquestion','/ask/',{parameters:formhash,onLoading:function(request){showIndicator(3)},onComplete:function(request){hideIndicator(3)}});	
}