var docPath='broucher/';
var pContent = new prop_page();
var thimagePath='gallery/';

var TmfactorX=200;
var TmfactorY=200;
var menuYloc ='';
$(document).ready(function(){
  $('#showImgDivClose').css('cursor','pointer').click(function(){
    $('#showImgDiv').fadeOut();
  });
 
  $(window).scroll(moveFloatMenu);
});


function moveFloatMenu(){
    var top= 80 + $(document).scrollTop();
     var menuoffset =  top +'px';
     if(top<=1000)
      $('#showImgDiv').animate({top:menuoffset,left:'38%'},{duration:200,queue:false});
  }

function docdblclick(filename){
	var src= docPath+filename;
	window.open(src);
	return false;
}

function checkImage(src,factorX,factorY,callBack)
{
  var mfx=factorX;
  var mfy=factorY;
  var img = new Image();
  img.src = src;
  if(img.width>img.height)
    mfy = findImageLayout(img.width,img.height,factorX);
  else
    mfx = findImageLayout(img.width,img.height,factorY);
  if(mfy>factorY)
  mfy=factorY;
  if(mfx>factorX )
  mfx=factorX;
  callBack(mfx,mfy);	
}


    

function findImageLayout(width, height,mfactor){
  var aspect=0;
  aspect = width/height;
  return mfactor*aspect;
}

