// var __domain = 'http://beta.philippinepropertyfinder.com/';
var __domain = 'http://www.philippinepropertyfinder.com/';

function init(){
	$( '.element input[type="text"], .element textarea, .element input[type="password"], .element select' ).validate({ 'process_numbers' : false, 'lengthlimit' : { 'enable' : true } });
	
	if( $( '#submit-post' ).length > 0 ){
		$( '#submit-post' ).validate_form( { 'callback' : true, 'callback_function' : function( options ){
				var $this = $( this );
				$this.attr( { 'disabled' : true, 'value' : 'Sending..' } );
				options.elements['description'] = getText();
				$.post( options.url, options.elements, function(data){
					$this.attr( { 'disabled' : false, 'value' : 'Send' } );
					if( data.response ){
						$( '#post-form .element input, #post-form .element textarea' ).each(function(){
							$( this ).val( '' );
						});
						alert( data.message );
						window.location = window.location.toString();
					} else{
						alert( data.message );
					}
				}, "json" );
			}, 'ajax' : true, 'url' : __domain + 'ajaxcalls/ajaxpost.inc.php?action=add-post', 'data_containers' : '#post-form .element', 'imgs' : '#post-form .validate img'
		}).attr( 'disabled', false );
	}
	
	if( $( '#new-posts' ).length > 0 ){
		var last_id = 0, t, last_ele;
		clearInterval( t );
		t = setInterval( function(){
			$( '#new-posts-loader' ).html( "<img src=\"" + __domain + "images/ajax-loader.gif\" width=\"20\" height=\"20\" />" );
			$.post( __domain + 'ajaxcalls/ajaxpost.inc.php?action=post-wheel', { 'lastid' : last_id, 'start_row' : 0, 'limit' : 5 }, function( data ){
				$( '#new-posts-loader' ).html( "New Post(s)" );
				if(
					data.response
					&& data.newposts.length > 0
				){
					for( var x = 0, len = data.newposts.length; x < len; x++ ){
						var user_posted = data.newposts[x]['user_posted'] == 1 ? ' user-posted' : '';
						var price = data.newposts[x]['hidden'] == 1 ? data.newposts[x]['rent_lease_price'] : data.newposts[x]['sale_price'];
						var __url = data.newposts[x]['id'] + '/' + data.newposts[x]['property_name_edited'];
						if( last_id > 0 ){
							last_ele = findLastElement();
							last_ele.before( '<div class="post-content rbroundbox" style="display: none;"><div class="rbtop"><div></div></div><div class="rbcontent' + user_posted + '"><div class="post-title"><a href="' + __domain + __url + '">' + data.newposts[x]['property_name'] + '</a></div><div class="post-price"><span class="strike">P</span>&nbsp;' + price + '</div><div class="post-contact">' + data.newposts[x]['description'] + '</div></div><div class="rbbot"><div></div></div></div>' );
							findLastElement().fadeIn( 'slow' );
						} else{
							$( '#new-posts-container' ).append( '<div class="post-content rbroundbox"><div class="rbtop"><div></div></div><div class="rbcontent' + user_posted + '"><div class="post-title"><a href="' + __domain + __url + '">' + data.newposts[x]['property_name'] + '</a></div><div class="post-price"><span class="strike">P</span>&nbsp;' + price + '</div><div class="post-contact">' + data.newposts[x]['description'] + '</div></div><div class="rbbot"><div></div></div></div>' );
						}
					}
					last_id = data.last_id;
					limitPosts( 5 );
				} else{
					$( '#new-posts-loader' ).html( data.message );
				}
			}, "json" );
		}, 10000 );
	}
}

function limitPosts( limit ){
	$( '.post-content' ).each(function( i ){
		if( i > limit ){
			$( this ).fadeOut( 'slow' ).remove();
		}
	});
}

function findLastElement(){
	var last = $( '.post-content:first' );
	return last;
}
