
// Set pagination to 1 and ajax request to empty
otherSimilarPropertiesPage = 1;
ajaxRequestSimilar = '';

function callInProgress (xmlhttp) {  
	switch (xmlhttp.readyState) {  
		case 1: case 2: case 3:  
			return true;  
		break;  
		// Case 4 and 0  
		default:  
			return false;  
		break;  
	}  
} 

// Refresh similar properties block with other similar properties
function getOtherSimilarProperties(id)
{
	// abort ajax request if call a new one while first is in progress
	if(ajaxRequestSimilar != '' && callInProgress(ajaxRequestSimilar.transport))
	{
		ajaxRequestSimilar.transport.abort();
		
	}
	
	
	urlAjax = "/search/similar-properties/id/" + id +  "/r/3/p/" + otherSimilarPropertiesPage + "/lang/" + lang  ;
	
	
	ajaxRequestSimilar = new Ajax.Request(
			
			urlAjax,
			{
				method: 'post',
				onSuccess: function(transport, json) 
				{
				 	createSimilarPropertiesList(json);
				}
			}
		);	
}

function createSimilarPropertiesList(json)
{
	
	// Properties array : similar prop. seo url , city , picture url , immotype , surface , price
	otherSimilarProperties = json [ 0 ];
	
	// Curent Similar Properties list page 
	curentSimilarPropertiesPage = json [ 1 ];
	
	// Total Similar Properties list page 
	otherSimilarPropertiesPages = json [ 2 ];
	
	// Dont show more similar link if there is not any similar properties left
	if (otherSimilarProperties!= null){
		if( curentSimilarPropertiesPage >= otherSimilarPropertiesPages)
		{
			$('get-other-similar-properties').style.display = "block";
			otherSimilarPropertiesPage =  1;
		}
		else
		{
			$('get-other-similar-properties').style.display = "block";
			otherSimilarPropertiesPage = curentSimilarPropertiesPage + 1;
		}
	}
	
	//otherSimilarProperties = [  ["897063" , "Luxembourg-Bonnevoie", "pictureurl" , "Appartement" , "135" , "780" ] ,["897063" , "Luxembourg-Bonnevoie", "pictureurl" , "Appartement" , "135" , "780" ] , ["897063" , "Luxembourg-Bonnevoie", "pictureurl" , "Appartement" , "135" , "780" ] , ["897063" , "Luxembourg-Bonnevoie", "pictureurl" , "Appartement" , "135" , "780" ]  ];
	
	
	oPropertyLinksContainer = document.createElement( "div" );
	oPropertyLinksContainer . setAttribute ( "id" , "current-similar-properties" ) ;
	//oPropertyLinksContainer . style . display = "none" ;
	
	if(otherSimilarProperties != null)
	{
		for( i = 0 ; i < otherSimilarProperties.length ; i ++ )
		{	
			
			// Create link block propety
			oPropertyLink = document . createElement ( "a" ) ;
			oPropertyLink . setAttribute ( "href" , otherSimilarProperties [ i ] [ 0 ] ) ;
			oPropertyLink . className = "similar-property" ;
				
				// Create property tilte
				oPropertyTitle = document.createElement ( "h3" ) ;
				oPropertyTitle . innerHTML = otherSimilarProperties [ i ] [ 1 ];
				oPropertyLink . appendChild ( oPropertyTitle ) ;
				
				// Create property info block
				oPropertyPictContainer = document.createElement ( "div" ) ;
				oPropertyPictContainer . className = "img-container" ;
				oPropertyLink . appendChild ( oPropertyPictContainer ) ;
				
					// Create property picture
					oPropertyPict = new Image() ;
					oPropertyPict . setAttribute ( "src" , otherSimilarProperties [ i ] [ 2 ] ) ;
					//oPropertyPict . setAttribute ( "onload" , "resizeImage ( this, 'similar' )");
					oPropertyPict . onload = function(e)
					{
						resizeImage ( this, 'similar' );
						
					}
					
					//oPropertyPict.addEventListener("load",resizeImage ( this, 'similar' ))
					oPropertyPictContainer . appendChild ( oPropertyPict ) ;
					
				
				// Create property info block
				oPropertyInfo = document.createElement ( "div" ) ;
				oPropertyInfo . className = "info" ;
				oPropertyLink . appendChild ( oPropertyInfo ) ;
				
					// Create property immotype block
					oPropertyType = document.createElement ( "div" ) ;
					oPropertyType . className = "type" ;
					oPropertyType . innerHTML = otherSimilarProperties [ i ] [ 3 ]  ;
					oPropertyInfo . appendChild ( oPropertyType ) ;
					
					// Create property immotype block
					oPropertySurface = document.createElement ( "div" ) ;
					oPropertySurface . className = "surface" ;
					oPropertySurface . innerHTML = otherSimilarProperties [ i ] [ 4 ]  ;
					oPropertyInfo . appendChild ( oPropertySurface ) ;
					
					// Create property price block
					oPropertyPrice = document.createElement ( "div" ) ;
					oPropertyPrice . className = "price" ;
					oPropertyPrice . innerHTML = otherSimilarProperties [ i ] [ 5 ];
					oPropertyInfo . appendChild ( oPropertyPrice ) ;
				
				// Create property clear block
				oPropertyClear = document.createElement ( "div" ) ;
				oPropertyClear . className = "clearBoth" ;
				oPropertyLink . appendChild ( oPropertyClear ) ;
				
			oPropertyLinksContainer . appendChild (oPropertyLink) ;
			
		}
	}
	else
	{
		// Show no similar properties 
		$("no-other-similar-properties") . style . display = "block";
		
	}
			
	
	
	// Remove previous similar properties list
	fadeOutDiv( "similar-bloc" , '1' ) ;
}

//Hide immosubtype container 
function fadeOutDiv(name,duration)
{ 	
	$('similar-bloc').hasLayout = true;
	$('bloc-similar-properties').className= "bloc loading";
	new Effect.Opacity( name , { duration: duration , from: 1 , to: 0 , afterFinish:changeDiv});
	
	// Add new similar properties list
	setTimeout("fadeInDiv()",1000);
	
	
} 

function fadeInDiv()
{
	$('similar-bloc').hasLayout = true;
	$('bloc-similar-properties').className= "bloc";
	new Effect.Opacity( "similar-bloc" , { duration: 1 , from: 0 , to: 1  });
}

function changeDiv(Effect)
{ 	
	$('similar-bloc') . innerHTML = "" ;
	$('similar-bloc') . appendChild (oPropertyLinksContainer) ;
	
}

function resizeImage(img, format) {
	switch(format) {
		case 'similar':
			var maxHeight = 35;
			var maxWidth =35;
			
			break;
			
		default:
			return;
			
	}
	
	var wScaleRatio;
	var hScaleRatio;
	
	
	wScaleRatio = img.width / maxWidth
	hScaleRatio = img.height / maxHeight
	
	
	
	var ratio = (wScaleRatio > hScaleRatio) ? hScaleRatio : wScaleRatio
	img.width = parseInt(img.width / ratio) // + 'px'
	//img.style.width = parseInt(img.width / ratio) + 'px'
	radioHeight = parseInt( img.height / ratio)
	img.style.top = ( ( maxHeight - radioHeight ) / 2 )+ 'px';
	img.style.left = ( ( maxWidth - img.width ) / 2 )+ 'px';
	
	// dunno why the hell I had to comment this to get resizing behaves as expected!
	// img.height = parseInt(img.height / ratio) // + 'px'
	
}

