$(document).ready(function() {
	$('#locale-list').change(function(e) {
		$('#locale').submit();
	});
});

var USER_BUSINESS_STATUS_LENGTH = 160;

var PVAccount = {
	setOfficeStatus: function(office_status) {
		office_status = parseInt(office_status);
		
		var url = PVStatic.buildUrl('account', 'setofficestatus', office_status);
		PVStatic.postHtml(url, {}, function(html) {
			var st = $('#status-target').html(html).show();
			setTimeout(function() { st.fadeOut(); }, 1500);
		});
	},
	
	setBusinessStatus: function() {
		var business_status = $('#business-status').val();
		if ( business_status.length > 0 ) {
			var url = PVStatic.buildUrl('account', 'setbusinessstatus');
			PVStatic.postHtml(url, {'business_status': business_status}, function(html) {
				$('#business-status').val('');
				$('#business-status-count').html(''+USER_BUSINESS_STATUS_LENGTH);
				
				var st = $('#status-target').html(html).show();
				setTimeout(function() { st.fadeOut(); }, 1500);
			});
		}
	},
	
	countBusinessStatusLength: function() {
		var bs_len = $('#business-status').val().length;
		var delta = (USER_BUSINESS_STATUS_LENGTH - bs_len)+'';
		$('#business-status-count').html(delta);
	},
	
	toggleOtherIndustry2Field: function(value) {
		if ( 1000 == value ) {
			$('#industry2_other').show();
		} else {
			$('#industry2_other').val('').hide();
		}
	},
	
	toggleOtherIndustry3Field: function(value) {
		if ( 1000 == value ) {
			$('#industry3_other').show();
		} else {
			$('#industry3_other').val('').hide();
		}
	}
}





