//Used with display messages
var timer;

/*
 * Display any message to the user. The message will be in a div tag that
 * does not have any data until a message needs to be displayed. The message
 * will have a timer to only be visible for a specific time period.
 * 
 * The message is retrieved from a ajax call.
 */
function displayMessage(){
	
	$.get(
		"/app/message.php",
		function(response){
			
			if(response.message.toUpperCase() != "NO MESSAGE"){
				clearTimeout(timer);
				$("#fixed-message-box").html('<p class="'+response.type+'">'+response.message+'</p>');
				timer = setTimeout(function(){$("#fixed-message-box").html("");},10000);
			}
		},
		"json"
	);
}

/*
 * Remove a message display by setting the html contents of a div
 * to be empty.
 */
function removeMessage(){
	clearTimeout(timer);
	$("#fixed-message-box").html("");
}

/*
 * Display no default image for a property that does not have a image.
 */
function killPicLink(x){
	if($("#picLink"+x).length > 0){
		$("#picLink"+x).html('<img src="/img/HL5/no-photo-60.gif" alt="missing photo" width="60" height="40" class="image-preview">');
	}
}




// ------------------------------------------------------ MY HAWAII LIFE


/*
 * Changes the page of search results (leads, agents, etc) on my hawaii life
 */
function changeMyHawaiiLifePage( formElement, pageTrackerElement, goToPage ){
		
	//Change page hidden input
	pageTrackerElement.value = goToPage;
	
	//Submit form
	formElement.submit();
	
}

/**
 * Removes a gallery from a user account, fades out the containing div element, then removes it
 */
function removeGallery(id){
	$.post(
		"/my-hawaii-life/galleries/remove.php",
		{ gallery_id : id },
		function(response){
			$('#gallery_'+id).fadeOut( "slow", function(){ $('#gallery'+id).remove() }); 
		}
	);
}

function removeReport(report_id){

	$.post(
		"/my-hawaii-life/property-stats/remove-report.php",
		{ report_id: report_id },
		function(response){
			$('#report'+report_id).fadeOut("slow", displayMessage());
		}
	);

}

function sendReport(report_id){

	$.post(
		"/my-hawaii-life/property-stats/send-report.php",
		{ report_id: report_id },
		function(response){
			displayMessage();
		}
	);

}



/**
 * Removes a saved search from a user account, fades out the containing div element, then removes it
 */
function removeSearch(id){	
	$.post(
		"/my-hawaii-life/saved-searches/remove.php",
		{ id : id },
		function(response){
			$('#search'+id).fadeOut( "slow", function(){ $('#search'+id).remove() }); 
		}
	);
}

function deleteNotification(id){	

	$.post(
		"/my-hawaii-life/notifications/delete.php",
		{ id : id },
		function(response){
			$('#'+id).fadeOut( "slow", displayMessage() );
		}
	);

	
var res = new Ajax.Request("/my-hawaii-life/notifications/delete.php", { 
				method: "post", 
				parameters: { id : id }, 
				onSuccess: function(response) { $(id).fade({ duration: 0.5, from: 1, to: 0 }); displayMessage(); },
				onFailure: function(response) { alert("Fail! "+response.responseText); }
				});  
}


/**
 * On the MY HL dashboard, a checkbox can be clicked to toggle email alerts on a saved search.
 */
function switchAlert(id, checked){

	$.post(
		"/my-hawaii-life/saved-searches/switch-alert.php",
		{ id : id, checked : checked },
		function(response){
			if(checked){
				$('#search'+id+'AlertStatus').html('<input type="checkbox" name="" value="" onchange="switchAlert('+id+', this.checked)" checked /> <span class="green">Email notifications are ON for this saved search</span>'); 
			}else{
				$('#search'+id+'AlertStatus').html('<input type="checkbox" name="" value="" onchange="switchAlert('+id+', this.checked)" /> Email notifications are <span class="red">OFF</span> for this saved search'); 
			}
		}
	);

}
 
/**
 * Creates a saved search and reloads the parent window.
 */
function addSavedSearch(sn, ea, reloadParent, uid){
	$.post(
		"/my-hawaii-life/save-search/process.php",
		{ 
			searchName : sn, 
			emailAlerts : ea, 
			user_id : uid 
		},
		function(response){
			window.location = '/re/';
		}
	);
	/*
		//alert('userid'+uid);
		var res = new Ajax.Request("/my-hawaii-life/save-search/process.php", { 
				method: "post", 
				parameters: { searchName : sn, emailAlerts : ea, user_id : uid }, 
				onSuccess: function(response) { parent.window.location = parent.window.location; },
				onFailure: function(response) { alert('FAIL!'); displayMessage(); }
				}); */
}
 
