var main = function(){

    //private function   
    //something
    //function
	function genPropertyResult (j) {
	           //property result
				//var res = eval(j);
				
				var tbody = new Element('tbody').setProperties({
					id : 'newPropertyResult'
				});
				
				if (eval(j).length > 0) {
					for (i=0; i<eval(j).length; i++) {
					    
					    //property icon
					    var img_icon;
					    //alert(result[i].propertyType);
					    switch (eval(j)[i].propertyType) {
					        case '1': img_icon = '<img src="/Img/icon/icon_apartment.png" />';break;
					        case '2': img_icon = '<img src="/Img/icon/icon_condominium.png" />';break;
					        case '3': img_icon = '<img src="/Img/icon/icon_village.png" />';break;
					        case '4': img_icon = '<img src="/Img/icon/icon_hotel.png" />';break;
					        case '5': img_icon = '<img src="/Img/icon/icon_roomcondo.png" />';break;
					        case '6': img_icon = '<img src="/Img/icon/icon_teedin.png" />';break;
					        case '7': img_icon = '<img src="/Img/icon/icon_home.png" />';break;
					        case '8': img_icon = '<img src="/Img/icon/icon_twinhome.png" />';break;
					        case '9': img_icon = '<img src="/Img/icon/icon_townhouse.png" />';break;
					        case '10': img_icon = '<img src="/Img/icon/icon_townhome.png" />';break;
					        case '11': img_icon = '<img src="/Img/icon/icon_building.png" />';break;
					        case '12': img_icon = '<img src="/Img/icon/icon_office.png" />';break;
					        case '13': img_icon = '<img src="/Img/icon/icon_warehouse.png" />';break;
					        defalut:break;
					    }
					    
						var tr = new Element('tr');
						
						var td1 = new Element('td').setHTML(img_icon + '&nbsp;' +result[i].propertyTypeName).setProperties({
							'valign' : 'top',
							'align' : 'left',
							'bgcolor': '#99b3cc'
						}).setStyles('cursor:hand;').injectInside(tr);
						var td2 = new Element('td').setHTML(result[i].propertyNameThai).setProperties({
							'valign' : 'top',
							'align' : 'left',
							'bgcolor': '#99b3cc'
						}).setStyles('cursor:hand;').injectAfter(td1);
						var td3 = new Element('td').setHTML(result[i].propertyPrice).setProperties({
							'valign' : 'top',
							'align' : 'left',
							'bgcolor': '#99b3cc'
						}).setStyles('cursor:hand;').injectAfter(td2);
						var td4 = new Element('td').setHTML(result[i].propertyStatusName).setProperties({
							'valign' : 'top',
							'align' : 'center',
							'bgcolor': '#99b3cc'
						}).setStyles('cursor:hand;').injectAfter(td3);
						var td5 = new Element('td').setHTML(result[i].propertyLatitude).setStyles('display:none;').injectAfter(td4);
						var td6 = new Element('td').setHTML(result[i].propertyLongitude).setStyles('display:none;').injectAfter(td5);
						var td7 = new Element('td').setHTML(result[i].propertyId).setStyles('display:none;').injectAfter(td6);
						var td8 = new Element('td').setHTML(result[i].propertyPhone).setStyles('display:none;').injectAfter(td7);
	                    			
						//click row event 
						tr.addEvent('click', function(){
						    var id    = this.cells[6].innerHTML;
						    var lat   = this.cells[4].innerHTML;
						    var lng   = this.cells[5].innerHTML;
						    var type  = this.cells[0].innerHTML;
						    var txt   = this.cells[1].innerHTML;
						    var price = this.cells[2].innerHTML;
						    var phone = this.cells[7].innerHTML;
						    //alert(txt + ' =>' + lat + ', ' + lng);
						    window.location = "#map";
						    TRMCenterAt(lat, lng, null);
						    TRMOpenInfoWindow(id,
						                      'Information',
						                      '<div style="align:left; width:240px;">'
						                       + img_icon + '&nbsp;<a href="javascript:void(0)" onclick="openPropertyDetail('+id+')">' + txt + '</a><br/>' 
						                       + ' ราคา : ' + price + ' บาท <br/>'
						                       + ' เบอร์ติดต่อ : ' + phone
						                       + '</div>'
						                      );
					    });
						tr.injectInside(tbody);
						
					}
				} 
				tbody.injectInside('sortPropertiesTable');

				//propertySearch.propSortTable = new sortableTable($('sortPropertiesTable'), 'propertySearch.propSortTable', {pageSize: 10, overCls: 'over'});
			}
	
	function getNewProperties(type) {
        var url = 'PropertyController/getNewProperties/'+type;
		var myAjax = new Ajax(url, {
			method: 'get',
			onComplete: function (o) {
				var rs = eval(o);
				//alert(result.length);
				for (i=0; i<result.length; i++)	{
					dd = result[i].propertyModifiedOn.split(' ');
					d = dd[0];
					var pic_url = (result[i].fileURL == null)? '/Img/no_pic.jpg' : result[i].fileURL;
					$('NP_'+(i+1)+'_Pic').setHTML('<img src="'+pic_url+'" width="115" height="74"/>');
					//$('NP_'+(i+1)+'_Pic2').setHTML('<img src="'+pic_url+'" width="127" height="86"/>');
					//$('NP_'+(i+1)+'_No').setText(i+1);
					$('NP_'+(i+1)+'_Name').setHTML('<a href="javascript:void(0)" onclick="openPropertyDetail('+result[i].propertyId+')" title="'+result[i].propertyNameThai+'">'+result[i].propertyNameThai+'</a>');
					$('NP_'+(i+1)+'_Type').setHTML("&nbsp;&nbsp;&nbsp;"+result[i].propertyTypeName);
					$('NP_'+(i+1)+'_Date').setHTML(d);
					$('NP_'+(i+1)+'_View').setHTML(result[i].view+"&nbsp;reviews");
					
					//alert(result[i].propertyNameThai+"<br />"+result[i].propertyNameEng);
				}
			}
		}).request();
	}

	function getPropertyType() {
        var url = 'PropertyController/getPropertyType';
		var myAjax = new Ajax(url, {
			method: 'get',
			onComplete: function (o) {
				var rs = eval(o);
				//alert(result.length);
				for (i=0; i<result.length; i++)	{
					var op = new Element('option').setProperties({
						'value': result[i].propertyTypeId
					}).setText(result[i].propertyTypeName).injectInside($('NP_TYPE'));
				}
			}
		}).request();
	}
    //public function
    return {
		
		init : function(){
            var url = 'PropertyController/newProperty';
		    //var newItems = new Ajax(url, {method: 'get'}).request();
		   
		    //jquery for chain select list
           	jQuery.noConflict();
            jQuery(function(){
		            //select province to get district
					jQuery("select#province").change(function(){
					    var pvnId = document.getElementById('province').value;
					    document.getElementById('district').innerHTML = 'ทุกอำเภอ';
					    jQuery.getJSON("/PropertyController/getDistrict/"+pvnId , function(j){
							var options = '';
							options += '<option value="0">ทุกอำเภอ</option>';
							for (var i = 0; i < j.length; i++) {
							   	options += '<option value="'+ j[i].dstId +'">' + j[i].dstName + '</option>';
							}
							jQuery("#district").html(options);
							jQuery('#district option:first').attr('selected', 'selected');
							
						})
					})
					
		    })
		    
		    
		    //TRMLoad($('map'), 13.7534, 100.4991, null, null, null);
		    window.addEvent('domready', function() {
				getPropertyType();
				getNewProperties(0);
				$('NP_TYPE').addEvent('change', function() {
					getNewProperties(this.value);
				});
			});
		    
		    //get new property
		    //genPropertyResult(newItems);
		    
		    //load new property
		    //window.addEvent('domready', function(e){
				//mySortableTable = new sortableTable('newPropertyListTable', 'mySortableTable', {pageSize: 3, overCls: 'over'});
			//});


       
        }
        
    };
}();