var PVComment = {
	_add: function(profile_id, text_id, status_id) {
		profile_id = parseInt(profile_id);
		if ( profile_id > 0 ) {
			var comment = $('#' + text_id).val();
			
			var url = PVStatic.buildUrl('comment', 'add', profile_id);
			PVStatic.postHtml(url, {'comment': comment}, function(html) {
				$('#' + text_id).val('');
				
				var cst = $('#' + status_id).html(html).show();
				setTimeout(function() { cst.fadeOut(); }, 1500);
			});
		}
	},
	
	_approve: function(comment_id) {
		var url = PVStatic.buildUrl('comment', 'approve', comment_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#comment-action-approve-' + comment_id).css('display', 'none');
			$('#comment-action-decline-' + comment_id).css('display', 'block');
			
			var cst = $('#comment-status-target-' + comment_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_decline: function(comment_id) {
		var url = PVStatic.buildUrl('comment', 'decline', comment_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#comment-action-approve-' + comment_id).css('display', 'block');
			$('#comment-action-decline-' + comment_id).css('display', 'none');
			
			var cst = $('#comment-status-target-' + comment_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_delete: function(comment_id) {
		var del = confirm('Are you sure you want to delete this comment?');
		
		if ( true == del ) {
			var url = PVStatic.buildUrl('comment', 'delete', comment_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#comment-action-approve-' + comment_id).css('display', 'none');
				$('#comment-action-decline-' + comment_id).css('display', 'none');
				$('#comment-action-delete-' + comment_id).css('display', 'none');
				
				var cst = $('#comment-status-target-' + comment_id).html(html).show();
				setTimeout(function() {
					$('#comment-' + comment_id).fadeOut();
					cst.fadeOut();
				}, 1500);
			});
		}
	}
}





var PVContact = {
	_add: function(profile_id) {
		var url = PVStatic.buildUrl('contact', 'add', profile_id);
		PVStatic.postHtml(url, {}, function(html) {
			$('#contact-action-add').css('display', 'none');
			
			var cst = $('#profile-status-target').html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_approve: function(contact_id) {
		var url = PVStatic.buildUrl('contact', 'approve', contact_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#contact-action-approve-' + contact_id).css('display', 'none');
			$('#contact-action-decline-' + contact_id).css('display', 'block');
			
			var cst = $('#contact-status-target-' + contact_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_decline: function(contact_id) {
		var url = PVStatic.buildUrl('contact', 'decline', contact_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#contact-action-approve-' + contact_id).css('display', 'block');
			$('#contact-action-decline-' + contact_id).css('display', 'none');
			
			var cst = $('#contact-status-target-' + contact_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_relate: function(contact_id) {
		var url = PVStatic.buildUrl('contact', 'approve', contact_id);
		
		PVStatic.postHtml(url, {'related': 1}, function(html) {
			$('#contact-action-decline-' + contact_id).css('display', 'block');
			$('#contact-action-unrelate-' + contact_id).css('display', 'block');
			$('#contact-action-approve-' + contact_id).css('display', 'none');
			$('#contact-action-relate-' + contact_id).css('display', 'none');
			
			var cst = $('#contact-status-target-' + contact_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_unrelate: function(contact_id) {
		var url = PVStatic.buildUrl('contact', 'approve', contact_id);
		
		PVStatic.postHtml(url, {'related': 0}, function(html) {
			$('#contact-action-relate-' + contact_id).css('display', 'block');
			$('#contact-action-decline-' + contact_id).css('display', 'block');
			$('#contact-action-unrelate-' + contact_id).css('display', 'none');
			$('#contact-action-approve-' + contact_id).css('display', 'none');
			
			var cst = $('#contact-status-target-' + contact_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	_delete: function(contact_id) {
		var del = confirm('Are you sure you want to delete this contact?');
		
		if ( true == del ) {
			var url = PVStatic.buildUrl('contact', 'delete', contact_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#contact-action-approve-' + contact_id).css('display', 'none');
				$('#contact-action-decline-' + contact_id).css('display', 'none');
				$('#contact-action-relate-' + contact_id).css('display', 'none');
				$('#contact-action-unrelate-' + contact_id).css('display', 'none');
				$('#contact-action-delete-' + contact_id).css('display', 'none');
				
				var cst = $('#contact-status-target-' + contact_id).html(html).show();
				setTimeout(function() {
					$('#contact-' + contact_id).fadeOut();
					cst.fadeOut();
				}, 1500);
			});
		}
	}
}





var PVFavorite = {
	_add: function(company_id) {
		company_id = parseInt(company_id);
		if ( company_id > 0 ) {
			var url = PVStatic.buildUrl('favorite', 'add', company_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#favorite-action-add').css('display', 'none');
				
				var fst = $('#profile-status-target').html(html).show();
				setTimeout(function() { fst.fadeOut(); }, 1500);
			});
		}
	},
	
	_delete: function(favorite_id) {
		var del = confirm('Are you sure you want to delete this favorite?');
		
		if ( true == del ) {
			var url = PVStatic.buildUrl('favorite', 'delete', favorite_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#favorite-action-delete-' + favorite_id).css('display', 'none');
				
				var fst = $('#favorite-status-target-' + favorite_id).html(html).show();
				setTimeout(function() {
					$('#favorite-' + favorite_id).fadeOut();
					fst.fadeOut();
				}, 1500);
			});
		}
	}
}





var PVInbox = {
	_delete: function(message_id) {
		var del = confirm('Are you sure you want to remove yourself from this message?');
		
		if ( true == del ) {
			var url = PVStatic.buildUrl('inbox', 'delete', message_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#message-action-delete-' + message_id).css('display', 'none');
				
				var mst = $('#message-status-target-' + message_id).html(html).show();
				setTimeout(function() {
					$('#message-' + message_id).fadeOut();
					mst.fadeOut();
				}, 1500);
			});
		}
	}
}





var PVMarket = {
	_subscribe: function(ad_id) {
		var url = PVStatic.buildUrl('market', 'subscribe', ad_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#ad-action-subscribe-' + ad_id).css('display', 'none');
			$('#ad-action-unsubscribe-' + ad_id).css('display', 'block');
			
			var at = $('#ad-status-target-' + ad_id).html(html).show();
			
			setTimeout(function() { at.fadeOut(); }, 2500);
		});
	},
	
	_unsubscribe: function(ad_id) {
		var url = PVStatic.buildUrl('market', 'unsubscribe', ad_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#ad-action-subscribe-' + ad_id).css('display', 'block');
			$('#ad-action-unsubscribe-' + ad_id).css('display', 'none');
			
			var at = $('#ad-status-target-' + ad_id).html(html).show();
			
			setTimeout(function() { at.fadeOut(); }, 2500);
		});
	},
	
	_close: function(ad_id) {
		var close = confirm('Are you sure you want to close this ad?');
		
		if ( true == close ) {
			var url = PVStatic.buildUrl('market', 'close', ad_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#ad-action-close-' + ad_id).css('display', 'none');
				
				var at = $('#ad-status-target-'+ad_id).html(html).show();
				
				setTimeout(function() {
					at.fadeOut();
					$('#ad-' + ad_id).fadeOut();
				}, 1500);
			});
		}
	}
}





var PVProfile = {
	viewPartialVideo: function(video_id) {
		video_id = parseInt(video_id, 10);
		if ( video_id > 0 ) {
			var url = PVStatic.buildUrl('profile', 'viewvideo', 'partial', video_id);
			PVStatic.postHtml(url, {}, function(html) {});
		}
	},
	
	viewFullVideo: function(video_id) {
		video_id = parseInt(video_id, 10);
		if ( video_id > 0 ) {
			var url = PVStatic.buildUrl('profile', 'viewvideo', 'full', video_id);
			PVStatic.postHtml(url, {}, function(html) {
				$('#video-view-count').hide().html(html).fadeIn();
			});
		}
	}
}





var PVRequest = {
	_add: function(request_id) {
		var url = PVStatic.buildUrl('request', 'add', request_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#request-action-add-' + request_id).css('display', 'none');
			$('#request-action-remove-' + request_id).css('display', 'block');
			
			var rst = $('#request-status-target-' + request_id).html(html).show();
			setTimeout(function() { rst.fadeOut(); }, 1500);
		});
	},
	
	_remove: function(request_id) {
		var url = PVStatic.buildUrl('request', 'remove', request_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			$('#request-action-remove-' + request_id).css('display', 'none');
			$('#request-action-add-' + request_id).css('display', 'block');
			
			var rst = $('#request-status-target-' + request_id).html(html).show();
			setTimeout(function() { rst.fadeOut(); }, 1500);
		});
	},
	
	_close: function(request_id) {
		var close = confirm('Are you sure you want to close this request? It can not be re-opened.');
		if ( true == close ) {
			var url = PVStatic.buildUrl('request', 'close', request_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				$('#request-action-close-' + request_id).css('display', 'none');
				
				var rst = $('#request-status-target-' + request_id).html(html).show();
				setTimeout(function() {
					$('#request-' + request_id).fadeOut();
					rst.fadeOut();
				}, 1500);
			});
		}
	}
}





var PVStatic = {
	postHtml: function(url, data, callback) {
		data.token = PVStatic.getToken();
		$.post(url, data, callback, 'html');
	},
	
	postJson: function(url, data, callback) {
		data.token = PVStatic.getToken();
		$.post(url, data, callback, 'json');
	},
	
	getToken: function() {
		return $('#token').val();
	},
	
	buildUrl: function() {
		var url = 'index.php?u=';
		var qs = '';
		if ( arguments.length > 0 ) {
			var i=1;
			var len = arguments.length;
			for ( i=0; i<len; i++) {
				qs += arguments[i];
				if ( i+1 != len ) {
					qs += '/';
				}
			}
		}
		
		url += qs;
		return url;
	}
}





var PVVideo = {
	_delete: function(video_id) {
		var del = confirm('Are you sure you want to delete this video?');
		if ( true == del ) {
			var url = PVStatic.buildUrl('video', 'delete', video_id);
			
			PVStatic.postHtml(url, {}, function(html) {
				var cst = $('#video-status-target-' + video_id).html(html).show();
				setTimeout(function() {
					cst.fadeOut();
					$('#video-' + video_id).fadeOut();
				}, 1500);
			});
		}
	},
	
	setCurrent: function(video_id) {
		var url = PVStatic.buildUrl('video', 'setcurrent', video_id);
		
		PVStatic.postHtml(url, {}, function(html) {
			var cst = $('#video-status-target-' + video_id).html(html).show();
			setTimeout(function() { cst.fadeOut(); }, 1500);
		});
	},
	
	showSpinner: function() {
		$('#video-uploader').css('display', 'block');
	}
}
