/*
 * EstateVue House Directory
 * Common JavaScript Library
 * Author: Sigit Noviandi
 * Copyright EstateVue 2010
 * 
 */

function init_validation(form_id){
	$(form_id).getElements('input,textarea').each(function(e){
		var id = e.getProperty('id');
		var cls = e.getProperty('class');
		if(cls!=''){
			cls = cls.split(' ');
			var validate = new LiveValidation(id);
			cls.each(function(c){
				assign_validation(validate,c);
			});
		}
		/*var validate = new LiveValidation(id);
		validate.add( Validate.Presence );*/
	});
}

function assign_validation(val_obj,cls){
	if(cls == 'required'){
		val_obj.add( Validate.Presence,{failureMessage:"Required Field!"});
	} else if(cls == 'email'){
		val_obj.add( Validate.Email,{failureMessage:"Invalid email address."});
	} else if(cls == 'number'){
		val_obj.add( Validate.Numericality,{failureMessage:"Invalid phone number."});
	}
}


window.addEvent('domready', function() {
	
	myElement =  $('search-pro');
	boxsearch =  $('search-popbox');
	url = site_url;
	myElement.addEvent('click', function(){
		if(!window.gus_overlay) {
			window.gus_overlay = new Element('div', {
				styles:{
					display:'block',
					position:'fixed',
					width:'100%',
					height:'100%',
					zIndex: 4,
					float:'left',
					top:0,
					left:0,
					
				},
				events: {
					click: function() {
						boxsearch.fade(0);
						window.gus_overlay.setStyle('display','none ');
					}	
				}
			}).inject(document.body);
		}
		
		boxsearch.morph({
			'display':'block',
			  'position':'absolute',
			  'z-index':'5',
			  'margin-left':'40px',
			  'margin-top':'-10px',
			  'background-color': '#FF6600',
		      'border-width': '1px',
		      'color': '#000',
		      'height': '50px',
		      'opacity': '1',
		      'width': '250px'
		    }
		);
		
		window.gus_overlay.setStyle('display','block');
		return false;
	});
	//boxsearch.addEvent('click',function(){ boxsearch.fade(0); });
	//$(document).keyup(function(e) {

		  //if (e.keyCode == 27) { boxsearch.fade(0); }   // esc
		//});
	document.onkeydown = function(evt) {
	    evt = evt || window.event;
	    if (evt.keyCode == 27) {
	    	boxsearch.fade(0);
	    }
	};
	
	/*boxsearch.addEvent('click',function(){
						boxsearch.fade(0);			
						});*/
	
	$('keyword').addEvent('keydown', function(evt){
		if (evt.key == 'enter') {
			evt.stop();
			var nilai = this.value;
			var link = url+"for-sale/search.html/keyword:"+nilai+" type:all";
			window.location=link;
			
		}
	});
	
	//set for feed
	
	var feed = $('feeds');
	var popfeed = $('feeds-pop');
	var feedpop = popfeed.getElements('ul');
	var feeding = $('feeding').getElements('li');
	
	feeding.addEvents({
		'click': function(){
			var a = this.getFirst();
			var id = a.get('href');
			var fee = feed.getFirst();
			var g = fee.get('text');
			fee.erase('text');
			fee.set('text',g);
			window.location = site_url + 'search/set_feed/'+id+'/'+enc_href;
			//alert(window.location.href);
			popfeed.set('styles',{
				display:'none'
			});
		return false;
	}
	});
	
	feed.addEvent('click', function(){	
		var h = popfeed.offsetHeight;
		if(popfeed.style.display == 'block'){
			popfeed.morph({
			    'z-index'	: '1',
			    'display'	: 'none',
			    'opacity'	: '0'
			});
		} else {
			popfeed.morph({
				'display': 'block',
				'opacity': '1',
				'position': 'absolute',
				'margin-left': '679px',
				'z-index': '5'
			});
		}
	  
	});
	
});

function captcha_refresh() {
    var img = $('captcha-img');
    if(!img) return;
    var rand = Math.round(Math.random() * 100000);
    img.src = site_url + 'property/captcha/rand-' + rand + '.jpg';
    $('captcha').value = '';
}