function setCookie(c_name,value){
	var expiredays = 30;
	var path = "/";
	var domain = ".hawaiilife.com"
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	( (expiredays==null) ? "" : ";expires="+exdate.toGMTString()) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" );
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function displayMissingImage(imageElement){
	imageElement.src = '/img/HL5/no-photo-60.gif';
}


/* NEW DETAIL PAGE FORM SCRIPTS
----------------------------------------------- */
function showFullDescription(){
	$('#partial-description').hide();
	$('#full-description').show();
}

function focusFormMessage(){
	if( $('textarea#message').val() == 'Request More Info...' ){
		$('textarea#message').val('');
	}
}

function blurFormMessage(){
	if( $('textarea#message').val() == '' ){
		$('textarea#message').val('Request More Info...');
	}
}

function focusFormSearch(){
	if( $('#search').val() == 'Island, district, town, resort or MLS#' ){
		$('#search').val('');
	}
}

function blurFormSearch(){
	if( $('#search').val() == '' ){
		$('#search').val('Island, district, town, resort or MLS#');
	}
}

function focusFormFirstName(){
	if( $('#firstName').val() == 'First Name' ){
		$('#firstName').val('');
	}
}

function blurFormFirstName(){
	if( $('#firstName').val() == '' ){
		$('#firstName').val('First Name');
	}
}

function focusFormLastName(){
	if( $('#lastName').val() == 'Last Name' ){
		$('#lastName').val('');
	}
}

function blurFormLastName(){
	if( $('#lastName').val() == '' ){
		$('#lastName').val('Last Name');
	}
}

function focusFormEmail(){
	if( $('#email').val() == 'Email' ){
		$('#email').val('');
	}
}

function blurFormEmail(){
	if( $('#email').val() == '' ){
		$('#email').val('Email');
	}
}

function focusFormPhone(){
	if( $('#phone').val() == 'Phone (optional)' ){
		$('#phone').val('');
	}
}

function blurFormPhone(){
	if( $('#phone').val() == '' ){
		$('#phone').val('Phone (optional)');
	}
}

/* NEW DETAIL PAGE HOVER BAR SCRIPTS
----------------------------------------------- */
// This is called on document ready
function showCompHoverBar(){

    // Hide prev arrow
	$("#comp-hover-bar-prev").hide();

	// Hide 'show comps' link
	$("#comp-open").hide();

	// Show page 1
	$("#comp-list-page1").show();

	// Reduce opacity
    $("#comp-hover-bar").fadeTo(250, 0.1, function(){

	    // Move bar into screen
  	  	$("#comp-hover-bar").animate({"bottom": "+=100px"}, 250);

  	  	// Increase opacity
  	  	$("#comp-hover-bar").fadeTo(1000,1);
    });
}

// This function hides the comparables hover bar
function hideCompHoverBar(){

    // Move entire hover bar out of screen
	$("#comp-hover-bar").animate({"bottom": "-=100px"}, 500, function(){

		// Hide close button and link
		$(".comp-close").hide();

		// Hide listings
		$("#comps").hide();

		// Hide text on far left of hover bar
		$("#comp-nav").hide();

		// Show 'show comps' link
		$("#comp-open").show();

		// Move hover bar back into screen
		$("#comp-hover-bar").animate({"bottom": "+=40px"}, 500);
		
		// Move hover bar back into screen
		$("#comp-hover-bar").css('position', 'fixed');
	});

}

// This function re-opens the hover bar after it has been close by the user
function reopenCompHoverBar(){

	// Move entire hover bar out of screen
	$("#comp-hover-bar").animate({"bottom": "-=40px"}, 500, function(){

		// Hide 'show comps' link
		$("#comp-open").hide();

		// Show listings
		$("#comps").show();

		// Show text on far left of hover bar
		$("#comp-nav").show();

		// Show close link and button
		$(".comp-close").show();

		// Move hover bar back into screen
		$("#comp-hover-bar").animate({"bottom": "+=100px"}, 500);

		// Move hover bar back into screen
		$("#comp-hover-bar").css('position', 'fixed');		
	});
}
