function Property(cntrl)
{
	var property = {};
		
	property = new NET(cntrl);// inherit net class
	
	/* Attributes */
	property.isLoaded 		= false;
	property.propertyArr 	= new Array();
	/* EOF Attributes */
	
	/* Function Declaration */
	
	//property.getData = getData;
	//property.getDataCallback = getDataCallback;
	property.getProperties			=	getProperties;
	property.getPropertiesCallback	=	getPropertiesCallback;
	property.loadPage 				= 	loadPage;
	property.loadSubPage 			= 	loadSubPage;
	property.refreshQty				=	refreshQty;
	property.publishFeed			=	publishFeed;
	property.buyProperties			=	buyProperties;
	property.buyPropertiesCallback	=	buyPropertiesCallback;
	property.sellProperties 		= 	sellProperties;
	property.sellPropertiesCallback = 	sellPropertiesCallback;
	property.refresh 				= 	refresh;	
	/* EOF Function Declaration */
	
	/* Function Definition */
	
	function loadPage()
	{	
		property.clearMessage();
		property.adjustWindowHeight();
		if(!owner.income) // display '0' when income is null
			$('#property_income').html("<span class=\"gold\">0</span>");
		else
			$('#property_income').html(numberFormat(owner.income));
		if(!owner.upkeep) //display '0' when upkeep is null
			$('#upkeep').html("<span class=\"gold\">0 g</span>");
		else
			$('#upkeep').html(numberFormat(owner.upkeep));
		var cash_flow = owner.income-owner.upkeep;
		if(!cash_flow) //display '0' when cash flow is null
			$('#cash_flow').html("<span class=\"gold\">0 g </span>");
		else
			$('#cash_flow').html("<span class=\"gold\">"+numberFormat(cash_flow)+" g </span>");
		
		if(!property.isLoaded)
		{
			$('#property_next_level').hide();
			$('#developed_property_list').hide();
			$('#undeveloped_property_list').hide();
			property.getProperties();
		}
		
	};
	
		
	function loadSubPage(sub_page)
	{ 
		$('#undeveloped_property_list').css('display', 'none');
		$('#developed_property_list').css('display', 'none');
		$('#property_collection > div').removeClass('img_selected');
		$('#property_collection1 > div').removeClass('img_selected');
		$('#property_'+sub_page).addClass('img_selected');
		$('#'+sub_page+'_property_list').css('display', 'block');
	};
	
	function getProperties(start, max_num )
	{
		property.makeRequest();
	};
	
	function getPropertiesCallback(xml)
	{
		property.isLoaded = true;
		$('#property_next_level').show();
		$('#developed_property_list').show();
		$('#undeveloped_property_list').show();
		var undeveloped_property ="";
		var developed_property ="";
		var str = "";
		$('property', xml).each(function(){
			var id = $(this).attr('id');
			var title = $(this).find('title').text();
			var owned = $(this).find('owned').text();
			var min_gang_size_req = $(this).find('min_req_gang_size').text();
			var cost = Math.floor($(this).find('cost').text());
			var parent_id =$(this).find('parent_id').text();
			var built_on =$(this).find('built_on').text();
			var income = $(this).find('income').text();
			var type = $(this).find('type').text();
			var favor_points = parseInt($(this).find('favor_points').text());
			property.propertyArr[id] = {'title':title, 'cost': cost, 'owned': owned, 'income': income, 'type':type, 'min_gang_size_req':min_gang_size_req, 'favor_points': favor_points};
			
			str = 				"<div class\"left\"><div class=\"desc_frame_box_small\" id=\"property_"+id+"\" ><!--desc_frame_box_small div starts-->"+
								"<div class=\"bgfix_IE8\"><img src=\""+config.imageUrl+"/top_frame.png\" /></div>"+								
								"<div class=\"mid_frame_small\">"+
								  "<div class=\"mid_frame_img_small\"  >"+
										"<div class=\"items_name\">"+$(this).find('title').text()+"</div>"+										
										"<div class=\"property_img\"><img src=\""+ config.propertyImgUrl+$(this).find('imageU').text()+"\" /></div>"+
										"<div class=\"attk_deff\">Income: <span id=\"property_income"+id+"\"><span class=\"gold\">"+numberFormat($(this).find('income').text())+" g</span></span></div>"+
								  "</div>"+	
								"</div>"+
								"<div class=\"bgfix_IE8\"><img src=\""+config.imageUrl+"/mid_frame_grad.png\" /></div>"+
								"<div class=\"desc_frame_small\"  ><!--desc_frame_small starts-->"+										
										"<span class=\"green\"><span id=\"property_cost"+id+"\">";
										if(favor_points>0) // check if property requires favor point
										{
											str+="<span class=\"coin\" id=\"property_favor_points"+id+"\">Coin: "+favor_points+"</span>";
										}
										else
										{
											str+= "<span class=\"gold\">"+numberFormat(cost)+ " g</span>";
										}
										str +=	"</span></span><br />"+
										"<div class=\"items_require\" >";
											  if(parent_id!=0)
											  {
											  str +="<span class=\"txt_builton\">Built On:<br /></span>"+
											  		"<span class=\"owned\">"+built_on+"</span><br />";
											  }
											  
											 else if(min_gang_size_req>0) //check if the property requires gang size
											 {
											 	str +=	"<span id=\"property_clan"+id+"\" class=\"green\">Needs "+min_gang_size_req+" Recuits</span><br />"+
											 			"<span id=\"property_needs"+id+"\" class=\"require\">";
														if(min_gang_size_req>owner.gang_size)
														{
															str += 	"<div style=\"margin-top:7px;\">"+
																	"<span class=\"red\">"+ numberFormat(min_gang_size_req-owner.gang_size) +" more needed</span></div>";
														}
														else
														{
															str += 	"<div style=\"margin-top:7px;\">"+
																	"<span class=\"red\">&nbsp;</span></div>";								
														}
											}
											else
											{			
												str += 	"<span id=\"property_clan"+id+"\" class=\"green\">&nbsp;</span><br />"+
											 			"<span id=\"property_needs"+id+"\" class=\"require\">"+
														"<div style=\"margin-top:7px;\">"+
														"<span class=\"red\">&nbsp;</span></div>";		
												
											} 
										str +=	"</span>"+
												"<br/>"+
												"<span class=\"require\">&nbsp;</span>";
											  
										str+=  "<span class=\"owned\">&nbsp;</span>"+
										 	"</div>"+
										"<div class=\"select_div\" ><!--select_div starts-->"+
											"<span>";
											
										// if (min_gang_size_req>0) // to display select option in property (select option one only in gang size requiring property)
										//  {
											//  str += "<select id=\"property_qty"+$(this).attr('id')+"\" ><option value=\"1\" selected=\"selected\">1</option></select>";
											  		
										//  }
										// else
										//  { 
										  	  //select option upto 10 in other property
											  str +="<select id=\"property_qty"+$(this).attr('id')+"\" onChange=\"javascript:property.refreshQty("+id+")\"><option value=\"1\" selected=\"selected\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"10\">10</option></select>";											  		
										//  }
									str +=	"<span class=\"btn_div\"><span class=\"button_new_buy\"  "+config.hoverEffect+" style=\"width:33px;height:18px\" onClick=\"property.buyProperties("+$(this).attr('id')+", $('#property_qty"+$(this).attr('id')+"').val())\">Buy</span></span>"+
											"</span>"+
											"<span id=\"property_sell_btn"+id+"\">";
										
										if(owned>0 && type==1) // check owned number of property
											{ //display sell option value of property 
												str +=	"<select id=\"property_sell_qty"+id+"\">";
													  	for(var i = 1; i<=10;i++)
													  	{
													  		str += "<option value='"+i+"'>"+i+"</option>";
													  		if(i==owned)
													  			break;
													  	}
													
												 str +="</select>"+
												 		"<span class=\"btn_div\"><span  class=\"button_new_buy\" "+config.hoverEffect+"  style=\"width:16px;height:18px\" onclick=\"property.sellProperties("+$(this).attr('id')+", $('#property_sell_qty"+$(this).attr('id')+"').val())\">s</span></span>";
														 
										 	}
									str += 	"</span>"+
											"<div class=\"clear\"></div>"+
										"</div><!--select_div ends-->"+
										"<div class=\"clear\"></div>"+
						      "</div><!--desc_frame_small ends-->"+
							"<div class=\"bgfix_IE8\"><img src=\""+config.imageUrl+"/below_frame.png\" /></div>"+
								"<div id=\"property_owned"+id+"\" class=\"owned\">";
								if(owned>0)
								{
									str+= "Owned: "+ owned;
								}
								else
								{
									str+= "&nbsp;";	
								}
								
							str +=	"</div></div>";	
			
						if(parent_id==0) // check whether property is developed or undeveloped
						{
							//developed property
							undeveloped_property += str;	
						}
						else
						{
							//undeveloped property
							developed_property += str;
						}						
		});
		
		
		$('#undeveloped_property_list').html(undeveloped_property);
		$('#developed_property_list').html(developed_property);
		var next_level = $(xml).find('next_level').text();
		$('#next_level').html(next_level);
		var nextpay = parseInt($(xml).find('next_pay').text());
		$('#next_pay').html(nextpay);
		var update_period = $(xml).find('update_period').text();
		$('#update_period').html(update_period);
		var next_level = $(xml).find('next_level').text();
		if(!next_level) // check next level to discover new property
		{
			$('#property_next_level').html('');
		}
		property.loadSubPage('undeveloped');
		//hover();
	};
	
	function buyProperties(id,qty)
	{
		var type = property.propertyArr[id].type;
		var title = property.propertyArr[id].title;
		var cost = property.propertyArr[id].cost;
		var favor_points =property.propertyArr[id].favor_points;
		var min_gang_size_req = property.propertyArr[id].min_gang_size_req;
		switch(type)
		{
			case '1':
				var total_cost = qty*cost;
				if(owner.cash>=total_cost)
				{
					property.makeRequest();
				}	
				else
				{
					property.outputMessage("You need <span class=\"gold\">"+numberFormat(total_cost-owner.cash)+" g </span>more gold to buy "+qty+" "+title+"(s)", "i");
					return;
				}
				break;
			
			case '2':
				var total_favor_points = favor_points*qty;
				if(owner.favour_point>=total_favor_points)
				{
					property.makeRequest();
				}	
				else
				{
					property.outputMessage("You need <a href=\"javascript:app.loadPage('godfather')\"><span class=\"coin\">"+numberFormat(total_favor_points-owner.favour_point)+" Coins</span></a> more to buy "+qty+" "+title+"(s)", "i");
					return;
				}
				break;
				
			case '3':
				var total_cost = qty*cost;
				if(owner.cash>=total_cost)
				{
					if(owner.gang_size >=min_gang_size_req)	
					{
						property.makeRequest();
					}	
					else
					{
						property.outputMessage("You need  more <a href=\"javascript:app.loadPage('gang')\">Clan</a> to buy"+qty+" "+title+"(s)", "i");
						return;
					}
				}
				else
				{
					property.outputMessage("You need <span class=\"gold\">"+numberFormat(total_cost-owner.cash)+" g</span> more gold to buy "+qty+" "+title+"(s)", "i");
					return;
				}
				break;	
		}
	};
	
	function buyPropertiesCallback(xml)
	{
		var id = $(xml).find('id').text(); 	
		var message=$('data',xml).find('message').text(); 
		var qty=parseInt($('data',xml).find('qty').text());
		var favPnt=parseInt($('data',xml).find('favPnt').text());
		var totalcost=parseInt($('data',xml).find('tcost').text());
		var itemType=parseInt($('data',xml).find('itemType').text());
		//message+="   <a href=\"javascript:property.publishFeed("+id+","+qty+","+favPnt+","+itemType+","+totalcost+")\">Brag about it</a>";
		property.outputMessage(message);
		
		var newTotalPropertyOwned = $(xml).find('newTotalPropertyOwned').text();
		
		property.propertyArr[id].min_gang_size_req = (newTotalPropertyOwned*5)+5;
		$('#property_owned'+id).html('Owned: ' + newTotalPropertyOwned);
		var type = property.propertyArr[id].type;
		if(newTotalPropertyOwned>0 && type==1) // to set sell option value in select 
		{ 
			var str ="<select id=\"property_sell_qty"+id+"\">";
					  	for(var i = 1; i<=10;i++)
					  	{
					  		str += "<option value='"+i+"'>"+i+"</option>";
					  		if(i==newTotalPropertyOwned)
					  			break;
					  	}
		    str +="</select>";
		    str+="<span class=\"btn_div\"><span  class=\"button_new_buy\" "+config.hoverEffect+"  style=\"width:16px;height:18px\" onclick=\"property.sellProperties("+id+", $('#property_sell_qty"+id+"').val())\">s</span></span>";
			$('#property_sell_btn'+id).html(str);
		}
		
		var newGangSize= $(xml).find('newGangSize').text();
		property.propertyArr[id].min_gang_size_req = newGangSize;
		if(newGangSize>0)//set the required clan size to buy property
		{
			$('#property_clan'+id).html('Needs '+newGangSize+' Recuits');
		}
		else
		{
			$('#property_clan'+id).html('&nbsp');
		}
		if(newGangSize>owner.gang_size)
		{
			var need = 	"<div style=\"margin-top:7px;\">"+
						"<span class=\"red\">"+ numberFormat(newGangSize-owner.gang_size) +" more needed</span></div>";
			$('#property_needs'+id).html(need);				
		}
		else
		{
			var need = 	"<div style=\"margin-top:7px;\">"+
						"<span class=\"red\">&nbsp</span></div>";
			$('#property_needs'+id).html(need); 
		}
		

		var newItemCost = parseInt($(xml).find('newItemCost').text());
		if(newItemCost>0)				
			$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(newItemCost)+" g </span>");
		var income =  $(xml).find('incm').text();
		$('#property_income').html(numberFormat(income));
		var cash_flow = income - owner.upkeep;
		$('#cash_flow').html("<span class=\"gold\">"+numberFormat(cash_flow)+" g </span>");
		property.propertyArr[id].cost = newItemCost;
		var undevelopedLandQty = $(xml).find('undevelopedLandQty').text();
		var parent_id = parseInt($(xml).find('parent_id').text());
		var undevelopedLandCost = $(xml).find('undevelopedLandCost').text();
		$('#property_cost'+parent_id).html("<span class=\"gold\">"+numberFormat(undevelopedLandCost)+" g </span>");
		if(parent_id>0) //check developed or undeveloped property
		{
			property.propertyArr[parent_id].cost = undevelopedLandCost;
			if(!undevelopedLandQty || undevelopedLandQty==0)//check the undeveloped land owned
			{
				//not to display owned if owned number is zero
				$('#property_owned'+parent_id).html("&nbsp;");	
				$('#property_sell_btn'+parent_id).html("&nbsp;");		
			}
			else
			{	
				//display owned number of property
				$('#property_owned'+parent_id).html('Owned ' + undevelopedLandQty);	
								
				var str ="<select id=\"property_sell_qty"+parent_id+"\">";
						  	for(var i = 1; i<=10;i++)
						  	{
						  		str += "<option value='"+i+"'>"+i+"</option>";
			  					if(i==undevelopedLandQty)
						  			break;
						  	}
			    str +="</select></div>"+
			    "<span class=\"btn_div\"><span  class=\"button_new_buy\" "+config.hoverEffect+" onclick=\"property.sellProperties("+parent_id+", $('#property_sell_qty"+parent_id+"').val())\">s</span></span>";
				
				$('#property_sell_btn'+parent_id).html(str);
			}
		}
		//property.publishFeed(id,qty,favPnt,itemType,totalcost);
	};
	
	function sellProperties(id,qty)
	{
		property.makeRequest();		
	};
	
	function sellPropertiesCallback(xml)
	{
		var id = $(xml).find('id').text(); 	
		var message=$('data',xml).find('message').text(); 
		property.outputMessage(message);
		
		var newTotalPropertyOwned = $(xml).find('newTotalPropertyOwned').text();
		if(!newTotalPropertyOwned || newTotalPropertyOwned==0)// check if property owned is zero
		{	////not to display owned if owned number is zero
			$('#property_owned'+id).html("&nbsp;");	
			$('#property_sell_btn'+id).html("&nbsp;");		
		}
		else
		{	
			//display owned number
			$('#property_owned'+id).html('Owned: ' + newTotalPropertyOwned);	
			var str ="<select id=\"property_sell_qty"+id+"\">";
					  	for(var i = 1; i<=10;i++)
					  	{
					  		str += "<option value='"+i+"'>"+i+"</option>";
					  		if(i==newTotalPropertyOwned)
					  			break;
					  	}
		    str +="</select></div>"+
		    "<span class=\"btn_div\"><span  class=\"button_new_buy\" "+config.hoverEffect+" onclick=\"property.sellProperties("+id+", $('#property_sell_qty"+id+"').val())\">s</span></span>";
			$('#property_sell_btn'+id).html(str);
		}
		var newItemCost = parseInt($(xml).find('newItemCost').text());		
		if(newItemCost>0)
			$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(newItemCost)+" g</span>");
		var income =  $(xml).find('incm').text();		
		$('#property_income').html(numberFormat(income));
		var cash_flow = income - owner.upkeep;
		$('#cash_flow').html("<span class=\"gold\">"+numberFormat(cash_flow)+" g</span>");
		var newGangSize= $(xml).find('newItemGangSize').text();
		property.propertyArr[id].cost = newItemCost;
		if(newGangSize>0)//check if property require gang size
		{	//refresh the new gang size require of a property
			$('#property_clan'+id).html('Clan: '+newGangSize);
		}
		//hover();
	};
	
	//refresh the cost income favor points required whend select option changes	
	function refreshQty(id)
	{ 	
		var qty = $('#property_qty'+id).val();
		var cost = parseInt(property.propertyArr[id].cost);
		var income = parseInt(property.propertyArr[id].income);
		var favor_points = property.propertyArr[id].favor_points;
		var type = property.propertyArr[id].type;
		var min_gang_size_req = property.propertyArr[id].min_gang_size_req;
		switch(type)
		{
			case '1':
				$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(cost*qty)+" g </span>");
				break;
			case '2':
				$('#property_favor_points'+id).html(numberFormat('Coins: '+favor_points*qty));	
				break;
			case '3':
				$('#property_clan'+id).html("<span class=\"green\">Needs "+ numberFormat(parseInt(min_gang_size_req) + ((qty-1)*5)) +" Recuits</span>");
				$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(cost*qty)+" g </span>");
				var str ="";
				if((parseInt(min_gang_size_req)+(qty-1)*5)>owner.gang_size)
				{
					str += 	"<div style=\"margin-top:7px;\">"+
							"<span class=\"red\">"+ numberFormat((parseInt(min_gang_size_req)+(qty-1)*5)-owner.gang_size) +" more needed</span></div>";
				}
				else
				{
					str += 	"<div style=\"margin-top:7px;\">"+
							"<span class=\"red\">&nbsp;</span></div>";								
				}
				$('#property_needs'+id).html(str);
				break;
		}
		$('#property_income'+id).html("<span class=\"gold\">"+numberFormat(income*qty)+" g</span>");
		/*if(type==2)
		{
			$('#property_favor_points'+id).html(numberFormat('Coins: '+favor_points*qty));		
		}
		else if(type==1)
		{
			$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(cost*qty)+" g </span>");
		}
		else
		{
			$('#property_clan'+id).html("<span class=\"green\">Needs "+numberFormat((min_gang_size_req+(qty*5)))+" Recuits</span>");
			$('#property_cost'+id).html("<span class=\"gold\">"+numberFormat(cost*qty)+" g </span>");
			if(min_gang_size_req*qty>owner.gang_size)
			{
				str += 	"<div style=\"margin-top:7px;\">"+
						"<span class=\"red\">"+ numberFormat(min_gang_size_req-owner.gang_size) +" more needed</span></div>";
			}
			else
			{
				str += 	"<div style=\"margin-top:7px;\">"+
						"<span class=\"red\">&nbsp;</span></div>";								
			}
			$('#property_needs'+id).html(str);
			
		}*/
		
	};
	function refresh()
	{
		property.isLoaded = false;
		property.propertyArr = new Array();
		property.loadPage();
	};
	
	function publishFeed(id,qty,fav,itemType,total)
	{
		switch(itemType)
		{
		case 1:
		case 3:
			message1="has bought "+qty+"X "+property.propertyArr[id].title+" for "+numberFormat(total)+" gold ";
		break;
		case 2:
			message1="has bought "+qty+"X "+property.propertyArr[id].title+" for "+fav+" Coins";
		break;
		}
		text1="Play Darkwars";
		//text1={"images":[{"src":config.timg['all']}]};
		link1=config.appUrl;
		template_data={"images":[{"src":config.timg['logo'],
								 	"href":link1},
								 	{"src":config.userImgUrl+owner.id,
								 	"href":link1},
								 	 {"src":getRaceImage(owner.race),
											 "href":link1}],
									"message":message1,
									"action_text":text1,
									"link":link1};
		//publishStory(config.templateBundleId,template_data);
	}
	
	/* EOF Function Definition */
	
	return property;
}