/*Rumah javascript*/
//common

document.domain = 'kapanlagi.com';
$(document).ready(function() {
   
   var cookiesMember = $.cookie('mbralname');
   if(cookiesMember != null || cookiesMember != '') {
      
      if(document.getElementById('content-member'))
      {
         $('#content-member').html('Halaman Admin');
      }
   }
    
   $('#CityOpSearch').change(function() {        
				 var city = document.getElementById('CityOpSearch').value;
				 if(city!='')
				 {
					 $.getJSON('http://rumah.kapanlagi.com/function/selectCity.php?city='+city+'&jsoncallback=?',
					 function(data){$('#AreaOpSearch').html(data.value);});
				 }      
		 });
		 
		 $('#searchbutton').click(function() {
			 
			 var param = '';
			 $(".formsearch select").each(function () {
				 var id = $(this).attr('id');
				 var v = $(this).attr('value');
				 var p = id + '=' + v + '&';
				 param += p;
				 
			 });
			 param += 'search=true' ;
			 
			 if(param!='')
			 {
				 var url = 'http://rumah.kapanlagi.com/search.php?' + param;
				 window.location = 'http://rumah.kapanlagi.com/search.php?' + param;         
			 }
			 
		 });
		 
   $('#buttoncari2').click(function() {
      
      var idsearch = document.getElementById('IDSearch').value;
      if(idsearch!='')
      {         
         window.location = 'http://rumah.kapanlagi.com/search.php?id=' + idsearch;         
      }      
   });
   
   $('#buttoncariagen').click(function() {
      var bySearch = document.getElementById('bySearch').value;
      var inputsearch = document.getElementById('inputsearch').value;
      $('#agentdiv').html('<center><img src="http://klimg.com/kapanlagi.com/v5/i/ajax-loader.gif" /> <br />sedang mencari...</center>');
      $.getJSON('http://rumah.kapanlagi.com/function/searchAgent.php?bySearch='+bySearch+'&inputsearch='+inputsearch+'&jsoncallback=?',
      function(data){$('#agentdiv').html(data.value);});
   });
   
   $(".headimagethumb img").each(function () {
      $(this).click(function() {
         var id = $(this).attr('id');
         var name = $(this).attr('name');
         $('.headimage').html('<a target="_self" href="'+ name +'"><img border="0" src="'+ id +'" width="400" /></a>');                
      });
   });
   
   
   $('#sendEmail').click(function() {
       $("#formsendmail").fadeIn("slow"); 
   });
   
   $('#closeformsendmail').click(function() {
       $("#formsendmail").fadeOut("slow"); 
   });
   
   $('#submitEmail').click(function() {
       var nameSender = document.getElementById("nameSender").value;
       var emailSender = document.getElementById("emailSender").value;
       var msgSender = document.getElementById("msgSender").value;
       var emailseller = document.getElementById("emailseller").value;
       var nameseller = document.getElementById("nameseller").value;
       var idproperty = document.getElementById("idproperty").value;
       var titleproperty = document.getElementById("titleproperty").value;
       var priceproperty = document.getElementById("priceproperty").value;
       if(nameSender=='')
       {
           $('#errorMessage').html("Nama tidak boleh kosong");
           $('#nameSender').focus();
       }
       else if(emailSender=='')
       {
           $('#errorMessage').html("Email tidak boleh kosong");
           $('#emailSender').focus();
       }
       else if(msgSender=='')
       {
           $('#errorMessage').html("Silahkan isi pesan Anda.");
           $('#msgSender').focus();
       }
       else
       {
           var urlsendmail = 'http://app.kapanlagi.com/property/sendEmail.php?email='+emailseller+'&name='+nameseller+'&id='+idproperty+'&title='+titleproperty+'&price='+priceproperty+'&nameSender='+nameSender+'&emailSender='+emailSender+'&subject=RESPON IKLAN PROPERTI KAPANLAGI.COM&msgSender='+msgSender;
           $.getJSON(''+urlsendmail+'&jsoncallback=?',function(data){
           //alert(data.value);
               if(data.value==1)
               {
                   $('#formsendmail').html('<a id="closeformsendmail" title="Close Form Email">X</a><br class="clear" /><br /><br /><center>Pesan Anda sudah terkirim.<br />Terima Kasih</center>');
                   $('#closeformsendmail').click(function() {
                       $("#formsendmail").fadeOut("slow"); 
                   });    
               }
               else
               {
                   $('#errorMessage').html(data.value);
               }
           });
       }
   });
   
   $('#submitComment').click(function() {
       var nameComment = document.getElementById("name").value;
       var emailComment = document.getElementById("email").value;
       var msgComment = document.getElementById("comment").value;
       var commentIdads = document.getElementById("commentIdads").value;
       var key = document.getElementById("urlKey").value;
       var hash = window.location.href.indexOf('#');
       var url = '';
       if(hash > -1)
         url = window.location.href.substr(0, window.location.href.indexOf('#'));
       else
         url = window.location.href;
       
       
            
       if(nameComment=='')
       {
           document.getElementById("name").value = "Nama tidak boleh kosong";
           $('#name').css("background","#e1e1e1");
           $('#name').focus();
           $('#name').click(function() {
               if(document.getElementById("name").value == "Nama tidak boleh kosong")
               {    
                   document.getElementById("name").value = "";
               }
               $('#name').css("background","none");
           });
       }
       
       if(emailComment=='')
       {
           document.getElementById("email").value = "Email tidak boleh kosong";
           $('#email').css("background","#e1e1e1");
           $('#email').focus();
           $('#email').click(function() {
               if(document.getElementById("email").value == "Email tidak boleh kosong")
               {    
                   document.getElementById("email").value = "";
               }
               $('#email').css("background","none");
           });
       }
       
       if(msgComment=='')
       {                    
           document.getElementById("comment").value = "Isi Komentar Anda";
           $('#comment').css("background","#e1e1e1");
           $('#comment').focus();
           $('#comment').click(function() {
               if(document.getElementById("comment").value == "Isi Komentar Anda")
               {
                  document.getElementById("comment").value = "";
               }
               $('#comment').css("background","none");
           });
       }
       
       if((document.getElementById("name").value == "Nama tidak boleh kosong") || (document.getElementById("email").value == "Email tidak boleh kosong") || (document.getElementById("comment").value == "Isi Komentar Anda"))
       {
         nameComment='';
         emailComment='';
         msgComment='';
       }
       
       if(nameComment!='' && emailComment!='' && msgComment!='')
       {
           var wait = '<div class="kom-18" id="mgS" style="display: block; ">Tunggu Sebentar</div>';
           $('#errorMessageComment').html(wait);
           var urlsendcomment = 'http://app.kapanlagi.com/property/detailProperty.php?submitcomment=1&email='+emailComment+'&name='+nameComment+'&commentIdads='+commentIdads+'&comment='+msgComment+'&url='+key;           
           $.getJSON(''+urlsendcomment+'&jsoncallback=?',function(data){
               
               
               var result = '<div class="kom-18" id="mgS" style="display: block; ">'+data.value+' <a href="'+url+'?1">Klik di sini</a></div>';
               
               setTimeout($('#errorMessageComment').html(result),10000);
               
               document.getElementById("name").value = "";
               document.getElementById("email").value = "";
               document.getElementById("comment").value = "";
           
           });
       }
       
   });
   
   if (document.getElementById("commentIdads"))
   {
      var commentIdads = document.getElementById("commentIdads").value;
      if(commentIdads)
      {
         var urlgetcomment = 'http://rumah.kapanlagi.com/function/detailProperty.php?checkcomment=1&commentIdads='+commentIdads;
         $.getJSON(''+urlgetcomment+'&jsoncallback=?',function(data){
             $('#listComment').html(data.value);
         });
      }
   }
});

(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);