function prop_page(){
	
	this.prop_list = new templates();
	this.prop_list.action = 'listxmlProp';
	this.prop_list.template = 'propertyMainTemplate';
	this.prop_list.row_prefix = 'prop';
	this.prop_list.class_name = 'pContent.prop_list';
	this.prop_list.table_wrapper = 'OrgTableWrap';
	this.prop_list.json_array_name = 'properties';
	this.prop_list.selected_css_class = 'sel_table';
	this.prop_list.loadDiv = '#propListDiv';
	this.prop_list.view = 'list';
	this.prop_list.type = '';// for search and search type
	this.prop_list.limit = 4;
	this.prop_list.phpPath='content/json_new_properties.php';
	this.prop_list.before_process = function(data) {
		data.rowPrefix = pContent.prop_list.row_prefix;
		return data;
	};
	 this.prop_list.format_data = function(p) {
		if(!p) {
			return;
		}
		p = format_prop(p);
		$.each(p,function(i,val){
			val.owner = val.owner==null?'':val.owner;
			val.contact = val.contact==null?'':val.contact;
			var array=[{name:val.street+' '+val.addr1},{name:val.city+' '+val.post}];
			val.locationName = create_location(array,', ');
			//$.preLoadImages([thimagePath+val.image],function(){});
			checkImage((thimagePath+val.image),TmfactorX,TmfactorY,function(fx,fy){
				if(!fx)
					val.width =TmfactorX;
				else	val.width =fx;
				
				if(!fy)
					val.width =TmfactorY;
				else	val.height= fy;;
				
			});
		  });
	};
	this.prop_list.post_process = function(){
	    $('.clickBrocure,.clickImg,#pSearchBtn,#homeBtn,#showall').unbind();
            $('.clickBrocure').css('cursor','pointer').click(function(){
                docdblclick($(this).attr('value'));
            });
	    $('.clickImg').css('cursor','pointer').click(function(){
		  $('#showImg').attr('src',$(this).attr('src'));
		  $('#showImgDiv').fadeIn();
		  menuYloc = $('#showImgDiv').offset();
	    });
	    $('#pSearchBtn').css('cursor','pointer').click(function(){
		$('#showpSearchDiv').fadeIn();
	    });
	    $('#homeBtn').css('cursor','pointer').click(function(){
		
	    });
	     $('#srchBtn').css('cursor','pointer').click(function(){
		var data = getData('#showpSearchDiv');
		data.start=0;
		data.limit=4;
		pContent.psearch.postData = data;
		postData('content/json_new_properties.php','propsearch',data,function(resp){
		  if(resp.response=='ok'){
		    if(resp.total!= 0){
		     // pContent.prop_list.load(resp);
		     pContent.psearch.page=1;
		      pContent.psearch.start=0;
		       pContent.psearch.load(resp);
		    }
		    else {
		      $('#showpSearchDiv').hide();
		      $('#norecordDiv').fadeIn();
		      setTimeout("$('#norecordDiv').fadeOut()",2000);
		    }
		  }
		  
		})
	    });
	     $('#showpSearchDivClose').css('cursor','pointer').click(function(){
	      $('#showpSearchDiv').fadeOut();
	    });
	     $('#showall').css('cursor','pointer').css('color','white').click(function(){
	       pContent.prop_list.page=1;
		    pContent.prop_list.start=0;
		    pContent.prop_list.load();
	    });
	};
	
	
	this.psearch = new search_templates();
	this.psearch.action = 'propsearch';
	this.psearch.template = 'propertyMainTemplate';
	this.psearch.row_prefix = 'propSearch';
	this.psearch.class_name = 'pContent.psearch';
	this.psearch.table_wrapper = 'OrgTableWrap';
	this.psearch.json_array_name = 'properties';
	this.psearch.selected_css_class = 'sel_table';
	this.psearch.loadDiv = '#propListDiv';
	this.psearch.view = 'list';
	this.psearch.type = '';// for search and search type
	this.psearch.limit = 4;
	this.psearch.phpPath='content/json_new_properties.php';
	this.psearch.before_process = function(data) {
		data.rowPrefix = pContent.psearch.row_prefix;
		return data;
	};
	 this.psearch.format_data = function(p) {
		if(!p) {
			return;
		}
		p = format_prop(p);
		$.each(p,function(i,val){
			val.owner = val.owner==null?'':val.owner;
			val.contact = val.contact==null?'':val.contact;
			var array=[{name:val.street+' '+val.addr1},{name:val.city+' '+val.post}];
			val.locationName = create_location(array,', ');
			//$.preLoadImages([thimagePath+val.image],function(){});
			checkImage((thimagePath+val.image),TmfactorX,TmfactorY,function(fx,fy){
				if(!fx)
					val.width =TmfactorX;
				else	val.width =fx;
				
				if(!fy)
					val.width =TmfactorY;
				else	val.height= fy;;
				
			});
		  });
	};
	this.psearch.post_process = function(){
	   $('.clickBrocure,.clickImg,#pSearchBtn,#homeBtn,#showall').unbind();
            $('.clickBrocure').css('cursor','pointer').click(function(){
                docdblclick($(this).attr('value'));
            });
	    $('.clickImg').css('cursor','pointer').click(function(){
		  $('#showImg').attr('src',$(this).attr('src'));
		  $('#showImgDiv').fadeIn();
		  menuYloc = $('#showImgDiv').offset();
	    });
	    $('#pSearchBtn').css('cursor','pointer').click(function(){
		$('#showpSearchDiv').fadeIn();
	    });
	    $('#homeBtn').css('cursor','pointer').click(function(){
		
	    });
	     $('#srchBtn').css('cursor','pointer').click(function(){
		var data = getData('#showpSearchDiv');
		data.start=0;
		data.limit=4;
		pContent.psearch.postData = data;
		postData('content/json_new_properties.php','propsearch',data,function(resp){
		  if(resp.response=='ok'){
		     if(resp.total!= 0){
		     pContent.psearch.page=1;
		      pContent.psearch.start=0;
		       pContent.psearch.load(resp);
		    }
		    else {
		      $('#showpSearchDiv').hide();
		      $('#norecordDiv').fadeIn();
		      setTimeout("$('#norecordDiv').fadeOut()",2000);
		    }
		  }
		  
		})
	    });
	    
	     $('#showpSearchDivClose').css('cursor','pointer').click(function(){
	      $('#showpSearchDiv').fadeOut();
	    });
	     //showall
	     $('#showall').css('cursor','pointer').css('color','white').click(function(){
		     pContent.prop_list.page=1;
		    pContent.prop_list.start=0;
		    pContent.prop_list.load();
	    });
	};
}
function format_prop(p){
	
	$.each(p, function(i, val) {
		if(val.type == 1) {
			val.typename = 'building';
			if(val.size!=0)
				val.sizename = area_format(val.type, val.size);
			else 	val.sizename='';
			if(val.sizeto!=0)
				val.sizetoname = area_format(val.type, val.sizeto);
			else 	val.sizetoname='';
			val.availablemin = area_format(val.type, val.available_min);
			val.availablemax = area_format(val.type, val.available_max);
		} else {
			val.typename = 'land';
			if(val.size!=0)
				val.sizename = area_format(val.type, val.size);
			else 	val.sizename='';
			if(val.sizeto!=0)
				val.sizetoname = area_format(val.type, val.sizeto);
			else 	val.sizetoname='';
			// end sizeto
			val.available_min = area_format(val.type, val.available_min);
			val.available_max = area_format(val.type, val.available_max);
		}
		val.sizeformat1='';
		val.sizeformat2='';
		var metric1='';
		var metric2='';
		if(val.type==1){
			metric1= 'sq.ft';
			metric2= 'sq.m';
		}else{
			metric1= 'ac.';
			metric2= 'hc.';
		}
		
		if(val.size!=0.00){
		var sto = integer_format(val.sizeto);
		val.sizeformat1 = integer_format(val.size) +(sto ==0 || val.sizeto==val.size?' '+metric1:' - '+sto+' '+metric1);
		var sf = imperial_to_metric(val.size,val.type);
		var st = imperial_to_metric(val.sizeto,val.type);
		var stf = integer_format(st);
		if(sf!=0)
			val.sizeformat2 = integer_format(sf) +(stf ==0  || val.sizeto==val.size?' '+metric2:' - '+stf+' '+metric2);
		}
	});
	
	return p;
}

function getData(id){
  var data = new Object();
   $.each($(id+' input, '+id+' select, '+id+' textarea'), function() {
      if($(this).attr('id')!='')
      data[$(this).attr('id')] = $(this).val();
    });
    $.each($(id+' input:checkbox'), function() {
	data[$(this).attr('id')] = $(this).attr("checked");
    });
   return data;
}

function postData(url,action,data,postSubmitActions,start,limit){
  $.post(url+"?action="+action, {'json' : JSON.stringify(data)}, function(resp) {
			if(resp.response == 'ok') {
				postSubmitActions(resp);
			} else {
				//$.event.trigger('check_logged_in');
			}
	}, 'json');
}


